5.7 KiB
Image Tool Documentation
Overview
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.
File Structure
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 theToolinterface andImageEditorContextjs/imageeditor/shared/Toolbar.svelte- Defines the tool types and subtool types
Implementation Details
Class: ImageTool
The ImageTool class implements the Tool interface defined in editor.ts. It provides the following functionality:
- Adding images to the canvas
- Managing image dimensions and positioning
- Integrating with the layer system
Key Components
ImageTool Class
The 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 subtool
AddImageCommand Class
Implements 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 canvas
Helper Functions
fit_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 canvas
Image Processing Flow
-
Image Acquisition: The image is acquired as a Blob or File from one of the sources (upload, clipboard, webcam)
-
Image Processing:
- The image is converted to a bitmap and then to a PIXI.js Texture
- The dimensions are calculated based on whether fixed_canvas is true or false
- If fixed_canvas is true, the image is scaled to fit the canvas while maintaining aspect ratio
- If fixed_canvas is false, the canvas is resized to match the image dimensions
-
Canvas Integration:
- The editor's image properties are updated with the new dimensions
- Existing layers are preserved and scaled to match the new dimensions
- A new background layer is created with the image sprite
- The image is centered in the viewport
-
Layer Management:
- The image is added as a sprite to a background layer
- Existing layers are preserved and scaled to match the new dimensions
- If no layers exist, an initial drawing layer is created
Command Pattern Implementation
The image tool uses the command pattern to implement undo/redo functionality:
- Command Creation: When adding an image, an
AddImageCommandis created - Command Execution: The command's
execute()method is called to add the image - Command Registration: The command is registered with the editor's command manager
- Undo Support: The command's
undo()method can be called to remove the image
Integration with Editor
The 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 manager
Usage Flow
- The user selects an image source (upload, clipboard, webcam)
- The image is acquired as a Blob or File
- The
add_imagemethod is called with the image and a flag indicating whether to maintain the canvas size - An
AddImageCommandis created and executed - The image is added to the canvas as a background layer
- The editor's state is updated with the new dimensions and position
Implementation Notes
Image Scaling
The tool provides two modes for handling image dimensions:
-
Fixed Canvas Mode (fixed_canvas = true):
- The image is scaled to fit within the canvas dimensions
- The aspect ratio is maintained
- The canvas size remains unchanged
-
Flexible Canvas Mode (fixed_canvas = false):
- The canvas is resized to match the image dimensions
- No scaling is applied to the image
- Existing layers are scaled to match the new dimensions
Layer Preservation
When adding a new background image:
- Existing layers are preserved
- Layer textures are captured before modification
- New layers are created with the new dimensions
- Content from old layers is scaled and centered on the new layers
- If no layers exist, an initial drawing layer is created
Maintenance Notes
When modifying the image tool, consider:
- Command Pattern: Ensure that all modifications to the canvas state are implemented as commands for proper undo/redo support
- Layer Management: Be careful with layer creation and destruction to avoid memory leaks
- Image Scaling: Ensure that aspect ratios are maintained when scaling images
- Performance: Large images may need to be downsampled for performance
- Memory Management: Properly destroy textures and sprites when they are no longer needed
Related Components
- Toolbar: Controls tool selection
- ImageEditor: Provides the context and manages the overall editor state
- LayerManager: Manages image layers
- Sources.svelte: Provides UI for selecting image sources