CanvasRenderingContext2D.globalCompositeOperation - Web APIs 编辑

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

See also Compositing and clipping in the Canvas Tutorial.

Syntax

ctx.globalCompositeOperation = type;

type is a String identifying which of the compositing or blending mode operations to use.

Examples

Changing the composite operation

This example uses the globalCompositeOperation property to draw two rectangles that exclude themselves where they overlap.

HTML

<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);

Result

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of 'CanvasRenderingContext2D.globalCompositeOperation' in that specification.
Living Standard 
Compositing and Blending Level 1Candidate Recommendation 

Browser compatibility

BCD tables only load in the browser

WebKit/Blink-specific notes

  • In WebKit- and Blink-based Browsers, a non-standard and deprecated method ctx.setCompositeOperation() is implemented besides this property.
  • Support for "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). See also this blog post that suggests using "difference" as a way to achieve a similar affect to "darker".

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:69 次

字数:3537

最后编辑:2 年前

最近更新:虐人心

编辑次数:1 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文