js/imageeditor/shared/brush/BRUSH_TOOL.md
The Brush Tool is a core component of the image editor that allows users to draw and erase on the canvas. It provides a flexible drawing experience with customizable brush size, color, and opacity. This document explains how the brush tool works and the relationships between its components.
js/imageeditor/shared/brush/brush.ts: Main implementation of the brush tooljs/imageeditor/shared/brush/BrushOptions.svelte: UI controls for brush settingsjs/imageeditor/shared/brush/ColorPicker.svelte: Color selection componentjs/imageeditor/shared/brush/ColorSwatch.svelte: Color swatch componentjs/imageeditor/shared/brush/ColorField.svelte: Color input field componentjs/imageeditor/shared/brush/BrushSize.svelte: Brush size slider componentjs/imageeditor/shared/Toolbar.svelte: Defines tool types and handles tool selectionjs/imageeditor/shared/core/editor.ts: Provides the editor context and tool interfaceThe brush tool follows the Tool interface defined in editor.ts. It integrates with the image editor through the ImageEditorContext which provides access to the PIXI.js application, containers, and other utilities.
The BrushTool class implements the Tool interface and provides the following functionality:
The brush tool maintains several state variables:
state: Contains the current brush settings (opacity, size, color, mode)brushSize and eraserSize: Separate size settings for drawing and erasingisDrawing: Tracks whether the user is currently drawingisCursorOverImage: Tracks whether the cursor is over the image containerThe brush tool uses multiple PIXI.js textures and containers to manage the drawing process:
left_texture: Stores the final result that is displayed to the userright_texture: Stores the current state before applying new strokesstroke_texture: Temporarily stores the current stroke being drawndisplayContainer: Contains all visual elements of the brush toolstroke_container: Contains the graphics for the current strokeerase_graphics: Used for masking when in erase modeProvides UI controls for:
The brush tool can show a preview of the current brush in the center of the screen, which helps users understand the brush size and color before drawing.
The brush tool sets up the following event listeners:
pointerdown: Starts a new strokepointermove: Continues the current stroke and updates the cursor positionpointerup/pointerupoutside: Ends the current strokeThe brush tool integrates with the editor through:
The brush tool uses several techniques to maintain performance:
The brush tool exposes several methods for customization:
setBrushSize(size): Sets the brush sizesetBrushColor(color): Sets the brush colorsetBrushOpacity(opacity): Sets the brush opacityset_brush_size(size): Sets the brush size (only affects drawing mode)set_eraser_size(size): Sets the eraser size (only affects eraser mode)set_brush_color(color): Sets the brush color (only affects drawing mode)preview_brush(show): Shows or hides the brush previewWhen modifying the brush tool, consider the following:
Potential areas for enhancement: