Removed experimental ready message
[guacamole.git] / src / main / webapp / guac-web-lib / javascript / layer.js
1
2 /*
3  *  Guacamole - Clientless Remote Desktop
4  *  Copyright (C) 2010  Michael Jumper
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 function Layer(width, height) {
21
22     // Off-screen buffer
23     var display = document.createElement("canvas");
24
25     display.style.position = "absolute";
26     display.style.left = "0px";
27     display.style.right = "0px";
28
29     display.width = width;
30     display.height = height;
31
32     var displayContext = display.getContext("2d");
33
34     var readyHandler = null;
35     var nextUpdateToDraw = 0;
36     var currentUpdate = 0;
37     var updates = new Array();
38
39     // Given an update ID, either call the provided update callback, or
40     // schedule the update for later.
41     function setUpdate(updateId, update) {
42
43         // If this update is the next to draw...
44         if (updateId == nextUpdateToDraw) {
45
46             // Call provided update handler.
47             update();
48
49             // Draw all pending updates.
50             var updateCallback;
51             while ((updateCallback = updates[++nextUpdateToDraw])) {
52                 updateCallback();
53                 delete updates[nextUpdateToDraw];
54             }
55
56             // If done with updates, call ready handler
57             if (display.isReady() && readyHandler != null)
58                 readyHandler();
59
60         }
61
62         // If not next to draw, set callback and wait.
63         else
64             updates[updateId] = update;
65
66     }
67
68     display.isReady = function() {
69         return currentUpdate == nextUpdateToDraw;
70     }
71
72     display.setReadyHandler = function(handler) {
73         readyHandler = handler;
74     }
75
76
77     display.drawImage = function(x, y, image) {
78         var updateId = currentUpdate++;
79
80         setUpdate(updateId, function() {
81             displayContext.drawImage(image, x, y);
82         });
83
84     }
85
86
87     display.draw = function(x, y, url) {
88         var updateId = currentUpdate++;
89
90         var image = new Image();
91         image.onload = function() {
92             setUpdate(updateId, function() {
93                 displayContext.drawImage(image, x, y);
94             });
95         };
96         image.src = url;
97     };
98
99
100     display.copyRect = function(srcx, srcy, w, h, x, y) {
101         var updateId = currentUpdate++;
102     
103         setUpdate(updateId, function() {
104             displayContext.drawImage(display, srcx, srcy, w, h, x, y, w, h);
105         });
106
107     };
108
109     display.drawRect = function(x, y, w, h, color) {
110         var updateId = currentUpdate++;
111
112         setUpdate(updateId, function() {
113             displayContext.fillStyle = color;
114             displayContext.fillRect(x, y, w, h);
115         });
116
117     };
118
119     display.clearRect = function(x, y, w, h) {
120         var updateId = currentUpdate++;
121
122         setUpdate(updateId, function() {
123             displayContext.clearRect(x, y, w, h);
124         });
125
126     };
127
128     display.filter = function(filter) {
129         var updateId = currentUpdate++;
130
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);
135         });
136
137     };
138
139     return display;
140
141 }
142