正如許多操作,錯誤對globalCompositeOperation的輸入只是被忽略,所以很容易看到是否支持給定模式:只需設置它,然後閱讀當前模式以檢查它是否是您提供的模式。
所以僅僅是明確的,你可以使用一些功能,像這樣的:
// setCompositeMode : sets the globalCompositeOperation on provided context.
// return true if mode is supported, false otherwise.
function setCompositeMode(ctx, newMode) {
ctx.globalCompositeOperation = newMode;
return (ctx.globalCompositeOperation == newMode) ;
}
只是一個小例子:
var cv=document.createElement('canvas');
var ctx= cv.getContext('2d');
console.log(" Current (default) composite mode : " + ctx.globalCompositeOperation) ;
console.log("setting 'destination-in'.");
ctx.globalCompositeOperation = 'destination-in';
console.log(" composite mode : " + ctx.globalCompositeOperation) ;
console.log("setting 'multiply'.");
ctx.globalCompositeOperation = 'multiply';
console.log(" composite mode : " + ctx.globalCompositeOperation) ;
console.log("setting 'i-don-t-exist' .");
ctx.globalCompositeOperation = 'i-don-t-exist';
console.log(" composite mode : " + ctx.globalCompositeOperation) ;
輸出(在Chrome/MAC OS,乘支持):
" Current (default) composite mode : source-over"
"setting 'destination-in'."
" composite mode : destination-in"
"setting 'multiply'."
" composite mode : multiply"
"setting 'i-don-t-exist' ."
" composite mode : multiply"
到目前爲止,移動瀏覽器仍然不支持'multiply'值嗎?在2016年甚至有問題,以獲得從ios設備上的桌面呈現相同的效果(根本沒有出現在ios safari和chrome上) – bigp 2016-05-30 11:39:02