Skip to content

Commit

Permalink
Add: accept a p5 sketch as a prop
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyketcham committed Jan 16, 2021
1 parent e6be1a4 commit 6c96029
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default as p5 } from './p5/p5.svelte';
export { default as default } from './p5/p5.svelte';
39 changes: 39 additions & 0 deletions src/p5/action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import p5 from 'p5';

export default function _p5(node, workspace) {
new p5(
(instance) => (workspace ? workspace(instance) : fallback(instance)),
node
);
}

/**
* The main method
*
* @param {p5} p5 sketch instance, scoped where many may exist on the same page
*/
const fallback = (p5) => {
let x = 0;
let y = 0;
let size = 10;
let threshold = 0;

p5.setup = () => {
p5.createCanvas(400, 400);
};

p5.draw = () => {
p5.stroke(0);
threshold = p5.random(1);

if (threshold < 0.1) p5.line(x, y, x + size, y + size);
else if (0.505 > threshold > 0.5) p5.line(x, y, x, y + size);
else p5.line(x, y + size, x + size, y);

x = x + size;
if (x > p5.width) {
x = 0;
y = y + size;
}
};
};
5 changes: 3 additions & 2 deletions src/p5/p5.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script>
import sketch from './sketch';
import _p5 from './action';
export let sketch = null;
</script>

<canvas use:sketch />
<figure use:_p5={sketch} />
30 changes: 0 additions & 30 deletions src/p5/sketch.js

This file was deleted.

0 comments on commit 6c96029

Please sign in to comment.