0.0.4 • Published 2 years ago
svelte-p5js v0.0.4
svelte-p5js (still under early development)
A svelte wrapper for the p5.js library.
npm install svelte-p5jsAll props and variables are an exact copy of the names used in the reference for the p5js library.
Alternatives
Currently spported components
Components used to simplify the p5.js API.
Rendering
<Canvas />(createCanvas)
Structure
<Draw /><Setup /><Sketch /><P5 />
Setting
<Background /><Clear /><Erase /><NoErase /><Fill /><NoFill /><Stroke /><NoStroke />
Shape
<Circle /><Ellipse />
Currently supported events (available on the <P5 /> component)
All events provide the current instance via event.detail.instance.
Acceleration events
on:setMoveThresholdon:setShakeThresholdon:deviceMovedon:deviceTurnedon:deviceShaken
Keyboard events
on:keyPressedon:keyReleasedon:keyTypedon:keyIsDown
Mouse events
on:mousePressedon:mouseMovedon:mouseDraggedon:mousePressedon:mouseReleasedon:mouseClickedon:doubleClickedon:mouseWheelon:requestPointerLockon:exitPointerLock
Touch events
on:touchStartedon:touchMovedon:touchEnded
Currently supported constants (available on the <P5 /> component)
Keyboard constants
keyIsPressedkeykeyCode
Mouse constants
movedXmovedYmouseXmouseYpmouseXpmouseYwinMouseXwinMouseYpwinMouseXpwinMouseYmouseButtonmouseIsPressed
Example usage
The P5 component is the only required component. It sets up a p5 instance and must be the parent of all other components.
<script>
import { P5, Setup, Canvas, Draw, Background, Fill, Circle, Ellipse } from 'svelte-p5js';
let x = 0;
let y = 0;
let d = 10;
let mouseIsPressed;
$: console.log('Mouse is pressed?', mouseIsPressed);
// Bind the current instance of the P5 component to a variable
// and get access to the whole API
let p5;
</script>
<P5
bind:p5
setup={() => console.log('I was fired during setup')}
draw={(p5) => {
x++;
y++;
d += 0.3;
// Get access to the whole p5js api via the first
// and only "instance" parameter
p5.mousePressed = () => {
p5.noLoop();
};
}}
bind:mouseIsPressed
on:mouseReleased={(event) => {
// Get access to the current instance via the event
const instance = event.detail.instance;
p5.loop();
}}
>
<Setup>
<Canvas w={500} h={500} />
</Setup>
<Draw>
<Background v1={105} v2={140} v3={99} />
<Fill color="pink" />
<Circle {x} {y} {d} />
<Fill v1={205} v2={40} v3={99} />
<Ellipse {x} {y} w={20} h={20} />
</Draw>
</P5>Run locally
npm installnpm run dev- Use the
src/routes/+page.svelteas a playground