34 Commits
v1.3.0 ... dev

Author SHA1 Message Date
Dariusz L
4966069b67 Refactor mask editor cropping and formatting in Canvas.js
Updated the mask processing logic to crop the mask based on the viewport pan position instead of scaling, ensuring accurate mask editing. Also standardized object formatting in logging and context creation for consistency.
2025-07-02 00:11:45 +02:00
Dariusz L
faa60d4c28 Add LayerForge badge, workflows, and example workflow
Introduces a dynamic LayerForge downloads badge (LAYERFORGE.md), new GitHub Actions for badge generation and improved release versioning, and example workflow files. Updates README to use the new badge and bumps version to 1.3.0 in pyproject.toml. Adds issue templates for bug reports and documentation requests.
2025-07-01 18:02:52 +02:00
Dariusz L
70ab561c3c Set default log level to NONE in logger configuration
Changed the default log level from DEBUG to NONE in both Python and JavaScript logger utilities. This reduces log output unless explicitly configured otherwise.
2025-07-01 16:53:53 +02:00
Dariusz L
e4da6e4d31 Remove comments and cleanup event handling code
Removed redundant and explanatory comments from CanvasInteractions.js, CanvasLayers.js, and ClipboardManager.js to improve code readability. Deleted the REFACTORING_GUIDE.md documentation file. Minor code cleanups were made to event handler logic and UI widget setup, with no changes to core functionality.
2025-07-01 16:42:48 +02:00
Dariusz L
b2ff5666f9 Add draggable blend mode menu and improve right-click UX
Introduces a draggable title bar to the blend mode menu for better usability and restricts its movement within the viewport. Right-clicking on selected layers now opens the blend mode menu, and the default context menu is suppressed on the canvas. Also refines tooltip table cell sizing for improved display.
2025-07-01 16:37:18 +02:00
Dariusz L
5a473cc14a Group clipboard buttons and add styles in CanvasView
Clipboard-related buttons are now grouped in a styled .painter-clipboard-group container for improved UI clarity. New CSS styles were added for the group, and the DOM structure was updated to wrap the 'Paste Image' and clipboard toggle buttons together.
2025-07-01 11:28:30 +02:00
Dariusz L
0512200b92 Enhance clipboard and drag & drop image handling
Adds robust clipboard and drag & drop support for images, including ComfyUI Clipspace integration, system clipboard fallback, and improved user feedback. Clipboard and paste logic is centralized and clarified, with priority handling for internal clipboard, ComfyUI Clipspace, and system clipboard. Drag & drop is now handled at the canvas level, and tooltips and notifications provide clearer guidance for users.
2025-07-01 11:15:40 +02:00
Dariusz L
0f05e36333 Enhance image path validation to support URLs
Updated isValidImagePath to recognize and validate image URLs in addition to local file paths. The function now checks for valid URL formats and logs debug information for both URLs and local paths.
2025-07-01 08:01:37 +02:00
Dariusz L
94ffc64f6e Refactor clipboard paste logic into ClipboardManager
Moved all system clipboard paste and image path handling logic from CanvasLayers.js into a new ClipboardManager utility class. This improves code organization and separation of concerns, making clipboard-related functionality easier to maintain and extend.
2025-07-01 07:57:05 +02:00
Dariusz L
03e76d5ecd Add support for loading images from file paths
Introduces a backend API endpoint to load images from local file paths and return them as base64-encoded PNGs. Updates CanvasLayers.js to detect image file paths in the clipboard, validate them, and attempt to load images using multiple strategies, including the new backend endpoint, ComfyUI view endpoints, and a file picker fallback. Also updates canvas context creation to use the 'willReadFrequently' option for improved performance.
2025-07-01 07:46:06 +02:00
Dariusz L
02bac6c624 Use willReadFrequently in 2D canvas contexts
Updated all calls to getContext('2d') to include the { willReadFrequently: true } option. This change improves performance for frequent pixel read operations, particularly for mask and image processing tasks.
2025-07-01 07:44:14 +02:00
Dariusz L
cf10322101 Replace console statements with structured logging
Refactored Canvas.js to use the 'log' object for debug, info, and warn messages instead of console.log, console.warn, and console.error. Added more detailed and structured logging throughout the file to improve traceability and debugging, including context information for key operations such as state changes, layer management, mask editor interactions, and image processing.
2025-07-01 07:02:28 +02:00
Dariusz L
d7701fd989 Remove unused handleMaskEditorClose method and clean logs
Deleted the unused handleMaskEditorClose method and removed excessive console logging from widget visibility and mask application logic. Increased the default timeout for waitForWidget to 20000ms for improved reliability.
2025-07-01 06:56:17 +02:00
Dariusz L
b89956d2ba Refactor code style and remove mask editor examples
This commit applies consistent code style changes (mainly spacing and formatting) across multiple JS files for improved readability and maintainability. Additionally, it removes the unused 'js/examples/mask_editor_examples.js' file.
2025-07-01 06:44:26 +02:00
Dariusz L
a0ceb3b97c Remove auto mask loading docs and clean up code comments
Deleted documentation files related to automatic mask loading. Cleaned up and streamlined comments across Canvas.js, CanvasInteractions.js, CanvasLayers.js, CanvasView.js, MaskTool.js, mask_utils.js, and example scripts for improved clarity and maintainability. No functional changes to core logic.
2025-07-01 06:42:20 +02:00
Dariusz L
30fb89451f Refactor canvas preview visibility and widget loading
Refactored Canvas.js to use an async waitForWidget method for reliably finding widgets before manipulating them. The setPreviewVisibility method is now async and simplified, with excessive debug logging removed. CanvasState.js now triggers a render after saving state. In CanvasView.js, initial state loading is deferred and preview visibility is managed in the Canvas constructor, removing redundant calls.
2025-07-01 06:32:06 +02:00
Dariusz L
40c1dbfb5d Add show/hide preview option for canvas image preview
Introduces a new 'show_preview' boolean option to CanvasNode and UI, allowing users to toggle the visibility of the canvas image preview widget. Updates Canvas.js with a setPreviewVisibility method to control the preview's display and size, and hooks this logic into CanvasView.js to respond to the new option. Adds detailed debug logging for widget state and size changes.
2025-07-01 05:09:48 +02:00
Dariusz L
688acd72fd Add cancel support to mask editor with state restore
Implemented logic in Canvas.js to save and restore mask state when the mask editor is cancelled. Added robust detection and event listener setup for the cancel button in mask_utils.js, including multiple selector strategies and fallback mechanisms. This improves user experience by allowing users to revert changes if they cancel out of the mask editor.
2025-06-30 22:57:06 +02:00
Dariusz L
acef58291c Add automatic mask loading to mask editor
Introduces functionality for automatically applying predefined masks in the ComfyUI mask editor. Adds new API methods and utilities in Canvas.js and mask_utils.js, including support for sending a clean image, handling both new and old mask editors, and flexible mask formats. Includes documentation, usage examples, and helper functions for mask processing and error handling.
2025-06-30 21:52:40 +02:00
Dariusz L
8a800a4bee Replace MaskEditor menu option with custom handler
Removes the default 'Open in MaskEditor' option from the menu and adds a new custom option with improved error handling and logging. This ensures the MaskEditor is opened through a controlled callback, providing better user feedback in case of errors.
2025-06-30 01:49:06 +02:00
Dariusz L
ed62d8df78 Add documentation for LitegraphService and MaskEditor
Introduced two new documentation files: one detailing the main functions and class structure of LitegraphService, and another describing the classes and methods in MaskEditor. These documents provide an overview of available APIs, internal logic, and UI management for developers.
2025-06-30 01:32:20 +02:00
Dariusz L
2624cf02a2 Add clipboard source toggle for paste operations
Introduces a clipboardPreference setting to choose between system clipboard and ComfyUI Clipspace as the source for paste operations in CanvasLayers. Adds a UI button in CanvasView to toggle the clipboard source, improving user control over paste behavior.
2025-06-30 01:14:13 +02:00
Dariusz L
6718198a27 Add documentation summaries for ComfyApi and ComfyApp
Introduced two new documentation files: ComfyApi and ComfyApp. These files provide concise summaries of the main functions, properties, and event types for the ComfyApi and ComfyApp classes, aiding developers in understanding their usage and structure.
2025-06-30 00:35:37 +02:00
Dariusz L
62a5af4287 Enhance paste functionality and clipboard handling
Improves the paste operation in CanvasLayers to prioritize internal clipboard, then ComfyUI Clipspace, and finally the system clipboard, pasting images at the mouse position. Also clears the internal clipboard when the mouse leaves the canvas in CanvasInteractions.
2025-06-30 00:35:25 +02:00
Dariusz L
2eaa3d6620 Improve mask editor integration and mask application logic
Replaces the mask editor's image preparation to use a new method that combines the full image with the current mask, ensuring the editor starts with the correct state. Updates mask application logic to fully replace the mask area instead of blending, and refactors mask extraction and application in CanvasLayers for consistency and correctness, including a new getFlattenedCanvasForMaskEditor method.
2025-06-29 23:16:22 +02:00
Dariusz L
abb0f8ef53 Add export of canvas with mask as alpha channel
Introduces a new method to export the flattened canvas with the mask applied as the alpha channel. Updates UI actions to allow previewing, copying, and saving the image with mask alpha, and ensures node previews use the new export method. This enhances workflows that require the mask to be embedded as transparency in the output image.
2025-06-29 21:26:53 +02:00
Dariusz L
0bb54a0a6d Refactor image layer addition to use addLayer method
Replaces manual image layer creation with calls to canvas.addLayer, passing options and addMode. This streamlines image addition logic and ensures consistent handling of layer properties.
2025-06-29 20:56:58 +02:00
Dariusz L
8efb9d91b0 Remove legacy delegating methods from Canvas.js
Removed all remaining delegating methods from Canvas.js, including handleMouseMove and garbage collection helpers, as part of finalizing the refactor to a pure facade. Updated REFACTORING_GUIDE.md to reflect the completion of this refactor and clarify the new architecture.
2025-06-29 20:43:02 +02:00
Dariusz L
0b3bdaf769 Remove CanvasLayers delegation methods from Canvas.js
Eliminated 14 delegation methods to CanvasLayers from Canvas.js. Updated all relevant calls in CanvasRenderer.js, CanvasIO.js, and CanvasInteractions.js to use canvas.canvasLayers directly. This streamlines the Canvas class to only expose core facade operations and necessary helpers.
2025-06-29 20:39:31 +02:00
Dariusz L
1bb4909438 Remove CanvasState delegating methods from Canvas
Removed delegating methods to CanvasState from Canvas.js and updated all references to use canvas.canvasState directly for state operations (undo, redo, saveStateToDB). Updated CanvasInteractions.js, CanvasIO.js, CanvasLayers.js, and CanvasView.js accordingly. Updated REFACTORING_GUIDE.md to reflect the completion of this refactor.
2025-06-29 14:11:34 +02:00
Dariusz L
fd611c5777 Refactor CanvasLayers to use consistent canvas reference
Renamed the CanvasLayers constructor parameter from 'canvasLayers' to 'canvas' and updated all internal references accordingly for clarity and consistency. Adjusted CanvasView.js to call layer operations via canvas.canvasLayers. Updated REFACTORING_GUIDE.md to document these architectural changes and ensure all modules follow a unified naming convention.
2025-06-29 13:57:53 +02:00
Dariusz L
22627b7532 Delegate layer resize and rotate to CanvasLayers
Moved the resizeLayer and rotateLayer logic from Canvas.js to CanvasLayers.js, improving modularity and consistency. Updated REFACTORING_GUIDE.md to reflect these changes and document the current architecture and status.
2025-06-29 13:47:08 +02:00
Dariusz L
b4a662b036 Refactor CanvasView.js to use new Canvas facade modules
Updated all CanvasView.js method calls to use the new modular structure (canvasIO, canvasLayers, imageReferenceManager) as part of the Canvas facade refactor. Updated REFACTORING_GUIDE.md to reflect completed migration, document new usage patterns, and outline next steps. This improves code clarity and modularity while maintaining backward compatibility.
2025-06-29 13:42:52 +02:00
Dariusz L
d50a0443c3 Set default log level to DEBUG and add refactoring guide
Changed the default log level from NONE to DEBUG in both canvas_node.py and LoggerUtils.js to improve logging visibility during development. Added js/REFACTORING_GUIDE.md with detailed documentation on the recent Canvas module refactor, outlining architectural changes, migration instructions, and developer notes.
2025-06-29 04:44:08 +02:00
5 changed files with 372 additions and 53 deletions

