-
Notifications
You must be signed in to change notification settings - Fork 914
/
Copy pathindex.tsx
63 lines (52 loc) · 1.56 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { Editor } from "~/Editor";
import { Box } from "~/Geometry";
import { Center } from "./Center";
import { Hand } from "./Hand";
import { Reset } from "./Reset";
import { Shortcuts } from "./Shortcut";
import { Zoom } from "./Zoom";
export type Camera = Box;
export declare namespace Camera {
export { Center, Hand, Reset, Shortcuts, Zoom };
}
export namespace Camera {
Camera.Center = Center;
Camera.Hand = Hand;
Camera.Reset = Reset;
Camera.Shortcuts = Shortcuts;
Camera.Zoom = Zoom;
export const useSet = () => {
const padding = 25;
const canvas = Editor.Canvas.use();
return useCallback(
(boxWithoutPadding: Box) => {
if (!canvas?.current) return;
const box = {
x: boxWithoutPadding.x - padding,
y: boxWithoutPadding.y - padding,
width: boxWithoutPadding.width + padding * 2,
height: boxWithoutPadding.height + padding * 2,
};
const scale = Math.min(
canvas.current.width() / box.width,
canvas.current.height() / box.height
);
canvas.current.scale({ x: scale, y: scale });
canvas.current.position({
x:
-box.x * canvas.current.scaleX() +
canvas.current.width() / 2 -
(box.width * canvas.current.scaleX()) / 2,
y:
-box.y * canvas.current.scaleY() +
canvas.current.height() / 2 -
(box.height * canvas.current.scaleY()) / 2,
});
},
[canvas, padding]
);
};
export function Tools() {
return <Hand.Tool />;
}
}