Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ SnapEngineJS/

**Package:** `@snap-engine/core`
**Purpose:** Core interactivity engine
**Build:** Yes → `dist/`
**Build:** Raw-source package (`exports` → `src/*.ts`); `npm run build` emits `dist/` as a build check

See `src/AGENTS.md` for module details.

**Entry points:**
- `@snap-engine/core` - Main export
- `@snap-engine/core/animation` - Animation system
- `@snap-engine/core/collision` - Collision detection
- `@snap-engine/core/geometry` - Shared geometry types and pure helpers
- `@snap-engine/core/layout` - Layout simulation over measured box trees
- `@snap-engine/core/debug` - Debug utilities

## Asset Packages (`assets/`)
Expand Down Expand Up @@ -149,7 +151,9 @@ Each asset package needs path mappings. A unified package at
"@snap-engine/core": ["../../src/index.ts"],
"@snap-engine/core/animation": ["../../src/animation.ts"],
"@snap-engine/core/collision": ["../../src/collision.ts"],
"@snap-engine/core/debug": ["../../src/debug.ts"]
"@snap-engine/core/debug": ["../../src/debug.ts"],
"@snap-engine/core/geometry": ["../../src/geometry.ts"],
"@snap-engine/core/layout": ["../../src/layout.ts"]
}
}
}
Expand Down
180 changes: 88 additions & 92 deletions assets/asset-base/src/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import type {
pointerUpProp,
mouseWheelProp,
pinchProp,
eventPosition,
PointerPosition,
EdgePanController,
} from "@snap-engine/core";
import { ElementObject } from "@snap-engine/core";
import { ElementObject, pointerPositionFromScreen } from "@snap-engine/core";
import { Camera } from "@snap-engine/core";
import { pointIntersectsRect, type Point } from "@snap-engine/core/geometry";
import type { CameraConfig } from "@snap-engine/core";

/** What the mouse wheel / trackpad two-finger scroll does. */
Expand Down Expand Up @@ -148,8 +149,8 @@ class CameraControl extends ElementObject {
#pinchAnchor: PinchAnchor | null = null;
#edgePanRequest: {
pointerId: number;
position: eventPosition;
onFrame: (position: eventPosition) => void;
position: PointerPosition;
onFrame: (position: PointerPosition) => void;
} | null = null;
#edgePanFrameId: number | null = null;
#edgePanTimestamp: number | null = null;
Expand Down Expand Up @@ -193,8 +194,7 @@ class CameraControl extends ElementObject {
*/
setCameraConfig(config: CameraConfig) {
this.config = { ...this.config, camera: { ...this.config.camera, ...config } };
this.camera?.setConfig(config);
this.paintCamera();
this.#commitCamera(() => this.camera?.setConfig(config));
}

set element(_element: HTMLElement) {
Expand All @@ -209,31 +209,61 @@ class CameraControl extends ElementObject {
// });
}

/**
* Paint the camera transform. The paint happens in the same commit as the
* camera state change whenever the stage allows a write: a DOM read later
* in the frame must never see the new zoom against the old transform, or
* it would convert the old on-screen size with the new zoom.
*/
paintCamera() {
// this.engine.camera?.updateCameraProperty();
this.engine.camera?.updateCamera();
this.style.transform = this.engine.camera?.canvasStyle as string;
if (this.element && this.#canWriteNow()) {
this.writeTransform();
return;
}
this.schedule(() => this.writeTransform(), {
stage: "WRITE_2",
queueId: `${this.id}-transform`,
});
}

#canWriteNow(): boolean {
const stage = this.global.currentStage;
return stage === "IDLE" || stage.startsWith("WRITE");
}

/**
* Apply a programmatic camera change and paint it as one commit. Requested
* during a read stage, both wait for the next write stage, so no read in
* this frame observes a camera the DOM does not show yet.
*/
#commitCamera(mutate: (camera: Camera) => void): void {
const commit = () => {
const camera = this.engine.camera;
if (!camera) return;
mutate(camera);
this.paintCamera();
};
if (this.#canWriteNow()) {
commit();
return;
}
this.schedule(commit, { stage: "WRITE_2" });
}

// Camera Methods

updateCameraCenterPosition(x: number = 0, y: number = 0) {
this.engine.camera?.setCameraCenterPosition(x, y);
this.paintCamera();
this.#commitCamera((camera) => camera.setCameraCenterPosition(x, y));
}

setCameraPosition(x: number, y: number) {
this.engine.camera?.setCameraPosition(x, y);
this.paintCamera();
this.#commitCamera((camera) => camera.setCameraPosition(x, y));
}

setCameraCenterPosition(x: number, y: number) {
this.engine.camera?.setCameraCenterPosition(x, y);
this.paintCamera();
this.#commitCamera((camera) => camera.setCameraCenterPosition(x, y));
}

