From b4ac0af5d86e51f9c317e7546a0e3865b1f06417 Mon Sep 17 00:00:00 2001 From: fredj Date: Wed, 18 Jan 2012 14:06:22 +0100 Subject: [PATCH] New OpenLayers.Tile.Image.getCanvasContext function --- lib/OpenLayers/Tile/Image.js | 32 ++++++++++++++++++++++++++++++++ tests/Tile/Image.html | 25 +++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/lib/OpenLayers/Tile/Image.js b/lib/OpenLayers/Tile/Image.js index 28dab38773..7baf8bf98e 100644 --- a/lib/OpenLayers/Tile/Image.js +++ b/lib/OpenLayers/Tile/Image.js @@ -81,6 +81,13 @@ OpenLayers.Tile.Image = OpenLayers.Class(OpenLayers.Tile, { */ maxGetUrlLength: null, + /** + * Property: canvasContext + * {CanvasRenderingContext2D} A canvas context associated with + * the tile image. + */ + canvasContext: null, + /** TBD 3.0 - reorder the parameters to the init function to remove * URL. the getUrl() function on the layer gets called on * each draw(), so no need to specify it here. @@ -216,6 +223,7 @@ OpenLayers.Tile.Image = OpenLayers.Class(OpenLayers.Tile, { } OpenLayers.Element.removeClass(img, "olImageLoadError"); } + this.canvasContext = null; }, /** @@ -369,6 +377,7 @@ OpenLayers.Tile.Image = OpenLayers.Class(OpenLayers.Tile, { img.style.opacity = this.layer.opacity; this.isLoading = false; + this.canvasContext = null; this.events.triggerEvent("loadend"); // IE<7 needs a reflow when the tiles are loaded because of the @@ -409,6 +418,29 @@ OpenLayers.Tile.Image = OpenLayers.Class(OpenLayers.Tile, { } }, + /** + * APIMethod: getCanvasContext + * Returns a canvas context associated with the tile image (with + * the image drawn on it). + * Returns undefined if the browser does not support canvas, if + * the tile has no image or if it's currently loading. + * + * Returns: + * {Boolean} + */ + getCanvasContext: function() { + if (OpenLayers.CANVAS_SUPPORTED && this.imgDiv && !this.isLoading) { + if (!this.canvasContext) { + var canvas = document.createElement("canvas"); + canvas.width = this.size.w; + canvas.height = this.size.h; + this.canvasContext = canvas.getContext("2d"); + this.canvasContext.drawImage(this.imgDiv, 0, 0); + } + return this.canvasContext; + } + }, + CLASS_NAME: "OpenLayers.Tile.Image" }); diff --git a/tests/Tile/Image.html b/tests/Tile/Image.html index 0b42064265..f202f6dfe1 100644 --- a/tests/Tile/Image.html +++ b/tests/Tile/Image.html @@ -399,6 +399,31 @@ map.destroy(); }); } + + function test_getCanvasContext(t) { + if (!OpenLayers.CANVAS_SUPPORTED) { + t.plan(0); + } else { + t.plan(1); + + var map = new OpenLayers.Map('map'); + var layer = new OpenLayers.Layer.WMS("OpenLayers WMS", + "http://labs.metacarta.com/wms/vmap0?", {layers: 'basic'}); + map.addLayer(layer); + map.setCenter(new OpenLayers.LonLat(0, 0), 5); + + t.delay_call(5, function() { + var tile = layer.grid[0][0]; + if (tile.isLoading) { + t.ok(false, "test_getCanvasContext timeout"); + } else { + t.ok(tile.getCanvasContext() instanceof CanvasRenderingContext2D, + "getCanvasContext() returns CanvasRenderingContext2D instance"); + } + map.destroy(); + }); + } + }