write a js class with a function that takes an options object that allows user to
drag to draw a rect on a web page, upon release the mouse, the rect stays there (options to not stay there)
the rect is moved as user scrolls if it stays just as its part of the page.
the class can remove existing rects by id
the class can retrieve all rects (their id and coordinates in the page)
the rects are overlays on the page, and they can be resized by user dragging from the edges of the rect
when user hovers on the edges of rect, cursor should show its draggable/clickable
when user hovers on rect, a button to delete that rect will show up allowing user to delete it. when hover on the delete button, it will show that its deletable.
if the class needs to inject css, it only injects once for all class instances.
use concise and descriptive property names/method names, function arguments, variable names etc.
Assistant
Here's a comprehensive JavaScript class named RectangleDrawer that fulfills all your requirements. This class allows users to draw, resize, and manage rectangles on a webpage with various interactive features.
Key Features:
Drawing Rectangles: Users can click and drag to draw rectangles. An option allows rectangles to either remain on the page or be temporary.
Resizable Rectangles: Rectangles can be resized by dragging their edges. The cursor changes to indicate possible actions.
Delete Functionality: Hovering over a rectangle reveals a delete button, enabling users to remove unwanted rectangles.
Management Methods: The class provides methods to remove rectangles by ID and retrieve all existing rectangles with their details.
CSS Management: Necessary CSS styles are injected once, regardless of the number of class instances.
Scroll Behavior: Rectangles positioned as part of the page will move appropriately when the user scrolls.
Implementation Details:
Singleton CSS Injection: The class uses a static property to ensure CSS styles are injected only once, even with multiple instances.
Event Handling: Mouse events (mousedown, mousemove, mouseup) handle drawing and resizing actions. Event listeners are appropriately managed to ensure smooth interactions.
Unique Identifiers: Each rectangle is assigned a unique ID for easy management and retrieval.
Responsive Design: Rectangles adjust their positions based on page scrolls to maintain their placement relative to the content.
Usage Example:
javascript
const drawer = new RectangleDrawer({
persistent: true, // Set to false if rectangles should not remain after drawing
});
// Remove a rectangle by ID
drawer.removeRect('rect-1');
// Retrieve all rectangles
const rects = drawer.getAllRects();
console.log(rects);
CSS Injection: The injectCSS method ensures that the necessary styles are added to the document only once, using a static property cssInjected to track this.
Event Initialization: The initEvents method sets up global event listeners for mousedown, mousemove, and mouseup to handle drawing and resizing actions.
Drawing Logic:
onMouseDown: Initiates the drawing process by creating a new rectangle element and positioning it based on the mouse’s starting coordinates.
onMouseMove: Adjusts the size and position of the rectangle as the user drags the mouse.
onMouseUp: Finalizes the rectangle's dimensions and decides whether to keep it on the page based on the persistent option.
Resizing Logic:
initResize: Sets up the necessary information to handle resizing when a resize handle is grabbed.
onMouseMove: Adjusts the rectangle's size and position based on the mouse movement during resizing.
Rectangle Management:
removeRect: Removes a rectangle from the DOM and the internal rects map based on its ID.
getAllRects: Returns an array of all rectangles with their IDs and positional data.
Cleanup: The destroy method removes all event listeners and optionally deletes all existing rectangles.
Notes:
Unique IDs: Rectangles are assigned unique IDs in the format rect-1, rect-2, etc., ensuring easy identification and management.
Delete Button: Each rectangle contains a delete button (×) that appears on hover, enabling users to remove the rectangle.
Resize Handles: Corner handles (nw, ne, sw, se) allow users to resize rectangles by dragging. The cursor changes to indicate the resize direction.
Positioning: Rectangles are absolutely positioned relative to the document, ensuring they move appropriately with page scrolls.
This class provides a robust and flexible way to manage draggable and resizable rectangles on a webpage, encapsulating all necessary functionality and ensuring ease of use.