Giter Site home page Giter Site logo

tonyketcham / p5-svelte Goto Github PK

View Code? Open in Web Editor NEW
171.0 6.0 7.0 281 KB

Easily add p5 sketches to a Svelte project ๐Ÿ› ๐ŸŒฑ

Home Page: https://p5-svelte.netlify.app

License: MIT License

JavaScript 5.18% Svelte 83.67% CSS 4.67% HTML 1.28% TypeScript 5.19%
svelte svelte-components p5js p5 generative-art

p5-svelte's Introduction


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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.