96
Doc/ComfyApi Normal file
View File

@@ -0,0 +1,96 @@
# ComfyApi - Function Documentation Summary import { api } from "../../scripts/api.js";
## Basic Information
ComfyApi is a class for communication with ComfyUI backend via WebSocket and REST API.
## Main Functions:
### Connection and Initialization
- constructor() - Initializes API, sets host and base path
- init() - Starts WebSocket connection for real-time updates
- #createSocket() - Creates and manages WebSocket connection
### URL Management
- internalURL(route) - Generates URL for internal endpoints
- apiURL(route) - Generates URL for public API endpoints
- fileURL(route) - Generates URL for static files
- fetchApi(route, options) - Performs HTTP requests with automatic user headers
### Event Handling
- addEventListener(type, callback) - Listens for API events (status, executing, progress, etc.)
- removeEventListener(type, callback) - Removes event listeners
- dispatchCustomEvent(type, detail) - Emits custom events
### Queue and Prompt Management
- queuePrompt(number, data) - Adds prompt to execution queue
- getQueue() - Gets current queue state (Running/Pending)
- interrupt() - Interrupts currently executing prompt
- clearItems(type) - Clears queue or history
- deleteItem(type, id) - Removes item from queue or history
### History and Statistics
- getHistory(max_items) - Gets history of executed prompts
- getSystemStats() - Gets system statistics (Python, OS, GPU, etc.)
- getLogs() - Gets system logs
- getRawLogs() - Gets raw logs
- subscribeLogs(enabled) - Enables/disables log subscription
### Model and Resource Management
- getNodeDefs(options) - Gets definitions of available nodes
- getExtensions() - List of installed extensions
- getEmbeddings() - List of available embeddings
- getModelFolders() - List of model folders
- getModels(folder) - List of models in given folder
- viewMetadata(folder, model) - Metadata of specific model
### Workflow Templates
- getWorkflowTemplates() - Gets workflow templates from custom nodes
- getCoreWorkflowTemplates() - Gets core workflow templates
### User Management
- getUserConfig() - Gets user configuration
- createUser(username) - Creates new user
- getSettings() - Gets all user settings
- getSetting(id) - Gets specific setting
- storeSettings(settings) - Saves settings dictionary
- storeSetting(id, value) - Saves single setting
### User Data
- getUserData(file) - Gets user data file
- storeUserData(file, data, options) - Saves user data
- deleteUserData(file) - Deletes user data file
- moveUserData(source, dest) - Moves data file
- listUserDataFullInfo(dir) - Lists files with full information
### Other
- getFolderPaths() - Gets system folder paths
- getCustomNodesI18n() - Gets internationalization data for custom nodes
## Important Properties
- clientId - Client ID from WebSocket
- authToken - Authorization token for ComfyOrg account
- apiKey - API key for ComfyOrg account
- socket - Active WebSocket connection
## WebSocket Event Types
- status - System status
- executing - Currently executing node
- progress - Execution progress
- executed - Node executed
- execution_start/success/error/interrupted/cached - Execution events
- logs - System logs
- b_preview - Image preview (binary)
- reconnecting/reconnected - Connection events

