【发布时间】:2021-05-16 20:40:26
【问题描述】:
有很多关于在画布中平移背景图像的问题(即在游戏中以角色为中心模拟“相机”) - 大多数答案建议使用画布的translate 方法。
但是既然你必须在每一帧中重新绘制图像,为什么不直接剪辑呢?效率方面重要吗?
尤其是这样的平移是个坏主意吗? (这个例子展示了一个简化的相机平移向一个方向移动)
let drawing = new Image();
drawing.src = "img_src";
drawing.onload = function () {
ctx.drawImage(drawing, 0, 0);
let pos = 0
setInterval(() => {
pos += 1
ctx.clearRect(0, 0, cnvs.width, cnvs.height);
ctx.drawImage(drawing, -pos, 0 ); // "pans" the image to the left using the option `drawImage` parameters `sx` and `sy`
}, 33);
};
示例结果:fiddle
提前致谢!
【问题讨论】:
-
你说得对,我以前的解决方案只适用于静态图像,我已经删除了它。
标签: javascript performance html5-canvas drawimage pan