Git Product home page Git Product logo

svelte-canvas's Introduction

svelte-canvas

Reactive canvas rendering with Svelte.

Installation

npm install svelte-canvas

Usage

<script>
  import { Canvas, Layer, t } from 'svelte-canvas';

  $: render = ({ context, width, height }) => {
    context.fillStyle = `hsl(${$t / 40}, 100%, 50%)`;
    context.beginPath();
    context.arc(($t / 4) % width, ($t / 4) % height, 100, 0, Math.PI * 2);
    context.fill();
  };
</script>

<Canvas width={640} height={640}>
  <Layer {render} />
</Canvas>

If you use typescript, add the Render type to your reactive statement:

import { ..., type Render } from "svelte-canvas";

let render: Render;
$: render = ({ context, width, height }) => {
  // ...
}

More examples:

API

Canvas

Canvas is the top-level element. It's a Svelte wrapper around an HTML <canvas> element.

Parameters

parameter default description
width 640 Canvas width
height 640 Canvas height
pixelRatio window.devicePixelRatio Canvas pixel ratio
style null A CSS style string which will be applied to the canvas element
class null A class string which will be applied to the canvas as class="..." element
autoclear true If true, will use context.clearRect to clear the canvas at the start of each render cycle

Methods

method description
getCanvas Returns a reference to the canvas DOM element
getContext Returns the canvas's 2D rendering context
redraw Forces a re-render of the canvas

Events

All DOM events on the <canvas> element are forwarded to the Canvas component, so handling an event is as simple as <Canvas on:click={handleClick}>.

Layer

Layer is a layer to be rendered onto the canvas. It takes two props, setup and render Both take functions with a single argument that receives an object with the properties context, width, and height. context is the 2D rendering context of the parent canvas. width and height are the canvas's dimensions.

setup is optional and is called once at initialization. render is called every time the canvas redraws.

Declaring your render function reactively allows svelte-canvas to re-render anytime the values that the function depends on change.

t

t is a readable store that provides the time in milliseconds since initialization. Subscribing to t within your render function lets you easily create animations.

Render

Render is a typing for your reactive statements that provides types for the JSON object: (canvas: CanvasRenderingContext2D, width: number, height: number)

svelte-canvas's People

Contributors

dnass avatar kaisermann avatar leodog896 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.