72
Doc/ComfyApp Normal file
View File

@@ -0,0 +1,72 @@
## __Main ComfyApp Functions__ import { app, ComfyApp } from "../../scripts/app.js";
### __Application Management__
- `setup(canvasEl)` - Initializes the application on the page, loads extensions, registers nodes
- `resizeCanvas()` - Adjusts canvas size to window
- `clean()` - Clears application state (node outputs, image previews, errors)
### __Workflow Management__
- `loadGraphData(graphData, clean, restore_view, workflow, options)` - Loads workflow data from JSON
- `loadApiJson(apiData, fileName)` - Loads workflow from API format
- `graphToPrompt(graph, options)` - Converts graph to prompt for execution
- `handleFile(file)` - Handles file loading (PNG, WebP, JSON, MP3, MP4, SVG, etc.)
### __Execution__
- `queuePrompt(number, batchCount, queueNodeIds)` - Queues prompt for execution
- `registerNodes()` - Registers node definitions from backend
- `registerNodeDef(nodeId, nodeDef)` - Registers single node definition
- `refreshComboInNodes()` - Refreshes combo lists in nodes
### __Node Management__
- `registerExtension(extension)` - Registers ComfyUI extension
- `updateVueAppNodeDefs(defs)` - Updates node definitions in Vue app
- `revokePreviews(nodeId)` - Frees memory for node previews
### __Clipboard__
- `copyToClipspace(node)` - Copies node to clipboard
- `pasteFromClipspace(node)` - Pastes data from clipboard to node
### __Position Conversion__
- `clientPosToCanvasPos(pos)` - Converts client position to canvas position
- `canvasPosToClientPos(pos)` - Converts canvas position to client position
### __Error Handling__
- `showErrorOnFileLoad(file)` - Displays file loading error
- `#showMissingNodesError(missingNodeTypes)` - Shows missing nodes error
- `#showMissingModelsError(missingModels, paths)` - Shows missing models error
### __Internal Handlers__
- `#addDropHandler()` - Handles drag and drop of files
- `#addProcessKeyHandler()` - Handles keyboard input
- `#addDrawNodeHandler()` - Modifies node drawing behavior
- `#addApiUpdateHandlers()` - Handles API updates
- `#addConfigureHandler()` - Graph configuration flag
- `#addAfterConfigureHandler()` - Post-configuration handling
### __Deprecated Properties__
Many properties are marked as deprecated and redirect to appropriate stores:
- `lastNodeErrors` → `useExecutionStore().lastNodeErrors`
- `lastExecutionError` → `useExecutionStore().lastExecutionError`
- `runningNodeId` → `useExecutionStore().executingNodeId`
- `shiftDown` → `useWorkspaceStore().shiftDown`
- `widgets` → `useWidgetStore().widgets`
- `extensions` → `useExtensionStore().extensions`
### __Utility Functions__
- `sanitizeNodeName(string)` - Cleans node name from dangerous characters
- `getPreviewFormatParam()` - Returns preview format parameter
- `getRandParam()` - Returns random parameter for refresh
- `isApiJson(data)` - Checks if data is in API JSON format
This application uses Vue and TypeScript composition pattern, where many functionalities are separated into different services and stores (e.g., `useExecutionStore`, `useWorkflowService`, `useExtensionService`, etc.).

