From c2f7fdb61d49ce14f599dc728f70fab1d164b5aa Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sat, 4 Mar 2017 20:33:57 -0800 Subject: [PATCH 1/2] GUACAMOLE-230: Provide means of retrieving a canvas with the same dimensions and content as a layer. --- .../src/main/webapp/modules/Layer.js | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/guacamole-common-js/src/main/webapp/modules/Layer.js b/guacamole-common-js/src/main/webapp/modules/Layer.js index 1ed9d4c54..5384d2c74 100644 --- a/guacamole-common-js/src/main/webapp/modules/Layer.js +++ b/guacamole-common-js/src/main/webapp/modules/Layer.js @@ -267,13 +267,41 @@ Guacamole.Layer = function(width, height) { this.height = height; /** - * Returns the canvas element backing this Layer. - * @returns {Element} The canvas element backing this Layer. + * Returns the canvas element backing this Layer. Note that the dimensions + * of the canvas may not exactly match those of the Layer, as resizing a + * canvas while maintaining its state is an expensive operation. + * + * @returns {HTMLCanvasElement} + * The canvas element backing this Layer. */ - this.getCanvas = function() { + this.getCanvas = function getCanvas() { return canvas; }; + /** + * Returns a new canvas element containing the same image as this Layer. + * Unlike getCanvas(), the canvas element returned is guaranteed to have + * the exact same dimensions as the Layer. + * + * @returns {HTMLCanvasElement} + * A new canvas element containing a copy of the image content this + * Layer. + */ + this.toCanvas = function toCanvas() { + + // Create new canvas having same dimensions + var canvas = document.createElement('canvas'); + canvas.width = layer.width; + canvas.height = layer.height; + + // Copy image contents to new canvas + var context = canvas.getContext('2d'); + context.drawImage(layer.getCanvas(), 0, 0); + + return canvas; + + }; + /** * Changes the size of this Layer to the given width and height. Resizing * is only attempted if the new size provided is actually different from From 69ff583a07c6644f75cd404c2c94a16489b88b3b Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sat, 4 Mar 2017 20:36:24 -0800 Subject: [PATCH 2/2] GUACAMOLE-230: Supply oncursor event with new canvas (having correct dimensions). --- guacamole-common-js/src/main/webapp/modules/Display.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guacamole-common-js/src/main/webapp/modules/Display.js b/guacamole-common-js/src/main/webapp/modules/Display.js index f14c6bf56..2c9ede6d8 100644 --- a/guacamole-common-js/src/main/webapp/modules/Display.js +++ b/guacamole-common-js/src/main/webapp/modules/Display.js @@ -423,7 +423,7 @@ Guacamole.Display = function() { // Fire cursor change event if (guac_display.oncursor) - guac_display.oncursor(cursor.getCanvas(), hotspotX, hotspotY); + guac_display.oncursor(cursor.toCanvas(), hotspotX, hotspotY); }); };