在 JS 中,我有一些性能关键代码,基本上看起来像这样:
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
context.drawImage(otherImage);
我使用 Chrome 分析我的代码,我发现瓶颈是...clearRect
。
等等,什么?
这真的很愚蠢。我什至不需要 clearRect
!我目前正在触摸 context
的每个像素两次(一次用于 clearRect,一次用于 drawImage),这完全是浪费。理论上,我应该只需要做一次,直接将每个像素从 otherImage
复制到 context
。
我怎么能说“拜托,拧紧 alpha 混合等等,只需将上下文的内容替换为 otherImage
中的内容!”
最佳答案
您可以通过调用 context.globalCompositeOperation = 'source-over';
(默认)确保不进行任何混合,这仅执行 alpha 混合或 context.globalCompositeOperation = 'copy' ;
从字面上看,它只是将图像复制到现有内容之上 ( see the examples here )
关于javascript - 如何绘制图像()并用新内容替换旧内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43757621/