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

v1.0.0

Published

Vue component wrapper for p5.js

Downloads

6

Readme

lg-p5

Create p5.js instance as a Vue component.

Quick start

Script

<script src="https://unpkg.com/vue@2"></script>
<script src="https://unpkg.com/lg-p5"></script>

<div id="app">
  <lg-p5 v-on="this"></lg-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@2 lg-p5
import Vue from 'vue';
import LgP5 from 'lg-p5';

export default {
  methods: {
    setup(sketch) {
      sketch.background('green');
      sketch.text('Hello p5!', 20, 20);
    }
  },
  render(h) {
    return h(LgP5, {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:

<lg-p5 v-on="{setup, draw, keypressed}"></lg-p5>
<!-- which is equivalent to: -->
<lg-p5
    @setup="setup"
    @draw="draw"
    @keypressed="keypressed">
</lg-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 lg-p5 has the same name as the corresponding p5 event, but lowercase.

mouseclicked, not ~~mouseClicked~~.

Missing events

Currently all p5 events [are supported], but there is an escape hatch. For example, if windowResized was missing, it's (lowercase) name could be passed to additional-events prop to make lg-p5 aware of it:

<lg-p5
  :additional-events="['windowresized']"
  @windowresized="windowresized"
></lg-p5>

Importing existing sketches

In addition to p5 events, there's a @sketch event for importing an existing p5 sketch written in instance mode.

<lg-p5 @sketch="sketch"></lg-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.