Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/macfja/svelte-p5

Small wrapper around P5 for Svelte
https://github.com/macfja/svelte-p5

p5js svelte svelte-component sveltejs

Last synced: 11 days ago
JSON representation

Small wrapper around P5 for Svelte

Awesome Lists containing this project

README

        

# Svelte P5 wrapper

The small wrapper allow the usage of p5*js in Svelte

## Installation

```
npm install @macfja/svelte-p5
```

## Usage & Examples

The component (``) execute every functions that are pass as a p5*js function.
Every function have access to 3 parameters:
- 1st parameter: The current sketch context (`createVector`, `circle`, `fill`, etc.)
- 2nd parameter: The map of p5*js classes (`Color`, `Geometry`, `Shader`, etc.)
- 3rd parameter: The map of p5*js constants (`BURN`, `DEG_TO_RAD`, `PI`, `RIGHT_ARROW`, etc.)

```html

import P5 from "@macfja/svelte-p5"

function setup(p5, classes, constants) {
p5.createCanvas(400, 400);
}

function draw(p5, classes, constants) {
p5.background(220);
p5.ellipse(50,50,80,80);
}

```

```html

import P5 from "@macfja/svelte-p5"

let y = 0;
let sketch = {
setup: p5 => {
p5.createCanvas(720, 400);
p5.stroke(255); // Set line drawing color to white
p5.frameRate(30);
},
draw: p5 => {
p5.background(0); // Set the background to black
y = y - 1;
if (y < 0) {
y = p5.height;
}
p5.line(0, y, p5.width, y);
}
}

```

```html

import P5 from "@macfja/svelte-p5"

let y = 0;
let speed = 30;
let current;
let sketch = {
setup: p5 => {
p5.createCanvas(720, 400);
p5.stroke(255); // Set line drawing color to white
},
draw: (p5) => {
p5.frameRate(speed);
p5.background(0); // Set the background to black
y = y - 1;
if (y < 0) {
y = p5.height;
}
p5.line(0, y, p5.width, y);
current = p5.frameRate()
}
}

label * {
vertical-align: middle;
}

Speed (frame rate): {speed} (Current framerate: {Math.round((current + Number.EPSILON) * 100) / 100})

```
([REPL for the 3 example above](https://svelte.dev/repl/5501d3af4d8e4c2caa17324c9724a8f8?version=3.29.4))

## Contributing

Contributions are welcome. Please open up an issue or create PR if you would like to help out.

Read more in the [Contributing file](CONTRIBUTING.md)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.