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");
24 var displayContext = display.getContext("2d");
26 function resize(newWidth, newHeight) {
27 display.style.position = "absolute";
28 display.style.left = "0px";
29 display.style.right = "0px";
31 display.width = newWidth;
32 display.height = newHeight;
38 display.resize = function(newWidth, newHeight) {
39 if (newWidth != width || newHeight != height)
40 resize(newWidth, newHeight);
43 resize(width, height);
45 var readyHandler = null;
46 var nextUpdateToDraw = 0;
47 var currentUpdate = 0;
48 var updates = new Array();
50 // Given an update ID, either call the provided update callback, or
51 // schedule the update for later.
52 function setUpdate(updateId, update) {
54 // If this update is the next to draw...
55 if (updateId == nextUpdateToDraw) {
57 // Call provided update handler.
60 // Draw all pending updates.
62 while ((updateCallback = updates[++nextUpdateToDraw])) {
64 delete updates[nextUpdateToDraw];
67 // If done with updates, call ready handler
68 if (display.isReady() && readyHandler != null)
73 // If not next to draw, set callback and wait.
75 updates[updateId] = update;
79 display.isReady = function() {
80 return currentUpdate == nextUpdateToDraw;
83 display.setReadyHandler = function(handler) {
84 readyHandler = handler;
88 display.drawImage = function(x, y, image) {
89 var updateId = currentUpdate++;
91 setUpdate(updateId, function() {
92 displayContext.drawImage(image, x, y);
98 display.draw = function(x, y, url) {
99 var updateId = currentUpdate++;
101 var image = new Image();
102 image.onload = function() {
103 setUpdate(updateId, function() {
104 displayContext.drawImage(image, x, y);
111 display.copyRect = function(srcLayer, srcx, srcy, w, h, x, y) {
112 var updateId = currentUpdate++;
114 setUpdate(updateId, function() {
115 displayContext.drawImage(srcLayer, srcx, srcy, w, h, x, y, w, h);
120 display.clearRect = function(x, y, w, h) {
121 var updateId = currentUpdate++;
123 setUpdate(updateId, function() {
124 displayContext.clearRect(x, y, w, h);
129 display.filter = function(filter) {
130 var updateId = currentUpdate++;
132 setUpdate(updateId, function() {
133 var imageData = displayContext.getImageData(0, 0, width, height);
134 filter(imageData.data, width, height);
135 displayContext.putImageData(imageData, 0, 0);