Public
Edited
Nov 1, 2024
Paused
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
frameCapture = {
// Get the extracted frame from framesMap
const extractedFrame = framesMap.get(frame);
// Create a 2D context with the specified width and height
const context = DOM.context2d(extractedFrame.width, extractedFrame.height);

// Draw the extracted frame onto the canvas
context.drawImage(extractedFrame, 0, 0, context.canvas.width, context.canvas.height);

// Set the fill style for the rectangle (semi-transparent red)
context.fillStyle = 'rgba(0, 0, 255, 0.4)';

// Draw the initial rectangle
const { topLeft, bottomRight } = rectangleCoordinates;
const widthRectangle = bottomRight.x - topLeft.x;
const heightRectangle = bottomRight.y - topLeft.y;

// Check if width or height is null or zero
if (widthRectangle <= 0 || heightRectangle <= 0) {
initializeRectangle(); // Call to initialize if necessary
// Recalculate after initialization
const { topLeft, bottomRight } = rectangleCoordinates;
const widthRectangle = bottomRight.x - topLeft.x;
const heightRectangle = bottomRight.y - topLeft.y;
}

// Draw the rectangle
context.fillRect(topLeft.x, topLeft.y, widthRectangle, heightRectangle);

// Variables to store rectangle coordinates
let startX, startY, endX, endY, isDrawing = false;

// Function to handle mouse down events
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;
};

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more