### Basic Stage, Layer, and Rect Setup Source: https://github.com/konvajs/svelte-konva/blob/master/README.md A minimal example demonstrating how to set up a Stage, Layer, and a Rect component in svelte-konva. ```svelte ``` -------------------------------- ### Install svelte-konva and konva Source: https://github.com/konvajs/svelte-konva/blob/master/README.md Install the necessary packages using npm. ```bash npm i svelte-konva konva ``` -------------------------------- ### Basic Stage, Layer, and Rect Setup in Svelte Source: https://context7.com/konvajs/svelte-konva/llms.txt Demonstrates the basic setup of a Stage, Layer, and Rect component in svelte-konva. The Stage component manages the canvas container and exposes its Konva instance via bind:this. It also shows how to handle click events on the stage. ```svelte ``` -------------------------------- ### Svelte Konva v1 Config Prop Reactivity Example (Svelte 5 Runes) Source: https://github.com/konvajs/svelte-konva/blob/master/docs/svelte-konva-v1-migration.md In v1, individual props replace the single `config` object, enabling fine-grained reactivity. Changes by Konva no longer 'leak' into parent state unless explicitly bound. This example demonstrates the new behavior with Svelte 5 runes. ```svelte ``` -------------------------------- ### Svelte Konva v0 Config Prop Reactivity Example Source: https://github.com/konvajs/svelte-konva/blob/master/docs/svelte-konva-v1-migration.md In v0, the `config` prop was a single object. Changes made by Konva (e.g., on dragend) would 'leak' into the parent's object, causing issues with Svelte's reactivity unless the prop was bound. This example shows the old behavior. ```svelte ``` -------------------------------- ### Animate Sprites with Sprite Sheet in Svelte Konva Source: https://context7.com/konvajs/svelte-konva/llms.txt Use the `Sprite` component with an `image`, `animations` map, `animation` name, and `frameRate`. Load the image using `onMount` and start the animation with `spriteRef?.node.start()`. ```svelte {#if spriteSheet} {/if} ``` -------------------------------- ### Multiple Layers with Shapes in Svelte Source: https://context7.com/konvajs/svelte-konva/llms.txt Illustrates the use of multiple Layer components within a Stage to manage different drawing areas and z-ordering. This example shows a background layer with a Rect and a foreground layer with a Circle and another Rect. ```svelte ``` -------------------------------- ### Svelte Konva Event Handler Syntax Change Source: https://github.com/konvajs/svelte-konva/blob/master/docs/svelte-konva-v1-migration.md In v1, the `on:event` syntax for Konva events is deprecated. Use the `on` callback prop syntax instead. This example shows the migration from the old to the new syntax. ```diff ``` -------------------------------- ### Attach Transformer to Shapes in Svelte Konva Source: https://context7.com/konvajs/svelte-konva/llms.txt Use `bind:this` to get the Transformer instance and `transformer.node.nodes([...])` to attach it to target shapes. Deselect shapes by clicking on the stage. ```svelte selectShape('rect', rectRef)} /> selectShape('circle', circleRef)} /> ``` -------------------------------- ### Svelte Konva Event Payload Change Source: https://github.com/konvajs/svelte-konva/blob/master/docs/svelte-konva-v1-migration.md The event payload structure has changed in v1. Konva event data is now directly accessible, not wrapped in `e.detail`. This example illustrates how to access the event type in the handler function. ```diff function handleClick(e) { - const konvaEvent = e.detail; - window.alert(`Clicked on rectangle: ${konvaEvent.type}`); + window.alert(`Clicked on rectangle: ${e.type}`); } ``` -------------------------------- ### Load and Display an Image Source: https://context7.com/konvajs/svelte-konva/llms.txt Demonstrates loading an image using `onMount` and displaying it on the canvas. Requires importing `onMount` and Konva components. The image source must be accessible. ```svelte {#if imgElement} {/if} ``` -------------------------------- ### Implement Freehand Drawing with Lines Source: https://context7.com/konvajs/svelte-konva/llms.txt Shows how to create a freehand drawing tool using the Line component. It captures pointer events on the Stage to draw continuous lines. Requires Stage, Layer, and Line imports. ```svelte {#each strokes as stroke} {/each} ``` -------------------------------- ### Render and Interact with a Circle Source: https://context7.com/konvajs/svelte-konva/llms.txt Demonstrates rendering a draggable circle with dynamic fill color changes on click. Requires importing Stage, Layer, and Circle from svelte-konva. ```svelte ``` -------------------------------- ### Bind Individual Props from Config Object Source: https://github.com/konvajs/svelte-konva/blob/master/docs/svelte-konva-v1-migration.md Migrate from `bind:config` to spreading the config object and binding individual properties like `x` and `y`. This allows for fine-grained reactivity and clearer control over which properties are two-way bound. ```html ``` -------------------------------- ### Dynamically Import svelte-konva Stage in SvelteKit Source: https://github.com/konvajs/svelte-konva/blob/master/README.md A recommended approach for larger projects to dynamically import svelte-konva components on the client-side only. This prevents errors during SSR or prerendering. ```html ``` ```html