75
Doc/LitegraphService Normal file
View File

@@ -0,0 +1,75 @@
LitegraphService Documentation
Main functions of useLitegraphService()
Node Registration and Creation Functions:
registerNodeDef(nodeId: string, nodeDefV1: ComfyNodeDefV1)
- Registers node definition in LiteGraph system
- Creates ComfyNode class with inputs, outputs and widgets
- Adds context menu, background drawing and keyboard handling
- Invokes extensions before registration
addNodeOnGraph(nodeDef, options)
- Adds new node to graph at specified position
- By default places node at canvas center
Navigation and View Functions:
getCanvasCenter(): Vector2
- Returns canvas center coordinates accounting for DPI
goToNode(nodeId: NodeId)
- Animates transition to specified node on canvas
resetView()
- Resets canvas view to default settings (scale 1, offset [0,0])
fitView()
- Fits canvas view to show all nodes
Node Handling Functions (internal):
addNodeContextMenuHandler(node)
- Adds context menu with options:
- Open/Copy/Save image (for image nodes)
- Bypass node
- Copy/Paste to Clipspace
- Open in MaskEditor (for image nodes)
addDrawBackgroundHandler(node)
- Adds node background drawing logic
- Handles image, animation and video previews
- Manages thumbnail display
addNodeKeyHandler(node)
- Adds keyboard handling:
- Left/Right arrows: navigate between images
- Escape: close image preview
ComfyNode Class (created by registerNodeDef):
Main methods:
- #addInputs() - adds inputs and widgets to node
- #addOutputs() - adds outputs to node
- configure() - configures node from serialized data
- #setupStrokeStyles() - sets border styles (errors, execution, etc.)
Properties:
- comfyClass - ComfyUI class name
- nodeData - node definition
- Automatic yellow coloring for API nodes

