Update Canvas.js

This commit is contained in:
Dariusz L
2025-06-21 02:01:20 +02:00
parent fe5ca7e958
commit c46054e442

View File

@@ -112,9 +112,7 @@ export class Canvas {
} }
} }
/**
* Resetuje stan interakcji do wartości domyślnych.
*/
resetInteractionState() { resetInteractionState() {
this.interaction.mode = 'none'; this.interaction.mode = 'none';
this.interaction.resizeHandle = null; this.interaction.resizeHandle = null;
@@ -126,9 +124,7 @@ export class Canvas {
this.canvas.style.cursor = 'default'; this.canvas.style.cursor = 'default';
} }
/**
* Główna metoda obsługująca wciśnięcie przycisku myszy.
*/
handleMouseDown(e) { handleMouseDown(e) {
const currentTime = Date.now(); const currentTime = Date.now();
const worldCoords = this.getMouseWorldCoordinates(e); const worldCoords = this.getMouseWorldCoordinates(e);
@@ -171,10 +167,7 @@ export class Canvas {
this.render(); this.render();
} }
/**
* Kopiuje zaznaczone warstwy do wewnętrznego schowka ORAZ
* jako spłaszczony obraz do globalnego schowka systemowego.
*/
async copySelectedLayers() { async copySelectedLayers() {
if (this.selectedLayers.length === 0) return; if (this.selectedLayers.length === 0) return;
this.internalClipboard = this.selectedLayers.map(layer => ({...layer})); this.internalClipboard = this.selectedLayers.map(layer => ({...layer}));
@@ -191,9 +184,7 @@ export class Canvas {
} }
} }
/**
* Wkleja warstwy z wewnętrznego schowka na płótno.
*/
pasteLayers() { pasteLayers() {
if (this.internalClipboard.length === 0) return; if (this.internalClipboard.length === 0) return;
@@ -216,11 +207,7 @@ export class Canvas {
console.log(`Pasted ${newLayers.length} layer(s).`); console.log(`Pasted ${newLayers.length} layer(s).`);
} }
/**
* Inteligentnie obsługuje operację wklejania.
* Najpierw próbuje wkleić obraz z globalnego schowka,
* a jeśli to się nie uda, wkleja z wewnętrznego schowka.
*/
async handlePaste() { async handlePaste() {
try { try {
if (!navigator.clipboard?.read) { if (!navigator.clipboard?.read) {
@@ -270,9 +257,7 @@ export class Canvas {
} }
} }
/**
* Główna metoda obsługująca ruch myszy.
*/
handleMouseMove(e) { handleMouseMove(e) {
const worldCoords = this.getMouseWorldCoordinates(e); const worldCoords = this.getMouseWorldCoordinates(e);
this.lastMousePosition = worldCoords; this.lastMousePosition = worldCoords;
@@ -302,9 +287,7 @@ export class Canvas {
} }
} }
/**
* Metoda obsługująca puszczenie przycisku myszy.
*/
handleMouseUp(e) { handleMouseUp(e) {
if (this.interaction.mode === 'resizingCanvas') { if (this.interaction.mode === 'resizingCanvas') {
this.finalizeCanvasResize(); this.finalizeCanvasResize();
@@ -315,9 +298,7 @@ export class Canvas {
this.render(); this.render();
} }
/**
* Metoda obsługująca opuszczenie obszaru canvas przez kursor.
*/
handleMouseLeave(e) { handleMouseLeave(e) {
if (this.interaction.mode !== 'none') { if (this.interaction.mode !== 'none') {
this.resetInteractionState(); this.resetInteractionState();
@@ -325,9 +306,7 @@ export class Canvas {
} }
} }
/**
* Metoda obsługująca kółko myszy (zoom / skalowanie / rotacja warstwy).
*/
handleWheel(e) { handleWheel(e) {
e.preventDefault(); e.preventDefault();
if (this.selectedLayer) { if (this.selectedLayer) {
@@ -402,9 +381,7 @@ export class Canvas {
this.render(); this.render();
} }
/**
* Metoda obsługująca wciśnięcie klawisza.
*/
handleKeyDown(e) { handleKeyDown(e) {
if (this.isMouseOver) { if (this.isMouseOver) {
if (e.ctrlKey && e.key.toLowerCase() === 'c') { if (e.ctrlKey && e.key.toLowerCase() === 'c') {
@@ -439,8 +416,7 @@ export class Canvas {
const step = e.shiftKey ? 10 : 1; const step = e.shiftKey ? 10 : 1;
let needsRender = false; let needsRender = false;
switch (e.code) {
switch (e.key) {
case 'ArrowLeft': case 'ArrowLeft':
this.selectedLayers.forEach(l => l.x -= step); this.selectedLayers.forEach(l => l.x -= step);
needsRender = true; needsRender = true;
@@ -457,11 +433,11 @@ export class Canvas {
this.selectedLayers.forEach(l => l.y += step); this.selectedLayers.forEach(l => l.y += step);
needsRender = true; needsRender = true;
break; break;
case '[': case 'BracketLeft':
this.selectedLayers.forEach(l => l.rotation -= step); this.selectedLayers.forEach(l => l.rotation -= step);
needsRender = true; needsRender = true;
break; break;
case ']': case 'BracketRight':
this.selectedLayers.forEach(l => l.rotation += step); this.selectedLayers.forEach(l => l.rotation += step);
needsRender = true; needsRender = true;
break; break;
@@ -474,9 +450,7 @@ export class Canvas {
} }
} }
/**
* Metoda obsługująca puszczenie klawisza.
*/
handleKeyUp(e) { handleKeyUp(e) {
if (e.key === 'Control') this.interaction.isCtrlPressed = false; if (e.key === 'Control') this.interaction.isCtrlPressed = false;
if (e.key === 'Alt') this.interaction.isAltPressed = false; if (e.key === 'Alt') this.interaction.isAltPressed = false;
@@ -486,13 +460,13 @@ export class Canvas {
const transformTarget = this.getHandleAtPosition(worldCoords.x, worldCoords.y); const transformTarget = this.getHandleAtPosition(worldCoords.x, worldCoords.y);
if (transformTarget) { if (transformTarget) {
const handleName = transformTarget.handle; // Wyciągamy nazwę uchwytu z obiektu const handleName = transformTarget.handle;
const cursorMap = { const cursorMap = {
'n': 'ns-resize', 's': 'ns-resize', 'e': 'ew-resize', 'w': 'ew-resize', 'n': 'ns-resize', 's': 'ns-resize', 'e': 'ew-resize', 'w': 'ew-resize',
'nw': 'nwse-resize', 'se': 'nwse-resize', 'ne': 'nesw-resize', 'sw': 'nesw-resize', 'nw': 'nwse-resize', 'se': 'nwse-resize', 'ne': 'nesw-resize', 'sw': 'nesw-resize',
'rot': 'grab' 'rot': 'grab'
}; };
this.canvas.style.cursor = cursorMap[handleName]; // Używamy nazwy jako klucza this.canvas.style.cursor = cursorMap[handleName];
} else if (this.getLayerAtPosition(worldCoords.x, worldCoords.y)) { } else if (this.getLayerAtPosition(worldCoords.x, worldCoords.y)) {
this.canvas.style.cursor = 'move'; this.canvas.style.cursor = 'move';
} else { } else {
@@ -579,9 +553,7 @@ export class Canvas {
this.render(); this.render();
} }
/**
* Aktualizuje pozycję "ducha" płótna podczas przesuwania.
*/
updateCanvasMove(worldCoords) { updateCanvasMove(worldCoords) {
if (!this.interaction.canvasMoveRect) return; if (!this.interaction.canvasMoveRect) return;
const dx = worldCoords.x - this.interaction.dragStart.x; const dx = worldCoords.x - this.interaction.dragStart.x;
@@ -594,9 +566,7 @@ export class Canvas {
this.render(); this.render();
} }
/**
* Kończy przesuwanie płótna i zatwierdza nową pozycję.
*/
finalizeCanvasMove() { finalizeCanvasMove() {
const moveRect = this.interaction.canvasMoveRect; const moveRect = this.interaction.canvasMoveRect;
@@ -1010,7 +980,7 @@ export class Canvas {
layer.width, layer.width,
layer.height layer.height
); );
if (layer.mask) { /* Logika maski */ if (layer.mask) {
} }
if (this.selectedLayers.includes(layer)) { if (this.selectedLayers.includes(layer)) {
this.drawSelectionFrame(ctx, layer); this.drawSelectionFrame(ctx, layer);
@@ -1474,10 +1444,7 @@ export class Canvas {
} }
/**
* Tworzy spłaszczony obraz z zaznaczonych warstw, przycięty do ich zawartości.
* @returns {Promise<Blob|null>} Obiekt Blob z obrazem PNG lub null, jeśli nic nie jest zaznaczone.
*/
async getFlattenedSelectionAsBlob() { async getFlattenedSelectionAsBlob() {
if (this.selectedLayers.length === 0) { if (this.selectedLayers.length === 0) {
return null; return null;