Write an HTML code for a tool that lets the user draw a layout (a uv map of a box with 6 faces—front, back, right, left, top, and bottom) on an image and export its data to a file ".uv1" to use later.
the UI should
the UI:
A canvas to show and handle the image that take the full page size
A button to import an image
Use middle mouse button for dragging. Use the mouse wheel to zoom in/out, focusing on the cursor position
let the user select multiple parts from the image (show the selection as rectangles)
make the selection rectangle stick in the image (zoom and move with the image).
make it to Hold Ctrl and click-and-drag to create selections
The selections will stick to the image when zooming or moving.
a list of the 6 faces of the box (front, back, right, left, top and bottom) that let the user add and delete the selection rectangles
add 4 handles in the corners of the rectangle an 4 in the edge of the rectangle that was created for selection to let the user modify the size and emplacements the selection rectangle.
.uv1 file is a text file. Follow this template
face_name, rect_x_cordinate, rect_y_cordinate, rect_x_size, rect_y_size
for example
front,100,500,300,600
back,500,600,900,700
..etc
Assistant
Certainly! Below is an HTML code that implements the tool you've described. The tool allows users to:
Import an image into a canvas that fills the page.
Pan and zoom the image using the middle mouse button and mouse wheel.
Create and modify selection rectangles by holding the Ctrl key and dragging the mouse.
Manage selections corresponding to the six faces of a box (front, back, right, left, top, bottom).
Export the selection data to a .uv1 file in the specified format.
Buttons:Import Image and Export UV1 for loading images and exporting data.
Face List: A list of the six faces that users can select to assign selections.
Canvas: The main drawing area where the image is displayed and manipulated.
Canvas and Context Initialization:
The canvas is set to full window size and resized on window resize events.
The drawing context (ctx) is used for rendering the image and selections.
Image Import:
When the Import Image button is clicked, an <input> element of type file is created to select an image file.
The image is read using a FileReader and set as the source of an Image object.
Panning and Zooming:
Panning: Activated by pressing the middle mouse button. The image can be dragged around the canvas.
Zooming: Performed using the mouse wheel, zooming in and out centered on the mouse cursor.
Selection Creation:
Holding the Ctrl key and left-clicking allows users to create a selection rectangle.
The rectangle is defined by its starting point (x, y) and its width and height.
Selection Management:
Selections are stored in an array with properties x, y, width, height, and face.
Users can select a face from the face list, and new selections will be assigned to that face.
Clicking on a selection rectangle selects it for editing.
Rectangle Modification:
Handles: Small squares at the corners and edges of a selected rectangle allow for resizing.
Dragging a handle adjusts the rectangle's dimensions accordingly.
The position of the handles is updated on every draw call.
Exporting Data:
Notes:
The code uses pure JavaScript without any external libraries for maximum compatibility.
The handles array stores HTML elements representing the resizing handles, which are positioned absolutely on the page.
The coordinate calculations take into account the current scale and originX/originY to ensure selections stick to the image during zooming and panning.
The draw() function is responsible for rendering the image, selections, and updating handle positions.
Usage Instructions:
Import an Image:
Click the Import Image button and select an image file from your computer.
The image will be displayed on the canvas.
Select a Face:
From the list on the left, click on one of the faces (e.g., Front).
The selected face will be highlighted.
Create Selections:
Hold down the Ctrl key.
Click and drag on the canvas to create a selection rectangle.
Release the mouse button to finalize the selection.
Modify Selections:
Click on an existing selection rectangle to select it.
Small handles will appear at the corners and edges.
Click and drag these handles to resize the rectangle.
Pan and Zoom:
Pan: Press and hold the middle mouse button (scroll wheel) and move the mouse to pan the image.
Zoom: Use the mouse wheel to zoom in and out, centering on the mouse cursor.
Export Data:
Click the Export UV1 button.
The .uv1 file, output.uv1, will be downloaded with the selection data.
Delete Selections:
(Optional Enhancement) To delete a selection, you could add a delete button or handle key events to remove the selected rectangle.
Example Output (output.uv1):
code
front,100,150,200,250
back,300,350,400,450
This output corresponds to the face names and rectangle positions/sizes specified during use.
Important Considerations:
Browser Compatibility: This code should work in modern browsers that support HTML5 and standard JavaScript.
Error Handling: The code assumes proper user interactions. Additional error handling can be added for robustness.
Performance: For very large images or numerous selections, performance optimizations may be necessary.
Feel free to modify and enhance the code to suit your specific needs or to add additional features such as selection deletion, keyboard shortcuts, or visual enhancements.