@nightshadeui/canvas 2.22.0 → 3.1.0
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/CanvasController.d.ts +3 -1
- package/dist/CanvasController.d.ts.map +1 -1
- package/dist/canvas.css +1 -1
- package/dist/canvas.mjs +825 -823
- package/package.json +4 -3
- package/src/CanvasBoxSelect.ts +129 -0
- package/src/CanvasConfig.ts +24 -0
- package/src/CanvasController.ts +74 -0
- package/src/CanvasEvents.ts +14 -0
- package/src/CanvasMove.ts +62 -0
- package/src/CanvasObjectController.ts +368 -0
- package/src/CanvasObjectRegistry.ts +23 -0
- package/src/CanvasPan.ts +49 -0
- package/src/CanvasSelection.ts +104 -0
- package/src/CanvasSpace.ts +69 -0
- package/src/CanvasViewport.ts +166 -0
- package/src/CanvasZoom.ts +59 -0
- package/src/components/CanvasBoxSelect.vue +44 -0
- package/src/components/CanvasDebugCoords.vue +85 -0
- package/src/components/CanvasObject.vue +142 -0
- package/src/components/CanvasObjectResize.vue +95 -0
- package/src/components/CanvasView.vue +104 -0
- package/src/index.ts +24 -0
- package/src/types.ts +3 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { CanvasObjectController } from './CanvasObjectController.js';
|
|
2
|
+
|
|
3
|
+
export class CanvasObjectRegistry {
|
|
4
|
+
|
|
5
|
+
private objects = new Map<string, CanvasObjectController>();
|
|
6
|
+
|
|
7
|
+
register(controller: CanvasObjectController) {
|
|
8
|
+
this.objects.set(controller.objectId, controller);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
unregister(objectId: string) {
|
|
12
|
+
this.objects.delete(objectId);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
get(objectId: string) {
|
|
16
|
+
return this.objects.get(objectId) ?? null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
getAll() {
|
|
20
|
+
return [...this.objects.values()];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
}
|
package/src/CanvasPan.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { init } from '@flexent/init-decorator';
|
|
2
|
+
import { DomEventProxy } from '@nightshadeui/util/src';
|
|
3
|
+
import { dependency } from 'mesh-ioc';
|
|
4
|
+
|
|
5
|
+
import { CanvasEvents } from './CanvasEvents.js';
|
|
6
|
+
import { CanvasViewport } from './CanvasViewport.js';
|
|
7
|
+
|
|
8
|
+
export class CanvasPan {
|
|
9
|
+
|
|
10
|
+
private viewport = dependency(this, CanvasViewport);
|
|
11
|
+
private events = dependency(this, CanvasEvents);
|
|
12
|
+
|
|
13
|
+
private windowEvents = new DomEventProxy(window);
|
|
14
|
+
private isPanning = false;
|
|
15
|
+
|
|
16
|
+
@init()
|
|
17
|
+
init() {
|
|
18
|
+
this.events.unmounted.on(() => this.stopPanning());
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
onMouseDown(ev: MouseEvent) {
|
|
22
|
+
if (ev.button !== 1) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
ev.preventDefault();
|
|
26
|
+
this.isPanning = true;
|
|
27
|
+
this.windowEvents.add('mousemove', event => this.onMouseMove(event as MouseEvent));
|
|
28
|
+
this.windowEvents.add('mouseup', event => this.onMouseUp(event as MouseEvent));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
stopPanning() {
|
|
32
|
+
this.isPanning = false;
|
|
33
|
+
this.windowEvents.removeAll();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private onMouseMove(ev: MouseEvent) {
|
|
37
|
+
if (!this.isPanning) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
this.viewport.scrollBy(-ev.movementX, -ev.movementY);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private onMouseUp(ev: MouseEvent) {
|
|
44
|
+
if (ev.button === 1) {
|
|
45
|
+
this.stopPanning();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { dependency } from 'mesh-ioc';
|
|
2
|
+
|
|
3
|
+
import { CanvasEvents } from './CanvasEvents.js';
|
|
4
|
+
|
|
5
|
+
export class CanvasSelection {
|
|
6
|
+
|
|
7
|
+
selectedIds = new Set<string>();
|
|
8
|
+
|
|
9
|
+
private events = dependency(this, CanvasEvents);
|
|
10
|
+
|
|
11
|
+
get size() {
|
|
12
|
+
return this.selectedIds.size;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
getSelectedIds() {
|
|
16
|
+
return [...this.selectedIds];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
getFirst() {
|
|
20
|
+
return [...this.selectedIds][0] ?? null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
isSelected(id: string) {
|
|
24
|
+
return this.selectedIds.has(id);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
isSingleSelected(id: string) {
|
|
28
|
+
return this.selectedIds.size === 1 && this.selectedIds.has(id);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
setSelectedIds(ids: Iterable<string>) {
|
|
32
|
+
const previousIds = new Set(this.selectedIds);
|
|
33
|
+
this.selectedIds = new Set(ids);
|
|
34
|
+
this.notifySelectionChanged(previousIds);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
selectSingle(id: string) {
|
|
38
|
+
if (this.isSingleSelected(id)) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const previousIds = new Set(this.selectedIds);
|
|
42
|
+
this.selectedIds.clear();
|
|
43
|
+
this.selectedIds.add(id);
|
|
44
|
+
this.notifySelectionChanged(previousIds);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
addToSelection(...ids: string[]) {
|
|
48
|
+
const previousIds = new Set(this.selectedIds);
|
|
49
|
+
for (const id of ids) {
|
|
50
|
+
this.selectedIds.add(id);
|
|
51
|
+
}
|
|
52
|
+
this.notifySelectionChanged(previousIds);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
removeFromSelection(...ids: string[]) {
|
|
56
|
+
const previousIds = new Set(this.selectedIds);
|
|
57
|
+
for (const id of ids) {
|
|
58
|
+
this.selectedIds.delete(id);
|
|
59
|
+
}
|
|
60
|
+
this.notifySelectionChanged(previousIds);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
toggleSelection(id: string) {
|
|
64
|
+
if (this.isSelected(id)) {
|
|
65
|
+
this.removeFromSelection(id);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
this.addToSelection(id);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
deselectAll() {
|
|
72
|
+
if (this.selectedIds.size === 0) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const previousIds = new Set(this.selectedIds);
|
|
76
|
+
this.selectedIds.clear();
|
|
77
|
+
this.notifySelectionChanged(previousIds);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private notifySelectionChanged(previousIds: Set<string>) {
|
|
81
|
+
if (previousIds.size === this.selectedIds.size) {
|
|
82
|
+
let hasChanges = false;
|
|
83
|
+
for (const id of previousIds) {
|
|
84
|
+
if (!this.selectedIds.has(id)) {
|
|
85
|
+
hasChanges = true;
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (!hasChanges) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
this.events.selectionChanged.emit(this.getSelectedIds());
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
setSelected(id: string, isSelected: boolean) {
|
|
97
|
+
if (isSelected) {
|
|
98
|
+
this.addToSelection(id);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
this.removeFromSelection(id);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Box, boxCenter, Point } from '@nightshadeui/util/src';
|
|
2
|
+
|
|
3
|
+
export class CanvasSpace {
|
|
4
|
+
|
|
5
|
+
zoom = 1;
|
|
6
|
+
cellSize = 32;
|
|
7
|
+
scrollPos: Point = { x: 0, y: 0 };
|
|
8
|
+
viewportBox: Box = [{ x: 0, y: 0 }, { x: 0, y: 0 }];
|
|
9
|
+
canvasOrigin: Point = { x: 0, y: 0 };
|
|
10
|
+
canvasSize: Point = { x: 0, y: 0 };
|
|
11
|
+
|
|
12
|
+
localToCanvas(pos: Point): Point {
|
|
13
|
+
return {
|
|
14
|
+
x: pos.x * this.cellSize + this.canvasOrigin.x,
|
|
15
|
+
y: pos.y * this.cellSize + this.canvasOrigin.y,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
canvasToLocal(pos: Point): Point {
|
|
20
|
+
return {
|
|
21
|
+
x: (pos.x - this.canvasOrigin.x) / this.cellSize,
|
|
22
|
+
y: (pos.y - this.canvasOrigin.y) / this.cellSize,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
viewportToCanvas(pos: Point): Point {
|
|
27
|
+
return {
|
|
28
|
+
x: (pos.x + this.scrollPos.x) / this.zoom,
|
|
29
|
+
y: (pos.y + this.scrollPos.y) / this.zoom,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
canvasToViewport(pos: Point): Point {
|
|
34
|
+
return {
|
|
35
|
+
x: pos.x * this.zoom - this.scrollPos.x,
|
|
36
|
+
y: pos.y * this.zoom - this.scrollPos.y,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
viewportToLocal(pos: Point): Point {
|
|
41
|
+
return this.canvasToLocal(this.viewportToCanvas(pos));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
localToViewport(pos: Point): Point {
|
|
45
|
+
return this.canvasToViewport(this.localToCanvas(pos));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
pageToViewport(pos: Point): Point {
|
|
49
|
+
return {
|
|
50
|
+
x: pos.x - this.viewportBox[0].x,
|
|
51
|
+
y: pos.y - this.viewportBox[0].y,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
pageToCanvas(pos: Point): Point {
|
|
56
|
+
return this.viewportToCanvas(this.pageToViewport(pos));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
pageToLocal(pos: Point): Point {
|
|
60
|
+
return this.canvasToLocal(this.pageToCanvas(pos));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
getViewportCenterInLocal(): Point {
|
|
64
|
+
const viewportCenterInPage = boxCenter(this.viewportBox);
|
|
65
|
+
const viewportCenter = this.pageToViewport(viewportCenterInPage);
|
|
66
|
+
return this.viewportToLocal(viewportCenter);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createGridDotsSvg,
|
|
3
|
+
DomEventProxy,
|
|
4
|
+
getCssColorVar,
|
|
5
|
+
getCssVarNumber,
|
|
6
|
+
Point,
|
|
7
|
+
} from '@nightshadeui/util/src';
|
|
8
|
+
import { dependency } from 'mesh-ioc';
|
|
9
|
+
|
|
10
|
+
import { CanvasConfig } from './CanvasConfig.js';
|
|
11
|
+
import { CanvasSpace } from './CanvasSpace.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Coordinate systems:
|
|
15
|
+
* - page: MouseEvent pageX/pageY.
|
|
16
|
+
* - viewport: (0, 0) at viewport element top-left, unscaled.
|
|
17
|
+
* - canvas: (0, 0) at canvas content top-left, scaled by zoom.
|
|
18
|
+
* - local: (0, 0) at canvas origin, measured in cell units.
|
|
19
|
+
*/
|
|
20
|
+
export class CanvasViewport {
|
|
21
|
+
|
|
22
|
+
viewportEl: HTMLElement | null = null;
|
|
23
|
+
|
|
24
|
+
private windowEvents = new DomEventProxy(window);
|
|
25
|
+
private config = dependency(this, CanvasConfig);
|
|
26
|
+
private space = dependency(this, CanvasSpace);
|
|
27
|
+
|
|
28
|
+
mount(viewportEl: HTMLElement) {
|
|
29
|
+
this.viewportEl = viewportEl;
|
|
30
|
+
this.recalcCanvasMetrics();
|
|
31
|
+
this.updateViewportBox();
|
|
32
|
+
this.centerOnOrigin();
|
|
33
|
+
this.onScroll();
|
|
34
|
+
this.windowEvents.add('resize', () => this.updateViewportBox());
|
|
35
|
+
this.windowEvents.add('scroll', () => this.updateViewportBox());
|
|
36
|
+
if (this.config.autoCenter) {
|
|
37
|
+
requestAnimationFrame(() => this.centerOnOrigin());
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
destroy() {
|
|
42
|
+
this.windowEvents.removeAll();
|
|
43
|
+
this.viewportEl = null;
|
|
44
|
+
this.space.viewportBox = [{ x: 0, y: 0 }, { x: 0, y: 0 }];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
getCanvasStyle() {
|
|
48
|
+
const majorColor = getCssColorVar(this.viewportEl, '--CanvasView-grid-major-fill', 'var(--border-deep)');
|
|
49
|
+
const minorColor = getCssColorVar(this.viewportEl, '--CanvasView-grid-minor-fill', 'var(--border-base)');
|
|
50
|
+
const subdivisions = getCssVarNumber(this.viewportEl, '--CanvasView-grid-subdivisions', 4);
|
|
51
|
+
const majorRadius = getCssVarNumber(this.viewportEl, '--CanvasView-grid-major-radius', 2);
|
|
52
|
+
const minorRadius = getCssVarNumber(this.viewportEl, '--CanvasView-grid-minor-radius', 1);
|
|
53
|
+
const svg = createGridDotsSvg(
|
|
54
|
+
this.space.cellSize,
|
|
55
|
+
subdivisions,
|
|
56
|
+
majorColor,
|
|
57
|
+
majorRadius,
|
|
58
|
+
minorColor,
|
|
59
|
+
minorRadius,
|
|
60
|
+
);
|
|
61
|
+
const url = `data:image/svg+xml;base64,${window.btoa(svg)}`;
|
|
62
|
+
return {
|
|
63
|
+
background: `url(${url}) repeat`,
|
|
64
|
+
width: `${this.space.canvasSize.x}px`,
|
|
65
|
+
height: `${this.space.canvasSize.y}px`,
|
|
66
|
+
transformOrigin: '0 0',
|
|
67
|
+
transform: `scale(${this.space.zoom})`,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
getOriginStyle() {
|
|
72
|
+
return {
|
|
73
|
+
left: `${this.space.canvasOrigin.x}px`,
|
|
74
|
+
top: `${this.space.canvasOrigin.y}px`,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
getSizerStyle() {
|
|
79
|
+
// The scaled scroll area keeps viewport scrollbars synced with transform: scale.
|
|
80
|
+
return {
|
|
81
|
+
width: `${this.space.canvasSize.x * this.space.zoom}px`,
|
|
82
|
+
height: `${this.space.canvasSize.y * this.space.zoom}px`,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
onScroll() {
|
|
87
|
+
if (!this.viewportEl) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
this.space.scrollPos = {
|
|
91
|
+
x: this.viewportEl.scrollLeft,
|
|
92
|
+
y: this.viewportEl.scrollTop,
|
|
93
|
+
};
|
|
94
|
+
// this.updateViewportBox();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
centerOnOrigin() {
|
|
98
|
+
if (!this.viewportEl) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const viewportSize = this.getViewportSize();
|
|
102
|
+
this.setViewportScroll({
|
|
103
|
+
x: this.space.canvasOrigin.x * this.space.zoom - viewportSize.x * 0.5,
|
|
104
|
+
y: this.space.canvasOrigin.y * this.space.zoom - viewportSize.y * 0.5,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
scrollBy(dx: number, dy: number) {
|
|
109
|
+
if (!this.viewportEl) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
this.setViewportScroll({
|
|
113
|
+
x: this.viewportEl.scrollLeft + dx,
|
|
114
|
+
y: this.viewportEl.scrollTop + dy,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
scrollToLocalPoint(pos: Point) {
|
|
119
|
+
const viewportSize = this.getViewportSize();
|
|
120
|
+
const canvasPos = this.space.localToCanvas(pos);
|
|
121
|
+
this.setViewportScroll({
|
|
122
|
+
x: canvasPos.x * this.space.zoom - viewportSize.x * 0.5,
|
|
123
|
+
y: canvasPos.y * this.space.zoom - viewportSize.y * 0.5,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private setViewportScroll(pos: Point) {
|
|
128
|
+
if (!this.viewportEl) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
this.viewportEl.scrollTo({
|
|
132
|
+
left: Math.max(0, pos.x),
|
|
133
|
+
top: Math.max(0, pos.y),
|
|
134
|
+
behavior: 'auto',
|
|
135
|
+
});
|
|
136
|
+
this.onScroll();
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private updateViewportBox() {
|
|
140
|
+
if (!this.viewportEl) {
|
|
141
|
+
this.space.viewportBox = [{ x: 0, y: 0 }, { x: 0, y: 0 }];
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const rect = this.viewportEl.getBoundingClientRect();
|
|
145
|
+
this.space.viewportBox = [
|
|
146
|
+
{ x: rect.left, y: rect.top },
|
|
147
|
+
{ x: rect.right, y: rect.bottom },
|
|
148
|
+
];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private getViewportSize() {
|
|
152
|
+
return {
|
|
153
|
+
x: this.space.viewportBox[1].x - this.space.viewportBox[0].x,
|
|
154
|
+
y: this.space.viewportBox[1].y - this.space.viewportBox[0].y,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private recalcCanvasMetrics() {
|
|
159
|
+
const marginCells = getCssVarNumber(this.viewportEl, '--CanvasView-margin-cells', 64);
|
|
160
|
+
this.space.cellSize = getCssVarNumber(this.viewportEl, '--CanvasView-grid-cell-size', 32);
|
|
161
|
+
const margin = marginCells * this.space.cellSize;
|
|
162
|
+
this.space.canvasSize = { x: margin * 2, y: margin * 2 };
|
|
163
|
+
this.space.canvasOrigin = { x: margin, y: margin };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { clamp } from '@nightshadeui/util/src';
|
|
2
|
+
import { dependency } from 'mesh-ioc';
|
|
3
|
+
|
|
4
|
+
import { CanvasSpace } from './CanvasSpace.js';
|
|
5
|
+
import { CanvasViewport } from './CanvasViewport.js';
|
|
6
|
+
|
|
7
|
+
const ZOOM_STEPS = [0.33, 0.5, 0.6, 0.75, 0.85, 1, 1.15, 1.25, 1.5, 1.75, 2];
|
|
8
|
+
const ZOOM_MIN = Math.min(...ZOOM_STEPS);
|
|
9
|
+
const ZOOM_MAX = Math.max(...ZOOM_STEPS);
|
|
10
|
+
|
|
11
|
+
export class CanvasZoom {
|
|
12
|
+
|
|
13
|
+
viewport = dependency(this, CanvasViewport);
|
|
14
|
+
space = dependency(this, CanvasSpace);
|
|
15
|
+
|
|
16
|
+
onWheel(ev: WheelEvent) {
|
|
17
|
+
if (!ev.ctrlKey) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
ev.preventDefault();
|
|
21
|
+
const delta = clamp(ev.deltaY, -40, 40) / 400;
|
|
22
|
+
const zoomFactor = Math.exp(-delta);
|
|
23
|
+
this.setZoom(this.space.zoom * zoomFactor);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
setZoom(newZoom: number) {
|
|
27
|
+
const viewCenter = this.space.getViewportCenterInLocal();
|
|
28
|
+
this.space.zoom = clamp(newZoom, ZOOM_MIN, ZOOM_MAX);
|
|
29
|
+
this.viewport.scrollToLocalPoint(viewCenter);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
zoomIn() {
|
|
33
|
+
const index = this.getClosestZoomStepIndex();
|
|
34
|
+
if (index < ZOOM_STEPS.length - 1) {
|
|
35
|
+
this.setZoom(ZOOM_STEPS[index + 1]);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
zoomOut() {
|
|
40
|
+
const index = this.getClosestZoomStepIndex();
|
|
41
|
+
if (index > 0) {
|
|
42
|
+
this.setZoom(ZOOM_STEPS[index - 1]);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private getClosestZoomStepIndex() {
|
|
47
|
+
let closestIndex = 0;
|
|
48
|
+
let minDelta = Number.POSITIVE_INFINITY;
|
|
49
|
+
for (let index = 0; index < ZOOM_STEPS.length; index++) {
|
|
50
|
+
const delta = Math.abs(ZOOM_STEPS[index] - this.space.zoom);
|
|
51
|
+
if (delta < minDelta) {
|
|
52
|
+
minDelta = delta;
|
|
53
|
+
closestIndex = index;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return closestIndex;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div
|
|
3
|
+
v-if="canvas.boxSelect.isSelecting()"
|
|
4
|
+
class="CanvasBoxSelect tone-primary">
|
|
5
|
+
<div
|
|
6
|
+
class="Box"
|
|
7
|
+
:style="canvas.boxSelect.getSelectionStyle()" />
|
|
8
|
+
</div>
|
|
9
|
+
</template>
|
|
10
|
+
|
|
11
|
+
<script>
|
|
12
|
+
export default {
|
|
13
|
+
|
|
14
|
+
props: {
|
|
15
|
+
canvas: { type: Object, required: true },
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
};
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
<style scoped>
|
|
22
|
+
.CanvasBoxSelect {
|
|
23
|
+
--CanvasBoxSelect-fill: var(--tone-fill);
|
|
24
|
+
--CanvasBoxSelect-opacity: 0.1;
|
|
25
|
+
|
|
26
|
+
position: absolute;
|
|
27
|
+
inset: 0;
|
|
28
|
+
z-index: 20;
|
|
29
|
+
pointer-events: none;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
.Box {
|
|
33
|
+
position: absolute;
|
|
34
|
+
border: 1px solid var(--CanvasBoxSelect-fill);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.Box::before {
|
|
38
|
+
content: '';
|
|
39
|
+
position: absolute;
|
|
40
|
+
inset: 0;
|
|
41
|
+
background: var(--CanvasBoxSelect-fill);
|
|
42
|
+
opacity: var(--CanvasBoxSelect-opacity);
|
|
43
|
+
}
|
|
44
|
+
</style>
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<VGroup
|
|
3
|
+
class="CanvasDebugCoords"
|
|
4
|
+
gap="s">
|
|
5
|
+
<HGroup class="Row">
|
|
6
|
+
<strong>page</strong>
|
|
7
|
+
<span>{{ format(pagePos) }}</span>
|
|
8
|
+
</HGroup>
|
|
9
|
+
<HGroup class="Row">
|
|
10
|
+
<strong>viewport</strong>
|
|
11
|
+
<span>{{ format(viewportPos) }}</span>
|
|
12
|
+
</HGroup>
|
|
13
|
+
<HGroup class="Row">
|
|
14
|
+
<strong>canvas</strong>
|
|
15
|
+
<span>{{ format(canvasPos) }}</span>
|
|
16
|
+
</HGroup>
|
|
17
|
+
<HGroup class="Row">
|
|
18
|
+
<strong>local</strong>
|
|
19
|
+
<span>{{ format(localPos) }}</span>
|
|
20
|
+
</HGroup>
|
|
21
|
+
<HGroup class="Row">
|
|
22
|
+
<strong>zoom</strong>
|
|
23
|
+
<span>{{ canvas.space.zoom.toFixed(2) }}</span>
|
|
24
|
+
</HGroup>
|
|
25
|
+
</VGroup>
|
|
26
|
+
</template>
|
|
27
|
+
|
|
28
|
+
<script>
|
|
29
|
+
export default {
|
|
30
|
+
|
|
31
|
+
props: {
|
|
32
|
+
canvas: { type: Object, required: true },
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
computed: {
|
|
36
|
+
|
|
37
|
+
pagePos() {
|
|
38
|
+
return this.canvas.inputStateManager.lastMousePos;
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
viewportPos() {
|
|
42
|
+
return this.canvas.space.pageToViewport(this.pagePos);
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
canvasPos() {
|
|
46
|
+
return this.canvas.space.pageToCanvas(this.pagePos);
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
localPos() {
|
|
50
|
+
return this.canvas.space.pageToLocal(this.pagePos);
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
methods: {
|
|
56
|
+
format(pos) {
|
|
57
|
+
return `(${pos.x.toFixed(2)}, ${pos.y.toFixed(2)})`;
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
};
|
|
62
|
+
</script>
|
|
63
|
+
|
|
64
|
+
<style scoped>
|
|
65
|
+
.CanvasDebugCoords {
|
|
66
|
+
position: absolute;
|
|
67
|
+
z-index: 10;
|
|
68
|
+
top: 0;
|
|
69
|
+
left: 0;
|
|
70
|
+
margin: var(--sp);
|
|
71
|
+
padding: var(--sp);
|
|
72
|
+
pointer-events: none;
|
|
73
|
+
|
|
74
|
+
font-family: var(--font-monospace);
|
|
75
|
+
font-size: var(--font-size-s);
|
|
76
|
+
border: var(--input-border-size) solid var(--border-base);
|
|
77
|
+
border-radius: var(--radius-m);
|
|
78
|
+
|
|
79
|
+
background: var(--surface-base);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
.Row {
|
|
83
|
+
white-space: nowrap;
|
|
84
|
+
}
|
|
85
|
+
</style>
|