Removed native components (now in own repositories)
[guacamole.git] / web / guacamole-default-webapp / 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 nextUpdateToDraw = 0;
35     var currentUpdate = 0;
36     var updates = new Array();
37
38     // Given an update ID, either call the provided update callback, or
39     // schedule the update for later.
40     function setUpdate(updateId, update) {
41
42         // If this update is the next to draw...
43         if (updateId == nextUpdateToDraw) {
44
45             // Call provided update handler.
46             update();
47
48             // Draw all pending updates.
49             var updateCallback;
50             while (updateCallback = updates[++nextUpdateToDraw]) {
51                 updateCallback();
52                 delete updates[nextUpdateToDraw];
53             }
54
55         }
56
57         // If not next to draw, set callback and wait.
58         else
59             updates[updateId] = update;
60
61     }
62
63     display.drawImage = function(x, y, image) {
64         var updateId = currentUpdate++;
65
66         setUpdate(updateId, function() {
67             displayContext.drawImage(image, x, y);
68         });
69
70     }
71
72
73     display.draw = function(x, y, url) {
74         var updateId = currentUpdate++;
75
76         var image = new Image();
77         image.onload = function() {
78             setUpdate(updateId, function() {
79                 displayContext.drawImage(image, x, y);
80             });
81         };
82         image.src = url;
83     };
84
85
86     display.copyRect = function(srcx, srcy, w, h, x, y) {
87         var updateId = currentUpdate++;
88     
89         setUpdate(updateId, function() {
90             displayContext.drawImage(display, srcx, srcy, w, h, x, y, w, h);
91         });
92
93     };
94
95     display.drawRect = function(x, y, w, h, color) {
96         var updateId = currentUpdate++;
97
98         setUpdate(updateId, function() {
99             displayContext.fillStyle = color;
100             displayContext.fillRect(x, y, w, h);
101         });
102
103     };
104
105     display.clearRect = function(x, y, w, h) {
106         var updateId = currentUpdate++;
107
108         setUpdate(updateId, function() {
109             displayContext.clearRect(x, y, w, h);
110         });
111
112     };
113
114     display.filter = function(filter) {
115         var updateId = currentUpdate++;
116
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);
121         });
122
123     };
124
125     return display;
126
127 }
128