New OpenLayers.Tile.Image.getCanvasContext function

This commit is contained in:
fredj
2012-01-18 14:06:22 +01:00
parent 57ae02f381
commit b4ac0af5d8
2 changed files with 57 additions and 0 deletions

View File

@@ -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"
});

View File

@@ -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();
});
}
}
</script>
</head>