76
Doc/MaskEditor Normal file
View File

@@ -0,0 +1,76 @@
MASKEDITOR.TS FUNCTION DOCUMENTATION
MaskEditorDialog - Main mask editor class
- getInstance() - Singleton pattern, returns editor instance
- show() - Opens the mask editor
- save() - Saves mask to server
- destroy() - Closes and cleans up editor
- isOpened() - Checks if editor is open
CanvasHistory - Change history management
- saveState() - Saves current canvas state
- undo() - Undo last operation
- redo() - Redo undone operation
- clearStates() - Clears history
BrushTool - Brush tool
- setBrushSize(size) - Sets brush size
- setBrushOpacity(opacity) - Sets brush opacity
- setBrushHardness(hardness) - Sets brush hardness
- setBrushType(type) - Sets brush shape (circle/square)
- startDrawing() - Starts drawing
- handleDrawing() - Handles drawing during movement
- drawEnd() - Ends drawing
PaintBucketTool - Fill tool
- floodFill(point) - Fills area with color from point
- setTolerance(tolerance) - Sets color tolerance
- setFillOpacity(opacity) - Sets fill opacity
- invertMask() - Inverts mask
ColorSelectTool - Color selection tool
- fillColorSelection(point) - Selects similar colors
- setTolerance(tolerance) - Sets selection tolerance
- setLivePreview(enabled) - Enables/disables live preview
- setComparisonMethod(method) - Sets color comparison method
- setApplyWholeImage(enabled) - Applies to whole image
- setSelectOpacity(opacity) - Sets selection opacity
UIManager - Interface management
- updateBrushPreview() - Updates brush preview
- setBrushVisibility(visible) - Shows/hides brush
- screenToCanvas(coords) - Converts screen coordinates to canvas
- getMaskColor() - Returns mask color
- setSaveButtonEnabled(enabled) - Enables/disables save button
ToolManager - Tool management
- setTool(tool) - Sets active tool
- getCurrentTool() - Returns active tool
- handlePointerDown/Move/Up() - Handles mouse/touch events
PanAndZoomManager - View management
- zoom(event) - Zooms in/out canvas
- handlePanStart/Move() - Handles canvas panning
- initializeCanvasPanZoom() - Initializes canvas view
- smoothResetView() - Smoothly resets view
MessageBroker - Communication system
- publish(topic, data) - Publishes message
- subscribe(topic, callback) - Subscribes to topic
- pull(topic, data) - Pulls data from topic
- createPullTopic/PushTopic() - Creates communication topics
KeyboardManager - Keyboard handling
- addListeners() - Adds keyboard listeners
- removeListeners() - Removes listeners
- isKeyDown(key) - Checks if key is pressed

