npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

p5-svelte

v3.1.2

Published

A component for easily using p5 sketches in Svelte. Allows using Svelte's reactivity system in p5.

Downloads

609

Readme

Trying to get p5 up and running in Svelte can be a pain. So here's an absolutely dead simple way of tossing it into your project.

The API is super simple; you get a P5 component which accepts a sketch prop. You can make use of Svelte's reactivity system to bind props or params within your p5 sketch just as you would with regular Svelte! You can even have multiple p5 components per page without any scoping issues!

Usage

Install:

pnpm i p5-svelte

Depending on your environment, you may be alerted upon installing p5-svelte that p5 is a required peer dependency which you must install yourself. Thus do:

pnpm i -D p5

Now add p5-svelte to your project (ex. src/App.svelte):

<script>
	import P5 from 'p5-svelte';
	let width = 55;
	let height = 55;

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

		p5.draw = () => {
			p5.ellipse(p5.width / 2, p5.height / 2, width, height);
		};
	};
</script>

<label>
	Width
	<input type="range" bind:value={width} min="100" max="1000" step="0.01" />
	{width}
</label>

<label>
	Height
	<input type="range" bind:value={height} min="100" max="1000" step="0.01" />
	{height}
</label>

<P5 {sketch} />

Output:

Available Props

| prop | description | type | default | required? | | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------- | ------------- | ------------------- | --------- | | sketch | Your p5 sketch in instance mode | Sketch | undefined | yes | | target | An HTML element/node to mount the p5 canvas onto giving you full control of the parent wrapper that p5 renders a canvas as a child to | HTMLElement | undefined | no | | parentDivStyle | CSS Styling for the underlying div target contained in <P5/>. Note: this will do nothing when paired with a target prop | string | 'display: block;' | no | | debug | Logs everything about the p5 instance to the console for debugging purposes | boolean | false | no |

p5.js instance mode

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.

Debug Mode

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 />

Events

The <P5/> component emits a few events you can listen to.

ref

Emits a reference to the DOM element target which the p5 instance mounts to.

instance

This event fires on init of the p5 project instance, emitting a reference to that p5 project instance object.

TypeScript support + Intellisense

p5-svelte exposes a 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.

😤 Before

https://user-images.githubusercontent.com/43280336/161372626-32634e5b-37b1-4f56-b708-518200b1021f.mov

🧙‍♀️ After

https://user-images.githubusercontent.com/43280336/161373024-01c1a01d-b9ea-4ce4-8787-c42329a50ff6.mov