js/imageeditor/shared/image/IMAGE.md
The Image Tool is a component of the image editor that handles adding and managing background images on the canvas. It allows users to upload, paste, or capture images from a webcam and set them as the background of the editing canvas. The tool manages image sizing, positioning, and integration with the layer system.
js/imageeditor/shared/image/image.ts - Main implementation of the image tooljs/imageeditor/shared/image/Sources.svelte - UI component for image source optionsjs/imageeditor/shared/core/editor.ts - Defines the Tool interface and ImageEditorContextjs/imageeditor/shared/Toolbar.svelte - Defines the tool types and subtool typesImageToolThe ImageTool class implements the Tool interface defined in editor.ts. It provides the following functionality:
ImageTool ClassThe main class that implements the Tool interface with methods:
setup(context, tool, subtool) - Initializes the tool with the editor contextcleanup() - Cleans up resourcesadd_image(image, fixed_canvas) - Adds an image to the canvasset_tool(tool, subtool) - Updates the current tool and subtoolAddImageCommand ClassImplements the command pattern for adding images, allowing for undo/redo functionality:
start() - Initializes the image sprite and calculates dimensionsexecute() - Adds the image to the canvas and updates the editor stateundo() - Removes the image from the canvasfit_image_to_canvas(image_width, image_height, canvas_width, canvas_height) - Calculates dimensions to fit an image within the canvas while maintaining aspect ratioadd_bg_color(container, renderer, color, width, height, resize) - Adds a solid color background to the canvasImage Acquisition: The image is acquired as a Blob or File from one of the sources (upload, clipboard, webcam)
Image Processing:
Canvas Integration:
Layer Management:
The image tool uses the command pattern to implement undo/redo functionality:
AddImageCommand is createdexecute() method is called to add the imageundo() method can be called to remove the imageThe image tool integrates with the editor through the ImageEditorContext interface, which provides:
app - The PIXI.js Application instancelayer_manager - Manages the layers in the editorset_image_properties - Updates the image dimensions and positionset_background_image - Sets the background image spriteexecute_command - Registers a command with the command manageradd_image method is called with the image and a flag indicating whether to maintain the canvas sizeAddImageCommand is created and executedThe tool provides two modes for handling image dimensions:
Fixed Canvas Mode (fixed_canvas = true):
Flexible Canvas Mode (fixed_canvas = false):
When adding a new background image:
When modifying the image tool, consider: