Visualize Your Ideas With Code
feat(ui): add custom presentation overlays (#884)
Jacob4696d3c8cb8b
diff --git a/packages/2d/src/editor/PreviewOverlayConfig.tsx b/packages/2d/src/editor/PreviewOverlayConfig.tsxindex 3c8a3dfa..d13b9aa2 100644--- a/packages/2d/src/editor/PreviewOverlayConfig.tsx+++ b/packages/2d/src/editor/PreviewOverlayConfig.tsx@@ -4,8 +4,8 @@ import {OverlayWrapper,PluginOverlayConfig,useCurrentScene,- usePreviewMatrix,useViewportContext,+ useViewportMatrix,} from '@motion-canvas/ui';import {ComponentChildren} from 'preact';import {useInspection} from './Provider';@@ -14,7 +14,7 @@ function Component({children}: {children?: ComponentChildren}) {const state = useViewportContext();const scene = useCurrentScene();const {nodeKey} = useInspection();- const matrix = usePreviewMatrix();+ const matrix = useViewportMatrix();return (<OverlayWrapper@@ -24,8 +24,8 @@ function Component({children}: {children?: ComponentChildren}) {event.stopPropagation();const position = new Vector2(- event.x - state.size.x,- event.y - state.size.y,+ event.x - state.rect.x,+ event.y - state.rect.y,).transformAsPoint(matrix.inverse());nodeKey.value = scene.inspectPosition(position.x, position.y) as string;@@ -39,10 +39,9 @@ function Component({children}: {children?: ComponentChildren}) {function drawHook() {const {nodeKey} = useInspection();const scene = useCurrentScene();- const matrix = usePreviewMatrix();nodeKey.value;- return (ctx: CanvasRenderingContext2D) => {+ return (ctx: CanvasRenderingContext2D, matrix: DOMMatrix) => {if (!isInspectable(scene)) return;const element = scene.validateInspection(nodeKey.value);if (!element || element !== nodeKey.value) {diff --git a/packages/2d/src/lib/scenes/Scene2D.ts b/packages/2d/src/lib/scenes/Scene2D.tsindex 4082499e..bfc2e619 100644--- a/packages/2d/src/lib/scenes/Scene2D.ts+++ b/packages/2d/src/lib/scenes/Scene2D.ts@@ -71,9 +71,7 @@ export class Scene2D extends GeneratorScene<View2D> implements Inspectable {public inspectPosition(x: number, y: number): InspectedElement | null {return this.execute(- () =>- this.getView().hit(new Vector2(x, y).scale(this.resolutionScale))- ?.key ?? null,+ () => this.getView().hit(new Vector2(x, y))?.key ?? null,);}@@ -109,20 +107,15 @@ export class Scene2D extends GeneratorScene<View2D> implements Inspectable {const node = this.getNode(element);if (node) {this.execute(() => {- node.drawOverlay(- context,- matrix- .scale(1 / this.resolutionScale, 1 / this.resolutionScale)- .multiplySelf(node.localToWorld()),- );+ node.drawOverlay(context, matrix.multiply(node.localToWorld()));});}}public transformMousePosition(x: number, y: number): Vector2 | null {- return new Vector2(x, y)- .scale(this.resolutionScale)- .transformAsPoint(this.getView().localToParent().inverse());+ return new Vector2(x, y).transformAsPoint(+ this.getView().localToParent().inverse(),+ );}public registerNode(node: Node, key?: string): [string, () => void] {diff --git a/packages/ui/src/components/controls/ReadOnlyInput.tsx b/packages/ui/src/components/controls/ReadOnlyInput.tsxnew file mode 100644index 00000000..cb027f1d--- /dev/null+++ b/packages/ui/src/components/controls/ReadOnlyInput.tsx@@ -0,0 +1,9 @@+import clsx from 'clsx';+import type {JSX} from 'preact';+import styles from './Controls.module.scss';++interface ReadOnlyInputProps extends JSX.HTMLAttributes<HTMLDivElement> {}++export function ReadOnlyInput({className, ...props}: ReadOnlyInputProps) {+ return <div className={clsx(className, styles.input)} {...props} />;+}diff --git a/packages/ui/src/components/controls/index.ts b/packages/ui/src/components/controls/index.tsindex 5716b125..3cb956ad 100644--- a/packages/ui/src/components/controls/index.ts+++ b/packages/ui/src/components/controls/index.ts@@ -9,6 +9,7 @@ export * from './Input';export * from './InputSelect';export * from './Label';export * from './Pill';+export * from './ReadOnlyInput';export * from './Select';export * from './Separator';export * from './Slider';diff --git a/packages/ui/src/components/presentation/PresentationMode.tsx b/packages/ui/src/components/presentation/PresentationMode.tsxindex bbe8820f..5316cf4c 100644--- a/packages/ui/src/components/presentation/PresentationMode.tsx+++ b/packages/ui/src/components/presentation/PresentationMode.tsx@@ -1,15 +1,65 @@-import {useApplication} from '../../contexts';-import {CustomStage} from '../viewport';+import clsx from 'clsx';+import {useMemo, useRef} from 'preact/hooks';+import {ViewportProvider, ViewportState, useApplication} from '../../contexts';+import {useRenderingSettings, useSharedSettings, useSize} from '../../hooks';+import {StageView} from '../viewport';+import {OverlayCanvas} from '../viewport/OverlayCanvas';+import styles from '../viewport/Viewport.module.scss';import {PresentationControls} from './PresentationControls';import {SlideGraph} from './SlideGraph';export function PresentationMode() {- const {presenter} = useApplication();+ const {plugins, presenter} = useApplication();+ const ref = useRef<HTMLDivElement>();+ const size = useSize(ref);+ const settings = useSharedSettings();+ const {resolutionScale} = useRenderingSettings();++ const drawHooks = useMemo(+ () =>+ plugins.map(plugin => plugin.presenterOverlay?.drawHook).filter(Boolean),+ [plugins],+ );++ const state: ViewportState = useMemo(() => {+ const state = {+ grid: false,+ rect: size,+ zoom: 1,+ x: 0,+ y: 0,+ resolutionScale,+ };++ const physicalSize = settings.size.scale(resolutionScale);+ if (physicalSize.width > size.width || physicalSize.height > size.height) {+ let newZoom = size.height / physicalSize.height;+ if (physicalSize.width * newZoom > size.width) {+ newZoom = size.width / physicalSize.width;+ }+ if (!isNaN(newZoom) && newZoom > 0 && newZoom < Infinity) {+ state.zoom = newZoom;+ }+ }+ state.zoom *= resolutionScale;++ return state;+ }, [settings, size, resolutionScale]);+return (- <>- <CustomStage stage={presenter.stage} />+ <ViewportProvider value={state}>+ <StageView+ forwardRef={ref}+ stage={presenter.stage}+ className={clsx(styles.viewport, styles.renderingPreview)}+ />+ <OverlayCanvas+ drawHooks={drawHooks}+ width={size.width}+ height={size.height}+ /><SlideGraph /><PresentationControls />- </>+ </ViewportProvider>);}diff --git a/packages/ui/src/components/viewport/Coordinates.tsx b/packages/ui/src/components/viewport/Coordinates.tsxindex 6ddb56dc..8d5fad4b 100644--- a/packages/ui/src/components/viewport/Coordinates.tsx+++ b/packages/ui/src/components/viewport/Coordinates.tsx@@ -1,68 +1,35 @@-import {isInspectable} from '@motion-canvas/core';+import {isInspectable, Vector2} from '@motion-canvas/core';import {useCallback} from 'preact/hooks';import {useEffect, useState} from 'react';import {useViewportContext} from '../../contexts';import {useCurrentScene,useDocumentEvent,- useSharedSettings,+ useViewportMatrix,} from '../../hooks';-import styles from '../controls/Controls.module.scss';+import {ReadOnlyInput} from '../controls';+import styles from './Viewport.module.scss';export function Coordinates() {const [mousePos, setMousePos] = useState({x: 0, y: 0});- const [hover, setHover] = useState(true);-const state = useViewportContext();- const settings = {- ...useSharedSettings(),- };const scene = useCurrentScene();+ const matrix = useViewportMatrix();useEffect(() => {const handleMouseMove = (event: {x: number; y: number}) => {if (!isInspectable(scene)) return;- const absoluteCoords = {- x: event.x - state.size.x,- y: event.y - state.size.y,- };-- absoluteCoords.x -= state.x + state.width / 2;- absoluteCoords.y -= state.y + state.height / 2;- absoluteCoords.x /= state.zoom;- absoluteCoords.y /= state.zoom;- absoluteCoords.x += settings.size.width / 2;- absoluteCoords.y += settings.size.height / 2;-- const point = scene.transformMousePosition(- absoluteCoords.x,- absoluteCoords.y,- );-- point.x = Math.round(point.x);- point.y = Math.round(point.y);- // These coordinates are used purely for bounds detection of the preview window.- const boundX = Math.round(- ((event.x - state.size.x) / state.width - 0.5) * state.width,- );- const boundY = Math.round(- ((event.y - state.size.y) / state.height - 0.5) * state.height,- );- if (- Math.abs(boundX) > state.width / 2 ||- Math.abs(boundY) > state.height / 2- ) {- setHover?.(false);- } else {- // Only need to set position coordinates if hovering over the overlay- setHover?.(true);+ let point = new Vector2(+ event.x - state.rect.x,+ event.y - state.rect.y,+ ).transformAsPoint(matrix.inverse());+ point = scene.transformMousePosition(point.x, point.y);- setMousePos({- x: point.x,- y: point.y,- });- }+ setMousePos({+ x: Math.round(point.x),+ y: Math.round(point.y),+ });};document.addEventListener('mousemove', handleMouseMove);@@ -70,7 +37,7 @@ export function Coordinates() {return () => {document.removeEventListener('mousemove', handleMouseMove);};- }, [state]);+ }, [state, matrix]);// Below method is used for the copy to clipboard keybinduseDocumentEvent(@@ -87,12 +54,8 @@ export function Coordinates() {);return (- <>- {hover && (- <div title={'Coordinates'} className={styles.input}>- ({mousePos.x}, {mousePos.y})- </div>- )}- </>+ <ReadOnlyInput className={styles.coordinates} title={'Coordinates'}>+ ({mousePos.x}, {mousePos.y})+ </ReadOnlyInput>);}diff --git a/packages/ui/src/components/viewport/CustomStage.tsx b/packages/ui/src/components/viewport/CustomStage.tsxdeleted file mode 100644index da4b059d..00000000--- a/packages/ui/src/components/viewport/CustomStage.tsx+++ /dev/null@@ -1,31 +0,0 @@-import type {Stage} from '@motion-canvas/core';-import {useLayoutEffect, useRef} from 'preact/hooks';-import {useSharedSettings} from '../../hooks';--import clsx from 'clsx';-import styles from './Viewport.module.scss';--export interface CustomStageProps {- stage: Stage;-}--export function CustomStage({stage}: CustomStageProps) {- const ref = useRef<HTMLDivElement>();- const settings = useSharedSettings();-- useLayoutEffect(() => {- ref.current.append(stage.finalBuffer);- return () => stage.finalBuffer.remove();- }, [stage]);-- return (- <div- className={clsx(- styles.viewport,- styles.renderingPreview,- settings.background ? styles.canvasOutline : styles.alphaBackground,- )}- ref={ref}- />- );-}diff --git a/packages/ui/src/components/viewport/EditorPreview.tsx b/packages/ui/src/components/viewport/EditorPreview.tsxindex fa199ef6..c6bddf01 100644--- a/packages/ui/src/components/viewport/EditorPreview.tsx+++ b/packages/ui/src/components/viewport/EditorPreview.tsx@@ -55,11 +55,10 @@ export function EditorPreview() {const state: ViewportState = useMemo(() => {const state = {grid,- size,- width: size.width,- height: size.height,+ rect: size,...position,zoom,+ resolutionScale: settings.resolutionScale,};if (zoomToFit) {const {width, height} = settings.size;@@ -163,22 +162,16 @@ export function EditorPreview() {ref={containerRef}><PreviewStage- className={clsx(- settings.background?.alpha() === 1- ? styles.canvasOutline- : styles.alphaBackground,- )}style={{transform: `translate(${state.x}px, ${state.y}px) scale(${state.zoom / settings.resolutionScale})`,- outlineWidth: `${1 / state.zoom}px`,}}/><OverlayCanvasdrawHooks={drawHooks}- width={state.width}- height={state.height}+ width={state.rect.width}+ height={state.rect.height}/><divref={overlayRef}diff --git a/packages/ui/src/components/viewport/OverlayCanvas.tsx b/packages/ui/src/components/viewport/OverlayCanvas.tsxindex 3646690e..d5654c97 100644--- a/packages/ui/src/components/viewport/OverlayCanvas.tsx+++ b/packages/ui/src/components/viewport/OverlayCanvas.tsx@@ -2,7 +2,7 @@ import clsx from 'clsx';import {JSX} from 'preact';import {useLayoutEffect, useRef, useState} from 'preact/hooks';import {useApplication} from '../../contexts';-import {useSubscribable} from '../../hooks';+import {useSubscribable, useViewportMatrix} from '../../hooks';import {PluginDrawFunction} from '../../plugin';import styles from './Viewport.module.scss';@@ -20,6 +20,7 @@ export function OverlayCanvas({const {player} = useApplication();const [renderCount, setRenderCount] = useState(0);+ const matrix = useViewportMatrix();const drawFunctions = drawHooks.map(hook => hook());useSubscribable(@@ -34,10 +35,10 @@ export function OverlayCanvas({ctx.clearRect(0, 0, canvasRef.current.width, canvasRef.current.height);for (const drawFunction of drawFunctions) {ctx.save();- drawFunction(ctx);+ drawFunction(ctx, matrix);ctx.restore();}- }, [drawFunctions, renderCount]);+ }, [drawFunctions, matrix, renderCount]);return (<canvasdiff --git a/packages/ui/src/components/viewport/PreviewStage.tsx b/packages/ui/src/components/viewport/PreviewStage.tsxindex 6ae4f476..5e8e3e4c 100644--- a/packages/ui/src/components/viewport/PreviewStage.tsx+++ b/packages/ui/src/components/viewport/PreviewStage.tsx@@ -1,16 +1,16 @@import {Stage} from '@motion-canvas/core';import {JSX} from 'preact';-import {useEffect, useRef, useState} from 'preact/hooks';+import {useEffect, useState} from 'preact/hooks';import {useApplication} from '../../contexts';import {usePreviewSettings,useSharedSettings,useSubscribable,} from '../../hooks';+import {StageView} from './StageView';export function PreviewStage(props: JSX.HTMLAttributes<HTMLDivElement>) {const [stage] = useState(() => new Stage());- const ref = useRef<HTMLDivElement>();const {player} = useApplication();const {size, background} = useSharedSettings();const {resolutionScale} = usePreviewSettings();@@ -31,10 +31,5 @@ export function PreviewStage(props: JSX.HTMLAttributes<HTMLDivElement>) {stage.render(player.playback.currentScene, player.playback.previousScene);}, [resolutionScale, size, background]);- useEffect(() => {- ref.current.append(stage.finalBuffer);- return () => stage.finalBuffer.remove();- }, []);-- return <div ref={ref} {...props} />;+ return <StageView stage={stage} {...props} />;}diff --git a/packages/ui/src/components/viewport/StageView.tsx b/packages/ui/src/components/viewport/StageView.tsxnew file mode 100644index 00000000..8365fb82--- /dev/null+++ b/packages/ui/src/components/viewport/StageView.tsx@@ -0,0 +1,38 @@+import type {Stage} from '@motion-canvas/core';+import clsx from 'clsx';+import {JSX} from 'preact';+import {MutableRef, useLayoutEffect, useRef} from 'preact/hooks';+import {useSharedSettings} from '../../hooks';+import styles from './Viewport.module.scss';++export interface StageViewProps extends JSX.HTMLAttributes<HTMLDivElement> {+ forwardRef?: MutableRef<HTMLDivElement>;+ stage: Stage;+}++export function StageView({+ stage,+ className,+ forwardRef,+ ...rest+}: StageViewProps) {+ const localRef = useRef<HTMLDivElement>();+ const {background} = useSharedSettings();+ const ref = forwardRef ?? localRef;++ useLayoutEffect(() => {+ ref.current.append(stage.finalBuffer);+ return () => stage.finalBuffer.remove();+ }, [stage, ref]);++ return (+ <div+ className={clsx(+ className,+ background?.alpha() < 1 && styles.alphaBackground,+ )}+ ref={ref}+ {...rest}+ />+ );+}diff --git a/packages/ui/src/components/viewport/Viewport.module.scss b/packages/ui/src/components/viewport/Viewport.module.scssindex d089d392..f96f5193 100644--- a/packages/ui/src/components/viewport/Viewport.module.scss+++ b/packages/ui/src/components/viewport/Viewport.module.scss@@ -41,10 +41,6 @@background-image: url('../../img/grid.svg');}-.canvasOutline canvas {- outline: 1px solid var(--surface-color);-}-.overlay {position: absolute;top: 0;@@ -83,3 +79,10 @@gap: 8px;display: flex;}++.coordinates {+ display: none;+ .viewport:hover & {+ display: block;+ }+}diff --git a/packages/ui/src/components/viewport/Viewport.tsx b/packages/ui/src/components/viewport/Viewport.tsxindex 0dec424a..1feb904a 100644--- a/packages/ui/src/components/viewport/Viewport.tsx+++ b/packages/ui/src/components/viewport/Viewport.tsx@@ -1,4 +1,5 @@import {RendererState} from '@motion-canvas/core';+import clsx from 'clsx';import {useEffect, useState} from 'preact/hooks';import {useApplication} from '../../contexts';import {useDuration, useRendererState} from '../../hooks';@@ -10,8 +11,8 @@ import {RenderingProgress,} from '../playback';import {CurrentTime} from '../playback/CurrentTime';-import {CustomStage} from './CustomStage';import {EditorPreview} from './EditorPreview';+import {StageView} from './StageView';import {Timestamp} from './Timestamp';import styles from './Viewport.module.scss';@@ -69,7 +70,10 @@ function RenderingViewport() {return (<div className={styles.root}>- <CustomStage stage={renderer.stage} />+ <StageView+ stage={renderer.stage}+ className={clsx(styles.viewport, styles.renderingPreview)}+ /><RenderingProgress /><div className={styles.playback}><codediff --git a/packages/ui/src/components/viewport/index.ts b/packages/ui/src/components/viewport/index.tsindex d396d7f3..88c21724 100644--- a/packages/ui/src/components/viewport/index.ts+++ b/packages/ui/src/components/viewport/index.ts@@ -1,2 +1,2 @@-export * from './CustomStage';+export * from './StageView';export * from './Viewport';diff --git a/packages/ui/src/contexts/viewport.ts b/packages/ui/src/contexts/viewport.tsindex e78a31e2..7f846c04 100644--- a/packages/ui/src/contexts/viewport.ts+++ b/packages/ui/src/contexts/viewport.ts@@ -2,23 +2,21 @@ import {createContext} from 'preact';import {useContext} from 'preact/hooks';export interface ViewportState {- width: number;- height: number;x: number;y: number;- size: {x: number; y: number};+ rect: DOMRectReadOnly;zoom: number;grid: boolean;+ resolutionScale: number;}const ViewportContext = createContext<ViewportState>({- width: 1920,- height: 1080,x: 0,y: 0,- size: {x: 0, y: 0},+ rect: new DOMRectReadOnly(),zoom: 1,grid: false,+ resolutionScale: 1,});export const ViewportProvider = ViewportContext.Provider;diff --git a/packages/ui/src/hooks/index.ts b/packages/ui/src/hooks/index.tsindex d40c56ac..499f59b7 100644--- a/packages/ui/src/hooks/index.ts+++ b/packages/ui/src/hooks/index.ts@@ -7,7 +7,6 @@ export * from './useKeyHold';export * from './usePlayerState';export * from './usePlayerTime';export * from './usePresenterState';-export * from './usePreviewMatrix';export * from './useReducedMotion';export * from './useRendererState';export * from './useScenes';@@ -16,3 +15,4 @@ export * from './useSize';export * from './useStateChange';export * from './useStorage';export * from './useSubscribable';+export * from './useViewportMatrix';diff --git a/packages/ui/src/hooks/usePreviewMatrix.ts b/packages/ui/src/hooks/usePreviewMatrix.tsdeleted file mode 100644index 8885f3f7..00000000--- a/packages/ui/src/hooks/usePreviewMatrix.ts+++ /dev/null@@ -1,35 +0,0 @@-import {useMemo} from 'preact/hooks';-import {useViewportContext} from '../contexts';-import {usePreviewSettings, useSharedSettings} from './useSettings';--/**- * Get a matrix that transforms the preview canvas space to the scene space.- *- * @remarks- * When drawing the preview overlay, the canvas overlays the entire preview- * panel, no matter the zoom and pan of the scene itself. It ensures that- * the gizmos drawn on top have high resolution no matter how zoomed in the user- * is.- *- * This matrix is used to transform the drawn points so that they appear where- * they should be in the scene.- */-export function usePreviewMatrix() {- const {size} = useSharedSettings();- const {resolutionScale} = usePreviewSettings();- const state = useViewportContext();-- return useMemo(() => {- const matrix = new DOMMatrix();- if (!size) {- return matrix;- }-- const offset = size.scale(-0.5);- matrix.translateSelf(state.x + state.width / 2, state.y + state.height / 2);- matrix.scaleSelf(state.zoom, state.zoom);- matrix.translateSelf(offset.width, offset.height);-- return matrix;- }, [size, state, resolutionScale]);-}diff --git a/packages/ui/src/hooks/useViewportMatrix.ts b/packages/ui/src/hooks/useViewportMatrix.tsnew file mode 100644index 00000000..715e3145--- /dev/null+++ b/packages/ui/src/hooks/useViewportMatrix.ts@@ -0,0 +1,38 @@+import {useMemo} from 'preact/hooks';+import {useViewportContext} from '../contexts';+import {useSharedSettings} from './useSettings';++/**+ * Get a matrix that transforms the overlay canvas space to the scene space.+ *+ * @remarks+ * When drawing a viewport overlay, the canvas overlays the entire preview+ * panel, no matter the zoom and pan of the scene itself. This ensures that+ * the gizmos drawn on top have high resolution no matter how zoomed-in the user+ * is.+ *+ * This matrix is used to transform the drawn points so that they appear where+ * they should be in the scene.+ */+export function useViewportMatrix() {+ const {size} = useSharedSettings();+ const state = useViewportContext();++ return useMemo(() => {+ const matrix = new DOMMatrix();+ if (!size) {+ return matrix;+ }++ const offset = size.scale(state.resolutionScale / -2);+ const scale = state.zoom / state.resolutionScale;+ matrix.translateSelf(+ state.x + state.rect.width / 2,+ state.y + state.rect.height / 2,+ );+ matrix.scaleSelf(scale, scale);+ matrix.translateSelf(offset.width, offset.height);++ return matrix;+ }, [size, state]);+}diff --git a/packages/ui/src/plugin/EditorPlugin.ts b/packages/ui/src/plugin/EditorPlugin.tsindex 29389110..518b0683 100644--- a/packages/ui/src/plugin/EditorPlugin.ts+++ b/packages/ui/src/plugin/EditorPlugin.ts@@ -39,7 +39,7 @@ export interface PluginTabConfig {}export interface PluginDrawFunction {- (ctx: CanvasRenderingContext2D): void;+ (ctx: CanvasRenderingContext2D, matrix: DOMMatrix): void;}/**@@ -92,4 +92,8 @@ export interface EditorPlugin {* Configuration for custom overlays displayed on top of the preview.*/previewOverlay?: PluginOverlayConfig;+ /**+ * Configuration for custom overlays displayed on top of the presentation.+ */+ presenterOverlay?: PluginOverlayConfig;}diff --git a/packages/ui/src/plugin/GridPlugin/index.ts b/packages/ui/src/plugin/GridPlugin/index.tsindex 70228a8a..0bb98460 100644--- a/packages/ui/src/plugin/GridPlugin/index.ts+++ b/packages/ui/src/plugin/GridPlugin/index.ts@@ -12,8 +12,10 @@ export default makeEditorPlugin({const path = useMemo(() => {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;+ const width =+ Math.ceil(state.rect.width / 2 / GRID_SIZE + 2) * GRID_SIZE;+ const height =+ Math.ceil(state.rect.height / 2 / GRID_SIZE + 2) * GRID_SIZE;for (let i = -width; i < width; i += GRID_SIZE) {path.moveTo(i, -height);