Skip to content

Commit

Permalink
Add worker.js
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaboleken authored Nov 28, 2024
1 parent 6e051f5 commit 6ea49ed
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/main/js/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
let canvas, ctx;
let canvasWidth = 1920;
let canvasHeight = 1080;
let overlayWidth, overlayHeight;

// Handle messages from the main thread
self.onmessage = function (event) {
const { type, data } = event.data;

switch (type) {
case 'init':
initializeCanvas(data.canvas, data.width, data.height);
break;
case 'frame':
drawFrame(data.screenFrame, data.cameraFrame);
break;
case 'resize':
resizeCanvas(data.width, data.height);
break;
default:
console.warn('Unknown message type:', type);
}
};

function initializeCanvas(offscreenCanvas, width, height) {
canvas = offscreenCanvas;
ctx = canvas.getContext('2d');
canvasWidth = width;
canvasHeight = height;

canvas.width = canvasWidth;
canvas.height = canvasHeight;

overlayWidth = canvasWidth / 4;
console.log('Canvas initialized in worker.');
}

function resizeCanvas(width, height) {
canvasWidth = width;
canvasHeight = height;
canvas.width = canvasWidth;
canvas.height = canvasHeight;

overlayWidth = canvasWidth / 4;
console.log('Canvas resized in worker:', canvasWidth, canvasHeight);
}

function drawFrame(screenFrame, cameraFrame) {
if (!ctx) return;

// Clear the canvas
ctx.clearRect(0, 0, canvasWidth, canvasHeight);

// Draw the screen video frame
ctx.drawImage(screenFrame, 0, 0, canvasWidth, canvasHeight);

// Draw the camera overlay
const overlayHeight = overlayWidth * (cameraFrame.height / cameraFrame.width);
const positionX = canvasWidth - overlayWidth - 20;
const positionY = canvasHeight - overlayHeight - 20;

ctx.drawImage(cameraFrame, positionX, positionY, overlayWidth, overlayHeight);

// Release ImageBitmap resources
screenFrame.close();
cameraFrame.close();
}

0 comments on commit 6ea49ed

Please sign in to comment.