All Projects → tonyketcham → p5-svelte

tonyketcham / p5-svelte

Licence: MIT License
Easily add p5 sketches to a Svelte project 🍛 🌱

Programming Languages

Svelte
593 projects
typescript
32286 projects
javascript
184084 projects - #8 most used programming language
CSS
56736 projects
HTML
75241 projects

Projects that are alternatives of or similar to p5-svelte

ui-svelte
A component library for Svelte
Stars: ✭ 18 (-80.85%)
Mutual labels:  svelte-components, svelte
svelte-lottie-player
Lottie Player component for Svelte
Stars: ✭ 90 (-4.26%)
Mutual labels:  svelte-components, svelte
svelte-progressbar
A multiseries, SVG progressbar component made with Svelte
Stars: ✭ 85 (-9.57%)
Mutual labels:  svelte-components, svelte
svelte-flat-ui
Flat UI Independent JS Components + English Docs =
Stars: ✭ 45 (-52.13%)
Mutual labels:  svelte-components, svelte
generative
A digital playground for experimenting with creative coding and WebGL
Stars: ✭ 50 (-46.81%)
Mutual labels:  p5, generative-art
tabler-icons-svelte
A library of SVG Svelte components for Tabler Icons.
Stars: ✭ 50 (-46.81%)
Mutual labels:  svelte-components, svelte
Svelte Material Ui
Svelte Material UI Components
Stars: ✭ 2,081 (+2113.83%)
Mutual labels:  svelte-components, svelte
svelte-headlessui
Unofficial Svelte port of Headless UI components
Stars: ✭ 564 (+500%)
Mutual labels:  svelte-components, svelte
s-date-range-picker
📅 A date range picker built with Svelte
Stars: ✭ 13 (-86.17%)
Mutual labels:  svelte-components, svelte
kahi-ui
Straight-forward Svelte UI for the Web
Stars: ✭ 169 (+79.79%)
Mutual labels:  svelte-components, svelte
sapper-template-rollup
Starter Rollup template for Sapper apps using postcss, cssnano, tailwindcss, and svelte-preprocess.
Stars: ✭ 32 (-65.96%)
Mutual labels:  svelte-components, svelte
carbon-components-svelte
Svelte implementation of the Carbon Design System
Stars: ✭ 1,615 (+1618.09%)
Mutual labels:  svelte-components, svelte
fa-svelte
Font Awesome 5 for svelte.js
Stars: ✭ 72 (-23.4%)
Mutual labels:  svelte-components, svelte
svelte-material
Modular and customizable Material Design UI components for Svelte.js
Stars: ✭ 30 (-68.09%)
Mutual labels:  svelte-components, svelte
focus-svelte
focus lock for svelte
Stars: ✭ 18 (-80.85%)
Mutual labels:  svelte-components, svelte
svelte-fast-dimension
Fast dimension bindings using ResizeObservers
Stars: ✭ 23 (-75.53%)
Mutual labels:  svelte
svelte-hamburgers
Custom Hamburger Icons in Svelte with ease
Stars: ✭ 27 (-71.28%)
Mutual labels:  svelte
sveltober
Cybernetically enhanced October applications
Stars: ✭ 19 (-79.79%)
Mutual labels:  svelte
svelte-trivia
A Quiz app completely made using Svelte
Stars: ✭ 25 (-73.4%)
Mutual labels:  svelte
svelvg
Convert SVG files into Svelte components with TypeScript definitions
Stars: ✭ 19 (-79.79%)
Mutual labels:  svelte

p5-svelte logo

p5-Svelte

Build CI Netlify Status NPM version

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!

📘 Docs + Examples

🌱 Simple Demo

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:

using Svelte's reactivity system to bind parameters to a p5 sketch

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

Screen.Recording.2022-04-02.at.2.42.50.AM.mov

🧙‍♀️ After

Screen.Recording.2022-04-02.at.2.53.15.AM.mov
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].