我有一个Sprite原型,它有一个构造函数...
function Sprite(img) {
for (var property in img) this[property] = image[property];
}...that接收一个图像对象并制作它的副本。现在,我尝试使用drawImage绘制雪碧图:
Sprite.prototype.draw = function(ctx, x, y) {
ctx.drawImage(this, x, y);
}这给了我一个错误:
Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': No function was found that matched the signature provided.但是,如果我使用实际的Image对象和其他相同的代码,即:
Sprite.prototype.draw = function(ctx, x, y) {
ctx.drawImage(img, x, y);
}它完全按照它应该的方式工作(img是Image对象的全局名称)。
这是Sprite原型的完整代码。我不明白是什么不同导致了这种情况的发生,因为我只给Sprite添加了一个函数: draw。
发布于 2014-09-30 07:56:49
我猜你的意思是:
this[property] = img[property]除此之外,我认为你的复制品比你需要的要浅。查看此讨论,将原型方法放入您的副本中。How do I correctly clone a JavaScript object?
最后,我要提醒您,除非您正在以某种方式更改图像,否则重用相同的图像会更有效率。让画布对象引用同一图像。
https://stackoverflow.com/questions/26110510
复制相似问题