From 73680d55c1822453e16b0b0faac98f81d0690c1a Mon Sep 17 00:00:00 2001 From: HxxxxxS <16c52527@opayq.com> Date: Mon, 6 May 2024 03:05:08 +0200 Subject: [PATCH] Properly position both layers centered --- script.js | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/script.js b/script.js index 73f1f8b..372bcad 100644 --- a/script.js +++ b/script.js @@ -99,43 +99,62 @@ function formatTime(seconds) { const remainingSeconds = Math.floor(seconds % 60); return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`; } - -// Modify the drawVideos function to include debug text drawing function drawVideos() { ctx.clearRect(0, 0, canvas.width, canvas.height); + // Calculate scaling factors for each video to fit the canvas + const scale1 = Math.max(canvas.width / videos[0].videoWidth, canvas.height / videos[0].videoHeight); + const scale2 = Math.max(canvas.width / videos[1].videoWidth, canvas.height / videos[1].videoHeight); - // Resize the canvas to accommodate both videos side by side - canvas.width = Math.max(videos[0].videoWidth, videos[1].videoWidth); - canvas.height = Math.max(videos[0].videoHeight, videos[1].videoHeight); + // Calculate scaled dimensions for each video + const scaledWidth1 = videos[0].videoWidth * scale1; + const scaledHeight1 = videos[0].videoHeight * scale1; + const scaledWidth2 = videos[1].videoWidth * scale2; + const scaledHeight2 = videos[1].videoHeight * scale2; - // Calculate positions for each video - const video1X = (canvas.width - videos[0].videoWidth) / 2; - const video1Y = (canvas.height - videos[0].videoHeight) / 2; - const video2X = (canvas.width - videos[1].videoWidth) / 2; - const video2Y = (canvas.height - videos[1].videoHeight) / 2; + // Calculate horizontal and vertical centering offsets for each video + const offsetX1 = (canvas.width - scaledWidth1) / 2; + const offsetY1 = (canvas.height - scaledHeight1) / 2; + const offsetX2 = (canvas.width - scaledWidth2) / 2; + const offsetY2 = (canvas.height - scaledHeight2) / 2; // Draw video 1 - ctx.drawImage(videos[0], video1X, video1Y, videos[0].videoWidth, videos[0].videoHeight); + ctx.drawImage(videos[0], offsetX1, offsetY1, scaledWidth1, scaledHeight1); + + // Draw bounding box for video 1 if debugMode is true + if (debugMode) { + ctx.strokeStyle = "red"; + ctx.lineWidth = 2; + ctx.strokeRect(offsetX1-1, offsetY1-1, scaledWidth1+1, scaledHeight1+1); + } // Set composite operation to overlay ctx.globalCompositeOperation = "overlay"; // Draw video 2 - ctx.drawImage(videos[1], video2X, video2Y, videos[1].videoWidth, videos[1].videoHeight); + ctx.drawImage(videos[1], offsetX2, offsetY2, scaledWidth2, scaledHeight2); + + // Draw bounding box for video 2 if debugMode is true + if (debugMode) { + ctx.strokeStyle = "blue"; + ctx.lineWidth = 2; + ctx.strokeRect(offsetX2-1, offsetY2-1, scaledWidth2+1, scaledHeight2+1); + } // Reset composite operation ctx.globalCompositeOperation = "source-over"; - // Draw debug text if debug mode is enabled + // If debugMode is true, draw debug text if (debugMode) { drawDebugText(); } + // Request next frame requestAnimationFrame(drawVideos); } + function updateCanvasSize() { aspectRatio = Math.max( videos[0].videoWidth / videos[0].videoHeight,