Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tonyketcham/p5-svelte
Easily add p5 sketches to a Svelte project 🍛 🌱
https://github.com/tonyketcham/p5-svelte
generative-art p5 p5js svelte svelte-components
Last synced: 4 days ago
JSON representation
Easily add p5 sketches to a Svelte project 🍛 🌱
- Host: GitHub
- URL: https://github.com/tonyketcham/p5-svelte
- Owner: tonyketcham
- License: mit
- Created: 2020-08-03T15:48:02.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-12-16T18:02:47.000Z (about 1 year ago)
- Last Synced: 2024-09-24T10:16:08.359Z (4 months ago)
- Topics: generative-art, p5, p5js, svelte, svelte-components
- Language: Svelte
- Homepage: https://p5-svelte.netlify.app
- Size: 274 KB
- Stars: 177
- Watchers: 6
- Forks: 7
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
p5-Svelte
Trying to get p5 up and running in [Svelte](https://svelte.dev/) 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 asketch
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:
```ps
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:
```ps
pnpm i -D p5
```Now add `p5-svelte` to your project (ex. `src/App.svelte`):
```svelte
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);
};
};Width
{width}Height
{height}```
### 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 ``. 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:
```svelte
```
## Events
The `` 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