Friday, 8 December 2017

javascript - getimagedata not working in firefox

I got a problem with the HTML5 element
getImageData()
I'm using wScratchPad for a scratch tool on a website and using
the percentage function in wScratchPad to determine when to remove the overlay. It's
working fine on IE9, Safari & Chrome. But it throws a SECURITY ERROR in
Firefox.



I had the same problem in Chrome and
IE9 too but fixed it by moving the JS, Images and HTML to the same
server.



I know it throws an error if the domain
of the JS and Canvas Image is not on the same domain so I checked the document.domain of
both the image and the js. It looks like the JS as the 'document.domain = domain.com'
and the image has document.domain =
www.domain.com.



Could that be the problem? If so
how can I fix it?
I already saw a way to override the browser to get a alert
for the user confirmation of the script but seeing as this is a high profile website
this is out of the question.



Below the code in
wScratchPad which causes the
error:



scratchPercentage:
function($this)
{
var hits = 0;
var imageData =
$this.ctx.getImageData(0,0,$this.canvas.width,$this.canvas.height)


for(var i=0, ii=imageData.data.length; i {

if(imageData.data[i] == 0 && imageData.data[i+1] == 0 &&
imageData.data[i+2] == 0 && imageData.data[i+3] == 0) hits++;

}

return (hits / $this.pixels) *
100;
},

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...