CanvasRenderingContext2D .globalCompositeOperation property of the Canvas 2D API sets the type of compositing operation to apply when drawing new shapes.

另请参阅 Compositing and clipping Canvas Tutorial .

句法

ctx.globalCompositeOperation = type;
					

type 字符串 identifying which of the compositing or blending mode operations to use.

类型

范例

Changing the composite operation

此范例使用 globalCompositeOperation property to draw two rectangles that exclude themselves where they overlap.

HTML

<canvas id="canvas"></canvas>
					

JavaScript

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.globalCompositeOperation = 'xor';
ctx.fillStyle = 'blue';
ctx.fillRect(10, 10, 100, 100);
ctx.fillStyle = 'red';
ctx.fillRect(50, 50, 100, 100);
					

结果

规范

规范 状态 注释
HTML 实时标准
The definition of 'CanvasRenderingContext2D.globalCompositeOperation' in that specification.
实时标准
Compositing and Blending Level 1 候选推荐

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
globalCompositeOperation Chrome 1 Edge 12 Firefox 1.5 IE 9 Opera 9 Safari 2 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0

图例

完整支持

完整支持

  • In WebKit- and Blink-based Browsers, a non-standard and deprecated method ctx.setCompositeOperation() is implemented besides this property.
  • 支持 "plus-darker" and "darker" were removed in Chrome 48. Developers looking for a replacement should use "darken" .

Gecko-specific notes

  • An early Canvas specification draft specified the value "darker" . However, Firefox removed support for "darker" in version 4 ( bug 571532 )。另请参阅 this blog post that suggests using "difference" as a way to achieve a similar affect to "darker" .

另请参阅

元数据

  • 最后修改: