@markup-canvas/core 1.1.7 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/lib/MarkupCanvas.d.ts +9 -12
- package/dist/lib/actions/config/getConfig.d.ts +2 -0
- package/dist/lib/actions/index.d.ts +3 -0
- package/dist/lib/actions/pan/centerContent.d.ts +2 -0
- package/dist/lib/actions/pan/index.d.ts +6 -0
- package/dist/lib/actions/pan/panDown.d.ts +2 -0
- package/dist/lib/actions/pan/panLeft.d.ts +2 -0
- package/dist/lib/actions/pan/panRight.d.ts +2 -0
- package/dist/lib/actions/pan/panUp.d.ts +2 -0
- package/dist/lib/actions/pan/scrollToPoint.d.ts +2 -0
- package/dist/lib/actions/transform/index.d.ts +2 -0
- package/dist/lib/actions/transform/resetTransform.d.ts +2 -0
- package/dist/lib/actions/transform/updateTransform.d.ts +2 -0
- package/dist/lib/actions/ui/grid/hideGrid.d.ts +2 -0
- package/dist/lib/actions/ui/grid/index.d.ts +4 -0
- package/dist/lib/actions/ui/grid/isGridVisible.d.ts +2 -0
- package/dist/lib/actions/ui/grid/showGrid.d.ts +2 -0
- package/dist/lib/actions/ui/grid/toggleGrid.d.ts +2 -0
- package/dist/lib/actions/ui/index.d.ts +4 -0
- package/dist/lib/actions/ui/rulers/areRulersVisible.d.ts +2 -0
- package/dist/lib/actions/ui/rulers/hideRulers.d.ts +2 -0
- package/dist/lib/actions/ui/rulers/index.d.ts +4 -0
- package/dist/lib/actions/ui/rulers/showRulers.d.ts +2 -0
- package/dist/lib/actions/ui/rulers/toggleRulers.d.ts +2 -0
- package/dist/lib/actions/ui/toggleTransition.d.ts +1 -0
- package/dist/lib/actions/ui/updateThemeMode.d.ts +2 -0
- package/dist/lib/actions/zoom/index.d.ts +6 -0
- package/dist/lib/actions/zoom/resetView.d.ts +2 -0
- package/dist/lib/actions/zoom/resetViewToCenter.d.ts +3 -0
- package/dist/lib/actions/zoom/setZoom.d.ts +3 -0
- package/dist/lib/actions/zoom/zoomIn.d.ts +3 -0
- package/dist/lib/actions/zoom/zoomOut.d.ts +3 -0
- package/dist/lib/actions/zoom/zoomToPoint.d.ts +2 -0
- package/dist/lib/canvas/createCanvas.d.ts +2 -2
- package/dist/lib/canvas/fitToScreen.d.ts +2 -0
- package/dist/lib/canvas/getCanvasBounds.d.ts +2 -2
- package/dist/lib/canvas/index.d.ts +1 -1
- package/dist/lib/events/emitTransformEvents.d.ts +3 -0
- package/dist/lib/events/keyboard/handleKeyDown.d.ts +3 -2
- package/dist/lib/events/keyboard/handleKeyUp.d.ts +3 -2
- package/dist/lib/events/keyboard/setupKeyboardEvents.d.ts +3 -2
- package/dist/lib/events/mouse/createMouseDragControls.d.ts +7 -0
- package/dist/lib/events/mouse/handleClickToZoom.d.ts +3 -2
- package/dist/lib/events/mouse/handleMouseDown.d.ts +3 -2
- package/dist/lib/events/mouse/handleMouseLeave.d.ts +3 -2
- package/dist/lib/events/mouse/handleMouseMove.d.ts +3 -2
- package/dist/lib/events/mouse/handleMouseUp.d.ts +3 -2
- package/dist/lib/events/mouse/setupMouseDrag.d.ts +4 -3
- package/dist/lib/events/mouse/setupMouseEvents.d.ts +4 -3
- package/dist/lib/events/touch/handleTouchMove.d.ts +3 -2
- package/dist/lib/events/touch/setupTouchEvents.d.ts +2 -2
- package/dist/lib/events/trackpad/createTrackpadPanHandler.d.ts +2 -2
- package/dist/lib/events/utils/getAdaptiveZoomSpeed.d.ts +2 -2
- package/dist/lib/events/utils/getViewportCenter.d.ts +2 -2
- package/dist/lib/events/utils/resetDragState.d.ts +3 -2
- package/dist/lib/events/utils/updateCursor.d.ts +3 -2
- package/dist/lib/events/wheel/handleWheel.d.ts +3 -2
- package/dist/lib/events/wheel/setupWheelEvents.d.ts +3 -2
- package/dist/lib/events/wheel/setupWheelHandler.d.ts +3 -2
- package/dist/lib/helpers/getVisibleArea.d.ts +7 -0
- package/dist/lib/helpers/index.d.ts +2 -0
- package/dist/lib/helpers/isPointVisible.d.ts +2 -0
- package/dist/lib/transform/applyZoomToCanvas.d.ts +2 -2
- package/dist/lib/window/bindCanvasToWindow.d.ts +3 -0
- package/dist/lib/window/broadcastEvent.d.ts +2 -0
- package/dist/lib/window/cleanupWindowBinding.d.ts +2 -0
- package/dist/lib/window/index.d.ts +3 -0
- package/dist/markup-canvas.cjs.js +689 -529
- package/dist/markup-canvas.esm.js +689 -529
- package/dist/markup-canvas.umd.js +687 -524
- package/dist/markup-canvas.umd.min.js +1 -1
- package/dist/types/canvas.d.ts +1 -48
- package/dist/types/config.d.ts +0 -3
- package/dist/types/events.d.ts +4 -1
- package/dist/types/index.d.ts +3 -2
- package/dist/types/window.d.ts +84 -0
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/lib/MarkupCanvas.ts +139 -315
- package/src/lib/actions/config/getConfig.ts +5 -0
- package/src/lib/actions/index.ts +6 -0
- package/src/lib/actions/pan/centerContent.ts +21 -0
- package/src/lib/actions/pan/index.ts +6 -0
- package/src/lib/actions/pan/panDown.ts +13 -0
- package/src/lib/actions/pan/panLeft.ts +13 -0
- package/src/lib/actions/pan/panRight.ts +13 -0
- package/src/lib/actions/pan/panUp.ts +13 -0
- package/src/lib/actions/pan/scrollToPoint.ts +27 -0
- package/src/lib/actions/transform/index.ts +2 -0
- package/src/lib/actions/transform/resetTransform.ts +11 -0
- package/src/lib/actions/transform/updateTransform.ts +9 -0
- package/src/lib/actions/ui/grid/hideGrid.ts +9 -0
- package/src/lib/actions/ui/grid/index.ts +4 -0
- package/src/lib/actions/ui/grid/isGridVisible.ts +8 -0
- package/src/lib/actions/ui/grid/showGrid.ts +9 -0
- package/src/lib/actions/ui/grid/toggleGrid.ts +9 -0
- package/src/lib/actions/ui/index.ts +4 -0
- package/src/lib/actions/ui/rulers/areRulersVisible.ts +8 -0
- package/src/lib/actions/ui/rulers/hideRulers.ts +9 -0
- package/src/lib/actions/ui/rulers/index.ts +4 -0
- package/src/lib/actions/ui/rulers/showRulers.ts +9 -0
- package/src/lib/actions/ui/rulers/toggleRulers.ts +14 -0
- package/src/lib/actions/ui/toggleTransition.ts +3 -0
- package/src/lib/actions/ui/updateThemeMode.ts +25 -0
- package/src/lib/actions/zoom/index.ts +6 -0
- package/src/lib/actions/zoom/resetView.ts +17 -0
- package/src/lib/actions/zoom/resetViewToCenter.ts +21 -0
- package/src/lib/actions/zoom/setZoom.ts +22 -0
- package/src/lib/actions/zoom/zoomIn.ts +21 -0
- package/src/lib/actions/zoom/zoomOut.ts +21 -0
- package/src/lib/actions/zoom/zoomToPoint.ts +18 -0
- package/src/lib/canvas/createCanvas.ts +6 -14
- package/src/lib/canvas/fitToScreen.ts +27 -0
- package/src/lib/canvas/getCanvasBounds.ts +3 -4
- package/src/lib/canvas/index.ts +1 -1
- package/src/lib/config/constants.ts +2 -6
- package/src/lib/config/presets/editor-preset.ts +2 -6
- package/src/lib/events/emitTransformEvents.ts +9 -0
- package/src/lib/events/keyboard/handleKeyDown.ts +3 -2
- package/src/lib/events/keyboard/handleKeyUp.ts +3 -2
- package/src/lib/events/keyboard/setupKeyboardEvents.ts +3 -2
- package/src/lib/events/mouse/createMouseDragControls.ts +21 -0
- package/src/lib/events/mouse/handleClickToZoom.ts +3 -2
- package/src/lib/events/mouse/handleMouseDown.ts +3 -2
- package/src/lib/events/mouse/handleMouseLeave.ts +3 -2
- package/src/lib/events/mouse/handleMouseMove.ts +3 -2
- package/src/lib/events/mouse/handleMouseUp.ts +3 -2
- package/src/lib/events/mouse/setupMouseDrag.ts +5 -4
- package/src/lib/events/mouse/setupMouseEvents.ts +5 -4
- package/src/lib/events/postMessage/setupPostMessageEvents.ts +10 -0
- package/src/lib/events/touch/handleTouchMove.ts +3 -2
- package/src/lib/events/touch/setupTouchEvents.ts +3 -2
- package/src/lib/events/trackpad/createTrackpadPanHandler.ts +3 -2
- package/src/lib/events/utils/getAdaptiveZoomSpeed.ts +2 -2
- package/src/lib/events/utils/getViewportCenter.ts +2 -2
- package/src/lib/events/utils/resetDragState.ts +3 -2
- package/src/lib/events/utils/updateCursor.ts +3 -2
- package/src/lib/events/wheel/handleWheel.ts +3 -2
- package/src/lib/events/wheel/setupWheelEvents.ts +3 -2
- package/src/lib/events/wheel/setupWheelHandler.ts +3 -2
- package/src/lib/helpers/getVisibleArea.ts +6 -0
- package/src/lib/helpers/index.ts +2 -0
- package/src/lib/helpers/isPointVisible.ts +7 -0
- package/src/lib/rulers/createRulers.ts +0 -1
- package/src/lib/transform/applyZoomToCanvas.ts +2 -2
- package/src/lib/window/bindCanvasToWindow.ts +128 -0
- package/src/lib/window/broadcastEvent.ts +38 -0
- package/src/lib/window/cleanupWindowBinding.ts +15 -0
- package/src/lib/window/index.ts +3 -0
- package/src/types/canvas.ts +1 -48
- package/src/types/config.ts +1 -7
- package/src/types/events.ts +7 -1
- package/src/types/index.ts +4 -2
- package/src/types/window.ts +77 -0
- package/dist/lib/canvas/config.d.ts +0 -2
- package/dist/lib/canvas/getCanvasMethods.d.ts +0 -12
- package/src/lib/canvas/config.ts +0 -29
- package/src/lib/canvas/getCanvasMethods.ts +0 -102
- /package/dist/lib/canvas/{calcVisibleArea.d.ts → calculateVisibleArea.d.ts} +0 -0
- /package/src/lib/canvas/{calcVisibleArea.ts → calculateVisibleArea.ts} +0 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { getCanvasBounds } from "@/lib/canvas/getCanvasBounds.js";
|
|
2
|
+
import { withTransition } from "@/lib/transition/withTransition.js";
|
|
3
|
+
import type { Canvas, MarkupCanvasConfig, Transform } from "@/types/index.js";
|
|
4
|
+
|
|
5
|
+
export function centerContent(
|
|
6
|
+
canvas: Canvas,
|
|
7
|
+
config: Required<MarkupCanvasConfig>,
|
|
8
|
+
updateTransformFn: (newTransform: Partial<Transform>) => boolean,
|
|
9
|
+
transformLayer: HTMLElement
|
|
10
|
+
): boolean {
|
|
11
|
+
return withTransition(transformLayer, config, () => {
|
|
12
|
+
const bounds = getCanvasBounds(canvas, config);
|
|
13
|
+
const centerX = (bounds.width - bounds.contentWidth * canvas.transform.scale) / 2;
|
|
14
|
+
const centerY = (bounds.height - bounds.contentHeight * canvas.transform.scale) / 2;
|
|
15
|
+
|
|
16
|
+
return updateTransformFn({
|
|
17
|
+
translateX: centerX,
|
|
18
|
+
translateY: centerY,
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { centerContent } from "./centerContent.js";
|
|
2
|
+
export { panDown } from "./panDown.js";
|
|
3
|
+
export { panLeft } from "./panLeft.js";
|
|
4
|
+
export { panRight } from "./panRight.js";
|
|
5
|
+
export { panUp } from "./panUp.js";
|
|
6
|
+
export { scrollToPoint } from "./scrollToPoint.js";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Canvas, MarkupCanvasConfig, Transform } from "@/types/index.js";
|
|
2
|
+
|
|
3
|
+
export function panDown(
|
|
4
|
+
canvas: Canvas,
|
|
5
|
+
config: Required<MarkupCanvasConfig>,
|
|
6
|
+
updateTransform: (newTransform: Partial<Transform>) => boolean
|
|
7
|
+
): boolean {
|
|
8
|
+
const panDistance = config.keyboardPanStep;
|
|
9
|
+
const newTransform: Partial<Transform> = {
|
|
10
|
+
translateY: canvas.transform.translateY - panDistance,
|
|
11
|
+
};
|
|
12
|
+
return updateTransform(newTransform);
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Canvas, MarkupCanvasConfig, Transform } from "@/types/index.js";
|
|
2
|
+
|
|
3
|
+
export function panLeft(
|
|
4
|
+
canvas: Canvas,
|
|
5
|
+
config: Required<MarkupCanvasConfig>,
|
|
6
|
+
updateTransform: (newTransform: Partial<Transform>) => boolean
|
|
7
|
+
): boolean {
|
|
8
|
+
const panDistance = config.keyboardPanStep;
|
|
9
|
+
const newTransform: Partial<Transform> = {
|
|
10
|
+
translateX: canvas.transform.translateX + panDistance,
|
|
11
|
+
};
|
|
12
|
+
return updateTransform(newTransform);
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Canvas, MarkupCanvasConfig, Transform } from "@/types/index.js";
|
|
2
|
+
|
|
3
|
+
export function panRight(
|
|
4
|
+
canvas: Canvas,
|
|
5
|
+
config: Required<MarkupCanvasConfig>,
|
|
6
|
+
updateTransform: (newTransform: Partial<Transform>) => boolean
|
|
7
|
+
): boolean {
|
|
8
|
+
const panDistance = config.keyboardPanStep;
|
|
9
|
+
const newTransform: Partial<Transform> = {
|
|
10
|
+
translateX: canvas.transform.translateX - panDistance,
|
|
11
|
+
};
|
|
12
|
+
return updateTransform(newTransform);
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Canvas, MarkupCanvasConfig, Transform } from "@/types/index.js";
|
|
2
|
+
|
|
3
|
+
export function panUp(
|
|
4
|
+
canvas: Canvas,
|
|
5
|
+
config: Required<MarkupCanvasConfig>,
|
|
6
|
+
updateTransform: (newTransform: Partial<Transform>) => boolean
|
|
7
|
+
): boolean {
|
|
8
|
+
const panDistance = config.keyboardPanStep;
|
|
9
|
+
const newTransform: Partial<Transform> = {
|
|
10
|
+
translateY: canvas.transform.translateY + panDistance,
|
|
11
|
+
};
|
|
12
|
+
return updateTransform(newTransform);
|
|
13
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { getCanvasBounds } from "@/lib/canvas/getCanvasBounds.js";
|
|
2
|
+
import { withTransition } from "@/lib/transition/withTransition.js";
|
|
3
|
+
import type { Canvas, MarkupCanvasConfig, Transform } from "@/types/index.js";
|
|
4
|
+
|
|
5
|
+
export function scrollToPoint(
|
|
6
|
+
canvas: Canvas,
|
|
7
|
+
config: Required<MarkupCanvasConfig>,
|
|
8
|
+
x: number,
|
|
9
|
+
y: number,
|
|
10
|
+
updateTransform: (newTransform: Partial<Transform>) => boolean,
|
|
11
|
+
transformLayer: HTMLElement
|
|
12
|
+
): boolean {
|
|
13
|
+
return withTransition(transformLayer, config, () => {
|
|
14
|
+
const bounds = getCanvasBounds(canvas, config);
|
|
15
|
+
const centerX = bounds.width / 2;
|
|
16
|
+
const centerY = bounds.height / 2;
|
|
17
|
+
|
|
18
|
+
// Calculate new translation to center the point
|
|
19
|
+
const newTranslateX = centerX - x * canvas.transform.scale;
|
|
20
|
+
const newTranslateY = centerY - y * canvas.transform.scale;
|
|
21
|
+
|
|
22
|
+
return updateTransform({
|
|
23
|
+
translateX: newTranslateX,
|
|
24
|
+
translateY: newTranslateY,
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Canvas, Transform } from "@/types/index.js";
|
|
2
|
+
import { updateTransform } from "./updateTransform.js";
|
|
3
|
+
|
|
4
|
+
export function resetTransform(canvas: Canvas): boolean {
|
|
5
|
+
const resetTransformData: Transform = {
|
|
6
|
+
scale: 1.0,
|
|
7
|
+
translateX: 0,
|
|
8
|
+
translateY: 0,
|
|
9
|
+
};
|
|
10
|
+
return updateTransform(canvas, resetTransformData);
|
|
11
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { createMatrix } from "@/lib/matrix/createMatrix.js";
|
|
2
|
+
import { applyTransform } from "@/lib/transform/index.js";
|
|
3
|
+
import type { Canvas, Transform } from "@/types/index.js";
|
|
4
|
+
|
|
5
|
+
export function updateTransform(canvas: Canvas, newTransform: Partial<Transform>): boolean {
|
|
6
|
+
canvas.transform = { ...canvas.transform, ...newTransform };
|
|
7
|
+
const matrix = createMatrix(canvas.transform.scale, canvas.transform.translateX, canvas.transform.translateY);
|
|
8
|
+
return applyTransform(canvas.transformLayer, matrix);
|
|
9
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { RulerSystem } from "@/types/index.js";
|
|
2
|
+
|
|
3
|
+
export function toggleRulers(rulers: RulerSystem | null, areRulersVisible: () => boolean): boolean {
|
|
4
|
+
if (rulers) {
|
|
5
|
+
const isVisible = areRulersVisible();
|
|
6
|
+
if (isVisible) {
|
|
7
|
+
rulers.hide();
|
|
8
|
+
} else {
|
|
9
|
+
rulers.show();
|
|
10
|
+
}
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createMarkupCanvasConfig } from "@/lib/config/createMarkupCanvasConfig.js";
|
|
2
|
+
import { getThemeValue } from "@/lib/helpers/index.js";
|
|
3
|
+
import type { MarkupCanvasConfig, RulerSystem } from "@/types/index.js";
|
|
4
|
+
|
|
5
|
+
export function updateThemeMode(
|
|
6
|
+
canvasContainer: HTMLElement,
|
|
7
|
+
config: Required<MarkupCanvasConfig>,
|
|
8
|
+
rulers: RulerSystem | null,
|
|
9
|
+
mode: "light" | "dark"
|
|
10
|
+
): void {
|
|
11
|
+
const newConfig = {
|
|
12
|
+
...config,
|
|
13
|
+
themeMode: mode,
|
|
14
|
+
};
|
|
15
|
+
const updatedConfig = createMarkupCanvasConfig(newConfig);
|
|
16
|
+
|
|
17
|
+
// Update canvas background color
|
|
18
|
+
const backgroundColor = getThemeValue(updatedConfig, "canvasBackgroundColor");
|
|
19
|
+
canvasContainer.style.backgroundColor = backgroundColor;
|
|
20
|
+
|
|
21
|
+
// Update rulers if they exist
|
|
22
|
+
if (rulers) {
|
|
23
|
+
rulers.updateTheme(updatedConfig);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { resetView } from "./resetView.js";
|
|
2
|
+
export { resetViewToCenter } from "./resetViewToCenter.js";
|
|
3
|
+
export { setZoom } from "./setZoom.js";
|
|
4
|
+
export { zoomIn } from "./zoomIn.js";
|
|
5
|
+
export { zoomOut } from "./zoomOut.js";
|
|
6
|
+
export { zoomToPoint } from "./zoomToPoint.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { updateTransform } from "@/lib/actions/transform/updateTransform.js";
|
|
2
|
+
import { withRulerSize } from "@/lib/helpers/index.js";
|
|
3
|
+
import { withTransition } from "@/lib/transition/index.js";
|
|
4
|
+
import type { Canvas, MarkupCanvasConfig, Transform } from "@/types/index.js";
|
|
5
|
+
|
|
6
|
+
export function resetView(canvas: Canvas, transformLayer: HTMLElement, config: Required<MarkupCanvasConfig>): boolean {
|
|
7
|
+
return withTransition(transformLayer, config, () => {
|
|
8
|
+
return withRulerSize(canvas, config.rulerSize, (rulerSize) => {
|
|
9
|
+
const resetTransformData: Transform = {
|
|
10
|
+
scale: 1.0,
|
|
11
|
+
translateX: rulerSize * -1,
|
|
12
|
+
translateY: rulerSize * -1,
|
|
13
|
+
};
|
|
14
|
+
return updateTransform(canvas, resetTransformData);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { getViewportCenter } from "@/lib/events/utils/getViewportCenter.js";
|
|
2
|
+
import { withClampedZoom } from "@/lib/helpers/index.js";
|
|
3
|
+
import type { MarkupCanvas } from "@/lib/MarkupCanvas.js";
|
|
4
|
+
import { withTransition } from "@/lib/transition/withTransition.js";
|
|
5
|
+
import type { MarkupCanvasConfig } from "@/types/index.js";
|
|
6
|
+
|
|
7
|
+
export function resetViewToCenter(
|
|
8
|
+
canvas: MarkupCanvas,
|
|
9
|
+
transformLayer: HTMLElement,
|
|
10
|
+
config: Required<MarkupCanvasConfig>,
|
|
11
|
+
zoomToPoint: (x: number, y: number, targetScale: number) => boolean
|
|
12
|
+
): boolean {
|
|
13
|
+
return withTransition(transformLayer, config, () => {
|
|
14
|
+
return withClampedZoom(config, (clamp) => {
|
|
15
|
+
const newScale = clamp(1.0);
|
|
16
|
+
|
|
17
|
+
const center = getViewportCenter(canvas);
|
|
18
|
+
return zoomToPoint(center.x, center.y, newScale);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { getViewportCenter } from "@/lib/events/utils/getViewportCenter.js";
|
|
2
|
+
import { withClampedZoom } from "@/lib/helpers/index.js";
|
|
3
|
+
import type { MarkupCanvas } from "@/lib/MarkupCanvas.js";
|
|
4
|
+
import { withTransition } from "@/lib/transition/withTransition.js";
|
|
5
|
+
import type { MarkupCanvasConfig } from "@/types/index.js";
|
|
6
|
+
|
|
7
|
+
export function setZoom(
|
|
8
|
+
canvas: MarkupCanvas,
|
|
9
|
+
transformLayer: HTMLElement,
|
|
10
|
+
config: Required<MarkupCanvasConfig>,
|
|
11
|
+
zoomToPoint: (x: number, y: number, targetScale: number) => boolean,
|
|
12
|
+
zoomLevel: number
|
|
13
|
+
): boolean {
|
|
14
|
+
return withTransition(transformLayer, config, () => {
|
|
15
|
+
return withClampedZoom(config, (clamp) => {
|
|
16
|
+
const newScale = clamp(zoomLevel);
|
|
17
|
+
|
|
18
|
+
const center = getViewportCenter(canvas);
|
|
19
|
+
return zoomToPoint(center.x, center.y, newScale);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { getViewportCenter } from "@/lib/events/utils/getViewportCenter.js";
|
|
2
|
+
import { withClampedZoom } from "@/lib/helpers/index.js";
|
|
3
|
+
import type { MarkupCanvas } from "@/lib/MarkupCanvas.js";
|
|
4
|
+
import { withTransition } from "@/lib/transition/withTransition.js";
|
|
5
|
+
import type { MarkupCanvasConfig } from "@/types/index.js";
|
|
6
|
+
|
|
7
|
+
export function zoomIn(
|
|
8
|
+
canvas: MarkupCanvas,
|
|
9
|
+
transformLayer: HTMLElement,
|
|
10
|
+
config: Required<MarkupCanvasConfig>,
|
|
11
|
+
zoomToPoint: (x: number, y: number, targetScale: number) => boolean,
|
|
12
|
+
factor: number = 0.5
|
|
13
|
+
): boolean {
|
|
14
|
+
return withTransition(transformLayer, config, () => {
|
|
15
|
+
return withClampedZoom(config, (clamp) => {
|
|
16
|
+
const newScale = clamp(canvas.transform.scale * (1 + factor));
|
|
17
|
+
const center = getViewportCenter(canvas);
|
|
18
|
+
return zoomToPoint(center.x, center.y, newScale);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { getViewportCenter } from "@/lib/events/utils/getViewportCenter.js";
|
|
2
|
+
import { withClampedZoom } from "@/lib/helpers/index.js";
|
|
3
|
+
import type { MarkupCanvas } from "@/lib/MarkupCanvas.js";
|
|
4
|
+
import { withTransition } from "@/lib/transition/withTransition.js";
|
|
5
|
+
import type { MarkupCanvasConfig } from "@/types/index.js";
|
|
6
|
+
|
|
7
|
+
export function zoomOut(
|
|
8
|
+
canvas: MarkupCanvas,
|
|
9
|
+
transformLayer: HTMLElement,
|
|
10
|
+
config: Required<MarkupCanvasConfig>,
|
|
11
|
+
zoomToPoint: (x: number, y: number, targetScale: number) => boolean,
|
|
12
|
+
factor: number = 0.5
|
|
13
|
+
): boolean {
|
|
14
|
+
return withTransition(transformLayer, config, () => {
|
|
15
|
+
return withClampedZoom(config, (clamp) => {
|
|
16
|
+
const newScale = clamp(canvas.transform.scale * (1 - factor));
|
|
17
|
+
const center = getViewportCenter(canvas);
|
|
18
|
+
return zoomToPoint(center.x, center.y, newScale);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { updateTransform } from "@/lib/actions/transform/updateTransform.js";
|
|
2
|
+
import { getZoomToMouseTransform } from "@/lib/matrix/getZoomToMouseTransform.js";
|
|
3
|
+
import { withTransition } from "@/lib/transition/index.js";
|
|
4
|
+
import type { Canvas, MarkupCanvasConfig } from "@/types/index.js";
|
|
5
|
+
|
|
6
|
+
export function zoomToPoint(
|
|
7
|
+
canvas: Canvas,
|
|
8
|
+
transformLayer: HTMLElement,
|
|
9
|
+
config: Required<MarkupCanvasConfig>,
|
|
10
|
+
x: number,
|
|
11
|
+
y: number,
|
|
12
|
+
targetScale: number
|
|
13
|
+
): boolean {
|
|
14
|
+
return withTransition(transformLayer, config, () => {
|
|
15
|
+
const newTransform = getZoomToMouseTransform(x, y, canvas.transform, targetScale / canvas.transform.scale, config);
|
|
16
|
+
return updateTransform(canvas, newTransform);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { createCanvasLayers } from "@/lib/canvas/createCanvasLayers.js";
|
|
2
|
-
import { getCanvasMethods } from "@/lib/canvas/getCanvasMethods.js";
|
|
3
2
|
import { setupCanvasContainer } from "@/lib/canvas/setupCanvasContainer.js";
|
|
4
3
|
import { DEFAULT_ZOOM } from "@/lib/constants.js";
|
|
4
|
+
import { withFeatureEnabled } from "@/lib/helpers/index.js";
|
|
5
5
|
import { createMatrix } from "@/lib/matrix/createMatrix.js";
|
|
6
6
|
import { applyTransform, enableHardwareAcceleration } from "@/lib/transform/index.js";
|
|
7
|
-
import type {
|
|
7
|
+
import type { Canvas, MarkupCanvasConfig, Transform } from "@/types/index.js";
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
export function createCanvas(container: HTMLElement, config: Required<MarkupCanvasConfig>): BaseCanvas | null {
|
|
9
|
+
export function createCanvas(container: HTMLElement, config: Required<MarkupCanvasConfig>): Canvas | null {
|
|
11
10
|
if (!container?.appendChild) {
|
|
12
11
|
console.error("Invalid container element provided to createCanvas");
|
|
13
12
|
return null;
|
|
@@ -18,10 +17,9 @@ export function createCanvas(container: HTMLElement, config: Required<MarkupCanv
|
|
|
18
17
|
|
|
19
18
|
const { transformLayer, contentLayer } = createCanvasLayers(container, config);
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
if (config.enableAcceleration) {
|
|
20
|
+
withFeatureEnabled(config, "enableAcceleration", () => {
|
|
23
21
|
enableHardwareAcceleration(transformLayer);
|
|
24
|
-
}
|
|
22
|
+
});
|
|
25
23
|
|
|
26
24
|
const rulerOffset = config.enableRulers ? -config.rulerSize : 0;
|
|
27
25
|
|
|
@@ -36,20 +34,14 @@ export function createCanvas(container: HTMLElement, config: Required<MarkupCanv
|
|
|
36
34
|
|
|
37
35
|
applyTransform(transformLayer, initialMatrix);
|
|
38
36
|
|
|
39
|
-
const canvas:
|
|
37
|
+
const canvas: Canvas = {
|
|
40
38
|
// DOM references
|
|
41
39
|
container,
|
|
42
40
|
transformLayer,
|
|
43
41
|
contentLayer,
|
|
44
42
|
|
|
45
|
-
// Configuration
|
|
46
|
-
config: config,
|
|
47
|
-
|
|
48
43
|
// Current state
|
|
49
44
|
transform: initialTransform,
|
|
50
|
-
|
|
51
|
-
// Add all canvas methods
|
|
52
|
-
...getCanvasMethods(),
|
|
53
45
|
};
|
|
54
46
|
|
|
55
47
|
return canvas;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { updateTransform } from "@/lib/actions/transform/updateTransform.js";
|
|
2
|
+
import { getCanvasBounds } from "@/lib/canvas/getCanvasBounds.js";
|
|
3
|
+
import { ZOOM_FIT_PADDING } from "@/lib/constants.js";
|
|
4
|
+
import { withClampedZoom } from "@/lib/helpers/index.js";
|
|
5
|
+
import { withTransition } from "@/lib/transition/index.js";
|
|
6
|
+
import type { Canvas, MarkupCanvasConfig } from "@/types/index.js";
|
|
7
|
+
|
|
8
|
+
export function fitToScreen(canvas: Canvas, transformLayer: HTMLElement, config: Required<MarkupCanvasConfig>): boolean {
|
|
9
|
+
return withTransition(transformLayer, config, () => {
|
|
10
|
+
const bounds = getCanvasBounds(canvas, config);
|
|
11
|
+
const scaleX = bounds.width / config.width;
|
|
12
|
+
const scaleY = bounds.height / config.height;
|
|
13
|
+
const fitScale = withClampedZoom(config, (clamp) => clamp(Math.min(scaleX, scaleY) * ZOOM_FIT_PADDING));
|
|
14
|
+
|
|
15
|
+
// Center the content
|
|
16
|
+
const scaledWidth = config.width * fitScale;
|
|
17
|
+
const scaledHeight = config.height * fitScale;
|
|
18
|
+
const centerX = (bounds.width - scaledWidth) / 2;
|
|
19
|
+
const centerY = (bounds.height - scaledHeight) / 2;
|
|
20
|
+
|
|
21
|
+
return updateTransform(canvas, {
|
|
22
|
+
scale: fitScale,
|
|
23
|
+
translateX: centerX,
|
|
24
|
+
translateY: centerY,
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import { calculateVisibleArea } from "@/lib/canvas/
|
|
1
|
+
import { calculateVisibleArea } from "@/lib/canvas/calculateVisibleArea.js";
|
|
2
2
|
import { getEmptyBounds } from "@/lib/canvas/getEmptyBounds.js";
|
|
3
3
|
import { withRulerSize } from "@/lib/helpers/index.js";
|
|
4
|
-
import type {
|
|
4
|
+
import type { Canvas, CanvasBounds, MarkupCanvasConfig } from "@/types/index.js";
|
|
5
5
|
import { DEFAULT_CONFIG } from "../config/constants";
|
|
6
6
|
|
|
7
|
-
export function getCanvasBounds(canvas:
|
|
7
|
+
export function getCanvasBounds(canvas: Canvas, config: Required<MarkupCanvasConfig>): CanvasBounds {
|
|
8
8
|
try {
|
|
9
9
|
const container = canvas.container;
|
|
10
|
-
const config = canvas.config;
|
|
11
10
|
const transform = canvas.transform || {
|
|
12
11
|
scale: 1.0,
|
|
13
12
|
translateX: 0,
|
package/src/lib/canvas/index.ts
CHANGED
|
@@ -6,8 +6,7 @@ export const DEFAULT_CONFIG: Required<MarkupCanvasConfig> = {
|
|
|
6
6
|
height: 8000,
|
|
7
7
|
enableAcceleration: true,
|
|
8
8
|
|
|
9
|
-
// Global
|
|
10
|
-
bindToWindow: false,
|
|
9
|
+
// Global Instance Access
|
|
11
10
|
name: "markupCanvas",
|
|
12
11
|
enablePostMessageAPI: false,
|
|
13
12
|
|
|
@@ -16,7 +15,7 @@ export const DEFAULT_CONFIG: Required<MarkupCanvasConfig> = {
|
|
|
16
15
|
enablePan: true,
|
|
17
16
|
enableTouch: true,
|
|
18
17
|
enableKeyboard: true,
|
|
19
|
-
bindKeyboardEventsTo: "
|
|
18
|
+
bindKeyboardEventsTo: "document",
|
|
20
19
|
|
|
21
20
|
// Zoom behavior
|
|
22
21
|
zoomSpeed: 1.5,
|
|
@@ -71,7 +70,4 @@ export const DEFAULT_CONFIG: Required<MarkupCanvasConfig> = {
|
|
|
71
70
|
|
|
72
71
|
// Theme
|
|
73
72
|
themeMode: "light",
|
|
74
|
-
|
|
75
|
-
// Callbacks
|
|
76
|
-
onTransformUpdate: () => {},
|
|
77
73
|
};
|
|
@@ -6,8 +6,7 @@ export const EDITOR_PRESET: Required<MarkupCanvasConfig> = {
|
|
|
6
6
|
height: 4000,
|
|
7
7
|
enableAcceleration: true,
|
|
8
8
|
|
|
9
|
-
// Global
|
|
10
|
-
bindToWindow: true,
|
|
9
|
+
// Global Instance Access
|
|
11
10
|
name: "canvas",
|
|
12
11
|
enablePostMessageAPI: true,
|
|
13
12
|
|
|
@@ -43,7 +42,7 @@ export const EDITOR_PRESET: Required<MarkupCanvasConfig> = {
|
|
|
43
42
|
|
|
44
43
|
// Visual elements
|
|
45
44
|
enableRulers: true,
|
|
46
|
-
enableGrid:
|
|
45
|
+
enableGrid: true,
|
|
47
46
|
showRulers: true,
|
|
48
47
|
showGrid: false,
|
|
49
48
|
|
|
@@ -72,7 +71,4 @@ export const EDITOR_PRESET: Required<MarkupCanvasConfig> = {
|
|
|
72
71
|
|
|
73
72
|
// Theme
|
|
74
73
|
themeMode: "light",
|
|
75
|
-
|
|
76
|
-
// Callbacks
|
|
77
|
-
onTransformUpdate: () => {},
|
|
78
74
|
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { EventEmitter } from "@/lib/events/EventEmitter.js";
|
|
2
|
+
import type { Canvas, MarkupCanvasEvents } from "@/types/index.js";
|
|
3
|
+
|
|
4
|
+
export function emitTransformEvents(listen: EventEmitter<MarkupCanvasEvents>, canvas: Canvas): void {
|
|
5
|
+
const transform = canvas.transform;
|
|
6
|
+
listen.emit("transform", transform);
|
|
7
|
+
listen.emit("zoom", transform.scale);
|
|
8
|
+
listen.emit("pan", { x: transform.translateX, y: transform.translateY });
|
|
9
|
+
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { updateCursor } from "@/lib/events/utils/updateCursor.js";
|
|
2
|
-
import type {
|
|
2
|
+
import type { MarkupCanvas } from "@/lib/MarkupCanvas.js";
|
|
3
|
+
import type { MarkupCanvasConfig } from "@/types/index.js";
|
|
3
4
|
|
|
4
5
|
export function handleKeyDown(
|
|
5
6
|
event: KeyboardEvent,
|
|
6
|
-
canvas:
|
|
7
|
+
canvas: MarkupCanvas,
|
|
7
8
|
config: Required<MarkupCanvasConfig>,
|
|
8
9
|
isDragEnabled: boolean,
|
|
9
10
|
isDragging: boolean,
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { resetDragState } from "@/lib/events/utils/resetDragState.js";
|
|
2
2
|
import { updateCursor } from "@/lib/events/utils/updateCursor.js";
|
|
3
|
-
import type {
|
|
3
|
+
import type { MarkupCanvas } from "@/lib/MarkupCanvas.js";
|
|
4
|
+
import type { MarkupCanvasConfig } from "@/types/index.js";
|
|
4
5
|
|
|
5
6
|
export function handleKeyUp(
|
|
6
7
|
event: KeyboardEvent,
|
|
7
|
-
canvas:
|
|
8
|
+
canvas: MarkupCanvas,
|
|
8
9
|
config: Required<MarkupCanvasConfig>,
|
|
9
10
|
isDragEnabled: boolean,
|
|
10
11
|
isDragging: boolean,
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { getAdaptiveZoomSpeed } from "@/lib/events/utils/getAdaptiveZoomSpeed.js";
|
|
2
|
-
import type {
|
|
2
|
+
import type { MarkupCanvas } from "@/lib/MarkupCanvas.js";
|
|
3
|
+
import type { MarkupCanvasConfig, Transform } from "@/types/index.js";
|
|
3
4
|
|
|
4
|
-
export function setupKeyboardEvents(canvas:
|
|
5
|
+
export function setupKeyboardEvents(canvas: MarkupCanvas, config: Required<MarkupCanvasConfig>): () => void {
|
|
5
6
|
function handleKeyDown(event: Event): void {
|
|
6
7
|
if (!(event instanceof KeyboardEvent)) return;
|
|
7
8
|
|