3 * Guacamole - Clientless Remote Desktop
4 * Copyright (C) 2010 Michael Jumper
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 function Layer(width, height) {
23 var display = document.createElement("canvas");
25 display.style.position = "absolute";
26 display.style.left = "0px";
27 display.style.right = "0px";
29 display.width = width;
30 display.height = height;
32 var displayContext = display.getContext("2d");
34 var nextUpdateToDraw = 0;
35 var currentUpdate = 0;
36 var updates = new Array();
38 // Given an update ID, either call the provided update callback, or
39 // schedule the update for later.
40 function setUpdate(updateId, update) {
42 // If this update is the next to draw...
43 if (updateId == nextUpdateToDraw) {
45 // Call provided update handler.
48 // Draw all pending updates.
50 while (updateCallback = updates[++nextUpdateToDraw]) {
52 delete updates[nextUpdateToDraw];
57 // If not next to draw, set callback and wait.
59 updates[updateId] = update;
63 display.drawImage = function(x, y, image) {
64 var updateId = currentUpdate++;
66 setUpdate(updateId, function() {
67 displayContext.drawImage(image, x, y);
73 display.draw = function(x, y, url) {
74 var updateId = currentUpdate++;
76 var image = new Image();
77 image.onload = function() {
78 setUpdate(updateId, function() {
79 displayContext.drawImage(image, x, y);
86 display.copyRect = function(srcx, srcy, w, h, x, y) {
87 var updateId = currentUpdate++;
89 setUpdate(updateId, function() {
90 displayContext.drawImage(display, srcx, srcy, w, h, x, y, w, h);
95 display.drawRect = function(x, y, w, h, color) {
96 var updateId = currentUpdate++;
98 setUpdate(updateId, function() {
99 displayContext.fillStyle = color;
100 displayContext.fillRect(x, y, w, h);
105 display.clearRect = function(x, y, w, h) {
106 var updateId = currentUpdate++;
108 setUpdate(updateId, function() {
109 displayContext.clearRect(x, y, w, h);
114 display.filter = function(filter) {
115 var updateId = currentUpdate++;
117 setUpdate(updateId, function() {
118 var imageData = displayContext.getImageData(0, 0, width, height);
119 filter(imageData.data, width, height);
120 displayContext.putImageData(imageData, 0, 0);