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 readyHandler = null;
35 var nextUpdateToDraw = 0;
36 var currentUpdate = 0;
37 var updates = new Array();
39 // Given an update ID, either call the provided update callback, or
40 // schedule the update for later.
41 function setUpdate(updateId, update) {
43 // If this update is the next to draw...
44 if (updateId == nextUpdateToDraw) {
46 // Call provided update handler.
49 // Draw all pending updates.
51 while ((updateCallback = updates[++nextUpdateToDraw])) {
53 delete updates[nextUpdateToDraw];
56 // If done with updates, call ready handler
57 if (display.isReady() && readyHandler != null)
62 // If not next to draw, set callback and wait.
64 updates[updateId] = update;
68 display.isReady = function() {
69 return currentUpdate == nextUpdateToDraw;
72 display.setReadyHandler = function(handler) {
73 readyHandler = handler;
77 display.drawImage = function(x, y, image) {
78 var updateId = currentUpdate++;
80 setUpdate(updateId, function() {
81 displayContext.drawImage(image, x, y);
87 display.draw = function(x, y, url) {
88 var updateId = currentUpdate++;
90 var image = new Image();
91 image.onload = function() {
92 setUpdate(updateId, function() {
93 displayContext.drawImage(image, x, y);
100 display.copyRect = function(srcx, srcy, w, h, x, y) {
101 var updateId = currentUpdate++;
103 setUpdate(updateId, function() {
104 displayContext.drawImage(display, srcx, srcy, w, h, x, y, w, h);
109 display.drawRect = function(x, y, w, h, color) {
110 var updateId = currentUpdate++;
112 setUpdate(updateId, function() {
113 displayContext.fillStyle = color;
114 displayContext.fillRect(x, y, w, h);
119 display.clearRect = function(x, y, w, h) {
120 var updateId = currentUpdate++;
122 setUpdate(updateId, function() {
123 displayContext.clearRect(x, y, w, h);
128 display.filter = function(filter) {
129 var updateId = currentUpdate++;
131 setUpdate(updateId, function() {
132 var imageData = displayContext.getImageData(0, 0, width, height);
133 filter(imageData.data, width, height);
134 displayContext.putImageData(imageData, 0, 0);