Skip to content

Visualize Your Ideas With Code

feat: grid and debug overlays

aarthificial895a53ab4222
diff --git a/src/components/viewport/Debug.tsx b/src/components/viewport/Debug.tsx
new file mode 100644
index 00000000..7e53d793
--- /dev/null
+++ b/src/components/viewport/Debug.tsx
@@ -0,0 +1,100 @@
+import styles from './Viewport.module.scss';
+import {usePlayer, usePlayerTime} from '../../hooks';
+import {useContext, useLayoutEffect, useRef} from 'preact/hooks';
+import {ViewportContext} from './ViewportContext';
+import {NODE_ID} from '@motion-canvas/core/symbols';
+
+interface DebugProps {
+ node: any;
+ setNode: (value: any) => void;
+}
+
+export function Debug({node, setNode}: DebugProps) {
+ const player = usePlayer();
+ const time = usePlayerTime();
+ const canvasRef = useRef<HTMLCanvasElement>();
+ const contextRef = useRef<CanvasRenderingContext2D>();
+ const state = useContext(ViewportContext);
+
+ useLayoutEffect(() => {
+ contextRef.current ??= canvasRef.current.getContext('2d');
+ const ctx = contextRef.current;
+ ctx.clearRect(0, 0, canvasRef.current.width, canvasRef.current.height);
+ if (!node) {
+ return;
+ }
+ if (!node.getLayer()) {
+ const id = node.attrs[NODE_ID];
+ setNode(player.project.findOne((node: any) => node.attrs[NODE_ID] === id));
+ return;
+ }
+
+ ctx.save();
+ ctx.translate(
+ state.x + canvasRef.current.width / 2,
+ state.y + canvasRef.current.height / 2,
+ );
+ ctx.scale(state.zoom, state.zoom);
+ ctx.translate(player.project.width() / -2, player.project.height() / -2);
+
+ const rect = node.getClientRect();
+ const padding = node.getPadd();
+ const margin = node.getMargin();
+ const position = node.getAbsolutePosition();
+ const offset = node.getOriginOffset();
+ const scale = node.getAbsoluteScale();
+
+ const contentRect = padding.scale(scale).shrink(rect);
+ const marginRect = margin.scale(scale).expand(rect);
+
+ ctx.beginPath();
+ ctx.rect(
+ contentRect.x,
+ contentRect.y,
+ contentRect.width,
+ contentRect.height,
+ );
+ ctx.rect(rect.x, rect.y, rect.width, rect.height);
+ ctx.closePath();
+ ctx.fillStyle = 'rgba(180,255,147,0.6)';
+ ctx.fill('evenodd');
+
+ ctx.beginPath();
+ ctx.rect(rect.x, rect.y, rect.width, rect.height);
+ ctx.closePath();
+ ctx.strokeStyle = 'rgba(255, 255, 255, 1)';
+ ctx.lineWidth = 2 / state.zoom;
+ ctx.stroke();
+
+ ctx.beginPath();
+ ctx.rect(rect.x, rect.y, rect.width, rect.height);
+ ctx.rect(marginRect.x, marginRect.y, marginRect.width, marginRect.height);
+ ctx.closePath();
+ ctx.fillStyle = 'rgba(255,193,125,0.6)';
+ ctx.fill('evenodd');
+
+ ctx.beginPath();
+ ctx.arc(position.x + offset.x, position.y + offset.y, 5 / state.zoom, 0, Math.PI * 2);
+ ctx.closePath();
+ ctx.fillStyle = 'rgba(255, 255, 255, 1)';
+ ctx.fill();
+
+ ctx.font = `${16 / state.zoom}px JetBrains Mono`;
+ ctx.fillText(
+ `${Math.floor(rect.width)} x ${Math.floor(rect.height)}`,
+ rect.x + 8 / state.zoom,
+ rect.y - 8 / state.zoom,
+ );
+
+ ctx.restore();
+ }, [state, node, time]);
+
+ return (
+ <canvas
+ className={styles.overlay}
+ ref={canvasRef}
+ width={state.width}
+ height={state.height}
+ />
+ );
+}
diff --git a/src/components/viewport/Grid.tsx b/src/components/viewport/Grid.tsx
new file mode 100644
index 00000000..6be1f908
--- /dev/null
+++ b/src/components/viewport/Grid.tsx
@@ -0,0 +1,68 @@
+import styles from './Viewport.module.scss';
+import {useContext, useLayoutEffect, useMemo, useRef} from 'preact/hooks';
+import {ViewportContext} from './ViewportContext';
+
+const GRID_SIZE = 40;
+
+export function Grid() {
+ const canvasRef = useRef<HTMLCanvasElement>();
+ const contextRef = useRef<CanvasRenderingContext2D>();
+ const state = useContext(ViewportContext);
+
+ const path = useMemo<Path2D>(() => {
+ const path = new Path2D();
+ const width = Math.ceil(state.width / 2 / GRID_SIZE + 2) * GRID_SIZE;
+ const height = Math.ceil(state.height / 2 / GRID_SIZE + 2) * GRID_SIZE;
+
+ for (let i = -width; i < width; i += GRID_SIZE) {
+ path.moveTo(i, -height);
+ path.lineTo(i, height);
+ }
+ for (let i = -height; i < height; i += GRID_SIZE) {
+ path.moveTo(-width, i);
+ path.lineTo(width, i);
+ }
+ path.closePath();
+ return path;
+ }, [state]);
+
+ useLayoutEffect(() => {
+ contextRef.current ??= canvasRef.current.getContext('2d');
+ const ctx = contextRef.current;
+ ctx.clearRect(0, 0, canvasRef.current.width, canvasRef.current.height);
+ if (!state.grid) {
+ return;
+ }
+
+ ctx.save();
+ const scale = Math.pow(
+ 2,
+ Math.log2(state.zoom) - Math.floor(Math.log2(state.zoom)),
+ );
+ ctx.translate(
+ canvasRef.current.width / 2 + (state.x % (GRID_SIZE * scale)),
+ canvasRef.current.height / 2 + (state.y % (GRID_SIZE * scale)),
+ );
+ ctx.scale(scale, scale);
+
+ ctx.strokeStyle = 'rgba(255, 255, 255, 0.24)';
+ ctx.lineWidth = 1 / scale;
+ ctx.stroke(path);
+
+ ctx.lineWidth = 1 / scale;
+ ctx.setLineDash([0, 1.25, 2.5, 1.25]);
+ ctx.translate(GRID_SIZE / 2, GRID_SIZE / 2);
+ ctx.stroke(path);
+
+ ctx.restore();
+ }, [state, path]);
+
+ return (
+ <canvas
+ className={styles.overlay}
+ ref={canvasRef}
+ width={state.width}
+ height={state.height}
+ />
+ );
+}
diff --git a/src/components/viewport/View.tsx b/src/components/viewport/View.tsx
index 412e3593..cbdc833a 100644
--- a/src/components/viewport/View.tsx
+++ b/src/components/viewport/View.tsx
@@ -1,7 +1,15 @@
import styles from './Viewport.module.scss';
-
-import {useDocumentEvent, useDrag, usePlayer, useStorage} from '../../hooks';
-import {useCallback, useEffect, useRef} from 'preact/hooks';
+import {
+ useDocumentEvent,
+ useDrag,
+ usePlayer,
+ useSize,
+ useStorage,
+} from '../../hooks';
+import {useCallback, useEffect, useRef, useState} from 'preact/hooks';
+import {Grid} from './Grid';
+import {Debug} from './Debug';
+import {ViewportContext, ViewportState} from './ViewportContext';
const ZOOM_SPEED = 0.1;
const konvaContainer = document.getElementById('konva');
@@ -10,14 +18,27 @@ export function View() {
const player = usePlayer();
const containerRef = useRef<HTMLDivElement>();
const viewportRef = useRef<HTMLDivElement>();
+ const overlayRef = useRef<HTMLDivElement>();
+ const size = useSize(containerRef);
+ const [node, setNode] = useState<any>(null);
- const [state, setState] = useStorage('viewport', {
+ const [state, setState] = useStorage<ViewportState>('viewport', {
+ width: 1920,
+ height: 1080,
x: 0,
y: 0,
zoom: 1,
grid: false,
});
+ useEffect(() => {
+ setState({
+ ...state,
+ width: size.width,
+ height: size.height,
+ });
+ }, [size]);
+
const [handleDrag, isDragging] = useDrag(
useCallback(
(x, y) =>
@@ -40,6 +61,29 @@ export function View() {
return () => konvaContainer.remove();
}, [viewportRef.current]);
+ useEffect(() => {
+ const animation = () =>
+ overlayRef.current.animate(
+ [
+ {
+ boxShadow: '0 0 0px 0 #ccc inset',
+ easing: 'cubic-bezier(0.33, 1, 0.68, 1)',
+ },
+ {
+ boxShadow: '0 0 0px 4px #ccc inset',
+ easing: 'cubic-bezier(0.32, 0, 0.67, 0)',
+ },
+ {boxShadow: '0 0 0px 0 #ccc inset'},
+ ],
+ {
+ duration: 300,
+ },
+ );
+
+ player.Reloaded.subscribe(animation);
+ return () => player.Reloaded.unsubscribe(animation);
+ });
+
useDocumentEvent(
'keydown',
useCallback(
@@ -62,43 +106,79 @@ export function View() {
break;
case "'":
setState({...state, grid: !state.grid});
- this.update();
+ break;
+ case "ArrowUp":
+ if (node?.parent) {
+ setNode(node.parent);
+ }
+ break;
+ case "ArrowDown":
+ if (node?.children?.length) {
+ setNode(node.children.at(-1));
+ }
break;
}
},
- [setState, state],
+ [setState, state, node],
),
);
return (
- <div
- className={styles.viewport}
- ref={containerRef}
- onMouseDown={handleDrag}
- onWheel={event => {
- if (isDragging) return;
- const rect = containerRef.current.getBoundingClientRect();
- const pointer = {
- x: event.x - rect.x - rect.width / 2,
- y: event.y - rect.y - rect.height / 2,
- };
-
- const ratio = 1 - Math.sign(event.deltaY) * ZOOM_SPEED;
- setState({
- ...state,
- zoom: state.zoom * ratio,
- x: pointer.x + (state.x - pointer.x) * ratio,
- y: pointer.y + (state.y - pointer.y) * ratio,
- });
- }}
- >
+ <ViewportContext.Provider value={state}>
<div
- style={{
- transform: `translate(${state.x}px, ${state.y}px) scale(${state.zoom})`,
+ className={styles.viewport}
+ ref={containerRef}
+ onMouseDown={event => {
+ if (event.button === 0) {
+ const position = {
+ x: event.x - size.x,
+ y: event.y - size.y,
+ };
+
+ position.x -= state.x + size.width / 2;
+ position.y -= state.y + size.height / 2;
+ position.x /= state.zoom;
+ position.y /= state.zoom;
+ position.x += player.project.width() / 2;
+ position.y += player.project.height() / 2;
+
+ player.project.listening(true);
+ player.project.master.drawHit();
+ const node = player.project.master.getIntersection(position);
+ player.project.listening(false);
+ setNode(node);
+ } else {
+ handleDrag(event);
+ }
+ }}
+ onWheel={event => {
+ if (isDragging) return;
+ const rect = containerRef.current.getBoundingClientRect();
+ const pointer = {
+ x: event.x - rect.x - rect.width / 2,
+ y: event.y - rect.y - rect.height / 2,
+ };
+
+ const ratio = 1 - Math.sign(event.deltaY) * ZOOM_SPEED;
+ setState({
+ ...state,
+ zoom: state.zoom * ratio,
+ x: pointer.x + (state.x - pointer.x) * ratio,
+ y: pointer.y + (state.y - pointer.y) * ratio,
+ });
}}
- id={'viewport'}
- ref={viewportRef}
- />
- </div>
+ >
+ <div
+ style={{
+ transform: `translate(${state.x}px, ${state.y}px) scale(${state.zoom})`,
+ }}
+ id={'viewport'}
+ ref={viewportRef}
+ />
+ <Grid />
+ <Debug node={node} setNode={setNode} />
+ <div ref={overlayRef} className={styles.overlay} />
+ </div>
+ </ViewportContext.Provider>
);
}
diff --git a/src/components/viewport/Viewport.module.scss b/src/components/viewport/Viewport.module.scss
index 63c55b1f..791e8686 100644
--- a/src/components/viewport/Viewport.module.scss
+++ b/src/components/viewport/Viewport.module.scss
@@ -12,11 +12,21 @@
display: flex;
align-items: center;
justify-content: center;
+ position: relative;
flex-grow: 1;
background-color: #000000;
}
+.overlay {
+ position: absolute;
+ top: 0;
+ left: 0;
+ right: 0;
+ bottom: 0;
+ pointer-events: none;
+}
+
.playback {
background: #181818;
display: grid;
@@ -41,4 +51,4 @@
.frames {
margin: 0 8px;
color: rgba(255, 255, 255, 0.32);
-}
\ No newline at end of file
+}
diff --git a/src/components/viewport/ViewportContext.ts b/src/components/viewport/ViewportContext.ts
new file mode 100644
index 00000000..a4b4b787
--- /dev/null
+++ b/src/components/viewport/ViewportContext.ts
@@ -0,0 +1,21 @@
+import {createContext} from 'preact';
+
+export interface ViewportState {
+ width: number;
+ height: number;
+ x: number;
+ y: number;
+ zoom: number;
+ grid: boolean;
+}
+
+const ViewportContext = createContext<ViewportState>({
+ width: 1920,
+ height: 1080,
+ x: 0,
+ y: 0,
+ zoom: 1,
+ grid: false,
+});
+
+export {ViewportContext};