vue-p5
vue-p5
Create p5.js instance as a Vue component.
Quick start
Script
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/vue-p5"></script>
<div id="app">
<vue-p5 v-on="this"></vue-p5>
</div>
<script>
new Vue({
el: '#app',
methods: {
setup(sketch) {
sketch.background('green');
sketch.text('Hello p5!', 20, 20);
}
}
});
</script>
NPM
npm install --save vue vue-p5
import Vue from 'vue';
import VueP5 from 'vue-p5';
export default {
methods: {
setup(sketch) {
sketch.background('green');
sketch.text('Hello p5!', 20, 20);
}
},
render(h) {
return h(VueP5, {on: this});
}
};
Usage
v-on object syntax
In the examples above v-on="this" and {on: this} are a short (and hacky) way to avoid handling every p5 event explicitly. You might want to use one of the other options:
<vue-p5 v-on="{setup, draw, keypressed}"></vue-p5>
<!-- which is equivalent to: -->
<vue-p5
@setup="setup"
@draw="draw"
@keypressed="keypressed">
</vue-p5>
on: {
setup: this.setup,
draw: this.draw,
keypressed: this.keypressed
}
See also v-on object syntax.
Events - p5 and Vue
Every p5 event is exposed as a Vue event. The first argument is the sketch object used for drawing and everything else:
methods: {
draw(sk) {
// draw a line between the previous
// and the current mouse position
sk.line(sk.pmouseX, sk.pmouseY, sk.mouseX, sk.mouseY);
},
keypressed(sk) {
// convert the key code to it's string
// representation and print it
const key = String.fromCharCode(sk.keyCode);
sk.print(key);
}
}
Using methods makes it possible to access the current component:
// green background
data: {
color: [0, 255, 0]
},
methods: {
draw(sketch) {
sketch.background(...this.color);
}
}
Event names
Each event emitted by vue-p5 has the same name as the corresponding p5 event, but lowercase.
mouseclicked, not , not mouseClicked, not mouse-clicked.mouse_clicked
Missing events
Currently all p5 events are supported, but there's an escape hatch for registering missing events:
<vue-p5 :additionalEvents="['windowResized']"></vue-p5>
Importing existing sketches
In addition to p5 events, there's a @sketch event for importing an existing p5 sketch written in instance mode.
<vue-p5 @sketch="sketch"></vue-p5>
<script>
new Vue({
methods: {
sketch(sk) {
const clicks = [];
sk.mouseClicked = () => {
// save clicks to array
clicks.push({ x: sk.mouseX, y: sk.mouseY });
}
sk.draw = () => {
// draw a circle around each clicked position
clicks.forEach(({ x, y }) => {
sk.ellipse(x, y, 10, 10);
});
}
}
}
});
</script>
Remember to use arrow functions if you need this.
@sketch can be used in parallel with other events. Functions defined in the @sketch handler will always be called first.
Examples
Hello world: codepen
Webpack project: vue-p5-example
A game of Snake: vue-p5-snake
Feedback
Feel free to open a new issue if you have a question or a feature request!
Versioning
This project adheres to semver. Minor changes are breaking.
Use vue-p5@next to get a version with future updates.
License
LGPL-2.1
Related Packages
Version History
| Version | Released | License |
|---|---|---|
| 0.8.4 | LGPL-2.1 | |
| 0.8.3 | LGPL-2.1 | |
| 0.8.2 | LGPL-2.1 | |
| 0.8.1 | LGPL-2.1 | |
| 0.7.2 | LGPL-2.1 | |
| 0.7.1 | LGPL-2.1 | |
| 0.7.0 | LGPL-2.1 | |
| 0.6.0 | LGPL-2.1 | |
| 0.5.2 | LGPL-2.1 | |
| 0.5.1 | LGPL-2.1 | |
| 0.5.0 | LGPL-2.1 | |
| 0.4.4 | LGPL-2.1 | |
| 0.4.3 | LGPL-2.1 | |
| 0.4.2 | LGPL-2.1 | |
| 0.4.1 | LGPL-2.1 | |
| 0.4.0 | LGPL-2.1 | |
| 0.3.4 | LGPL-2.1 | |
| 0.3.3 | LGPL-2.1 | |
| 0.3.2 | LGPL-2.1 | |
| 0.3.1 | LGPL-2.1 | |
| 0.3.0 | MIT | |
| 0.2.0 | MIT | |
| 0.1.6 | MIT | |
| 0.1.5 | MIT | |
| 0.1.4 | MIT | |
| 0.1.3 | MIT | |
| 0.1.2 | MIT | |
| 0.1.1 | MIT | |
| 0.1.0 | MIT |
Pre-releases (23)
| Version | Released | License |
|---|---|---|
| 1.0.0-beta.11 | LGPL-2.1 | |
| 1.0.0-beta.10 | LGPL-2.1 | |
| 1.0.0-beta.9 | LGPL-2.1 | |
| 1.0.0-beta.8 | LGPL-2.1 | |
| 1.0.0-beta.7 | LGPL-2.1 | |
| 1.0.0-beta.6 | LGPL-2.1 | |
| 1.0.0-beta.5 | LGPL-2.1 | |
| 1.0.0-beta.4 | LGPL-2.1 | |
| 1.0.0-beta.3 | LGPL-2.1 | |
| 1.0.0-beta.2 | LGPL-2.1 | |
| 1.0.0-beta.1 | LGPL-2.1 | |
| 0.9.0-rc2 | LGPL-2.1 | |
| 0.9.0-rc1 | LGPL-2.1 | |
| 0.8.4-rc.1 | LGPL-2.1 | |
| 0.8.0-rc4 | LGPL-2.1 | |
| 0.8.0-rc2 | LGPL-2.1 | |
| 0.6.0-rc1 | LGPL-2.1 | |
| 0.5.0-rc3 | LGPL-2.1 | |
| 0.5.0-rc2 | LGPL-2.1 | |
| 0.5.0-rc1 | LGPL-2.1 | |
| 0.4.3-rc2 | LGPL-2.1 | |
| 0.4.3-rc1 | LGPL-2.1 | |
| 0.4.0-rc1 | LGPL-2.1 |