javascript - HTML5 Canvas 混合模式

标签 javascript canvas

我是 HTML5 Canvas 的新手,我想在 ActionScript 3 中重现 BlendMode.ADD 的结果。

根据文档,这是 BlendMode.ADD 的作用:

Adds the values of the constituent colors of the display object to the colors of its background, applying a ceiling of 0xFF. This setting is commonly used for animating a lightening dissolve between two objects.

For example, if the display object has a pixel with an RGB value of 0xAAA633, and the background pixel has an RGB value of 0xDD2200, the resulting RGB value for the displayed pixel is 0xFFC833 (because 0xAA + 0xDD > 0xFF, 0xA6 + 0x22 = 0xC8, and 0x33 + 0x00 = 0x33).

来源: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BlendMode.html#ADD

如何在 HTML5 Canvas 中对图像执行相同的操作?

最佳答案

specification的 2D Canvas 实现了名为“lighter”的混合模式(不要与 “lighten” 混淆,后者是不同的模式),它将执行“add” .

var ctx = c.getContext("2d");
ctx.fillStyle = "#037";
ctx.fillRect(0, 0, 130, 130);

ctx.globalCompositeOperation = "lighter";  // AKA add / linear-dodge
ctx.fillStyle = "#777";
ctx.fillRect(90, 20, 130, 130);
<canvas id=c></canvas>

(更新:我记得(错误地)lighten 作为它的名称,对于原始版本答案中额外的手动步骤感到抱歉)。

关于javascript - HTML5 Canvas 混合模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42740839/

相关文章:

iOS - 如何在 iOS 上实现类似粒子动画的效果

javascript - 平铺 map 坐标

javascript - 如何从我的 EJS 模板中获取属性列表?

javascript - 将事件添加到 HTML 输入数组的所有元素并获取数组中的值

javascript - 逻辑相似代码的圈复杂度

javascript - 在 javascript 图表上显示交易量配置文件

javascript - image.onload 到方法中

javascript - 每次我再次运行时,动画变得越来越快

javascript - 使用 Chart.js 绘制条形图

javascript - 从 10 到零的循环倒计时 JavaScript