@deepnoid/canvas 0.1.58 → 0.1.60

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.
Files changed (84) hide show
  1. package/README.md +202 -3
  2. package/dist/engine/annotation/annotationTypes.d.ts +21 -0
  3. package/dist/engine/annotation/annotationTypes.js +6 -0
  4. package/dist/engine/annotation/rectangle/rectangleController.d.ts +26 -0
  5. package/dist/engine/annotation/rectangle/rectangleController.js +115 -0
  6. package/dist/engine/annotation/rectangle/rectangleHitTest.d.ts +7 -0
  7. package/dist/engine/annotation/rectangle/rectangleHitTest.js +102 -0
  8. package/dist/engine/annotation/rectangle/rectangleInteraction.d.ts +17 -0
  9. package/dist/engine/annotation/rectangle/rectangleInteraction.js +30 -0
  10. package/dist/engine/annotation/rectangle/rectangleMath.d.ts +10 -0
  11. package/dist/engine/annotation/rectangle/rectangleMath.js +29 -0
  12. package/dist/engine/annotation/rectangle/rectangleRenderer.d.ts +5 -0
  13. package/dist/engine/annotation/rectangle/rectangleRenderer.js +88 -0
  14. package/dist/engine/annotation/rectangle/rectangleTransform.d.ts +14 -0
  15. package/dist/engine/annotation/rectangle/rectangleTransform.js +65 -0
  16. package/dist/{enum/common.d.ts → engine/annotation/rectangle/rectangleTypes.d.ts} +0 -3
  17. package/dist/{enum/common.js → engine/annotation/rectangle/rectangleTypes.js} +0 -4
  18. package/dist/engine/history.d.ts +11 -0
  19. package/dist/{components/AnnotationCanvas/_utils/createHistory.js → engine/history.js} +4 -4
  20. package/dist/engine/interaction/drawModeRouter.d.ts +3 -0
  21. package/dist/engine/interaction/drawModeRouter.js +56 -0
  22. package/dist/engine/interaction/interactionController.d.ts +13 -0
  23. package/dist/engine/interaction/interactionController.js +53 -0
  24. package/dist/engine/interaction/interface.d.ts +15 -0
  25. package/dist/engine/interaction/panZoomInteraction.d.ts +3 -0
  26. package/dist/engine/interaction/panZoomInteraction.js +29 -0
  27. package/dist/engine/interaction/pointerInteraction.d.ts +16 -0
  28. package/dist/engine/interaction/pointerInteraction.js +48 -0
  29. package/dist/engine/pan-zoom/panZoomController.d.ts +26 -0
  30. package/dist/engine/pan-zoom/panZoomController.js +148 -0
  31. package/dist/engine/pan-zoom/panZoomUtils.d.ts +10 -0
  32. package/dist/engine/pan-zoom/panZoomUtils.js +24 -0
  33. package/dist/engine/public/annotationEngine.d.ts +75 -0
  34. package/dist/engine/public/annotationEngine.js +263 -0
  35. package/dist/engine/renderer/drawCross.d.ts +2 -0
  36. package/dist/engine/renderer/drawCross.js +19 -0
  37. package/dist/engine/renderer/interface.d.ts +6 -0
  38. package/dist/engine/renderer/interface.js +1 -0
  39. package/dist/{types/index.d.ts → engine/types.d.ts} +12 -21
  40. package/dist/engine/types.js +1 -0
  41. package/dist/engine/utils/mousePoint.d.ts +3 -0
  42. package/dist/engine/utils/mousePoint.js +52 -0
  43. package/dist/index.d.ts +4 -5
  44. package/dist/index.js +2 -2
  45. package/dist/{components/AnnotationCanvas/index.d.ts → react/AnnotationCanvas.d.ts} +7 -6
  46. package/dist/react/AnnotationCanvas.js +110 -0
  47. package/dist/{components → react}/index.d.ts +1 -1
  48. package/dist/{components → react}/index.js +1 -1
  49. package/package.json +1 -1
  50. package/dist/components/AnnotationCanvas/_hooks/useImagePanZoom.d.ts +0 -24
  51. package/dist/components/AnnotationCanvas/_hooks/useImagePanZoom.js +0 -143
  52. package/dist/components/AnnotationCanvas/_utils/createHistory.d.ts +0 -11
  53. package/dist/components/AnnotationCanvas/_utils/panZoom.d.ts +0 -10
  54. package/dist/components/AnnotationCanvas/_utils/panZoom.js +0 -29
  55. package/dist/components/AnnotationCanvas/index.js +0 -96
  56. package/dist/components/AnnotationLayer/_hooks/drawEvents/rectangle.d.ts +0 -5
  57. package/dist/components/AnnotationLayer/_hooks/drawEvents/rectangle.js +0 -88
  58. package/dist/components/AnnotationLayer/_hooks/drawEvents/rectangleUtils.d.ts +0 -28
  59. package/dist/components/AnnotationLayer/_hooks/drawEvents/rectangleUtils.js +0 -204
  60. package/dist/components/AnnotationLayer/_hooks/drawEvents/useDrawEvents.d.ts +0 -25
  61. package/dist/components/AnnotationLayer/_hooks/drawEvents/useDrawEvents.js +0 -43
  62. package/dist/components/AnnotationLayer/_hooks/useCanvasDraw.d.ts +0 -13
  63. package/dist/components/AnnotationLayer/_hooks/useCanvasDraw.js +0 -115
  64. package/dist/components/AnnotationLayer/index.d.ts +0 -14
  65. package/dist/components/AnnotationLayer/index.js +0 -122
  66. package/dist/utils/canvas.d.ts +0 -3
  67. package/dist/utils/canvas.js +0 -37
  68. package/dist/utils/pointTransform.d.ts +0 -2
  69. package/dist/utils/pointTransform.js +0 -46
  70. /package/dist/{types/index.js → engine/interaction/interface.js} +0 -0
  71. /package/dist/{utils/common → engine/utils}/cloneDeep.d.ts +0 -0
  72. /package/dist/{utils/common → engine/utils}/cloneDeep.js +0 -0
  73. /package/dist/{utils/common → engine/utils}/deepEqual.d.ts +0 -0
  74. /package/dist/{utils/common → engine/utils}/deepEqual.js +0 -0
  75. /package/dist/{utils/common → engine/utils}/isEqualWith.d.ts +0 -0
  76. /package/dist/{utils/common → engine/utils}/isEqualWith.js +0 -0
  77. /package/dist/{utils → engine/utils}/mouseActions.d.ts +0 -0
  78. /package/dist/{utils → engine/utils}/mouseActions.js +0 -0
  79. /package/dist/{hooks → react/hooks}/useDebounce.d.ts +0 -0
  80. /package/dist/{hooks → react/hooks}/useDebounce.js +0 -0
  81. /package/dist/{components/AnnotationLayer/_hooks → react/hooks}/useHotkeys.d.ts +0 -0
  82. /package/dist/{components/AnnotationLayer/_hooks → react/hooks}/useHotkeys.js +0 -0
  83. /package/dist/{hooks → react/hooks}/useResizeObserver.d.ts +0 -0
  84. /package/dist/{hooks → react/hooks}/useResizeObserver.js +0 -0
