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
This commit is contained in:
Chad Walker 2014-09-06 02:35:11 -05:00
parent 318f5833bf
commit 12fd18f9f4

View file

@ -234,31 +234,30 @@ Sprite.prototype.onClick = function(evt) {
var mouseX = runtime.mousePos[0] + 240; var mouseX = runtime.mousePos[0] + 240;
var mouseY = 180 - runtime.mousePos[1]; var mouseY = 180 - runtime.mousePos[1];
if (this.mesh.src.indexOf('.svg') == -1) { var canv = document.createElement('canvas');
// HACK - if the image SRC doesn't indicate it's an SVG, canv.width = 480;
// then we'll try to detect if the point we clicked is transparent canv.height = 360;
// by rendering the sprite on a canvas. With an SVG, var ctx = canv.getContext('2d');
// we are forced not to do this for now by Chrome/Webkit SOP: var drawWidth = this.textures[this.currentCostumeIndex].width;
// http://code.google.com/p/chromium/issues/detail?id=68568 var drawHeight = this.textures[this.currentCostumeIndex].height;
var canv = document.createElement('canvas'); var scale = this.scale / (this.costumes[this.currentCostumeIndex].bitmapResolution || 1);
canv.width = 480; var rotationCenterX = this.costumes[this.currentCostumeIndex].rotationCenterX;
canv.height = 360; var rotationCenterY = this.costumes[this.currentCostumeIndex].rotationCenterY;
var ctx = canv.getContext('2d'); ctx.translate(240 + this.scratchX, 180 - this.scratchY);
var drawWidth = this.textures[this.currentCostumeIndex].width; ctx.rotate(this.rotation * Math.PI / 180.0);
var drawHeight = this.textures[this.currentCostumeIndex].height; ctx.scale(scale, scale);
var scale = this.scale / (this.costumes[this.currentCostumeIndex].bitmapResolution || 1); ctx.translate(-rotationCenterX, -rotationCenterY);
var rotationCenterX = this.costumes[this.currentCostumeIndex].rotationCenterX; ctx.drawImage(this.mesh, 0, 0);
var rotationCenterY = this.costumes[this.currentCostumeIndex].rotationCenterY;
ctx.translate(240 + this.scratchX, 180 - this.scratchY);
ctx.rotate(this.rotation * Math.PI / 180.0);
ctx.scale(scale, scale);
ctx.translate(-rotationCenterX, -rotationCenterY);
ctx.drawImage(this.mesh, 0, 0);
var alpha;
try {
var idata = ctx.getImageData(mouseX, mouseY, 1, 1).data; var idata = ctx.getImageData(mouseX, mouseY, 1, 1).data;
var alpha = idata[3]; alpha = idata[3];
} else { } catch (e) {
var alpha = 1; // as of 2014-09-06, WebKit/Safari still throws a security exception trying to read
// image data from a canvas after any svg has been draw to it. Version 7.0.6 (9537.78.2)
// WebKit-nightly (SVN-r173347) does not have this issue.
alpha = 1;
} }
if (alpha > 0) { if (alpha > 0) {