frameCapture = {
const extractedFrame = framesMap.get(frame);
const context = DOM.context2d(extractedFrame.width, extractedFrame.height);
context.drawImage(extractedFrame, 0, 0, context.canvas.width, context.canvas.height);
context.fillStyle = 'rgba(0, 0, 255, 0.4)';
const { topLeft, bottomRight } = rectangleCoordinates;
const widthRectangle = bottomRight.x - topLeft.x;
const heightRectangle = bottomRight.y - topLeft.y;
if (widthRectangle <= 0 || heightRectangle <= 0) {
initializeRectangle();
const { topLeft, bottomRight } = rectangleCoordinates;
const widthRectangle = bottomRight.x - topLeft.x;
const heightRectangle = bottomRight.y - topLeft.y;
}
context.fillRect(topLeft.x, topLeft.y, widthRectangle, heightRectangle);
let startX, startY, endX, endY, isDrawing = false;
context.canvas.addEventListener('mousedown', (event) => {
const rect = context.canvas.getBoundingClientRect();
startX = Math.floor(event.clientX - rect.left);
startY = Math.floor(event.clientY - rect.top);
isDrawing = true;
});
// Function to handle mouse move events
context.canvas.addEventListener('mousemove', (event) => {
if (isDrawing) {
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
context.drawImage(extractedFrame, 0, 0, context.canvas.width, context.canvas.height);
const rect = context.canvas.getBoundingClientRect();
endX = Math.floor(event.clientX - rect.left);
endY = Math.floor(event.clientY - rect.top);
const widthRectangle = endX - startX;
const heightRectangle = endY - startY;
context.fillRect(startX, startY, widthRectangle, heightRectangle);
}
});
// Function to handle mouse up events
context.canvas.addEventListener('mouseup', () => {
isDrawing = false;
// Only update rectangleCoordinates if endX and endY are defined
if (endX !== undefined && endY !== undefined) {
// Ensure topLeft and bottomRight are correctly assigned
const topLeft = {
x: Math.min(startX, endX),
y: Math.min(startY, endY),
};
const bottomRight = {
x: Math.max(startX, endX),
y: Math.max(startY, endY),
};
mutable rectangleCoordinates = { topLeft, bottomRight };
// Redraw the final rectangle after the mouse is released
const widthRectangle = bottomRight.x - topLeft.x;
const heightRectangle = bottomRight.y - topLeft.y;
context.fillRect(topLeft.x, topLeft.y, widthRectangle, heightRectangle);
}
});
mutable imageData = context.getImageData(0, 0, context.canvas.width, context.canvas.height);
// Return the canvas element
return context.canvas;
};