Here is an example image drawing here: https://webglfundamentals.org/webgl/lessons/webgl-image-processing.html
WebGL doesn't care if the sources are images, canvases, or videos. Therefore change the patterns from
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, someImage);
to
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, someCanvas);
Then write a fragment shader to mix 2 textures, as in
precision mediump float;
You need to set up 2 textures and tell the GLSL program which texture units you insert them into.
function setupTexture(canvas, textureUnit, program, uniformName) { var tex = gl.createTexture(); updateTextureFromCanvas(tex, canvas, textureUnit);
An example is here:
function main() { var canvas1 = document.getElementById("canvas1"); var canvas2 = document.getElementById("canvas2"); var ctx1 = canvas1.getContext("2d"); var ctx2 = canvas2.getContext("2d"); ctx1.fillStyle = "purple"; ctx1.arc(64, 64, 30, 0, Math.PI * 2, false); ctx1.fill(); ctx2.fillStyle = "cyan"; ctx2.fillRect(50, 10, 28, 108);
canvas { border: 2px solid black; width: 128px; height: 128px; }
<script src="https://twgljs.org/dist/3.x/twgl.min.js"></script> <script id="2d-vertex-shader" type="x-shader/x-vertex"> attribute vec2 a_position; attribute vec2 a_texCoord; uniform vec2 u_resolution; varying vec2 v_texCoord; void main() { </script> <script id="2d-fragment-shader" type="x-shader/x-fragment"> precision mediump float; </script> <script id="aa2d-fragment-shader" type="x-shader/x-fragment"> precision mediump float; </script> <canvas id="canvas1" width="128" height="128"></canvas> <canvas id="canvas2" width="128" height="128"></canvas> <canvas id="webgl" width="128" height="128"></canvas>
source share