This is my fancy server side rendered (or prerendered) page.

{#await MyCanvas}

Loading...

{:then Component} {:catch error}

Something went wrong: {error.message}

{/await}
``` -------------------------------- ### Create Tooltip Labels with Label and Tag in Svelte Konva Source: https://context7.com/konvajs/svelte-konva/llms.txt The `Label` component must contain `Tag` first, followed by `Text`. Configure `Tag` for background and pointer, and `Text` for the label content. ```svelte ``` -------------------------------- ### Spread Old Config Object to Individual Props Source: https://github.com/konvajs/svelte-konva/blob/master/docs/svelte-konva-v1-migration.md Use the spread syntax `{...config}` to pass properties from an existing config object to individual props. This is a common pattern for migrating from the v0 `config` prop. ```html ``` -------------------------------- ### Wrap svelte-konva Components in Browser Checks Source: https://github.com/konvajs/svelte-konva/blob/master/README.md Use this method for small projects to conditionally render svelte-konva components only in the browser. Ensure '$app/environment' is imported. ```html {#if browser} {/if} ``` -------------------------------- ### Accessing Konva Stage Node Immediately Source: https://github.com/konvajs/svelte-konva/blob/master/docs/svelte-konva-v1-migration.md The Konva `node` is now available immediately after component instantiation using `bind:this`. No need to wait for `tick()`. ```svelte ``` -------------------------------- ### Handling Konva Events with Callbacks Source: https://github.com/konvajs/svelte-konva/blob/master/README.md Listen to Konva events using callback props named `on{konva event name}`. All Konva events are supported. ```svelte ``` -------------------------------- ### Previous Konva Stage Access with tick() Source: https://github.com/konvajs/svelte-konva/blob/master/docs/svelte-konva-v1-migration.md In previous versions, `tick()` was required to wait for the Konva stage handle to be defined after DOM rendering. ```svelte ``` -------------------------------- ### Display and Interact with Text Source: https://context7.com/konvajs/svelte-konva/llms.txt Renders text on the canvas with customizable font properties and allows interaction. Imports Stage, Layer, and Text from svelte-konva. ```svelte (message = 'Clicked!')} /> ``` -------------------------------- ### Group and Transform Multiple Shapes Source: https://context7.com/konvajs/svelte-konva/llms.txt Illustrates grouping multiple shapes (Circles and Rect) into a single `Group` component that can be dragged together. Imports Stage, Layer, Group, Circle, and Rect. ```svelte

Group position: {groupX}, {groupY}

``` -------------------------------- ### Binding Config Props for State Synchronization Source: https://github.com/konvajs/svelte-konva/blob/master/README.md Bind props like `x` and `y` to synchronize component state with the Konva node's internal state after drag or transform events. Use `staticConfig` to disable internal syncing. ```svelte ``` -------------------------------- ### Browser-Safe Dynamic Import for SvelteKit SSR Source: https://context7.com/konvajs/svelte-konva/llms.txt Use a browser-conditional dynamic import in SvelteKit to load svelte-konva components only on the client-side, preventing server-side errors. ```svelte

My App

{#await MyCanvas}

Loading canvas…

{:then Component} {:catch err}

Failed to load canvas: {err.message}

{/await}
``` ```svelte

{title}

``` -------------------------------- ### Access Underlying Konva Node via `bind:this` Source: https://context7.com/konvajs/svelte-konva/llms.txt Access the underlying Konva node immediately via the `node` property on the component instance using `bind:this`. This avoids the need for `await tick()`. ```svelte ``` -------------------------------- ### Create Rotating Star Shapes in Svelte Konva Source: https://context7.com/konvajs/svelte-konva/llms.txt The `Star` component allows configuration of `numPoints`, `innerRadius`, and `outerRadius`. Rotation can be controlled via a bound variable. ```svelte ``` -------------------------------- ### Stage Component Source: https://context7.com/konvajs/svelte-konva/llms.txt The `Stage` component is the root element for your canvas. It creates a `
` wrapper and exposes the Konva.Stage instance via the `node` property. It accepts `divWrapperProps` to customize the wrapper `
`. ```APIDOC ## Stage Component ### Description The `Stage` component is the top-level component that creates a `
` wrapper for the Konva canvas. It exposes the underlying `Konva.Stage` instance immediately via the `node` property. The `divWrapperProps` prop allows forwarding arbitrary attributes to the wrapper `
`. ### Props - `width` (number) - The width of the canvas. - `height` (number) - The height of the canvas. - `bind:this` - Binds the Konva.Stage instance to a variable. - `onclick` - Callback for stage click events. - `divWrapperProps` (object) - Props to be applied to the wrapper `
`. ### Example ```svelte ``` ``` -------------------------------- ### Accessing Konva Node with bind:this Source: https://github.com/konvajs/svelte-konva/blob/master/docs/svelte-konva-v1-migration.md Access the underlying Konva node using `bind:this` on the component and accessing the `node` property. The `handle` prop has been removed and renamed to `node`. ```diff ``` -------------------------------- ### Layer Component Source: https://context7.com/konvajs/svelte-konva/llms.txt The `Layer` component represents a drawing layer within the `Stage`. It must be a direct child of `Stage` and supports bindable transform props like `x`, `y`, `rotation`, etc. ```APIDOC ## Layer Component ### Description The `Layer` component is a drawing layer that must be a direct child of `Stage`. Multiple layers can be used for z-ordering. It supports bindable transform props. ### Props - `bind:this` - Binds the Konva.Layer instance to a variable. - `x` (number) - X position of the layer. - `y` (number) - Y position of the layer. - `scaleX` (number) - X scale of the layer. - `scaleY` (number) - Y scale of the layer. - `rotation` (number) - Rotation of the layer in degrees. - `skewX` (number) - Skew on the X axis. - `skewY` (number) - Skew on the Y axis. ### Example ```svelte ``` ``` -------------------------------- ### Rect Component Source: https://context7.com/konvajs/svelte-konva/llms.txt The `Rect` component renders a rectangle shape on the canvas. It accepts all `Konva.Rect` configuration properties as Svelte props and supports bindable transform properties. ```APIDOC ## Rect Component ### Description Renders a rectangle shape. Accepts all `Konva.Rect` config props and supports bindable transform props. ### Props - `x` (number) - X position of the rectangle. - `y` (number) - Y position of the rectangle. - `width` (number) - Width of the rectangle. - `height` (number) - Height of the rectangle. - `fill` (string) - Fill color of the rectangle. - `stroke` (string) - Stroke color of the rectangle. - `strokeWidth` (number) - Stroke width of the rectangle. - `cornerRadius` (number) - Corner radius of the rectangle. - `draggable` (boolean) - Enables dragging of the rectangle. - `shadowBlur` (number) - Blur radius of the shadow. - `shadowColor` (string) - Color of the shadow. - `ondragend` - Callback for drag end event. - `bind:x` - Binds the x position. - `bind:y` - Binds the y position. ### Example ```svelte console.log('Drag ended', e.target.x())} /> ``` ``` -------------------------------- ### Draw Directed Arrows with Arrow in Svelte Konva Source: https://context7.com/konvajs/svelte-konva/llms.txt The `Arrow` component uses `points` similar to `Line`, with configurable `pointerLength`, `pointerWidth`, and `pointerAtBeginning` for double-headed arrows. ```svelte ``` -------------------------------- ### Disable Automatic Prop Synchronization with `staticConfig` Source: https://context7.com/konvajs/svelte-konva/llms.txt Pass `staticConfig` to disable automatic synchronization of bindable props with Konva's internal state on `dragend` and `transformend`. This reduces Svelte reactivity overhead. ```svelte ``` -------------------------------- ### Importing and Using Konva Event Types in svelte-konva Source: https://context7.com/konvajs/svelte-konva/llms.txt Import Konva event types for precise event handling in your Svelte components. Use KonvaEventHooks to type props for custom wrapper components, ensuring type safety for event callbacks. ```typescript import type { KonvaMouseEvent, KonvaWheelEvent, KonvaTouchEvent, KonvaPointerEvent, KonvaDragTransformEvent, KonvaEventHooks } from 'svelte-konva'; // KonvaEventHooks can be used to type a props object for a custom wrapper component type MyShapeProps = { x: number; y: number; } & KonvaEventHooks; ``` -------------------------------- ### Update Config Prop to Individual Props in svelte-konva Source: https://github.com/konvajs/svelte-konva/blob/master/docs/svelte-konva-v1-migration.md Migrate from using a single `config` object to individual props for Konva node properties. This change aligns with react-konva's approach. ```html ``` -------------------------------- ### Draggable Rectangle with Bound Position in Svelte Source: https://context7.com/konvajs/svelte-konva/llms.txt Shows how to create a draggable Rect component in svelte-konva. The x and y properties of the rectangle are bound to Svelte state variables, allowing for two-way data binding. The dragend event is logged to the console. ```svelte console.log('Drag ended', e.target.x())} /> ``` -------------------------------- ### Pass Props to Wrapper Div in Svelte Konva Source: https://github.com/konvajs/svelte-konva/blob/master/docs/svelte-konva-v1-migration.md Use the `divWrapperProps` prop on the `Stage` component to pass attributes like `id` and `class` to the wrapper div. This replaces the previous method of using rest props. ```svelte ``` -------------------------------- ### Handle Konva Events with Typed Callbacks Source: https://context7.com/konvajs/svelte-konva/llms.txt Use typed `on` props for all svelte-konva components. The payload is the raw `KonvaEventObject`. Event bubbling can be cancelled by setting `e.cancelBubble = true`. ```svelte add(`Enter ${e.target.getType()}`)} onmouseleave={(e: KonvaMouseEvent) => add(`Leave`)} onclick={(e: KonvaMouseEvent) => add(`Click at ${e.evt.clientX},${e.evt.clientY}`)} ondblclick={(e: KonvaMouseEvent) => add(`Double-click`)} ondragstart={(e: KonvaDragTransformEvent) => add(`Drag start`)} ondragend={(e: KonvaDragTransformEvent) => { add(`Drag end x=${e.target.x().toFixed(0)}`); }} oncontextmenu={(e: KonvaPointerEvent) => { e.evt.preventDefault(); add('Context menu'); }} /> add(`Touch start`)} ontouchend={(e: KonvaTouchEvent) => add(`Touch end`)} onpointerclick={(e: KonvaPointerEvent) => { e.cancelBubble = true; // stop bubbling to Stage add('Pointer click (bubble stopped)'); }} />
    {#each log as entry}
  • {entry}
  • {/each}
``` -------------------------------- ### Cancel Event Bubbling in svelte-konva Source: https://github.com/konvajs/svelte-konva/blob/master/docs/svelte-konva-v1-migration.md To cancel event bubbling, set `e.cancelBubble = true` on the event payload. `e.preventDefault()` is no longer supported for this purpose. ```diff function handleClick(e) { window.alert(`Clicked on rectangle: ${e.type}`); - e.preventDefault(); + e.cancelBubble = true; } ``` -------------------------------- ### Accessing Underlying Konva Node Source: https://github.com/konvajs/svelte-konva/blob/master/README.md Access the underlying Konva node of a svelte-konva component using the `node` property. This is useful for direct manipulation or serialization. ```svelte ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.