Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

globalCompositeOperation

I have tried to use globalCompositeOperation in a loop passing it different string (source-atop, source-over etc.) in the same 2D context but I noticed that Firefox let draw me only few shapes while Opera only the last.

Now, my question is can I use only ONE globalCompositeOperation at time into the current context?

like image 370
xdevel2000 Avatar asked Dec 19 '25 08:12

xdevel2000


2 Answers

The reason you're noticing this issue is the modes you're choosing aren't supported by the browser properly. There are some issues between browsers concerning the globalCompositeOperation. At this moment, there are only a few modes that work between browsers (Chrome/Safari/Opera/Firefox) without quirks:

  • source-over
  • source-atop
  • destination-over
  • destination-out
  • lighter
  • xor

To learn more check out the following link;

http://www.rekim.com/2011/02/11/html5-canvas-globalcompositeoperation-browser-handling/

As for your 2nd question, you can only use one mode at a time. This is unfortunate, because "light" and "darker" are more-like "blend-modes", and would be very useful to use with some of the other composite modes. I would love to see this change.

like image 164
Michael Deal Avatar answered Dec 20 '25 21:12

Michael Deal


In short, yes.

The last globalCompositeOperation value takes place before a render, e.g. drawImage(),fillRect().

You can change it immediately after drawing to apply it to the next drawing like:

http://jsfiddle.net/eCDRN/

ctx.globalCompositeOperation = "copy";
ctx.fillRect(100, 100, 100, 100);
ctx.globalCompositeOperation = "destination-in";
ctx.fillRect(150, 150, 100, 100);
ctx.globalCompositeOperation = "xor";
ctx.fillRect(175, 175, 100, 100);
like image 30
Jaibuu Avatar answered Dec 20 '25 21:12

Jaibuu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!