简体   繁体   English

Canvas globalCompositeOperation模式应该来自drawImage吗?

[英]Should the Canvas globalCompositeOperation modes work from drawImage?

I am using globalCompositeOperation = 'copy' then a drawImage so that my new image replaces what is underneath. 我正在使用globalCompositeOperation ='copy'然后使用drawImage,以便我的新图像替换下面的内容。

This works fine in the latest Safari and Chrome browsers on Mac and Windows and also on the iPad. 这适用于Mac和Windows以及iPad上的最新Safari和Chrome浏览器。

It doesn't work in IE 9 and while it works in Firefox 3.6, it doesn't work in Firefox 4.0. 它在IE 9中不起作用,虽然它在Firefox 3.6中有效,但它在Firefox 4.0中不起作用。

What seems to happen when it isn't working is the whole canvas gets erased, not just the area in the rect I pass to drawImage. 当它不工作时似乎发生的是整个画布被擦除,而不仅仅是我传递给drawImage的矩形区域。

I think this is a bug in IE 9 and firefox 4.0 but should I not expect it to work? 我认为这是IE 9和Firefox 4.0中的一个错误,但我不应该期望它能够工作吗?

These are the images of what Mozilla is assuming the Canvas is supposed to work: 这就是所谓的Mozilla是假设画布应该工作的图片

https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Canvas_tutorial/Compositing#globalCompositeOperation https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Canvas_tutorial/Compositing#globalCompositeOperation

The problem is that the spec is written rather vaguely here. 问题是规范在这里写得相当模糊。 For example, for the copy composite type, it reads: 例如,对于复制复合类型,它显示为:

Display the source image instead of the destination image.

Which could be taken to be a synonym of source-over, or it could be taken to mean "clear the entire canvas and then copy the new drawing operation onto the canvas" 这可以被视为source-over的同义词,或者它可以被理解为“清除整个画布然后将新的绘制操作复制到画布上”

To see how the different browsers differ in their implementations, use the images provided by mozilla above with this live-firing (not images, canvases) of all the different composite shapes that has also provided by Mozilla: 要了解不同浏览器的实现方式有何不同,请使用上面mozilla提供的图像,以及Mozilla提供的所有不同复合形状的实时触发(不是图像,画布):

https://developer.mozilla.org/samples/canvas-tutorial/6_1_canvas_composite.html https://developer.mozilla.org/samples/canvas-tutorial/6_1_canvas_composite.html

Just looking quickly it seems that Chrome 11 disagrees with Firefox 4 on source-in, source-out, destination-in, destination-atop, darker, and copy. 快速浏览一下,Chrome 11似乎不同意Firefox 4的源代码,源代码,目标位置,目标位置,更暗和复制。

It seems at a glance that the thing to note is that all of the differences are related to whether the non-pertinent pixels should be cleared or not. 一目了然,需要注意的是,所有差异都与是否应该清除不相关的像素有关。 Mozilla seems to think so, Google does not. Mozilla似乎这么认为,Google没有。

When you use drawImage() with globalCompositeOperation="copy", it's unexpected that the canvas is cleared outside the destination rectangle. 当您使用带有globalCompositeOperation =“copy”的drawImage()时,意外的是在目标矩形之外清除画布。 But this seems to be the consensus of how to interpret the standard. 但这似乎是如何解释标准的共识。 Even with fillRect() or when drawing a line, the whole canvas is cleared. 即使使用fillRect()或绘制线条,也会清除整个画布。 You have to set a clipping region to prevent that. 您必须设置剪裁区域以防止这种情况。

For a discussion see: https://bugzilla.mozilla.org/show_bug.cgi?id=366283 有关讨论,请参阅: https//bugzilla.mozilla.org/show_bug.cgi?id = 366283

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM