diff --git a/src/lib/components/Scene.svelte b/src/lib/components/Scene.svelte index 4b1799d1..3a2bb681 100644 --- a/src/lib/components/Scene.svelte +++ b/src/lib/components/Scene.svelte @@ -6,21 +6,21 @@ showFormulasDefault?: boolean; draggables?: Draggable[]; title?: string; - children?: Snippet; + sceneChildren?: Snippet<[number, number]>; }; - { - globalState.height = height; - globalState.width = width; - }} -/> -
- {#if children} - {@render children()} + {#if sceneChildren} + {@render sceneChildren(width, height)} {:else}

Please enter some content using the Canvas2D or Canvas3D components

{/if} diff --git a/src/lib/d3/Canvas2D.svelte b/src/lib/d3/Canvas2D.svelte index 949e2380..dc86109e 100644 --- a/src/lib/d3/Canvas2D.svelte +++ b/src/lib/d3/Canvas2D.svelte @@ -33,8 +33,8 @@ formulas, controls, splitFormulas, - splitCanvas2DProps = {}, - splitCanvas3DProps = {}, + splitCanvas2DProps, + splitCanvas3DProps, children, splitCanvas2DChildren, splitCanvas3DChildren, @@ -48,10 +48,8 @@ draggables = [] }: CanvasProps = $props(); - const canvasWidth = $derived( - hasProps(splitCanvas2DProps) || hasProps(splitCanvas3DProps) - ? globalState.width / 2 - : globalState.width + const hasSplitCanvas = $derived( + splitCanvas2DChildren != undefined || splitCanvas3DChildren != undefined ); // Concat all draggables and pass them to the Scene component to be able to reset them @@ -73,17 +71,17 @@ if (urlProps.params2D.zoom2D) cameraZoom = urlProps.params2D.zoom2D; // 2d split props - if (urlProps.paramsSplit2D.position2D) + if (splitCanvas2DProps && urlProps.paramsSplit2D.position2D) splitCanvas2DProps.cameraPosition = urlProps.paramsSplit2D.position2D; - if (urlProps.paramsSplit2D.zoom2D) + if (splitCanvas2DProps && urlProps.paramsSplit2D.zoom2D) splitCanvas2DProps.cameraZoom = urlProps.paramsSplit2D.zoom2D; // 3d split props - if (urlProps.paramsSplit3D.position3D) + if (splitCanvas3DProps && urlProps.paramsSplit3D.position3D) splitCanvas3DProps.cameraPosition = urlProps.paramsSplit3D.position3D; - if (urlProps.paramsSplit3D.zoom3D) + if (splitCanvas3DProps && urlProps.paramsSplit3D.zoom3D) splitCanvas3DProps.cameraZoom = urlProps.paramsSplit3D.zoom3D; }); @@ -131,36 +129,40 @@ {formulas} {splitFormulas} > - - {@render children()} - - - {#if splitCanvas2DChildren} - - {@render splitCanvas2DChildren()} + {#snippet sceneChildren(width, height)} + {@const canvasWidth = hasSplitCanvas ? width / 2 : width} + + {@render children()} - {:else if splitCanvas3DChildren} - -
- - - - {@render splitCanvas3DChildren()} - - {#if enableEasterEgg} - - {/if} - -
- {/if} + + {#if splitCanvas2DChildren} + + {@render splitCanvas2DChildren()} + + {:else if splitCanvas3DChildren} + +
+ + + + {@render splitCanvas3DChildren()} + + {#if enableEasterEgg} + + {/if} + +
+ {/if} + {/snippet} (enableEasterEgg = !enableEasterEgg)} /> diff --git a/src/lib/d3/CanvasD3.svelte b/src/lib/d3/CanvasD3.svelte index b4e69cb3..28e660e6 100644 --- a/src/lib/d3/CanvasD3.svelte +++ b/src/lib/d3/CanvasD3.svelte @@ -8,6 +8,7 @@ tickLength?: number; // TODO: move axis to separate component showAxisNumbers?: boolean; width: number; + height: number; enablePan?: boolean; draggables?: Draggable[]; isSplit?: boolean; @@ -42,6 +43,7 @@ tickLength, showAxisNumbers, width, + height, enablePan = true, draggables = [], isSplit = false, @@ -50,8 +52,6 @@ let id = 'canvas-' + generateUUID(); - let height = $derived(globalState.height); - function update2DCamera(transform2d: Transform2D) { // Update camera if (isSplit) cameraState.splitCamera2D = Camera2D.new(transform2d, enablePan); @@ -65,6 +65,8 @@ * @param transform {x: number, y: number, k: number} - k is zoom */ function transformScene(transform: Transform2D) { + if (!transform.k) return; + if (enablePan) { select(`#${id} g`).attr('transform', transform).attr('transform-origin', '0 0'); } else { diff --git a/src/lib/stores/globalState.svelte.ts b/src/lib/stores/globalState.svelte.ts index cf8b857a..92e1dd14 100644 --- a/src/lib/stores/globalState.svelte.ts +++ b/src/lib/stores/globalState.svelte.ts @@ -35,18 +35,6 @@ class GlobalState { */ title = $state(''); - /** - * Represents the width of the application. - * @type {number} - */ - width = $state(0); - - /** - * Represents the height of the application. - * @type {number} - */ - height = $state(0); - /** * Represents when the application should be reset. */ diff --git a/src/lib/stories/Canvas2D.stories.svelte b/src/lib/stories/Canvas2D.stories.svelte index c1deafc9..e1e15ceb 100644 --- a/src/lib/stories/Canvas2D.stories.svelte +++ b/src/lib/stories/Canvas2D.stories.svelte @@ -47,7 +47,14 @@ {#snippet template(args)} diff --git a/src/lib/stories/Canvas3D.stories.svelte b/src/lib/stories/Canvas3D.stories.svelte index 1edc71ee..fe476db3 100644 --- a/src/lib/stories/Canvas3D.stories.svelte +++ b/src/lib/stories/Canvas3D.stories.svelte @@ -31,10 +31,16 @@ {#snippet template(args)} diff --git a/src/lib/threlte/Camera3D.svelte b/src/lib/threlte/Camera3D.svelte index 3915c2ac..13630b8d 100644 --- a/src/lib/threlte/Camera3D.svelte +++ b/src/lib/threlte/Camera3D.svelte @@ -92,8 +92,6 @@ // Function that changes the camera State for 3D camera // Updates when the camera "changes" function handleCameraChange() { - console.log('update'); - const cam = $camera as OrthographicCamera; const splitCamera3D = new Camera3D(cam); diff --git a/src/lib/threlte/Canvas3D.svelte b/src/lib/threlte/Canvas3D.svelte index 6a1679e9..96638084 100644 --- a/src/lib/threlte/Canvas3D.svelte +++ b/src/lib/threlte/Canvas3D.svelte @@ -34,8 +34,8 @@ formulas, controls, splitFormulas, - splitCanvas2DProps = {}, - splitCanvas3DProps = {}, + splitCanvas2DProps, + splitCanvas3DProps, children, splitCanvas2DChildren, splitCanvas3DChildren, @@ -46,10 +46,8 @@ enablePan = false }: CanvasProps = $props(); - const canvasWidth = $derived( - hasProps(splitCanvas2DProps) || hasProps(splitCanvas3DProps) - ? globalState.width / 2 - : globalState.width + const hasSplitCanvas = $derived( + splitCanvas2DChildren != undefined || splitCanvas3DChildren != undefined ); // Concat all draggables and pass them to the Scene component to be able to reset them @@ -71,17 +69,17 @@ if (urlProps.params3D.zoom3D) cameraZoom = urlProps.params3D.zoom3D; // 2d split props - if (urlProps.paramsSplit2D.position2D) + if (splitCanvas2DProps && urlProps.paramsSplit2D.position2D) splitCanvas2DProps.cameraPosition = urlProps.paramsSplit2D.position2D; - if (urlProps.paramsSplit2D.zoom2D) + if (splitCanvas2DProps && urlProps.paramsSplit2D.zoom2D) splitCanvas2DProps.cameraZoom = urlProps.paramsSplit2D.zoom2D; // 3d split props - if (urlProps.paramsSplit3D.position3D) + if (splitCanvas3DProps && urlProps.paramsSplit3D.position3D) splitCanvas3DProps.cameraPosition = urlProps.paramsSplit3D.position3D; - if (urlProps.paramsSplit3D.zoom3D) + if (splitCanvas3DProps && urlProps.paramsSplit3D.zoom3D) splitCanvas3DProps.cameraZoom = urlProps.paramsSplit3D.zoom3D; }); @@ -117,41 +115,44 @@ {formulas} {splitFormulas} > -
- {#if confettiState.side === 'left' || confettiState.side === 'center'} - - {/if} - - - - {@render children()} - - {#if enableEasterEgg} - - {/if} - -
- - {#if splitCanvas2DChildren} - - {@render splitCanvas2DChildren()} - - {:else if splitCanvas3DChildren} -
- {#if confettiState.side === 'right'} - + {#snippet sceneChildren(width, height)} + {@const canvasWidth = hasSplitCanvas ? width / 2 : width} +
+ {#if confettiState.side === 'left' || confettiState.side === 'center'} + {/if} - + - {@render splitCanvas3DChildren()} + {@render children()} {#if enableEasterEgg} {/if}
- {/if} + + {#if splitCanvas2DChildren} + + {@render splitCanvas2DChildren()} + + {:else if splitCanvas3DChildren} +
+ {#if confettiState.side === 'right'} + + {/if} + + + + {@render splitCanvas3DChildren()} + + {#if enableEasterEgg} + + {/if} + +
+ {/if} + {/snippet} (enableEasterEgg = !enableEasterEgg)} />