Visualize Your Ideas With Code
fix: use correct scene sizes (#146)
Jacobf279638f9ad7
diff --git a/packages/2d/src/components/View2D.ts b/packages/2d/src/components/View2D.tsindex f26c27d7..9ab8c9f2 100644--- a/packages/2d/src/components/View2D.ts+++ b/packages/2d/src/components/View2D.ts@@ -1,4 +1,4 @@-import {Layout} from './Layout';+import {Layout, LayoutProps} from './Layout';export class View2D extends Layout {public static frameID = 'motion-canvas-2d-frame';@@ -20,19 +20,15 @@ export class View2D extends Layout {View2D.shadowRoot = frame.shadowRoot ?? frame.attachShadow({mode: 'open'});}- public constructor() {+ public constructor(props: LayoutProps) {super({- // TODO Sync with the project size- width: 1920,- height: 1080,- x: 960,- y: 540,composite: true,fontFamily: 'Roboto',fontSize: 48,lineHeight: 64,textWrap: false,fontStyle: 'normal',+ ...props,});View2D.shadowRoot.append(this.element);diff --git a/packages/2d/src/scenes/Scene2D.ts b/packages/2d/src/scenes/Scene2D.tsindex 54542c52..cd015953 100644--- a/packages/2d/src/scenes/Scene2D.ts+++ b/packages/2d/src/scenes/Scene2D.ts@@ -22,7 +22,11 @@ export class Scene2D extends GeneratorScene<View2D> implements Inspectable {) {super(description);startScene(this);- this.view = new View2D();+ const size = this.getSize();+ this.view = new View2D({+ position: size.scale(0.5),+ size,+ });endScene(this);}@@ -49,6 +53,9 @@ export class Scene2D extends GeneratorScene<View2D> implements Inspectable {this.registeredNodes = {};this.nodeCounters = {};this.registerNode(this.view, this.view.key);+ const size = this.getSize();+ this.view.position(size.scale(0.5));+ this.view.size(size);return super.reset(previousScene);}diff --git a/packages/core/src/Project.ts b/packages/core/src/Project.tsindex abfaa488..8d74e428 100644--- a/packages/core/src/Project.ts+++ b/packages/core/src/Project.ts@@ -1,7 +1,12 @@import {FullSceneDescription, Scene} from './scenes';import {Meta, Metadata} from './Meta';import {EventDispatcher, ValueDispatcher} from './events';-import {CanvasColorSpace, CanvasOutputMimeType, Vector2} from './types';+import {+ CanvasColorSpace,+ CanvasOutputMimeType,+ PossibleVector2,+ Vector2,+} from './types';import {AudioManager} from './media';import {getContext} from './utils';import {Logger} from './Logger';@@ -119,11 +124,11 @@ export class Project {this.canvas.width =this.buffer.width =this.previousBuffer.width =- this.width * this._resolutionScale;+ this.size.x * this._resolutionScale;this.canvas.height =this.buffer.height =this.previousBuffer.height =- this.height * this._resolutionScale;+ this.size.y * this._resolutionScale;this.render();}@@ -133,22 +138,14 @@ export class Project {this.updateCanvas();}- public setSize(size: Vector2): void;- public setSize(width: number, height: number): void;- public setSize(value: Vector2 | number, height?: number): void {- if (typeof value === 'object') {- this.width = value.width;- this.height = value.height;- } else {- this.width = value;- this.height = height!;- }+ public setSize(size: PossibleVector2): void {+ this.size = new Vector2(size);this.updateCanvas();this.reloadAll();}public getSize(): Vector2 {- return new Vector2(this.width, this.height);+ return this.size;}public readonly name: string;@@ -172,9 +169,7 @@ export class Project {private previousBuffer = document.createElement('canvas');private previousBufferContext: CanvasRenderingContext2D | null = null;private exportCounter = 0;-- private width = 0;- private height = 0;+ private size = Vector2.zero;/*** @deprecated Use {@link makeProject} instead.@@ -244,8 +239,8 @@ export class Project {0,0,this._resolutionScale,- (this.width * this._resolutionScale) / 2,- (this.height * this._resolutionScale) / 2,+ (this.size.x * this._resolutionScale) / 2,+ (this.size.y * this._resolutionScale) / 2,);}@@ -268,7 +263,7 @@ export class Project {this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);this.context.restore();} else {- this.context.clearRect(0, 0, this.width, this.height);+ this.context.clearRect(0, 0, this.size.x, this.size.y);}if (this.previousScene) {diff --git a/packages/core/src/player/Player.ts b/packages/core/src/player/Player.tsindex ea9519c4..b367605e 100644--- a/packages/core/src/player/Player.ts+++ b/packages/core/src/player/Player.ts@@ -174,13 +174,11 @@ export class Player {public setScale(scale: number) {this.project.resolutionScale = scale;this.updateState({scale});- this.project.render();}public setColorSpace(colorSpace: CanvasColorSpace) {this.project.colorSpace = colorSpace;this.updateState({colorSpace});- this.project.render();}public setFileType(fileType: CanvasOutputMimeType) {diff --git a/packages/docs/docs/guides/getting-started/flow.mdx b/packages/docs/docs/guides/getting-started/flow.mdxindex d49b4eb9..cc835938 100644--- a/packages/docs/docs/guides/getting-started/flow.mdx+++ b/packages/docs/docs/guides/getting-started/flow.mdx@@ -38,14 +38,14 @@ With that in mind, we can make a circle flicker on the screen using the followin```tsxexport default makeScene2D(function* (view) {- const circle = useRef<Circle>();+ const circle = createRef<Circle>();view.add(<Circle ref={circle} width={100} height={100} />);- circle.value.fill('red');+ circle().fill('red');yield;- circle.value.fill('blue');+ circle().fill('blue');yield;- circle.value.fill('red');+ circle().fill('red');yield;});```@@ -59,10 +59,10 @@ a separate generator and delegate our scene function to it:```tsxexport default makeScene2D(function* (view) {- const circle = useRef<Circle>();+ const circle = createRef<Circle>();view.add(<Circle ref={circle} width={100} height={100} />);- yield* flicker(circle.value);+ yield* flicker(circle());});function* flicker(circle: Circle) {@@ -82,7 +82,7 @@ Motion Canvas provides a lot of useful generators like this.You may remember this snippet from [quickstart](/guides/getting-started/quickstart):```ts-yield * myCircle.value.fill('#e6a700', 1);+yield * myCircle().fill('#e6a700', 1);```It animates the fill color of the circle from its current value to `#e6a700`diff --git a/packages/docs/docs/guides/getting-started/quickstart.mdx b/packages/docs/docs/guides/getting-started/quickstart.mdxindex 4755f9c8..aa2be991 100644--- a/packages/docs/docs/guides/getting-started/quickstart.mdx+++ b/packages/docs/docs/guides/getting-started/quickstart.mdx@@ -156,13 +156,13 @@ view.add(```To animate our circle we first need to grab a reference to it.-That's the purpose of the `useRef` function.+That's the purpose of the `createRef` function.We use it to create a reference and pass it to our circle using the `ref`attribute:```tsx// highlight-next-line-const myCircle = useRef<Circle>();+const myCircle = createRef<Circle>();view.add(<Circle@@ -176,13 +176,13 @@ view.add();```-We then access the circle through `myCircle.value` and animate its properties:+We then access the circle through `myCircle()` and animate its properties:```tsxyield *all(- myCircle.value.fill('#e6a700', 1).to('#e13238', 1),- myCircle.value.position.x(300, 1).to(-300, 1),+ myCircle().fill('#e6a700', 1).to('#e13238', 1),+ myCircle().position.x(300, 1).to(-300, 1),);```@@ -205,13 +205,13 @@ For example, in the circle above we defined its `fill` property as `'#e13238'`:Using our reference we can now retrieve this property's value:```ts-const fill = myCircle.value.fill(); // '#e13238'+const fill = myCircle().fill(); // '#e13238'```We can also update it by passing the new value as the first argument:```ts-myCircle.value.fill('#e6a700');+myCircle().fill('#e6a700');```This will immediately update the color of our circle.@@ -219,7 +219,7 @@ If we want to transition to a new value over some time,we can pass the transition duration (in seconds) as the second argument:```ts-myCircle.value.fill('#e6a700', 1);+myCircle().fill('#e6a700', 1);```This creates a tween animation that smoothly changes the fill color over one@@ -233,7 +233,7 @@ different instructions we can tell the scene animation to do different things.For example, to play the tween we created, we can do:```ts-yield * myCircle.value.fill('#e6a700', 1);+yield * myCircle().fill('#e6a700', 1);```This will pause the generator, play out the animation we yielded, and then@@ -243,23 +243,23 @@ To play another animation, right after the first one, we can simply writeanother `yield*` statement:```ts-yield * myCircle.value.fill('#e6a700', 1);-yield * myCircle.value.fill('#e13238', 1);+yield * myCircle().fill('#e6a700', 1);+yield * myCircle().fill('#e13238', 1);```But since we're animating the same property,we can write it in a more compact way:```ts-yield * myCircle.value.fill('#e6a700', 1).to('#e13238', 1);+yield * myCircle().fill('#e6a700', 1).to('#e13238', 1);```In our example, aside from changing the color, we also move our circle around.We can try doing it the same way we animated the color:```ts-yield * myCircle.value.fill('#e6a700', 1).to('#e13238', 1);-yield * myCircle.value.position.x(300, 1).to(-300, 1);+yield * myCircle().fill('#e6a700', 1).to('#e13238', 1);+yield * myCircle().position.x(300, 1).to(-300, 1);```This works, but the position will start animating **after** the fill color.@@ -268,8 +268,8 @@ To make them happen at the same time, we use the `all()` function:```tsyield *all(- myCircle.value.fill('#e6a700', 1).to('#e13238', 1),- myCircle.value.position.x(300, 1).to(-300, 1),+ myCircle().fill('#e6a700', 1).to('#e13238', 1),+ myCircle().position.x(300, 1).to(-300, 1),);```diff --git a/packages/docs/docs/guides/getting-started/tweening.mdx b/packages/docs/docs/guides/getting-started/tweening.mdxindex 5964cf3b..bdf572f6 100644--- a/packages/docs/docs/guides/getting-started/tweening.mdx+++ b/packages/docs/docs/guides/getting-started/tweening.mdx@@ -53,7 +53,7 @@ to fix our animation:```tsyield* tween(2, value => {- circle.value.position.x(map(-300, 300, easeInOutCubic(value)));+ circle().position.x(map(-300, 300, easeInOutCubic(value)));});```@@ -85,7 +85,7 @@ functions. Consider the following example:```tsyield* tween(2, value => {- circle.value.color(+ circle().color(Color.lerp(new Color('#e6a700'),new Color('#e13238'),@@ -113,7 +113,7 @@ It makes the object follow a curved path from point a to b:```tsyield* tween(2, value => {- circle.value.position(+ circle().position(// highlight-next-lineVector2.arcLerp(new Vector2(-300, 200),@@ -136,7 +136,7 @@ tween:```tsyield* tween(2, value => {- circle.value.color(+ circle().color(Color.lerp(new Color('#e6a700'),new Color('#e13238'),@@ -149,7 +149,7 @@ yield* tween(2, value => {Can be written as:```ts-yield* circle.value.color('#e13238', 2);+yield* circle().color('#e13238', 2);```Here, we use a [`SignalTween`](/api/core/signals/SignalTween) signature that@@ -160,14 +160,14 @@ starts with the current value and ends with the newly provided one.We can chain multiple tweens together by calling the`to()` method on the returned object:```ts-yield* circle.value.color('#e13238', 2).to('#e6a700', 2);+yield* circle().color('#e13238', 2).to('#e6a700', 2);```By default, property tweens use `easeInOutCubic` as the timing function.We can override that by providing a third argument:```ts-yield* circle.value.color(+yield* circle().color('#e13238',2,// highlight-next-line@@ -178,7 +178,7 @@ yield* circle.value.color(Similarly, we can pass a custom interpolation function as the forth argument:```ts-yield* circle.value.position(+yield* circle().position(new Vector2(300, -200),2,easeInOutCubic,diff --git a/packages/examples/src/scenes/example.meta b/packages/examples/src/scenes/example.metadeleted file mode 100644index 0e33e3fe..00000000--- a/packages/examples/src/scenes/example.meta+++ /dev/null@@ -1,3 +0,0 @@-{- "version": 0-}\ No newline at end of filediff --git a/packages/examples/src/scenes/quickstart.tsx b/packages/examples/src/scenes/quickstart.tsxindex ecaaeb7e..ea0bf250 100644--- a/packages/examples/src/scenes/quickstart.tsx+++ b/packages/examples/src/scenes/quickstart.tsx@@ -18,7 +18,7 @@ export default makeScene2D(function* (view) {);yield* all(- myCircle.value.position.x(300, 1).to(-300, 1),- myCircle.value.fill('#e6a700', 1).to('#e13238', 1),+ myCircle().position.x(300, 1).to(-300, 1),+ myCircle().fill('#e6a700', 1).to('#e13238', 1),);});diff --git a/packages/examples/src/scenes/tweening-color.tsx b/packages/examples/src/scenes/tweening-color.tsxindex 925ad690..9ba8e690 100644--- a/packages/examples/src/scenes/tweening-color.tsx+++ b/packages/examples/src/scenes/tweening-color.tsx@@ -18,7 +18,7 @@ export default makeScene2D(function* (view) {);//highlight-startyield* tween(2, value => {- circle.value.fill(+ circle().fill(Color.lerp(new Color('#e13238'),new Color('#e6a700'),diff --git a/packages/examples/src/scenes/tweening-cubic.tsx b/packages/examples/src/scenes/tweening-cubic.tsxindex b783b371..8ba45f2e 100644--- a/packages/examples/src/scenes/tweening-cubic.tsx+++ b/packages/examples/src/scenes/tweening-cubic.tsx@@ -18,7 +18,7 @@ export default makeScene2D(function* (view) {);//highlight-startyield* tween(2, value => {- circle.value.position.x(map(-300, 300, easeInOutCubic(value)));+ circle().position.x(map(-300, 300, easeInOutCubic(value)));});//highlight-end});diff --git a/packages/examples/src/scenes/tweening-linear.tsx b/packages/examples/src/scenes/tweening-linear.tsxindex 23c3aa10..650e4446 100644--- a/packages/examples/src/scenes/tweening-linear.tsx+++ b/packages/examples/src/scenes/tweening-linear.tsx@@ -18,7 +18,7 @@ export default makeScene2D(function* (view) {);//highlight-startyield* tween(2, value => {- circle.value.position.x(map(-300, 300, value));+ circle().position.x(map(-300, 300, value));});//highlight-end});diff --git a/packages/examples/src/scenes/tweening-vector.tsx b/packages/examples/src/scenes/tweening-vector.tsxindex 5bd50a43..37ac84ad 100644--- a/packages/examples/src/scenes/tweening-vector.tsx+++ b/packages/examples/src/scenes/tweening-vector.tsx@@ -20,7 +20,7 @@ export default makeScene2D(function* (view) {);//highlight-startyield* tween(2, value => {- circle.value.position(+ circle().position(Vector2.arcLerp(new Vector2(-300, 200),new Vector2(300, -200),diff --git a/packages/player/src/main.ts b/packages/player/src/main.tsindex 1fa1529a..8f97418e 100644--- a/packages/player/src/main.ts+++ b/packages/player/src/main.ts@@ -189,7 +189,7 @@ class MotionCanvasPlayer extends HTMLElement {this.finished = false;this.project = project;this.project.resolutionScale = this.quality;- this.project.setSize(this.width, this.height);+ this.project.setSize([this.width, this.height]);this.project.logger.onLogged.subscribe(console.log);this.project.setCanvas(this.canvas);@@ -242,7 +242,7 @@ class MotionCanvasPlayer extends HTMLElement {case 'width':case 'height':if (this.project) {- this.project.setSize(this.width, this.height);+ this.project.setSize([this.width, this.height]);}break;}diff --git a/packages/template/src/scenes/example.tsx b/packages/template/src/scenes/example.tsxindex 00e61003..f1e93e41 100644--- a/packages/template/src/scenes/example.tsx+++ b/packages/template/src/scenes/example.tsx@@ -11,7 +11,7 @@ export default makeScene2D(function* (view) {);yield* waitUntil('circle');- yield* circle.value.scale(2, 2).to(1, 2);+ yield* circle().scale(2, 2).to(1, 2);yield* waitFor(5);});diff --git a/packages/ui/src/components/playback/PlaybackControls.tsx b/packages/ui/src/components/playback/PlaybackControls.tsxindex e3466ffb..bb3e57ce 100644--- a/packages/ui/src/components/playback/PlaybackControls.tsx+++ b/packages/ui/src/components/playback/PlaybackControls.tsx@@ -27,6 +27,9 @@ export function PlaybackControls() {'keydown',useCallback(event => {+ if (document.activeElement.tagName === 'INPUT') {+ return;+ }switch (event.key) {case ' ':player.togglePlayback();diff --git a/packages/ui/src/components/sidebar/Rendering.tsx b/packages/ui/src/components/sidebar/Rendering.tsxindex a553a3a4..e52701ca 100644--- a/packages/ui/src/components/sidebar/Rendering.tsx+++ b/packages/ui/src/components/sidebar/Rendering.tsx@@ -82,7 +82,7 @@ export function Rendering() {onChange={event => {const value = parseInt((event.target as HTMLInputElement).value);const {height} = player.project.getSize();- player.project.setSize(value, height);+ player.project.setSize([value, height]);}}/><Add className={styles.times} />@@ -93,7 +93,7 @@ export function Rendering() {onChange={event => {const value = parseInt((event.target as HTMLInputElement).value);const {width} = player.project.getSize();- player.project.setSize(width, value);+ player.project.setSize([width, value]);}}/></Group>diff --git a/packages/ui/src/components/timeline/Timeline.tsx b/packages/ui/src/components/timeline/Timeline.tsxindex 7c962acb..19f1abd8 100644--- a/packages/ui/src/components/timeline/Timeline.tsx+++ b/packages/ui/src/components/timeline/Timeline.tsx@@ -117,8 +117,7 @@ export function Timeline() {if (event.key !== 'f') return;const frame = player.onFrameChanged.current;const maxOffset = sizes.fullLength - sizes.viewLength;- const playheadPosition = state.framesToPixels(frame);- const scrollLeft = playheadPosition - sizes.viewLength / 2;+ const scrollLeft = state.framesToPixels(frame);const newOffset = clamp(0, maxOffset, scrollLeft);containerRef.current.scrollLeft = newOffset;setOffset(newOffset);diff --git a/packages/ui/src/components/viewport/Debug.tsx b/packages/ui/src/components/viewport/Debug.tsxindex e5cf41a2..5bc7b001 100644--- a/packages/ui/src/components/viewport/Debug.tsx+++ b/packages/ui/src/components/viewport/Debug.tsx@@ -1,33 +1,51 @@import styles from './Viewport.module.scss';-import {useCurrentScene, usePlayerState, usePlayerTime} from '../../hooks';-import {useContext, useLayoutEffect, useMemo, useRef} from 'preact/hooks';+import {+ useCurrentScene,+ usePlayerState,+ usePlayerTime,+ useSubscribable,+} from '../../hooks';+import {+ useContext,+ useLayoutEffect,+ useMemo,+ useRef,+ useState,+} from 'preact/hooks';import {ViewportContext} from './ViewportContext';import {isInspectable} from '@motion-canvas/core/lib/scenes/Inspectable';-import {useInspection} from '../../contexts';+import {useInspection, usePlayer} from '../../contexts';export function Debug() {const time = usePlayerTime();const scene = useCurrentScene();+ const player = usePlayer();const {scale} = usePlayerState();const canvasRef = useRef<HTMLCanvasElement>();const contextRef = useRef<CanvasRenderingContext2D>();const state = useContext(ViewportContext);const {inspectedElement, setInspectedElement} = useInspection();+ const [renderCount, setRenderCount] = useState(0);+ const size = player.project.getSize();const matrix = useMemo(() => {const matrix = new DOMMatrix();- if (!scene) {+ if (!size) {return matrix;}- const size = scene.getSize().scale(-0.5);+ const offset = size.scale(-0.5);matrix.translateSelf(state.x + state.width / 2, state.y + state.height / 2);matrix.scaleSelf(state.zoom * scale, state.zoom * scale);- matrix.translateSelf(size.width, size.height);+ matrix.translateSelf(offset.width, offset.height);return matrix;- }, [scene, state, scale]);+ }, [size, state, scale]);++ useSubscribable(player.onReloaded, () => setRenderCount(renderCount + 1), [+ renderCount,+ ]);useLayoutEffect(() => {contextRef.current ??= canvasRef.current.getContext('2d');@@ -44,7 +62,7 @@ export function Debug() {ctx.save();scene.drawOverlay(element, matrix, ctx);ctx.restore();- }, [matrix, scene, inspectedElement, time]);+ }, [matrix, renderCount, scene, inspectedElement, time]);return (<canvasdiff --git a/packages/ui/src/components/viewport/View.tsx b/packages/ui/src/components/viewport/View.tsxindex 4dfb2fd3..69ec26b0 100644--- a/packages/ui/src/components/viewport/View.tsx+++ b/packages/ui/src/components/viewport/View.tsx@@ -105,6 +105,9 @@ export function View() {'keydown',useCallback(event => {+ if (document.activeElement.tagName === 'INPUT') {+ return;+ }switch (event.key) {case '0': {resetZoom();diff --git a/packages/ui/src/hooks/useSubscribable.ts b/packages/ui/src/hooks/useSubscribable.tsindex babc1d36..936c0899 100644--- a/packages/ui/src/hooks/useSubscribable.ts+++ b/packages/ui/src/hooks/useSubscribable.ts@@ -10,7 +10,7 @@ export function useSubscribable<TValue, THandler extends EventHandler<TValue>>(handler: THandler,inputs: Inputs,) {- useEffect(() => event.subscribe(handler), [event, ...inputs]);+ useEffect(() => event?.subscribe(handler), [event, ...inputs]);}export function useSubscribableValue<TValue>(