您可以使用 2D 画布作为屏幕画布,并在更新 WebGL 画布时绘制 WebGL 画布,然后再绘制任何注释。
drawWebGLStuff(webGLCtx);
// Copy image data of WebGL canvas to 2D canvas.
// This should be done right after WebGL rendering,
// unless preserveDrawingBuffer: true is passed during WebGL context creation,
// since WebGL will swap buffers by default, leaving no image in the drawing buffer,
// which is what we want to copy.
ctx2D.drawImage(webGLCanvas, 0, 0);
drawAnnotations(ctx2D);
(注释可以从形状列表中呈现每一帧,或绘制到另一个屏幕外画布,然后将其绘制到类似于 WebGL 画布的主(屏幕上)画布。)
或者您可以简单地在页面上使用绝对定位和分层画布z-index
:
<div style="position: relative;">
<canvas id="layer1" width="100" height="100"
style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas>
<canvas id="layer2" width="100" height="100"
style="position: absolute; left: 0; top: 0; z-index: 1;"></canvas>
</div>