Skip to content

Commit 477ccc2

Browse files
authored
Include Sapper documentation
1 parent d55cb51 commit 477ccc2

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

README.md

+48
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The API is super simple; you get a <code>P5</code> component which accepts a <co
1717

1818

1919
## Usage
20+
### Svelte
2021
Install:
2122
```ps
2223
yarn add p5-svelte
@@ -66,5 +67,52 @@ Now add `p5-svelte` to your project (ex. `src/App.svelte`):
6667

6768
It's that easy!
6869

70+
### Sapper
71+
Install:
72+
```ps
73+
yarn add p5-svelte p5 -D
74+
```
75+
Now add `p5-svelte` to your project (ex. `src/App.svelte`):
76+
77+
```svelte
78+
<script>
79+
import { onMount } from "svelte";
80+
81+
let P5;
82+
83+
onMount(async () => {
84+
const module = await import("p5-svelte");
85+
P5 = module.default;
86+
});
87+
88+
let width = 55;
89+
let height = 55;
90+
91+
const sketch = (p5) => {
92+
p5.setup = () => {
93+
p5.createCanvas(400, 400);
94+
};
95+
96+
p5.draw = () => {
97+
p5.ellipse(p5.width / 2, p5.height / 2, width, height);
98+
};
99+
};
100+
</script>
101+
102+
<label>
103+
Width
104+
<input type="range" bind:value={width} min="100" max="1000" step="0.01" />
105+
{width}
106+
</label>
107+
108+
<label>
109+
Height
110+
<input type="range" bind:value={height} min="100" max="1000" step="0.01" />
111+
{height}
112+
</label>
113+
114+
<svelte:component this={P5} {sketch} />
115+
```
116+
69117
## p5.js instance mode
70118
Svelte doesn't allow us to globally expose the p5 library by installing it to the `window` (which is how p5 is commonly installed in vanilla js projects). Therefore, p5 must be used in instance mode with this component. That means you'll have to call library functions with a `p5.` preceding them like in the example above.

0 commit comments

Comments
 (0)