Easily add p5 sketches to a Svelte project 🍛 🌱
A major pain-point while writing sketches with p5-svelte has been the lack of passthrough to p5's types. You and your editor were both flying blind. It was up to you to install and import p5's types - a bit of a clunky DX.
This PR integrates p5's types throughout the <P5/>
component (+ the docs site 🖍️ ) and exposes a new Sketch
type representing the p5 sketch in instance mode. This gives your editor prying eyes into p5's type system, supports autocomplete, and inline documentation of all the p5 goodies to make your life a little easier.
We've also re-exported the p5
type directly from p5 to give you a convenient single point of entry into the p5.js type system in advanced, modular sketches.
Here are the updated component props & their types:
// Component props
export let target: HTMLElement = undefined;
export let sketch: Sketch = undefined;
export let parentDivStyle: string = 'display: block;';
export let debug = false;
project
prop. Closes #124
docs
section on TypeScript & Intellisense.Check out all the updates on the p5-svelte docs site!
We now have updated branding around p5-svelte thanks to the great work of @rbrdl who made the new logo, redesigned the docs site theme, and added more examples to explore p5-svelte with!
I'm delighted by the growing community around the project and some of the amazing work I've come across that's created with this project!
If you'd like to contribute to the project, we welcome more examples especially those highlighting how powerful Svelte's reactivity system can be leveraged in p5-svelte. We'd also like to increase the third-party library compatibility with p5-svelte, adapting great projects like p5.riso and more.
This release involved a heavy refactor under the hood! p5-svelte should play nicely in whatever framework you use it in now - Svelte, SvelteKit, etc.
There were no breaking changes for typical use-cases but some popular bug workarounds are now obsolete and may no longer work as expected.
p5-svelte
within onMount
is no longer necessary for Sapper/SvelteKit.p5.Vector
)You can access the internals of your p5 instance and the available native classes that p5-svelte
automatically makes available to your project via passing the debug
prop:
<P5 {sketch} debug/>
<P5/>
componentp5-svelte
now fires off a few events you can listen to on a <P5/>
component.
ref
dispatches a reference to the DOM element target which the p5 instance mounts to.init
fires on init of the p5 project instance, dispatching a reference to that p5 project instance object.There's now a documentation site that'll be continually built out with installation tutorials and usage examples: https://p5-svelte.netlify.app/
Sapper installation and example now included!
Rollup was previously bundling the minified p5 lib with the compiled js which is totally unnecessary given that p5 is a peer dependency.
Now that's not a problem. The new unpacked size of the build is optimized down to only 26.4 kB. Just for comparison, it was previously 1.25 MB. That's a whopping 97.89% savings in bundle size!
You no longer need to manually declare p5
as a dependency. Now simply installing this component will ensure that p5 is automatically installed alongside it. npm install p5-svelte
or yarn add p5-svelte
is all ya need!
https://github.com/tonyketcham/p5-svelte/compare/v2.2.0-0...v2.2.0
https://github.com/tonyketcham/p5-svelte/compare/v2.1.0...v2.2.0-0
The component now shouldn't come with any CSS to fight. I discovered that the browser liked to give the P5
component's figure
element some annoying default margin and display: block
, so I baked in an override that sets it to display: inline
and margin: 0
.
Now you should have smooth sailing to wrap any additive styles you want around the P5
component!
https://github.com/tonyketcham/p5-svelte/compare/v2.0.1...v2.1.0
Restructured the default export of the component such that it can be simply used with a custom sketch.
Here's an example:
yarn add p5-svelte
src/App.svelte
:
<script>
import P5 from 'p5-svelte';
const sketch = (p5) => {
let x = 0;
let y = 0;
let size = 15;
let threshold = 0;
p5.setup = () => {
p5.createCanvas(400, 400);
};
p5.draw = () => {
p5.stroke(1);
threshold = p5.random(0.75);
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;
}
};
};
</script>
<P5 {sketch} />
1fbb635: 2.0.0 6c96029: Add: accept a p5 sketch as a prop e6be1a4: Add: output.globals for p5 module