getCameraCenterPosition() {
Expand All @@ -244,26 +274,19 @@ class CameraControl extends ElementObject {
if (this.config.zoomLock) {
return;
}
const camera = this.engine.camera;
if (!camera) {
return;
}

const targetX = originX ?? camera.cameraWidth / 2;
const targetY = originY ?? camera.cameraHeight / 2;

camera.handleScroll(deltaZoom, targetX, targetY);
this.style.transform = camera.canvasStyle as string;
this.schedule(() => this.writeTransform(), {
stage: "WRITE_2",
queueId: `${this.id}-transform`,
});
this.#commitCamera((camera) =>
camera.handleScroll(
deltaZoom,
originX ?? camera.cameraWidth / 2,
originY ?? camera.cameraHeight / 2,
),
);
}

startEdgePan(
pointerId: number,
position: eventPosition,
onFrame: (position: eventPosition) => void,
position: PointerPosition,
onFrame: (position: PointerPosition) => void,
): void {
if (!this.config.edgePan?.enabled) {
return;
Expand All @@ -273,7 +296,7 @@ class CameraControl extends ElementObject {
this.#scheduleEdgePanFrame();
}

updateEdgePan(pointerId: number, position: eventPosition): void {
updateEdgePan(pointerId: number, position: PointerPosition): void {
if (!this.config.edgePan?.enabled) {
this.stopEdgePan(pointerId);
return;
Expand Down Expand Up @@ -336,9 +359,9 @@ class CameraControl extends ElementObject {
return 0;
};
const velocityX =
axisSpeed(request.position.screenX, left, right) * maxSpeed;
axisSpeed(request.position.screen.x, left, right) * maxSpeed;
const velocityY =
axisSpeed(request.position.screenY, top, bottom) * maxSpeed;
axisSpeed(request.position.screen.y, top, bottom) * maxSpeed;
const previousTimestamp = this.#edgePanTimestamp ?? timestamp;
const elapsedSeconds =
Math.min(32, Math.max(0, timestamp - previousTimestamp)) / 1000;
Expand All @@ -356,32 +379,30 @@ class CameraControl extends ElementObject {
this.paintCamera();
request.onFrame(
this.#positionFromScreen(
request.position.screenX,
request.position.screenY,
request.position.screen.x,
request.position.screen.y,
),
);
}

this.#scheduleEdgePanFrame();
};

#positionFromScreen(screenX: number, screenY: number): eventPosition {
const camera = this.engine.camera;
if (!camera) {
return {
x: screenX,
y: screenY,
cameraX: screenX,
cameraY: screenY,
screenX,
screenY,
};
}
const [cameraX, cameraY] = camera.getCameraFromScreen(screenX, screenY);
const [x, y] = camera.getWorldFromCamera(cameraX, cameraY);
return { x, y, cameraX, cameraY, screenX, screenY };
#positionFromScreen(screenX: number, screenY: number): PointerPosition {
return pointerPositionFromScreen(this.engine.camera, screenX, screenY);
}

/** Whether a viewport point lies inside the camera's container. */
#isWithinCamera(camera: Camera, screen: Point): boolean {
return pointIntersectsRect(screen, {
x: camera.containerOffsetX,
y: camera.containerOffsetY,
width: camera.cameraWidth,
height: camera.cameraHeight,
});
}


// Event Handlers

onCursorDown(prop: pointerDownProp) {
Expand Down Expand Up @@ -419,8 +440,8 @@ class CameraControl extends ElementObject {
}
this.#state = "panning";
this.#panPointerId = prop.event.pointerId;
this.#mouseDownX = prop.position.screenX;
this.#mouseDownY = prop.position.screenY;
this.#mouseDownX = prop.position.screen.x;
this.#mouseDownY = prop.position.screen.y;
this.#pinchAnchor = null;
this.engine.camera?.handlePanStart();
prop.event.preventDefault();
Expand All @@ -438,14 +459,10 @@ class CameraControl extends ElementObject {
if (this.global.data.allowCameraControl === false) {
return;
}
const dx = prop.position.screenX - this.#mouseDownX;
const dy = prop.position.screenY - this.#mouseDownY;
const dx = prop.position.screen.x - this.#mouseDownX;
const dy = prop.position.screen.y - this.#mouseDownY;
this.engine.camera?.handlePanDrag(dx, dy);
this.style.transform = this.engine.camera?.canvasStyle as string;
this.schedule(() => this.writeTransform(), {
stage: "WRITE_2",
queueId: `${this.id}-transform`,
});
this.paintCamera();
}

onCursorUp(prop: pointerUpProp) {
Expand All @@ -459,11 +476,7 @@ class CameraControl extends ElementObject {
this.#state = "idle";
this.#panPointerId = null;
this.engine.camera?.handlePanEnd();
this.style.transform = this.engine.camera?.canvasStyle as string;
this.schedule(() => this.writeTransform(), {
stage: "WRITE_2",
queueId: `${this.id}-transform`,
});
this.paintCamera();
}

onZoom(prop: mouseWheelProp) {
Expand All @@ -484,12 +497,7 @@ class CameraControl extends ElementObject {
return;
}
const camera = this.engine.camera!;
if (
prop.position.screenX < camera.containerOffsetX ||
prop.position.screenX > camera.containerOffsetX + camera.cameraWidth ||
prop.position.screenY < camera.containerOffsetY ||
prop.position.screenY > camera.containerOffsetY + camera.cameraHeight
) {
if (!this.#isWithinCamera(camera, prop.position.screen)) {
return;
}
// A trackpad pinch is a ctrl-wheel; Cmd+scroll is a meta-wheel with much larger
Expand All @@ -500,8 +508,8 @@ class CameraControl extends ElementObject {
options.zoomSensitivity * (pinch ? options.pinchZoomGain : 1);
this.zoomBy(
(-prop.delta * sensitivity) / 2000,
prop.position.cameraX,
prop.position.cameraY,
prop.position.camera.x,
prop.position.camera.y,
);
prop.event.preventDefault();
}
Expand All @@ -519,24 +527,15 @@ class CameraControl extends ElementObject {
if (!camera) {
return;
}
if (
prop.position.screenX < camera.containerOffsetX ||
prop.position.screenX > camera.containerOffsetX + camera.cameraWidth ||
prop.position.screenY < camera.containerOffsetY ||
prop.position.screenY > camera.containerOffsetY + camera.cameraHeight
) {
if (!this.#isWithinCamera(camera, prop.position.screen)) {
return;
}
// Wheel deltas are screen pixels in the document-scroll sense (deltaY > 0 =
// scroll down); handlePan reads them the same way (positive = pan down) and
// divides by zoom, giving 1:1 screen-pixel panning like a pointer drag.
const event = prop.event as WheelEvent;
camera.handlePan(event.deltaX * sensitivity, event.deltaY * sensitivity);
this.style.transform = camera.canvasStyle as string;
this.schedule(() => this.writeTransform(), {
stage: "WRITE_2",
queueId: `${this.id}-transform`,
});
this.paintCamera();
prop.event.preventDefault();
}

Expand Down Expand Up @@ -566,8 +565,8 @@ class CameraControl extends ElementObject {
}
const [pointer0, pointer1] = prop.current.pointerList;
const center = {
x: (pointer0.cameraX + pointer1.cameraX) / 2,
y: (pointer0.cameraY + pointer1.cameraY) / 2,
x: (pointer0.camera.x + pointer1.camera.x) / 2,
y: (pointer0.camera.y + pointer1.camera.y) / 2,
};
const camera = this.engine.camera;
if (!camera) {
Expand Down Expand Up @@ -600,11 +599,7 @@ class CameraControl extends ElementObject {
: center.y,
});

this.style.transform = camera.canvasStyle as string;
this.schedule(() => this.writeTransform(), {
stage: "WRITE_2",
queueId: `${this.id}-transform`,
});
this.paintCamera();
}

onPinchEnd() {
Expand All @@ -625,14 +620,15 @@ class CameraControl extends ElementObject {
super.destroy(removeDom);
}

#createPinchAnchor(center: { x: number; y: number }, distance: number) {
#createPinchAnchor(center: Point, distance: number) {
const camera = this.engine.camera!;
const [worldX, worldY] = camera.getWorldFromCamera(center.x, center.y);
return {
centerX: center.x,
centerY: center.y,
distance,
worldX: camera.cameraPositionX + center.x / camera.zoom,
worldY: camera.cameraPositionY + center.y / camera.zoom,
worldX,
worldY,
zoom: camera.zoom,
};
}
Expand Down
2 changes: 2 additions & 0 deletions assets/asset-base/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"@snap-engine/core/animation": ["../../src/animation.ts"],
"@snap-engine/core/collision": ["../../src/collision.ts"],
"@snap-engine/core/debug": ["../../src/debug.ts"],
"@snap-engine/core/geometry": ["../../src/geometry.ts"],
"@snap-engine/core/layout": ["../../src/layout.ts"],
"@snap-engine/asset-base": ["./src/index.ts"],
"@snap-engine/asset-base/*": ["./src/*"],
"@snap-engine/asset-base/svelte": ["./src/svelte/index.ts"],
Expand Down
4 changes: 3 additions & 1 deletion assets/snapline/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ snapline/
**Key Methods:**
- `addConnectorObject(connector)` - Register connector
- `getConnector(name)` - Look up a connector by name
- `remeasureDomGeometry()` - Re-measure the box and re-glue lines
- `remeasureDomGeometry()` - Re-measure the box and re-glue lines. Measured
sizes are world-space (CSS pixels), so the hitbox matches the authored
size at any camera zoom
- `setSize(width, height)` / `setSizeState(width, height)` - Drive size

### ConnectorMirror
Expand Down
Loading
Loading