【发布时间】:2016-04-06 08:43:14
【问题描述】:
我在这里查看所有不同类型的全局复合操作:
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation
他们都没有做我想做的事。有没有办法定义自定义 globalCompositeOperation?如果我可以创建一个着色器,然后在每次使用 CanvasRenderingContext2D.draw 方法绘制某些东西时使用它,那将是完美的。
具体来说,在每个像素的基础上,我希望 CanvasRenderingContext2D.draw 方法使用以下(伪代码)操作:
if the existing canvas color alpha is 0.0,
then draw the new shape's color and set alpha to 0.1
if the existing canvas color is the same as the new shape's color
then increase the alpha, by 0.1
if the existing canvas color is different from the the new shape's color
then decrease the alpha by 0.1
我是否正确地考虑了这一点?我感觉我应该以某种方式使用 WebGLRenderingContext,但我对它们如何组合在一起有点犹豫。
【问题讨论】:
-
简短回答:不,你不能(或没有太多工作)。你应该在网上搜索,有各种各样的项目使用 webgl 进行图像处理,你可以轻松修改以适应你的需要。不要忘记 FF 和 Chrome 现在都提供了很棒的 webgl 调试工具。
-
您可以为 webGL 重构您的代码并获得着色器的好处。但是不要尝试扩展 CanvasRenderingContext2D —— 不需要。但是您可以使用现有画布和另一个内存画布轻松创建自己的自定义图像过滤器功能。内存中的画布将保存新图纸。然后在两个画布上
getImageData并根据您的自定义混合操作比较 + 操作 + 替换现有画布上的每个像素。注意:这是相当资源和 cpu 密集型的,因为使用 getImageData 进行合成无法卸载到 gpu。
标签: html5-canvas webgl globalcompositeoperation