Simplifying hit detection for external graphics rendered with canvas. With this change, events within the graphic bounds will be considered hits (previously only events over visible pixels in the external graphic were considered hits). This simplification gets around same origin limitations. r=erilem (closes #3238)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@11859 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2011-04-03 04:58:49 +00:00
parent 7e60535fe7
commit 62dd64c1c9

View File

@@ -60,8 +60,6 @@ OpenLayers.Renderer.Canvas = OpenLayers.Class(OpenLayers.Renderer, {
if (this.hitDetection) {
this.hitCanvas = document.createElement("canvas");
this.hitContext = this.hitCanvas.getContext("2d");
this.hitGraphicCanvas = document.createElement("canvas");
this.hitGraphicContext = this.hitGraphicCanvas.getContext("2d");
}
},
@@ -113,11 +111,6 @@ OpenLayers.Renderer.Canvas = OpenLayers.Class(OpenLayers.Renderer, {
hitCanvas.style.height = size.h + "px";
hitCanvas.width = size.w;
hitCanvas.height = size.h;
var hitGraphicCanvas = this.hitGraphicCanvas;
hitGraphicCanvas.style.width = size.w + "px";
hitGraphicCanvas.style.height = size.h + "px";
hitGraphicCanvas.width = size.w;
hitGraphicCanvas.height = size.h;
}
},
@@ -232,26 +225,8 @@ OpenLayers.Renderer.Canvas = OpenLayers.Class(OpenLayers.Renderer, {
img, x, y, width, height
);
if (this.hitDetection) {
var hitGraphicContext = this.hitGraphicContext;
var hitContext = this.hitContext;
hitGraphicContext.clearRect(0, 0, numRows, numCols);
hitGraphicContext.drawImage(
img, 0, 0, width, height
);
var imagePixels = hitGraphicContext.getImageData(0, 0, width, height).data;
var indexData = hitContext.createImageData(width, height);
var indexPixels = indexData.data;
var pixelIndex;
for (var i=0, len=imagePixels.length; i<len; i+=4) {
// look for visible pixels
if (imagePixels[i+3] > 0) {
indexData[i] = red;
indexPixels[i+1] = green;
indexPixels[i+2] = blue;
indexPixels[i+3] = 255;
}
}
hitContext.putImageData(indexData, x, y);
this.setHitContextStyle("fill", featureId);
this.hitContext.fillRect(x, y, width, height);
}
};