我已经盯着这个太久了,需要一些帮助。我有以下简化的小提琴示例,适用于 IE,但不适用于 Chrome(没有错误,只是在 Chrome 中清除画布后没有重绘图像):
var myApp = {};
// redraw function that will be called during init and then will loop.
myApp.redrawFunction = function() {
window.requestAnimationFrame(myApp.redrawFunction);
// clear canvas (not needed in this simplified example, but I need it in my real app)
myApp.drawingContext.clearRect(0, 0, myApp.canvas.width, myApp.canvas.height);
// draw an image.
var myImage = new Image();
myImage.onload = function() {
myApp.drawingContext.drawImage(myImage, 0, 0);
};
myImage.src= "http://www.w3.org/html/logo/img/mark-word-icon.png";
};
// initialize
$(function () {
// setup the animation frame objects.
window.requestAnimationFrame = window.requestAnimationFrame
|| window.mozRequestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.msRequestAnimationFrame;
window.cancelAnimationFrame = window.CancelAnimationFrame
|| window.CancelRequestAnimationFrame
|| window.mozCancelAnimationFrame
|| window.mozCancelRequestAnimationFrame
|| window.msCancelAnimationFrame
|| window.msCancelRequestAnimationFrame
|| window.webkitCancelAnimationFrame
|| window.webkitCancelRequestAnimationFrame;
// get canvas references.
myApp.canvas = $("canvas")[0];
myApp.drawingContext = myApp.canvas.getContext("2d");
$(myApp.canvas).css("background-color", "#999999");
// update canvas size to fill screen.
myApp.canvas.width = $(window).width();
myApp.canvas.height = $(window).height();
$(window).resize(function (){
myApp.canvas.width = $(window).width();
myApp.canvas.height = $(window).height();
});
// start redraw loop
myApp.redrawFunction();
});
如果你在 IE 10 中打开这个小提琴,它就可以工作(你会看到一个徽标)。如果你在 Chrome 中打开它,你会看到一个空白的画布,这表明我的 drawImage 调用在清除画布后不起作用。有什么我想念的想法吗?