@@ -1,43 +0,0 @@
1
- import { useEffect } from 'react';
2
- import * as Rectangle from './rectangle';
3
- import { DrawMode } from '../../../../enum/common';
4
- export const useDrawEvents = (params) => {
5
- const { canvasRef, setMousePoint, startMousePointRef, currentCoordinateRef, coordinatesRef, setCoordinates, maskImageRef, selectedDrawMode, editable, } = params;
6
- const initSelector = (coordinates) => {
7
- coordinates.map((coordinate) => {
8
- coordinate.selected = false;
9
- return coordinate;
10
- });
11
- return coordinates;
12
- };
13
- useEffect(() => {
14
- if (!canvasRef.current || !editable || !selectedDrawMode)
15
- return;
16
- const handlers = selectedDrawMode === DrawMode.RECTANGLE
17
- ? {
18
- down: Rectangle.handleMouseDownRectangle(params),
19
- move: Rectangle.handleMouseMoveRectangle(params),
20
- up: Rectangle.handleMouseUpRectangle(params),
21
- leave: Rectangle.handleMouseLeaveRectangle(params),
22
- }
23
- : null;
24
- if (!handlers)
25
- return;
26
- canvasRef.current.addEventListener('mousedown', handlers.down);
27
- canvasRef.current.addEventListener('mousemove', handlers.move);
28
- canvasRef.current.addEventListener('mouseup', handlers.up);
29
- canvasRef.current.addEventListener('mouseleave', handlers.leave);
30
- setMousePoint({ x: 0, y: 0, selected: false });
31
- if (coordinatesRef.current.length > 0)
32
- setCoordinates(initSelector(coordinatesRef.current));
33
- currentCoordinateRef.current = null;
34
- startMousePointRef.current = null;
35
- maskImageRef.current = new Image();
36
- return () => {
37
- canvasRef.current?.removeEventListener('mousedown', handlers.down);
38
- canvasRef.current?.removeEventListener('mousemove', handlers.move);
39
- canvasRef.current?.removeEventListener('mouseup', handlers.up);
40
- canvasRef.current?.removeEventListener('mouseleave', handlers.leave);
41
- };
42
- }, [canvasRef, selectedDrawMode]);
43
- };
@@ -1,13 +0,0 @@
1
- import { RefObject } from 'react';
2
- import { Coordinate, Point } from '../../../types';
3
- type UseCanvasDrawParams = {
4
- drawLineSize: number;
5
- drawColor?: string;
6
- };
7
- export declare function useCanvasDraw({ drawLineSize, drawColor }: UseCanvasDrawParams): {
8
- drawCross: (context: CanvasRenderingContext2D, dw: number, dh: number, zoom: number, point: Point) => void;
9
- drawActiveRect: (context: CanvasRenderingContext2D, coordinate: Coordinate, zoom: number) => void;
10
- drawRectFromPoints: (context: CanvasRenderingContext2D, startPoint: Point, endPoint: Point, zoom: number, color?: string | undefined) => void;
11
- setCursorStyle: (canvas: HTMLCanvasElement, mousePointRef: RefObject<Point>, coordinate: Coordinate, zoom: number) => void;
12
- };
13
- export {};
@@ -1,115 +0,0 @@
1
- import { ACTIVE_POINT_SIZE, getRectangleCorners } from './drawEvents/rectangleUtils';
2
- export function useCanvasDraw({ drawLineSize, drawColor }) {
3
- const drawCross = (context, dw, dh, zoom, point) => {
4
- if (point.x > 0 && point.y > 0 && point.x < dw && point.y < dh) {
5
- if (context)
6
- context.lineWidth = (drawLineSize / 2) * (1 / zoom);
7
- context?.setLineDash([5]);
8
- context?.beginPath();
9
- context?.moveTo(point.x, 0);
10
- context?.lineTo(point.x, dh);
11
- if (context)
12
- context.strokeStyle = '#001f3f';
13
- context?.stroke();
14
- context?.beginPath();
15
- context?.moveTo(0, point.y);
16
- context?.lineTo(dw, point.y);
17
- if (context)
18
- context.strokeStyle = '#001f3f';
19
- context?.stroke();
20
- }
21
- };
22
- const drawActiveRect = (context, coordinate, zoom) => {
23
- const { x, y, width, height, selected } = coordinate;
24
- if (!selected)
25
- return;
26
- context.lineWidth = drawLineSize / zoom;
27
- context.fillStyle = '#FFF';
28
- const pointSize = ACTIVE_POINT_SIZE / zoom;
29
- const points = getRectangleCorners({ x, y, width, height });
30
- points.forEach((point) => {
31
- context.fillStyle = '#FFF';
32
- context.fillRect(point.x - pointSize / 2, point.y - pointSize / 2, pointSize, pointSize);
33
- context.strokeStyle = '#000';
34
- context.lineWidth = 1 / zoom;
35
- context.strokeRect(point.x - pointSize / 2, point.y - pointSize / 2, pointSize, pointSize);
36
- });
37
- };
38
- const drawRectFromPoints = (context, startPoint, endPoint, zoom, color = drawColor) => {
39
- if (!color)
40
- return;
41
- const x = Math.min(endPoint.x, startPoint.x);
42
- const y = Math.min(endPoint.y, startPoint.y);
43
- const width = Math.abs(endPoint.x - startPoint.x);
44
- const height = Math.abs(endPoint.y - startPoint.y);
45
- context.beginPath();
46
- context.lineWidth = drawLineSize / zoom;
47
- context.rect(x, y, width, height);
48
- context.strokeStyle = color;
49
- context.stroke();
50
- };
51
- const setCursorStyle = (canvas, mousePointRef, coordinate, zoom) => {
52
- if (coordinate.selected) {
53
- let cursorStyle = 'default';
54
- const pointSize = ACTIVE_POINT_SIZE / zoom;
55
- const mouseX = mousePointRef.current.x;
56
- const mouseY = mousePointRef.current.y;
57
- const { x, y, width: w, height: h } = coordinate;
58
- if (x && y && w && h) {
59
- const points = getRectangleCorners({ x, y, width: w, height: h });
60
- if (mouseX >= points[0].x - pointSize / 2 &&
61
- mouseX <= points[0].x + pointSize / 2 &&
62
- mouseY >= points[0].y - pointSize / 2 &&
63
- mouseY <= points[0].y + pointSize / 2) {
64
- cursorStyle = 'nwse-resize';
65
- }
66
- else if (mouseX >= points[1].x - pointSize / 2 &&
67
- mouseX <= points[1].x + pointSize / 2 &&
68
- mouseY >= points[1].y - pointSize / 2 &&
69
- mouseY <= points[1].y + pointSize / 2) {
70
- cursorStyle = 'nesw-resize';
71
- }
72
- else if (mouseX >= points[2].x - pointSize / 2 &&
73
- mouseX <= points[2].x + pointSize / 2 &&
74
- mouseY >= points[2].y - pointSize / 2 &&
75
- mouseY <= points[2].y + pointSize / 2) {
76
- cursorStyle = 'nesw-resize';
77
- }
78
- else if (mouseX >= points[3].x - pointSize / 2 &&
79
- mouseX <= points[3].x + pointSize / 2 &&
80
- mouseY >= points[3].y - pointSize / 2 &&
81
- mouseY <= points[3].y + pointSize / 2) {
82
- cursorStyle = 'nwse-resize';
83
- }
84
- else if (mouseX >= x && mouseX <= x + w && mouseY >= y - pointSize / 2 && mouseY <= y + pointSize / 2) {
85
- cursorStyle = 'ns-resize';
86
- }
87
- else if (mouseX >= x &&
88
- mouseX <= x + w &&
89
- mouseY >= y + h - pointSize / 2 &&
90
- mouseY <= y + h + pointSize / 2) {
91
- cursorStyle = 'ns-resize';
92
- }
93
- else if (mouseY >= y && mouseY <= y + h && mouseX >= x - pointSize / 2 && mouseX <= x + pointSize / 2) {
94
- cursorStyle = 'ew-resize';
95
- }
96
- else if (mouseY >= y &&
97
- mouseY <= y + h &&
98
- mouseX >= x + w - pointSize / 2 &&
99
- mouseX <= x + w + pointSize / 2) {
100
- cursorStyle = 'ew-resize';
101
- }
102
- else {
103
- cursorStyle = 'default';
104
- }
105
- }
106
- canvas.style.cursor = cursorStyle;
107
- }
108
- };
109
- return {
110
- drawCross,
111
- drawActiveRect,
112
- drawRectFromPoints,
113
- setCursorStyle,
114
- };
115
- }
@@ -1,14 +0,0 @@
1
- import { Dispatch, RefObject, SetStateAction } from 'react';
2
- import { CanvasState, Coordinate, AnnotationCanvasDrawing } from '../../types';
3
- import { CoordinateHistory } from '../AnnotationCanvas/_utils/createHistory';
4
- type Props = {
5
- canvasState: CanvasState;
6
- coordinates: Coordinate[] | undefined;
7
- setCoordinates?: Dispatch<SetStateAction<Coordinate[] | undefined>>;
8
- historyRef: RefObject<CoordinateHistory | null>;
9
- drawing: AnnotationCanvasDrawing;
10
- enableHotkeys?: boolean;
11
- editable: boolean;
12
- };
13
- declare const AnnotationLayer: ({ canvasState, coordinates, setCoordinates: propSetCoordinates, historyRef, drawing, enableHotkeys, editable, }: Props) => import("react/jsx-runtime").JSX.Element;
14
- export default AnnotationLayer;
@@ -1,122 +0,0 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- import { useCallback, useEffect, useRef, useState } from 'react';
3
- import { DrawMode } from '../../enum/common';
4
- import { ignoreImageKey, isEqualWith } from '../../utils/common/isEqualWith';
5
- import { cloneDeep } from '../../utils/common/cloneDeep';
6
- import { MouseAction } from '../../utils/mouseActions';
7
- import { useDrawEvents } from './_hooks/drawEvents/useDrawEvents';
8
- import { useHotkeys } from './_hooks/useHotkeys';
9
- import { useCanvasDraw } from './_hooks/useCanvasDraw';
10
- import { drawCanvas, resolutionCanvas } from '../../utils/canvas';
11
- const INIT_POINT = { x: 0, y: 0, selected: false };
12
- const INIT_CANVAS_STATE = { moveX: 0, moveY: 0, zoomX: 0, zoomY: 0, zoom: 0, dx: 0, dy: 0, dw: 0, dh: 0 };
13
- const AnnotationLayer = ({ canvasState, coordinates = [], setCoordinates: propSetCoordinates, historyRef, drawing, enableHotkeys, editable, }) => {
14
- const { lineSize: drawLineSize, applyStyle, label: drawLabel, mode: selectedDrawMode, color: drawColor } = drawing;
15
- const canvasRef = useRef(null);
16
- const canvasStateRef = useRef(INIT_CANVAS_STATE);
17
- const animationFrameId = useRef(0);
18
- const coordinatesRef = useRef([]);
19
- const currentCoordinateRef = useRef(null);
20
- const maskImageRef = useRef(typeof window !== 'undefined' ? new window.Image() : null);
21
- const mousePointRef = useRef(INIT_POINT);
22
- const mouseActionRef = useRef(null);
23
- const startMousePointRef = useRef(null);
24
- const prevMousePointRef = useRef(null);
25
- const rectangleAnchorRef = useRef(null);
26
- const drawLabelRef = useRef(drawLabel);
27
- const [showSelectedOnly, setShowSelectedOnly] = useState(false);
28
- useEffect(() => {
29
- drawLabelRef.current = drawLabel;
30
- }, [drawLabel]);
31
- const setCoordinates = useCallback((coordinates) => {
32
- coordinatesRef.current = cloneDeep(coordinates) || [];
33
- propSetCoordinates && propSetCoordinates(coordinates);
34
- }, [propSetCoordinates]);
35
- const setMousePoint = useCallback((point) => (mousePointRef.current = point), []);
36
- const { drawCross, drawActiveRect, drawRectFromPoints, setCursorStyle } = useCanvasDraw({ drawLineSize, drawColor });
37
- useDrawEvents({
38
- canvasRef,
39
- canvasStateRef,
40
- mousePointRef,
41
- setMousePoint,
42
- mouseActionRef,
43
- startMousePointRef,
44
- prevMousePointRef,
45
- currentCoordinateRef,
46
- coordinatesRef,
47
- setCoordinates,
48
- rectangleAnchorRef,
49
- maskImageRef,
50
- drawLabelRef,
51
- historyRef,
52
- selectedDrawMode,
53
- editable,
54
- });
55
- useHotkeys({
56
- onDelete: () => {
57
- coordinatesRef.current = coordinatesRef.current.filter((coordinate) => !coordinate.selected);
58
- setCoordinates(coordinatesRef.current);
59
- historyRef.current?.add(cloneDeep(coordinatesRef.current));
60
- },
61
- toggleSelectionOnly: () => setShowSelectedOnly((prev) => !prev),
62
- onUndoRedo: (isRedo) => {
63
- const result = isRedo ? historyRef.current?.redo() : historyRef.current?.undo();
64
- if (result)
65
- setCoordinates(result);
66
- const coordinate = coordinatesRef.current.find((coordinate) => coordinate.selected);
67
- currentCoordinateRef.current = coordinate || null;
68
- },
69
- enabled: enableHotkeys,
70
- });
71
- useEffect(() => {
72
- if (!isEqualWith(coordinates, coordinatesRef.current, ignoreImageKey)) {
73
- coordinatesRef.current = cloneDeep(coordinates) || [];
74
- }
75
- }, [coordinates]);
76
- useEffect(() => {
77
- const initialize = () => {
78
- const { moveX, moveY, zoomX, zoomY, zoom, dx, dy, dw, dh } = canvasState;
79
- canvasStateRef.current = { moveX, moveY, zoomX, zoomY, zoom, dx, dy, dw, dh };
80
- };
81
- const drawNewBBox = (context) => {
82
- if (mouseActionRef.current === MouseAction.LEFT && startMousePointRef.current) {
83
- drawRectFromPoints(context, mousePointRef.current, startMousePointRef.current, canvasStateRef.current.zoom);
84
- }
85
- };
86
- const redraw = () => {
87
- const canvas = resolutionCanvas(canvasRef.current);
88
- if (!canvas)
89
- return;
90
- const { moveX, moveY, zoomX, zoomY, zoom, dx, dy, dw, dh } = canvasState;
91
- drawCanvas(moveX, moveY, zoomX, zoomY, zoom, dx, dy, canvas, undefined, true);
92
- const context = canvas.getContext('2d');
93
- if (context && coordinates) {
94
- const cloneCoordinates = cloneDeep(coordinatesRef.current);
95
- cloneCoordinates.reverse().forEach((coordinate) => {
96
- if (coordinate.type === DrawMode.RECTANGLE) {
97
- if (!showSelectedOnly || coordinate.selected) {
98
- applyStyle({ variant: 'drawRect', context, coordinate, drawLineSize, zoom });
99
- applyStyle({ variant: 'drawText', context, coordinate, drawLineSize, zoom });
100
- drawActiveRect(context, coordinate, zoom);
101
- setCursorStyle(canvas, mousePointRef, coordinate, zoom);
102
- }
103
- }
104
- });
105
- if (mousePointRef.current && editable && selectedDrawMode && !currentCoordinateRef.current) {
106
- drawNewBBox(context);
107
- drawCross(context, dw, dh, zoom, mousePointRef.current);
108
- }
109
- }
110
- animationFrameId.current = requestAnimationFrame(redraw);
111
- };
112
- initialize();
113
- redraw();
114
- window.addEventListener('resize', redraw);
115
- return () => {
116
- window.removeEventListener('resize', redraw);
117
- cancelAnimationFrame(animationFrameId.current);
118
- };
119
- }, [canvasState, coordinates, selectedDrawMode, editable]);
120
- return (_jsx("canvas", { ref: canvasRef, style: { position: 'absolute', height: '100%', width: '100%' } }));
121
- };
122
- export default AnnotationLayer;
@@ -1,3 +0,0 @@
1
- export declare const drawCanvas: (moveX: number, moveY: number, zoomX: number, zoomY: number, zoom: number, dx: number, dy: number, canvas?: HTMLCanvasElement, image?: HTMLImageElement, isClear?: boolean) => void;
2
- export declare const resolutionCanvas: (canvas: HTMLCanvasElement | null | undefined, width?: number, height?: number) => HTMLCanvasElement | undefined;
3
- export declare const clearCanvasRect: (canvas: HTMLCanvasElement | null | undefined) => void;
@@ -1,37 +0,0 @@
1
- export const drawCanvas = (moveX, moveY, zoomX, zoomY, zoom, dx, dy, canvas, image, isClear) => {
2
- if (canvas) {
3
- const context = canvas.getContext('2d');
4
- if (isClear)
5
- context?.clearRect(0, 0, canvas.width, canvas.height);
6
- const resolution_ratio_x = canvas.width / canvas.clientWidth;
7
- const resolution_ratio_y = canvas.height / canvas.clientHeight;
8
- context?.translate(dx * resolution_ratio_x, dy * resolution_ratio_y);
9
- context?.translate(moveX * resolution_ratio_x, moveY * resolution_ratio_y);
10
- context?.translate(zoomX, zoomY);
11
- context?.scale(zoom, zoom);
12
- context?.translate(-zoomX, -zoomY);
13
- try {
14
- if (image)
15
- context?.drawImage(image, 0, 0, image.width, image.height);
16
- }
17
- catch (error) {
18
- console.error('Failed to draw image', error);
19
- }
20
- }
21
- };
22
- export const resolutionCanvas = (canvas, width, height) => {
23
- if (!canvas)
24
- return undefined;
25
- const newCanvas = canvas;
26
- newCanvas.width = width || newCanvas.clientWidth;
27
- newCanvas.height = height || newCanvas.clientHeight;
28
- return newCanvas;
29
- };
30
- export const clearCanvasRect = (canvas) => {
31
- if (!canvas)
32
- return;
33
- const ctx = canvas.getContext('2d');
34
- const resolvedCanvas = resolutionCanvas(canvas);
35
- if (resolvedCanvas)
36
- ctx?.clearRect(0, 0, resolvedCanvas.width, resolvedCanvas.height);
37
- };
@@ -1,2 +0,0 @@
1
- import { Point } from '../types';
2
- export declare const getMousePointTransform: (mousePoint: Point, movePoint: Point, zoomPoint: Point, originPoint: Point, zoom: number, canvas: HTMLCanvasElement, rotate?: number) => Point;
@@ -1,46 +0,0 @@
1
- const applyOrigin = (mouse, origin) => {
2
- const x = mouse.x - origin.x;
3
- const y = mouse.y - origin.y;
4
- return {
5
- x,
6
- y,
7
- };
8
- };
9
- const applyMove = (mouse, move) => {
10
- const x = mouse.x - move.x;
11
- const y = mouse.y - move.y;
12
- return {
13
- x,
14
- y,
15
- };
16
- };
17
- const applyZoom = (mouse, zoomPoint, zoom, canvas) => {
18
- const resolution_ratio_x = canvas.width / canvas.clientWidth;
19
- const resolution_ratio_y = canvas.height / canvas.clientHeight;
20
- let x = mouse.x * (1 / zoom) * resolution_ratio_x;
21
- let y = mouse.y * (1 / zoom) * resolution_ratio_y;
22
- x = x + zoomPoint.x;
23
- y = y + zoomPoint.y;
24
- x = x - zoomPoint.x * (1 / zoom);
25
- y = y - zoomPoint.y * (1 / zoom);
26
- return {
27
- x,
28
- y,
29
- };
30
- };
31
- const applyRotate = (mouse, rotate, center) => {
32
- const rad = (rotate * Math.PI) / 180;
33
- const cos = Math.cos(rad);
34
- const sin = Math.sin(rad);
35
- const x = cos * (mouse.x - center.x) - sin * (mouse.y - center.y) + center.x;
36
- const y = sin * (mouse.x - center.x) + cos * (mouse.y - center.y) + center.y;
37
- return { x, y };
38
- };
39
- export const getMousePointTransform = (mousePoint, movePoint, zoomPoint, originPoint, zoom, canvas, rotate = 0) => {
40
- let mouse = mousePoint;
41
- mouse = applyRotate(mouse, rotate, { x: canvas.width / 2, y: canvas.height / 2 });
42
- mouse = applyOrigin(mouse, originPoint);
43
- mouse = applyMove(mouse, movePoint);
44
- mouse = applyZoom(mouse, zoomPoint, zoom, canvas);
45
- return mouse;
46
- };
File without changes
File without changes
File without changes
File without changes