Skip to content

Commit 12fd18f

Browse files
committed
mostly remove the svg onClick hack
- chrome no longer throws a security warning when trying to read pixel data on a canvas after drawing *any* svg; only svgs with a <foreignObject> tag in them - Standard Safari is still an old enough WebKit that it has this problem, but WebKit-nightly (SVN r173347) does not. - instead of the blanket check, just try it and catch errors
1 parent 318f583 commit 12fd18f

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

js/Sprite.js

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -234,31 +234,30 @@ Sprite.prototype.onClick = function(evt) {
234234
var mouseX = runtime.mousePos[0] + 240;
235235
var mouseY = 180 - runtime.mousePos[1];
236236

237-
if (this.mesh.src.indexOf('.svg') == -1) {
238-
// HACK - if the image SRC doesn't indicate it's an SVG,
239-
// then we'll try to detect if the point we clicked is transparent
240-
// by rendering the sprite on a canvas. With an SVG,
241-
// we are forced not to do this for now by Chrome/Webkit SOP:
242-
// http://code.google.com/p/chromium/issues/detail?id=68568
243-
var canv = document.createElement('canvas');
244-
canv.width = 480;
245-
canv.height = 360;
246-
var ctx = canv.getContext('2d');
247-
var drawWidth = this.textures[this.currentCostumeIndex].width;
248-
var drawHeight = this.textures[this.currentCostumeIndex].height;
249-
var scale = this.scale / (this.costumes[this.currentCostumeIndex].bitmapResolution || 1);
250-
var rotationCenterX = this.costumes[this.currentCostumeIndex].rotationCenterX;
251-
var rotationCenterY = this.costumes[this.currentCostumeIndex].rotationCenterY;
252-
ctx.translate(240 + this.scratchX, 180 - this.scratchY);
253-
ctx.rotate(this.rotation * Math.PI / 180.0);
254-
ctx.scale(scale, scale);
255-
ctx.translate(-rotationCenterX, -rotationCenterY);
256-
ctx.drawImage(this.mesh, 0, 0);
257-
237+
var canv = document.createElement('canvas');
238+
canv.width = 480;
239+
canv.height = 360;
240+
var ctx = canv.getContext('2d');
241+
var drawWidth = this.textures[this.currentCostumeIndex].width;
242+
var drawHeight = this.textures[this.currentCostumeIndex].height;
243+
var scale = this.scale / (this.costumes[this.currentCostumeIndex].bitmapResolution || 1);
244+
var rotationCenterX = this.costumes[this.currentCostumeIndex].rotationCenterX;
245+
var rotationCenterY = this.costumes[this.currentCostumeIndex].rotationCenterY;
246+
ctx.translate(240 + this.scratchX, 180 - this.scratchY);
247+
ctx.rotate(this.rotation * Math.PI / 180.0);
248+
ctx.scale(scale, scale);
249+
ctx.translate(-rotationCenterX, -rotationCenterY);
250+
ctx.drawImage(this.mesh, 0, 0);
251+
252+
var alpha;
253+
try {
258254
var idata = ctx.getImageData(mouseX, mouseY, 1, 1).data;
259-
var alpha = idata[3];
260-
} else {
261-
var alpha = 1;
255+
alpha = idata[3];
256+
} catch (e) {
257+
// as of 2014-09-06, WebKit/Safari still throws a security exception trying to read
258+
// image data from a canvas after any svg has been draw to it. Version 7.0.6 (9537.78.2)
259+
// WebKit-nightly (SVN-r173347) does not have this issue.
260+
alpha = 1;
262261
}
263262

264263
if (alpha > 0) {

0 commit comments

Comments
 (0)