简体   繁体   English

使用F5刷新页面时引发的画布变色错误

[英]Tainted canvas error thrown when refreshing the page with F5

I have problem with my code, which is quite similar to this CodePen . 我的代码有问题,这与此CodePen非常相似。

 var img = new Image(); img.src = 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/63/Diceros_bicornis.jpg/320px-Diceros_bicornis.jpg'; img.crossOrigin = "Anonymous" var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); img.onload = function() { ctx.drawImage(img, 0, 0); img.style.display = 'none'; }; var color = document.getElementById('color'); function pick(event) { var x = event.layerX; var y = event.layerY; var pixel = ctx.getImageData(x, y, 1, 1); var data = pixel.data; var rgba = 'rgba(' + data[0] + ', ' + data[1] + ', ' + data[2] + ', ' + (data[3] / 255) + ')'; color.style.background = rgba; color.textContent = rgba; } canvas.addEventListener('mousemove', pick); 
 <!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Pixel_manipulation_with_canvas --> <canvas id="canvas" width="300" height="227" style="float:left"></canvas> <div id="color" style="width:200px;height:50px;float:left"></div> 

It somehow certainly works when the page is first open. 当页面首次打开时,它肯定可以工作。 But when I refresh the page with F5 , it SOMETIMES starts throwing the tainted canvas error . 但是,当我用F5刷新页面时, 有时它会引发脏污画布错误 Only when I click x to close the tab, and then click the link again to open a brand new tab will it certainly work again. 只有当我单击x关闭选项卡,然后再次单击链接以打开全新的选项卡时,它肯定会再次起作用。

I already have img.crossOrigin = "Anonymous" . 我已经有了img.crossOrigin = "Anonymous"

That sounds like a chrome bug, but easily workaround-able : Set your crossOrigin attribute first. 听起来像chrome的bug,但是很容易解决:首先设置crossOrigin属性。

The first time the request is made, the crossOrigin attribute is set before the image has loaded, hence your browser will redo the request. 第一次发出请求时,将在加载图像之前设置crossOrigin属性,因此您的浏览器将重做该请求。

But when you reload the page, the image is already cached, and loads before the crossOrigin attribute is set. 但是,当您重新加载页面时,图像已经被缓存,并在设置crossOrigin属性之前加载。 Then your canvas will be tainted. 然后,您的画布将被污染。

For similar little bugs cases, you should also define your onload event handler before setting this src attribute : 对于类似的小bug,您还应该在设置此src属性之前定义onload事件处理程序:

 var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); var img = new Image(); img.crossOrigin = "Anonymous" img.onload = function() { ctx.drawImage(img, 0, 0); img.style.display = 'none'; }; // set the src last img.src = 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/63/Diceros_bicornis.jpg/320px-Diceros_bicornis.jpg'; var color = document.getElementById('color'); function pick(event) { var x = event.layerX; var y = event.layerY; var pixel = ctx.getImageData(x, y, 1, 1); var data = pixel.data; var rgba = 'rgba(' + data[0] + ', ' + data[1] + ', ' + data[2] + ', ' + (data[3] / 255) + ')'; color.style.background = rgba; color.textContent = rgba; } canvas.addEventListener('mousemove', pick); 
 <!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Pixel_manipulation_with_canvas --> <canvas id="canvas" width="300" height="227" style="float:left"></canvas> <div id="color" style="width:200px;height:50px;float:left"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM