Skip to content

Commit 6c96029

Browse files
committed
Add: accept a p5 sketch as a prop
1 parent e6be1a4 commit 6c96029

File tree

4 files changed

+43
-33
lines changed

4 files changed

+43
-33
lines changed

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { default as p5 } from './p5/p5.svelte';
1+
export { default as default } from './p5/p5.svelte';

src/p5/action.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import p5 from 'p5';
2+
3+
export default function _p5(node, workspace) {
4+
new p5(
5+
(instance) => (workspace ? workspace(instance) : fallback(instance)),
6+
node
7+
);
8+
}
9+
10+
/**
11+
* The main method
12+
*
13+
* @param {p5} p5 sketch instance, scoped where many may exist on the same page
14+
*/
15+
const fallback = (p5) => {
16+
let x = 0;
17+
let y = 0;
18+
let size = 10;
19+
let threshold = 0;
20+
21+
p5.setup = () => {
22+
p5.createCanvas(400, 400);
23+
};
24+
25+
p5.draw = () => {
26+
p5.stroke(0);
27+
threshold = p5.random(1);
28+
29+
if (threshold < 0.1) p5.line(x, y, x + size, y + size);
30+
else if (0.505 > threshold > 0.5) p5.line(x, y, x, y + size);
31+
else p5.line(x, y + size, x + size, y);
32+
33+
x = x + size;
34+
if (x > p5.width) {
35+
x = 0;
36+
y = y + size;
37+
}
38+
};
39+
};

src/p5/p5.svelte

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script>
2-
import sketch from './sketch';
2+
import _p5 from './action';
3+
export let sketch = null;
34
</script>
45

5-
<canvas use:sketch />
6+
<figure use:_p5={sketch} />

src/p5/sketch.js

-30
This file was deleted.

0 commit comments

Comments
 (0)