javascript - context.drawImage 在 Chrome 中工作但在 Firefox 中不工作

标签 javascript jquery html css google-chrome

所以出于某种原因,我用于显示图像的 javascript 代码只能在 Chrome 中使用,而不能在任何其他浏览器中使用。

这是 JSFiddle:http://jsfiddle.net/LLy42oa0/

 var snowflakeImage = new Image('');
    snowflakeImage.ready = true;
    snowflakeImage.src = 'http://i.imgur.com/W0rqcLP.png';  

function drawSnowflakes() {
        for(var i = 0; i < snowflakesInAir.length; i++) {
            //Failsafe
            if(snowflakesInAir[i].x > canvas.width || snowflakesInAir[i].y > canvas.height) {
                i++;
            }
                context.drawImage(snowflakeImage, snowflakesInAir[i].x, snowflakesInAir[i].y);
        }
    }

任何帮助将不胜感激! 我试过切换到 image.onload() 但这似乎打破了我的 gameLoop。谢谢!

最佳答案

这是在 FireFox 中产生的错误:

IndexSizeError: Index or size is negative or greater than the allowed amount

,您可以找到问题的解决方案 here ,还有:

To clarify as well. Applying Math.round on the variables used in .drawImage() fixed this issue for me. Seems Firefox didn't like a fractioned number. – Justin Beaudry

关于javascript - context.drawImage 在 Chrome 中工作但在 Firefox 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27537963/

相关文章:

javascript - 如何设置 moment js 对象的 Unix 纪元?

javascript - 如何以图像居中的方式获取网站的位置设置?

javascript - 在JS中添加字符串

javascript - 当文件到达浏览器时捕获文件

javascript - 预填充页面上的表单数据从 session 存储重新加载

javascript - 如何更改按钮文本

html - 背景图片没有显示在 div 中?

html - Internet Explorer 中的大型重复背景图像

javascript - 如何使用 jQuery 将列表项存储在数组中

javascript - jQuery Tablesorter + 分页,对所有表格重新分页