画布图像行为在Safari中正常,但在其他浏览器中则不然
Canvas image behaviour normal in Safari, but not other browsers
我在Safari中成功地将图像加载到了画布元素中。它的行为正常,我可以按预期在画布上移动它。然而,当在Firefox、Chrome或IE中测试时,图像会加载,但只要我按键移动它,图像就会消失。
我看过谷歌Chrome中没有显示的画布上绘制的图像,认为加速2D功能可能也会导致它无法正确绘制,但这已经被禁用了。我真的不确定是什么原因造成了这个问题?
编辑:我在控制台上也出现了以下错误:"未捕获的安全性错误:无法在'CanvasRenderingContext2D'上执行'getImageData':画布已被交叉源数据污染。checkForCollision drawFrame"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title> My title </title>
</head>
<h2>Your current time:</h2>
<h1>
<label class="timer" id="minutes">00</label>:<label id="seconds">00
</label>
</h1>
<body>
<section>
<canvas id="canvas"> </canvas>
<img id="sprite" src="http://i.imgur.com/dudnUyL.png">
<div>
<h3><button id="resetme" onclick="loadHard()">Reload Maze</button></h3>
</div>
<script type="text/javascript">
var canvas;
var context;
var x = 0;
var y = 0; //positioning of the sprite
var dx = 0;
var dy = 0; //momentum of the sprite at start
window.onload = function() {
canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
//ctx.save();
drawMaze("http://i.imgur.com/Fbhw8NT.png", 268, 225);
window.onkeydown = processKey;
};
var timer;
function drawMaze(mazeFile, Xstart, Ystart) {
clearTimeout(timer);
dx = 0;
dy = 0; //if the sprite is already moving, stop it
var imgMaze = new Image();
imgMaze.onload = function() {
canvas.width = imgMaze.width;
canvas.height = imgMaze.height;
context.drawImage(imgMaze, 0,0);
x = Xstart;
y = Ystart;
var imgSprite = document.getElementById("sprite");
context.drawImage(imgSprite, x, y);
context.stroke();
timer = setTimeout(drawFrame, 10);
};
imgMaze.src = mazeFile;
}
function processKey(e) { //e needs to be used for event handling
dx = 0;
dy = 0;
//condition for the Up arrow being pressed
if (e.keyCode == 38) {
dy = -1;
}
//condition for the Left arrow being pressed
if (e.keyCode == 37) {
dx = -1;
}
//condition for the Down arrow being pressed
if (e.keyCode == 40) {
dy = 1;
}
//condition for the Right arrow being pressed
if (e.keyCode == 39) {
dx = 1;
}
//all of the above numbers from 37-40 are representative of ASCII values
}
function checkForCollision() {
var imgData = context.getImageData(x-1, y-1, 15+2, 15+2);
var pixels = imgData.data;
for (var i = 0; n = pixels.length, i < n; i += 4) {
var red = pixels[i];
var green = pixels[i+1];
var blue = pixels[i+2];
var alpha = pixels[i+3];
//now check for the black pixels for a wall
if (red == 0 && green == 0 && blue == 0) {
return true;
} //checks for a greyish colour - possibly the edge of a wall
if (red == 169 && green == 169 && blue == 169) {
return true;
}
}
return false; //there was no collision
}
function drawFrame() {
if (dx != 0 || dy != 0) {
context.beginPath();
context.fillStyle = "rgb(254,244,207)";
context.rect(x, y, 15, 15);
context.fill()
x += dx;
y += dy;
if (checkForCollision()) {
x -= dx;
y -= dy;
dx = 0;
dy = 0;
}
var imgSprite = document.getElementById("sprite");
context.drawImage(imgSprite, x, y);
if (y > (canvas.height - 17)) {
alert("You succeeded - hooray! That, or Pi's done..");
clearInterval(timeout);
minutesLabel.innerHTML = secondsLabel.innerHTML = "00";
location.reload();
return;
}
}
timer = setTimeout(drawFrame, 10);
}
function loadHard() {
drawMaze('http://i.imgur.com/Fbhw8NT.png', 268, 225);
console.log('idle');
totalSeconds = -1;
setTime();
}
//just some more JS code about a timer
</script>
</section>
</body>
</html>
这似乎是大多数浏览器都会检查的安全问题。如何修复getImageData()错误画布已被跨原点数据污染?
这导致JavaScript错误,导致页面无法工作。我可以通过在将所有图像加载到画布之前将其转换为DataURL来实现这一点,但您可能需要尝试上面链接中的一些其他建议。
相关文章:
- 是什么让一个“;Uncaught RangeError:超过了最大调用堆栈大小“;错误(Chrome,在其他浏览器中显示
- jquery代码在Mozilla中有效,但在其他浏览器上无效
- java脚本在Chrome和其他浏览器中对LocaleTimeString的不同行为
- Javascript的某些部分在Chrome中不起作用,但在其他浏览器中可以完美工作
- 如何隐藏空<span>在IE中使用javascript.(注意,在其他浏览器中工作正常)
- 我的时间倒计时脚本在谷歌浏览器中工作正常,但在其他浏览器中打开时它显示为南楠
- 使用一个Javascript函数提交多个表单可以在FF中工作,而不需要其他浏览器
- 带有Safari的Javascript;不起作用——所有其他浏览器都起作用
- Javascript可以在chrome中使用,但不能在其他浏览器中使用
- 有没有一种方法可以在过滤掉iOS Chrome、Firefox和其他浏览器的同时检测iOS safari
- 在其他浏览器中打开页面时,令牌未保存在$window.sessionStorage中
- 无法从IE中的data.responseText获取结果,但在其他浏览器中工作
- 在Firefox中使用锤子捏 错误地定向 - 适用于所有其他浏览器
- 索引在IE中导致错误;在其他浏览器中工作
- 将JSON对象转换为敲除observable在IE8中不起作用,但在所有其他浏览器中都起作用
- Javascript可以在Safari上运行,但不能在任何其他浏览器上运行
- 滑动在IE中不起作用.但它在所有其他浏览器中都能正常工作
- 滑块图像在Internet Explorer中不起作用,但在其他浏览器中有效
- 我怎样才能摆脱chrome或其他浏览器中的录音符号
- 这个JavaScript / jQuery代码是否在ie8或其他浏览器中泄漏