I'm trying to use the new API for the OffScreenCanvas. The idea is to move all logic regarding drawing and updating data for player, but leave some other logic in the main thread, like touch events (because worker cannot reach window object).
So I have class Player.js
export class Player {
constructor() {
this.width = 100;
this.height = 100;
this.posX = 0;
this.posY = 0;
}
draw = (ctx) => {
ctx.fillRect(this.posX, this.posY, this.width, this.height);
}
updatePos = (x, y) => {
this.posX = x;
this.posY = y;
}
}
I generate instance of player in another module named playerObject.js
import { Player } from "./Player.js";
export const player = new Player();
OffScreenCanvas is created like this
const canvas = document.querySelector('#myGame');
const offscreen = canvas.transferControlToOffscreen();
const worker = new Worker('offscreencanvas.js', { type: "module" });
worker.postMessage({ canvas: offscreen }, [offscreen]);
Now I import playerObject to OffScreenCanvas worker
import {player} from "./playerObject.js";
addEventListener('message', (evt) => {
const canvas = evt.data.canvas;
const ctx = canvas.getContext("2d");
const render = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
player.draw(ctx);
requestAnimationFrame(render);
}
render();
});
and also to a class (module) that contains touch events, that are modifying player position:
import {player} from "./playerObject.js";
export class Game {
constructor() {
this.touch();
}
touch = () => {
window.addEventListener('touchstart', (e) => {
player.updatePos(e.touches[0].clientX, e.touches[0].clientY);
}, {passive: true});
}
}
The problem is that OffScreenCanvas doesn't see changes that are made by Game class. Touch itself is working (console.log shows event and also modified player object) but in OffScreenCanvas player still has the initial coorinations.
I'm still not sure whats happening there. Is worker creating a new instance of class and thats why it doesn't see changes from touch event?
Is there a way to achieve that?