I’m trying to use a web-worker to render a scene with threejs. I want to render some dynamic font using a CanvasTexture. But I find that when I use canvas in web-worker from transferControlToOffscreen, if I change the text, the render loop will stop, but this doesn't happen with new OffscreenCanvas().
//if use new OffscreenCanvas, render loop will work fine.
//if not, canvas2 is transferd from main thread, when change the text, render loop will stop sometimes
//canvas2=new OffscreenCanvas(200,200);
canvas2.height = 200;
canvas2.width = 200;
let context = canvas2.getContext('2d');
context.font = "40px Helvetica";
context.fillStyle = 'red';
context.fillText("123", canvas2.width / 2, canvas2.height / 2);
this.sprite = new THREE.Sprite(new THREE.SpriteMaterial({map: new THREE.CanvasTexture(canvas2)}));
this.sprite.scale.set(50, 50, 50);
this.scene.add(this.sprite);
If I declare a canvas in the HTML and simply hide it, after transfered to worker, the problem doesn’t occur.
<canvas id="canvas3"style="display: none;" ></canvas>
// spriteCanvas2 = document.createElement('canvas').transferControlToOffscreen();
spriteCanvas2=document.getElementById('canvas3').transferControlToOffscreen();
The live sample is here.The top canvas run in main thread and the bottom run in worker. If click the bottom canvas, it will stop render sometimes. Some additional information is in the forum.
If run in local not codepen, it will trigger consistently in three clicks. If not trigger, please refresh the browser.
Could anyone tell the difference between them? Thanks in advance.
CanvasTexturewithtransferControlToOffscreen. If click the bottom canvas, it will stop render sometimes. If not, please refresh the browser.requestAnimationFrame(() => () => console.log("aa"))from the Worker's console never calls the callback. Removing the <canvas> from the main thread will unlock the rAF calls. Educated guess: they lock on the rendering pipeline and missed a message somewhere. However for me it's a 1 / 100 or so repro, not 1/3 at all.