简体   繁体   English

html5 Canvas中的自定义globalCompositeOperation

[英]Custom globalCompositeOperation in html5 Canvas

I'm looking at all of the different types of global composite operations here: 我在这里查看所有不同类型的全局复合操作:

https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation

None of them do exactly what I want to do. 他们都没有完全按照我的意愿去做。 Is there a way to define a custom globalCompositeOperation? 有没有一种方法可以定义自定义的globalCompositeOperation? It would be perfect if I could create a shader and then have it be used everytime I draw something with a CanvasRenderingContext2D.draw method. 如果我可以创建一个着色器,然后每次使用CanvasRenderingContext2D.draw方法绘制某些东西时都使用它,那将是完美的。

Specifically, on a per pixel basis, I want the following (psuedo code) operation to be used by the CanvasRenderingContext2D.draw methods: 具体来说,基于每个像素,我希望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

Am I even thinking about this correctly? 我什至在考虑正确吗? I'm sensing that I should be using WebGLRenderingContext somehow, but I'm a little shaky on how it all fits together. 我感觉到我应该以某种方式使用WebGLRenderingContext,但是我对如何将它们组合在一起感到有些困惑。

The answer is mostly no. 答案大多是不。

There is no way to define your own globalCompositeOperation with Canvas2D. 有没有办法来定义自己的globalCompositeOperation与Canvas2D。

2 solutions off the top of my head 2种解决方案

  1. Use 2 canvases, a 2d one and WebGL one 使用2个画布,一个2d画布和一个WebGL画布

     for each shape clear the 2d canvas draw the shape into the 2d canvas upload the canvas to a webgl texture composite that texture with the previous results using a custom shader. 

    The problem with this solution is it will be slow because uploading your canvas to a texture is a relatively slow operation. 此解决方案的问题是,它会很慢,因为将画布上传到纹理是相对缓慢的操作。 But, it means you can use all of the canvas functions like stroke and arc and gradients etc to build it shape. 但是,这意味着您可以使用所有画布功能(例如strokearc以及渐变等)来构建形状。

  2. Switch entirely to WebGL 完全切换到WebGL

    The problem here is you won't have access to all the features of the 2D API and reproducing ALL of them is a lot of work. 这里的问题是您将无法访问2D API的所有功能,因此要重现所有这些功能需要做很多工作。

    On the other hand, if you are only using a limited set of them then it may not be too much work. 另一方面,如果仅使用有限的一组,则可能不会做太多工作。 For example if you are only using drawImage , fillRect , clear , and maybe moveTo and lineTo , fill and stroke then it would be relatively easy to reproduce in WebGL. 例如,如果仅使用drawImagefillRectclear ,还可能使用moveTolineTofillstroke那么在WebGL中复制起来将相对容易。 If you are using lots of the features like masking, bezier curves, gradients, or patterns it starts to become much more work. 如果您使用诸如遮罩,贝塞尔曲线,渐变或图案之类的许多功能,那么它将开始变得越来越有用。

As a starter here's a tutorial that presents a certain kind of compositing or image processing which is the basic technique for globalCompositeOperation in WebGL. 首先, 这里是一个教程,介绍某种合成或图像处理 ,这是WebGL中globalCompositeOperation的基本技术。 Both of the solutions above would require this basic type of solution to composite the results after each shape . 上面的两个解决方案都需要这种基本类型的解决方案,以便在每个shape之后合成结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM