@asyra/render 0.2.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/.turbo/turbo-test$colon$local.log +45 -0
- package/README.md +1 -0
- package/dist/__tests__/render.test.d.ts +2 -0
- package/dist/__tests__/render.test.d.ts.map +1 -0
- package/dist/__tests__/render.test.js +148 -0
- package/dist/__tests__/render.test.js.map +1 -0
- package/dist/handlers/element-selection.d.ts +4 -0
- package/dist/handlers/element-selection.d.ts.map +1 -0
- package/dist/handlers/element-selection.js +7 -0
- package/dist/handlers/element-selection.js.map +1 -0
- package/dist/handlers/index.d.ts +2 -0
- package/dist/handlers/index.d.ts.map +1 -0
- package/dist/handlers/index.js +2 -0
- package/dist/handlers/index.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/render-layer/element-interaction-handler.d.ts +8 -0
- package/dist/render-layer/element-interaction-handler.d.ts.map +1 -0
- package/dist/render-layer/element-interaction-handler.js +20 -0
- package/dist/render-layer/element-interaction-handler.js.map +1 -0
- package/dist/render-layer/index.d.ts +2 -0
- package/dist/render-layer/index.d.ts.map +1 -0
- package/dist/render-layer/index.js +2 -0
- package/dist/render-layer/index.js.map +1 -0
- package/dist/render-layer/render-layer.d.ts +41 -0
- package/dist/render-layer/render-layer.d.ts.map +1 -0
- package/dist/render-layer/render-layer.js +189 -0
- package/dist/render-layer/render-layer.js.map +1 -0
- package/dist/render.d.ts +53 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/render.js +116 -0
- package/dist/render.js.map +1 -0
- package/dist/selection-layer/index.d.ts +3 -0
- package/dist/selection-layer/index.d.ts.map +1 -0
- package/dist/selection-layer/index.js +3 -0
- package/dist/selection-layer/index.js.map +1 -0
- package/dist/selection-layer/selection-layer.d.ts +29 -0
- package/dist/selection-layer/selection-layer.d.ts.map +1 -0
- package/dist/selection-layer/selection-layer.js +63 -0
- package/dist/selection-layer/selection-layer.js.map +1 -0
- package/dist/selection-layer/utils.d.ts +13 -0
- package/dist/selection-layer/utils.d.ts.map +1 -0
- package/dist/selection-layer/utils.js +27 -0
- package/dist/selection-layer/utils.js.map +1 -0
- package/dist/stores/scene-tree.d.ts +16 -0
- package/dist/stores/scene-tree.d.ts.map +1 -0
- package/dist/stores/scene-tree.js +57 -0
- package/dist/stores/scene-tree.js.map +1 -0
- package/dist/stores/selection.d.ts +13 -0
- package/dist/stores/selection.d.ts.map +1 -0
- package/dist/stores/selection.js +31 -0
- package/dist/stores/selection.js.map +1 -0
- package/dist/subscribes/index.d.ts +2 -0
- package/dist/subscribes/index.d.ts.map +1 -0
- package/dist/subscribes/index.js +11 -0
- package/dist/subscribes/index.js.map +1 -0
- package/dist/subscribes/scene-tree.d.ts +4 -0
- package/dist/subscribes/scene-tree.d.ts.map +1 -0
- package/dist/subscribes/scene-tree.js +56 -0
- package/dist/subscribes/scene-tree.js.map +1 -0
- package/dist/subscribes/selection.d.ts +3 -0
- package/dist/subscribes/selection.d.ts.map +1 -0
- package/dist/subscribes/selection.js +31 -0
- package/dist/subscribes/selection.js.map +1 -0
- package/dist/subscribes/system.d.ts +2 -0
- package/dist/subscribes/system.d.ts.map +1 -0
- package/dist/subscribes/system.js +15 -0
- package/dist/subscribes/system.js.map +1 -0
- package/dist/subscribes/viewport.d.ts +2 -0
- package/dist/subscribes/viewport.d.ts.map +1 -0
- package/dist/subscribes/viewport.js +36 -0
- package/dist/subscribes/viewport.js.map +1 -0
- package/dist/types.d.ts +11 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/viewport-layer/index.d.ts +4 -0
- package/dist/viewport-layer/index.d.ts.map +1 -0
- package/dist/viewport-layer/index.js +4 -0
- package/dist/viewport-layer/index.js.map +1 -0
- package/dist/viewport-layer/types.d.ts +17 -0
- package/dist/viewport-layer/types.d.ts.map +1 -0
- package/dist/viewport-layer/types.js +2 -0
- package/dist/viewport-layer/types.js.map +1 -0
- package/dist/viewport-layer/utils.d.ts +8 -0
- package/dist/viewport-layer/utils.d.ts.map +1 -0
- package/dist/viewport-layer/utils.js +49 -0
- package/dist/viewport-layer/utils.js.map +1 -0
- package/dist/viewport-layer/viewport-layer.d.ts +59 -0
- package/dist/viewport-layer/viewport-layer.d.ts.map +1 -0
- package/dist/viewport-layer/viewport-layer.js +125 -0
- package/dist/viewport-layer/viewport-layer.js.map +1 -0
- package/package.json +32 -0
- package/src/__tests__/render.test.ts +209 -0
- package/src/handlers/element-selection.ts +7 -0
- package/src/handlers/index.ts +1 -0
- package/src/index.ts +7 -0
- package/src/render-layer/element-interaction-handler.ts +31 -0
- package/src/render-layer/index.ts +1 -0
- package/src/render-layer/render-layer.ts +238 -0
- package/src/render.ts +161 -0
- package/src/selection-layer/index.ts +2 -0
- package/src/selection-layer/selection-layer.ts +90 -0
- package/src/selection-layer/utils.ts +37 -0
- package/src/stores/scene-tree.ts +79 -0
- package/src/stores/selection.ts +40 -0
- package/src/subscribes/index.ts +11 -0
- package/src/subscribes/scene-tree.ts +72 -0
- package/src/subscribes/selection.ts +40 -0
- package/src/subscribes/system.ts +18 -0
- package/src/subscribes/viewport.ts +54 -0
- package/src/types.ts +15 -0
- package/src/viewport-layer/index.ts +3 -0
- package/src/viewport-layer/types.ts +18 -0
- package/src/viewport-layer/utils.ts +55 -0
- package/src/viewport-layer/viewport-layer.ts +172 -0
- package/test-global.d.ts +11 -0
- package/test-setup.ts +28 -0
- package/tsconfig.json +22 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SceneTreeYjsChange,
|
|
3
|
+
AddRemoveElementChange,
|
|
4
|
+
UpdateElementChange
|
|
5
|
+
} from '@asyra/utils'
|
|
6
|
+
import factory from '@asyra/factory'
|
|
7
|
+
import { SCENE_TREE_ACTIONS } from '@asyra/utils'
|
|
8
|
+
import { subscribeToSceneTreeLoadComplete } from '@asyra/reactive-events'
|
|
9
|
+
import renderSceneTree from '../stores/scene-tree'
|
|
10
|
+
|
|
11
|
+
const updateRenderSceneTree = (change: SceneTreeYjsChange['payload']) => {
|
|
12
|
+
switch (change.action) {
|
|
13
|
+
case SCENE_TREE_ACTIONS.ADD_ELEMENT: {
|
|
14
|
+
renderSceneTree.addElementById((change as AddRemoveElementChange).data.id)
|
|
15
|
+
break
|
|
16
|
+
}
|
|
17
|
+
case SCENE_TREE_ACTIONS.REMOVE_ELEMENT: {
|
|
18
|
+
const { parentId, data } = change as AddRemoveElementChange
|
|
19
|
+
renderSceneTree.removeElement(data, parentId)
|
|
20
|
+
break
|
|
21
|
+
}
|
|
22
|
+
case SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA: {
|
|
23
|
+
const { id, key, before, after } = change as UpdateElementChange
|
|
24
|
+
renderSceneTree.updateElement(id, key, before, after)
|
|
25
|
+
break
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// @ts-expect-error: It's YJS event
|
|
31
|
+
export const handleSceneTreeChange = (event) => {
|
|
32
|
+
const processChanges = (
|
|
33
|
+
items: typeof event.changes.added | typeof event.changes.deleted
|
|
34
|
+
) => {
|
|
35
|
+
// @ts-expect-error: It's YJS event
|
|
36
|
+
items.forEach((item) => {
|
|
37
|
+
// @ts-expect-error: It's YJS event
|
|
38
|
+
item.content.getContent().forEach((change) => {
|
|
39
|
+
updateRenderSceneTree(change)
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
processChanges(event.changes.added)
|
|
45
|
+
processChanges(event.changes.deleted)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let hasInit = false
|
|
49
|
+
|
|
50
|
+
let sceneTreeLoadCompleteSubscription = {
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
52
|
+
unsubscribe: () => {}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const initSceneTreeDataContext = () => {
|
|
56
|
+
if (hasInit) {
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
sceneTreeLoadCompleteSubscription = subscribeToSceneTreeLoadComplete(() => {
|
|
61
|
+
renderSceneTree.reload()
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
const sceneTreeArray = factory.sceneTreeMap
|
|
65
|
+
sceneTreeArray.observe(handleSceneTreeChange)
|
|
66
|
+
|
|
67
|
+
hasInit = true
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export const renderSceneTreeClear = () => {
|
|
71
|
+
sceneTreeLoadCompleteSubscription.unsubscribe()
|
|
72
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import factory from '@asyra/factory'
|
|
2
|
+
import {
|
|
3
|
+
SELECTION_ACTIONS,
|
|
4
|
+
SELECTION_TYPES,
|
|
5
|
+
SelectionYjsChange
|
|
6
|
+
} from '@asyra/utils'
|
|
7
|
+
import renderSelection from '../stores/selection'
|
|
8
|
+
|
|
9
|
+
const updateUIElementSelection = (change: SelectionYjsChange['payload']) => {
|
|
10
|
+
switch (change.action) {
|
|
11
|
+
case SELECTION_ACTIONS.SELECT_ELEMENTS:
|
|
12
|
+
case SELECTION_ACTIONS.DESELECT_ELEMENTS:
|
|
13
|
+
renderSelection.updateSelection(SELECTION_TYPES.ELEMENT)
|
|
14
|
+
break
|
|
15
|
+
case SELECTION_ACTIONS.SELECT_VERTICES:
|
|
16
|
+
case SELECTION_ACTIONS.DESELECT_VERTICES:
|
|
17
|
+
renderSelection.updateSelection(SELECTION_TYPES.VERTEX)
|
|
18
|
+
break
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// @ts-expect-error: It's YJS event
|
|
23
|
+
export const collectElementSelectionChange = (event) => {
|
|
24
|
+
const processChanges = (
|
|
25
|
+
items: typeof event.changes.added | typeof event.changes.deleted
|
|
26
|
+
) => {
|
|
27
|
+
// @ts-expect-error: It's YJS event
|
|
28
|
+
items.forEach((item) => {
|
|
29
|
+
item.content.getContent().forEach(updateUIElementSelection)
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
processChanges(event.changes.added)
|
|
34
|
+
processChanges(event.changes.deleted)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const initSelectionContext = () => {
|
|
38
|
+
const elementSelectionArray = factory.elementSelectionMap
|
|
39
|
+
elementSelectionArray.observe(collectElementSelectionChange)
|
|
40
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { emitInitRender, subscribeToInitRender } from '@asyra/reactive-events'
|
|
2
|
+
import render from '../render'
|
|
3
|
+
|
|
4
|
+
let hasInit = false
|
|
5
|
+
|
|
6
|
+
export const initSystemContext = () => {
|
|
7
|
+
if (hasInit) {
|
|
8
|
+
return
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
subscribeToInitRender(async ({ payload }) => {
|
|
12
|
+
const { requestId, width, height, color } = payload
|
|
13
|
+
const newApp = await render.init(width, height, color)
|
|
14
|
+
emitInitRender(requestId, newApp)
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
hasInit = true
|
|
18
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {
|
|
2
|
+
finishRequestRenderZoom,
|
|
3
|
+
finishRequestViewportPosition,
|
|
4
|
+
finishRequestViewportScale,
|
|
5
|
+
subscribeToPanTo,
|
|
6
|
+
subscribeToRequestRenderZoom,
|
|
7
|
+
subscribeToRequestViewportPosition,
|
|
8
|
+
subscribeToRequestViewportScale,
|
|
9
|
+
subscribeToZoomFit,
|
|
10
|
+
subscribeToZoomToCenter
|
|
11
|
+
} from '@asyra/reactive-events'
|
|
12
|
+
import render from '../render'
|
|
13
|
+
|
|
14
|
+
let hasInit = false
|
|
15
|
+
|
|
16
|
+
export const initViewportContext = () => {
|
|
17
|
+
if (hasInit) {
|
|
18
|
+
return
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
subscribeToRequestRenderZoom(({ payload }) => {
|
|
22
|
+
const zoom = render.getViewportScale()
|
|
23
|
+
finishRequestRenderZoom(payload.requestId, zoom)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
subscribeToZoomFit(({ payload }) => {
|
|
27
|
+
render.zoomFit(payload.rect)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
subscribeToPanTo(({ payload }) => {
|
|
31
|
+
const { x, y } = payload
|
|
32
|
+
render.panTo(x, y)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
subscribeToZoomToCenter(({ payload }) => {
|
|
36
|
+
const { scale, centerX, centerY } = payload
|
|
37
|
+
render.zoomToCenter(scale, centerX, centerY)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
subscribeToRequestViewportPosition(({ payload }) => {
|
|
41
|
+
const viewportPosition = render.getViewportPosition()
|
|
42
|
+
finishRequestViewportPosition(payload.requestId, {
|
|
43
|
+
x: viewportPosition.x,
|
|
44
|
+
y: viewportPosition.y
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
subscribeToRequestViewportScale(({ payload }) => {
|
|
49
|
+
const viewportScale = render.getViewportScale()
|
|
50
|
+
finishRequestViewportScale(payload.requestId, viewportScale)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
hasInit = true
|
|
54
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Container, Graphics } from 'pixi.js'
|
|
2
|
+
import { ComputedAttrs, ElementRawData } from '@asyra/utils'
|
|
3
|
+
|
|
4
|
+
export type RenderElementData = Omit<ElementRawData, 'props'> & ComputedAttrs
|
|
5
|
+
|
|
6
|
+
export type RenderElementsData = Record<string, RenderElementData>
|
|
7
|
+
|
|
8
|
+
export interface RenderContainerData {
|
|
9
|
+
label: string
|
|
10
|
+
x: number
|
|
11
|
+
y: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// The type of elements that can be selected
|
|
15
|
+
export type SceneElement = Container | Graphics
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Vec2, Rect, Bounds } from './types'
|
|
2
|
+
|
|
3
|
+
export function rectToBounds(rect: Rect): Bounds {
|
|
4
|
+
return {
|
|
5
|
+
minX: rect.x,
|
|
6
|
+
minY: rect.y,
|
|
7
|
+
maxX: rect.x + rect.width,
|
|
8
|
+
maxY: rect.y + rect.height
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function boundsToRect(bounds: Bounds): Rect {
|
|
13
|
+
return {
|
|
14
|
+
x: bounds.minX,
|
|
15
|
+
y: bounds.minY,
|
|
16
|
+
width: bounds.maxX - bounds.minX,
|
|
17
|
+
height: bounds.maxY - bounds.minY
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function getCenter(rect: Rect): Vec2 {
|
|
22
|
+
return {
|
|
23
|
+
x: rect.x + rect.width / 2,
|
|
24
|
+
y: rect.y + rect.height / 2
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function unionBounds(boundsList: Bounds[]): Bounds | undefined {
|
|
29
|
+
if (boundsList.length === 0) return undefined
|
|
30
|
+
|
|
31
|
+
return boundsList.reduce((acc, bounds) => ({
|
|
32
|
+
minX: Math.min(acc.minX, bounds.minX),
|
|
33
|
+
minY: Math.min(acc.minY, bounds.minY),
|
|
34
|
+
maxX: Math.max(acc.maxX, bounds.maxX),
|
|
35
|
+
maxY: Math.max(acc.maxY, bounds.maxY)
|
|
36
|
+
}))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function scaleRect(rect: Rect, scale: number): Rect {
|
|
40
|
+
return {
|
|
41
|
+
x: rect.x * scale,
|
|
42
|
+
y: rect.y * scale,
|
|
43
|
+
width: rect.width * scale,
|
|
44
|
+
height: rect.height * scale
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function translateRect(rect: Rect, offset: Vec2): Rect {
|
|
49
|
+
return {
|
|
50
|
+
x: rect.x + offset.x,
|
|
51
|
+
y: rect.y + offset.y,
|
|
52
|
+
width: rect.width,
|
|
53
|
+
height: rect.height
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { Container, Graphics } from 'pixi.js'
|
|
2
|
+
import { DataTypes, DEFAULT_CANVAS_PADDING, MouseData } from '@asyra/utils'
|
|
3
|
+
import { Bounds } from './types'
|
|
4
|
+
import { RenderContainerData, RenderElementData, SceneElement } from '../types'
|
|
5
|
+
import { RenderLayer } from '../render-layer'
|
|
6
|
+
import { rectToBounds } from './utils'
|
|
7
|
+
|
|
8
|
+
export class ViewportLayer {
|
|
9
|
+
layer: Container
|
|
10
|
+
private renderLayer: RenderLayer
|
|
11
|
+
|
|
12
|
+
constructor() {
|
|
13
|
+
this.layer = new Container()
|
|
14
|
+
this.renderLayer = new RenderLayer()
|
|
15
|
+
|
|
16
|
+
this.layer.addChild(this.renderLayer.view)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get view() {
|
|
20
|
+
return this.layer
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
getElementById(elementId: string): SceneElement | undefined {
|
|
24
|
+
return this.renderLayer.getElementById(elementId)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
switchWorkspace(workspaceData: RenderContainerData) {
|
|
28
|
+
this.renderLayer.switchWorkspace(workspaceData)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
addContainer(containerData: RenderContainerData) {
|
|
32
|
+
return this.renderLayer.addContainer(containerData)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
addElement(data: RenderElementData) {
|
|
36
|
+
return this.renderLayer.addElement(data)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
removeElement(elementId: string, parentId?: string) {
|
|
40
|
+
return this.renderLayer.removeElement(elementId, parentId)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
updateElement(
|
|
44
|
+
elementId: string,
|
|
45
|
+
key: string,
|
|
46
|
+
before: DataTypes,
|
|
47
|
+
after: DataTypes
|
|
48
|
+
) {
|
|
49
|
+
this.renderLayer.updateElement(elementId, key, before, after)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
updateElementProperties(
|
|
53
|
+
element: Container | Graphics,
|
|
54
|
+
key: string,
|
|
55
|
+
after: DataTypes
|
|
56
|
+
) {
|
|
57
|
+
this.renderLayer.updateElementProperties(element, key, after)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
zoomFit(uiBounds: DOMRect) {
|
|
61
|
+
if (this.renderLayer.getAllElements().size) {
|
|
62
|
+
const elementsBounds = this.renderLayer.getAllElementsBounds(
|
|
63
|
+
this.renderLayer.view
|
|
64
|
+
)
|
|
65
|
+
this.fitBounds(elementsBounds, rectToBounds(uiBounds))
|
|
66
|
+
} else {
|
|
67
|
+
this.panTo(
|
|
68
|
+
uiBounds.x + DEFAULT_CANVAS_PADDING,
|
|
69
|
+
uiBounds.y + DEFAULT_CANVAS_PADDING
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Set the canvas zoom level centered on a specific point
|
|
76
|
+
* @param scale - The zoom scale factor
|
|
77
|
+
* @param centerX - The x-coordinate of the zoom center
|
|
78
|
+
* @param centerY - The y-coordinate of the zoom center
|
|
79
|
+
* @returns void
|
|
80
|
+
*/
|
|
81
|
+
zoomToCenter(scale: number, centerX: number, centerY: number) {
|
|
82
|
+
const currentScale = this.getScale()
|
|
83
|
+
const currentPosition = this.getPosition()
|
|
84
|
+
|
|
85
|
+
// Calculate the position of the mouse in world coordinates
|
|
86
|
+
const worldX = (centerX - currentPosition.x) / currentScale
|
|
87
|
+
const worldY = (centerY - currentPosition.y) / currentScale
|
|
88
|
+
|
|
89
|
+
// Calculate the new position after zoom
|
|
90
|
+
const newX = centerX - worldX * scale
|
|
91
|
+
const newY = centerY - worldY * scale
|
|
92
|
+
|
|
93
|
+
// Apply the new scale and position
|
|
94
|
+
this.zoomTo(scale)
|
|
95
|
+
this.panTo(newX, newY)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Move the canvas to the specified position
|
|
100
|
+
* @param x - The x-coordinate to move the canvas to
|
|
101
|
+
* @param y - The y-coordinate to move the canvas to
|
|
102
|
+
* @returns void
|
|
103
|
+
*/
|
|
104
|
+
panTo(x: number, y: number) {
|
|
105
|
+
this.layer.position.set(x, y)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Set the canvas zoom level
|
|
110
|
+
* @param scale - The zoom scale factor. A value of 1.0 represents 100% zoom.
|
|
111
|
+
* Values greater than 1.0 zoom in, values less than 1.0 zoom out.
|
|
112
|
+
* @returns void
|
|
113
|
+
*/
|
|
114
|
+
zoomTo(scale: number) {
|
|
115
|
+
this.layer.scale.set(scale)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Automatically fits all elements into the visible canvas area,
|
|
120
|
+
* scaling and positioning them proportionally while maintaining aspect ratio.
|
|
121
|
+
*
|
|
122
|
+
* @param elementsBounds - The global bounding box of all elements
|
|
123
|
+
* @param uiBounds - The visible UI canvas bounds
|
|
124
|
+
* @param padding - The desired padding between elements and the canvas edges
|
|
125
|
+
*/
|
|
126
|
+
fitBounds(
|
|
127
|
+
elementsBounds: Bounds,
|
|
128
|
+
uiBounds: Bounds,
|
|
129
|
+
padding = DEFAULT_CANVAS_PADDING
|
|
130
|
+
) {
|
|
131
|
+
// The available inner canvas area (excluding padding)
|
|
132
|
+
const availableWidth = uiBounds.maxX - uiBounds.minX - padding * 2
|
|
133
|
+
const availableHeight = uiBounds.maxY - uiBounds.minY - padding * 2
|
|
134
|
+
|
|
135
|
+
const contentWidth = elementsBounds.maxX - elementsBounds.minX
|
|
136
|
+
const contentHeight = elementsBounds.maxY - elementsBounds.minY
|
|
137
|
+
|
|
138
|
+
// Calculate proportional zoom ratio
|
|
139
|
+
const scaleX = availableWidth / contentWidth
|
|
140
|
+
const scaleY = availableHeight / contentHeight
|
|
141
|
+
|
|
142
|
+
const newZoom = Math.min(scaleX, scaleY)
|
|
143
|
+
|
|
144
|
+
// Compute the offset to align the content to the padded area
|
|
145
|
+
const offsetX = uiBounds.minX + padding - elementsBounds.minX * newZoom
|
|
146
|
+
const offsetY = uiBounds.minY + padding - elementsBounds.minY * newZoom
|
|
147
|
+
|
|
148
|
+
this.panTo(offsetX, offsetY)
|
|
149
|
+
this.zoomTo(newZoom)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Get the current zoom level
|
|
154
|
+
*/
|
|
155
|
+
getScale() {
|
|
156
|
+
return this.layer.scale.x
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Get the current canvas position
|
|
161
|
+
*/
|
|
162
|
+
getPosition() {
|
|
163
|
+
return this.layer.position
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
getMousePosInWorkspace(mousePos: MouseData) {
|
|
167
|
+
return this.layer.toLocal({
|
|
168
|
+
x: mousePos.clientX,
|
|
169
|
+
y: mousePos.clientY
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
}
|
package/test-global.d.ts
ADDED
package/test-setup.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
class MockDOMRect {
|
|
2
|
+
x: number
|
|
3
|
+
y: number
|
|
4
|
+
width: number
|
|
5
|
+
height: number
|
|
6
|
+
|
|
7
|
+
constructor(x = 0, y = 0, width = 0, height = 0) {
|
|
8
|
+
this.x = x
|
|
9
|
+
this.y = y
|
|
10
|
+
this.width = width
|
|
11
|
+
this.height = height
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
get left() {
|
|
15
|
+
return this.x
|
|
16
|
+
}
|
|
17
|
+
get top() {
|
|
18
|
+
return this.y
|
|
19
|
+
}
|
|
20
|
+
get right() {
|
|
21
|
+
return this.x + this.width
|
|
22
|
+
}
|
|
23
|
+
get bottom() {
|
|
24
|
+
return this.y + this.height
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
globalThis.DOMRect = MockDOMRect as unknown as typeof DOMRect
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"declarationMap": true,
|
|
7
|
+
"sourceMap": true,
|
|
8
|
+
"outDir": "./dist",
|
|
9
|
+
"strict": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"moduleResolution": "Node",
|
|
13
|
+
"resolveJsonModule": true
|
|
14
|
+
},
|
|
15
|
+
"include": [
|
|
16
|
+
"src"
|
|
17
|
+
],
|
|
18
|
+
"exclude": [
|
|
19
|
+
"node_modules",
|
|
20
|
+
"dist"
|
|
21
|
+
]
|
|
22
|
+
}
|