View File

@@ -26,7 +26,7 @@ export class Canvas {
this.node = node;
this.widget = widget;
this.canvas = document.createElement('canvas');
this.ctx = this.canvas.getContext('2d', { willReadFrequently: true });
this.ctx = this.canvas.getContext('2d', {willReadFrequently: true});
this.width = 512;
this.height = 512;
this.layers = [];
@@ -60,7 +60,7 @@ export class Canvas {
log.debug('Canvas widget element:', this.node);
log.info('Canvas initialized', {
nodeId: this.node.id,
dimensions: { width: this.width, height: this.height },
dimensions: {width: this.width, height: this.height},
viewport: this.viewport
});
@@ -187,7 +187,7 @@ export class Canvas {
* @param {boolean} replaceLast - Czy zastąpić ostatni stan w historii
*/
saveState(replaceLast = false) {
log.debug('Saving canvas state', { replaceLast, layersCount: this.layers.length });
log.debug('Saving canvas state', {replaceLast, layersCount: this.layers.length});
this.canvasState.saveState(replaceLast);
this.incrementOperationCount();
this._notifyStateChange();
@@ -200,11 +200,11 @@ export class Canvas {
log.info('Performing undo operation');
const historyInfo = this.canvasState.getHistoryInfo();
log.debug('History state before undo:', historyInfo);
this.canvasState.undo();
this.incrementOperationCount();
this._notifyStateChange();
log.debug('Undo completed, layers count:', this.layers.length);
}
@@ -216,11 +216,11 @@ export class Canvas {
log.info('Performing redo operation');
const historyInfo = this.canvasState.getHistoryInfo();
log.debug('History state before redo:', historyInfo);
this.canvasState.redo();
this.incrementOperationCount();
this._notifyStateChange();
log.debug('Redo completed, layers count:', this.layers.length);
}
@@ -246,17 +246,17 @@ export class Canvas {
*/
removeSelectedLayers() {
if (this.selectedLayers.length > 0) {
log.info('Removing selected layers', {
log.info('Removing selected layers', {
layersToRemove: this.selectedLayers.length,
totalLayers: this.layers.length
totalLayers: this.layers.length
});
this.saveState();
this.layers = this.layers.filter(l => !this.selectedLayers.includes(l));
this.updateSelection([]);
this.render();
this.saveState();
log.debug('Layers removed successfully, remaining layers:', this.layers.length);
} else {
log.debug('No layers selected for removal');
@@ -271,13 +271,13 @@ export class Canvas {
const previousSelection = this.selectedLayers.length;
this.selectedLayers = newSelection || [];
this.selectedLayer = this.selectedLayers.length > 0 ? this.selectedLayers[this.selectedLayers.length - 1] : null;
log.debug('Selection updated', {
previousCount: previousSelection,
newCount: this.selectedLayers.length,
selectedLayerIds: this.selectedLayers.map(l => l.id || 'unknown')
});
if (this.onSelectionChange) {
this.onSelectionChange();
}
@@ -321,10 +321,10 @@ export class Canvas {
* @param {boolean} sendCleanImage - Czy wysłać czysty obraz (bez maski) do editora
*/
async startMaskEditor(predefinedMask = null, sendCleanImage = true) {
log.info('Starting mask editor', {
hasPredefinedMask: !!predefinedMask,
log.info('Starting mask editor', {
hasPredefinedMask: !!predefinedMask,
sendCleanImage,
layersCount: this.layers.length
layersCount: this.layers.length
});
this.savedMaskState = await this.saveMaskState();
@@ -683,7 +683,7 @@ export class Canvas {
throw new Error("Old mask editor canvas not found");
}
const maskCtx = maskCanvas.getContext('2d', { willReadFrequently: true });
const maskCtx = maskCanvas.getContext('2d', {willReadFrequently: true});
const maskColor = {r: 255, g: 255, b: 255};
const processedMask = await this.processMaskForEditor(maskData, maskCanvas.width, maskCanvas.height, maskColor);
@@ -699,59 +699,58 @@ export class Canvas {
* @param {number} targetHeight - Docelowa wysokość
* @param {Object} maskColor - Kolor maski {r, g, b}
* @returns {HTMLCanvasElement} Przetworzona maska
*/
async processMaskForEditor(maskData, targetWidth, targetHeight, maskColor) {
const originalWidth = maskData.width || maskData.naturalWidth || this.width;
const originalHeight = maskData.height || maskData.naturalHeight || this.height;
*/async processMaskForEditor(maskData, targetWidth, targetHeight, maskColor) {
// Współrzędne przesunięcia (pan) widoku edytora
const panX = this.maskTool.x;
const panY = this.maskTool.y;
log.info("Processing mask for editor:", {
originalSize: {width: originalWidth, height: originalHeight},
sourceSize: {width: maskData.width, height: maskData.height},
targetSize: {width: targetWidth, height: targetHeight},
canvasSize: {width: this.width, height: this.height}
viewportPan: {x: panX, y: panY}
});
const tempCanvas = document.createElement('canvas');
tempCanvas.width = targetWidth;
tempCanvas.height = targetHeight;
const tempCtx = tempCanvas.getContext('2d', { willReadFrequently: true });
const tempCtx = tempCanvas.getContext('2d', {willReadFrequently: true});
tempCtx.clearRect(0, 0, targetWidth, targetHeight);
const sourceX = -panX;
const sourceY = -panY;
tempCtx.drawImage(
maskData, // Źródło: pełna maska z "output area"
sourceX, // sx: Prawdziwa współrzędna X na dużej masce (np. 1000)
sourceY, // sy: Prawdziwa współrzędna Y na dużej masce (np. 1000)
targetWidth, // sWidth: Szerokość wycinanego fragmentu
targetHeight, // sHeight: Wysokość wycinanego fragmentu
0, // dx: Gdzie wkleić w płótnie docelowym (zawsze 0)
0, // dy: Gdzie wkleić w płótnie docelowym (zawsze 0)
targetWidth, // dWidth: Szerokość wklejanego obrazu
targetHeight // dHeight: Wysokość wklejanego obrazu
);
const scaleToOriginal = Math.min(originalWidth / this.width, originalHeight / this.height);
const scaledWidth = this.width * scaleToOriginal;
const scaledHeight = this.height * scaleToOriginal;
const offsetX = (targetWidth - scaledWidth) / 2;
const offsetY = (targetHeight - scaledHeight) / 2;
tempCtx.drawImage(maskData, offsetX, offsetY, scaledWidth, scaledHeight);
log.info("Mask drawn scaled to original image size:", {
originalSize: {width: originalWidth, height: originalHeight},
targetSize: {width: targetWidth, height: targetHeight},
canvasSize: {width: this.width, height: this.height},
scaleToOriginal: scaleToOriginal,
finalSize: {width: scaledWidth, height: scaledHeight},
offset: {x: offsetX, y: offsetY}
log.info("Mask viewport cropped correctly.", {
source: "maskData",
cropArea: {x: sourceX, y: sourceY, width: targetWidth, height: targetHeight}
});
// Reszta kodu (zmiana koloru) pozostaje bez zmian
const imageData = tempCtx.getImageData(0, 0, targetWidth, targetHeight);
const data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
const alpha = data[i + 3]; // Oryginalny kanał alpha
data[i] = maskColor.r; // R
data[i + 1] = maskColor.g; // G
data[i + 2] = maskColor.b; // B
data[i + 3] = alpha; // Zachowaj oryginalny alpha
const alpha = data[i + 3];
if (alpha > 0) {
data[i] = maskColor.r;
data[i + 1] = maskColor.g;
data[i + 2] = maskColor.b;
}
}
tempCtx.putImageData(imageData, 0, 0);
log.info("Mask processing completed - full size scaling applied");
log.info("Mask processing completed - color applied.");
return tempCanvas;
}
@@ -784,6 +783,7 @@ export class Canvas {
setTimeout(this.waitWhileMaskEditing.bind(this), 100);
}
}
/**
* Zapisuje obecny stan maski przed otwarciem editora
* @returns {Object} Zapisany stan maski
@@ -797,7 +797,7 @@ export class Canvas {
const savedCanvas = document.createElement('canvas');
savedCanvas.width = maskCanvas.width;
savedCanvas.height = maskCanvas.height;
const savedCtx = savedCanvas.getContext('2d', { willReadFrequently: true });
const savedCtx = savedCanvas.getContext('2d', {willReadFrequently: true});
savedCtx.drawImage(maskCanvas, 0, 0);
return {
@@ -878,7 +878,7 @@ export class Canvas {
resultImage.onload = resolve;
resultImage.onerror = reject;
});
log.debug("Result image loaded successfully", {
width: resultImage.width,
height: resultImage.height
@@ -893,7 +893,7 @@ export class Canvas {
const tempCanvas = document.createElement('canvas');
tempCanvas.width = this.width;
tempCanvas.height = this.height;
const tempCtx = tempCanvas.getContext('2d', { willReadFrequently: true });
const tempCtx = tempCanvas.getContext('2d', {willReadFrequently: true});
tempCtx.drawImage(resultImage, 0, 0, this.width, this.height);
@@ -920,7 +920,7 @@ export class Canvas {
const destX = -this.maskTool.x;
const destY = -this.maskTool.y;
log.debug("Applying mask to canvas", { destX, destY });
log.debug("Applying mask to canvas", {destX, destY});
maskCtx.globalCompositeOperation = 'source-over';
maskCtx.clearRect(destX, destY, this.width, this.height);