I want to make a HTML select
list, with which I can choose which type of globalCompositeOperation
will be applied when blending two canvas
elements, like this:
<select name="blending-modes" id="blending-modes">
<option value="source-over">source-over</option>
<option value="source-in">source-in</option>
<option value="source-out">source-out</option>
...
</select>
Is there a way to programatically get list of available globalCompositeOperation
types as a Javascript object or array, so it could be used to populate select
element with data, instead of filling it manually? Is this information stored in some native variable?
I do not want to just verify whether or not some blending mode is supported by user's browser, as discussed here. I want to get a full list of supported globalCompositeOperation
types in order to chose blending mode in a browser.
I've just converted Kaiido's solution into an js object with a public test(blendModeName) method. Maybe it's of use to someone.
With this you can test for specific blend modes, instead of testing all at once.
No there is no native property telling us which are the
globalCompositeOperation
modes that the browser supports.You'll have to test it by looping through all spec defined ones, and check if it is still the one you just set :
But there is one caveat / bug because Safari (at least 9.0.1) does accept the
"hue"
,"saturation"
,"color"
and"luminosity""
modes, but doesn't actually support it...So here I made a function to test the different modes.
The idea is to draw two 3x3px canvases filled with a solid color onto a third one. The first one is painted in the top-left corner and the second one in the bottom-left, each of them sharing a pixel in the central pixel of the third canvas.
Obviously this is slower than the property check, but you should only need it once per page so performance might not be an issue.