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

p5i

v0.5.0

Published

p5.js, but with more friendly instance mode APIs

Downloads

284

Readme

p5i

p5.js, but with more friendly instance mode APIs

  • ES6 Destructurable
  • Declare first, initialize / reuse later
  • Cleaner setup
  • TypeScript type definitions
  • Accessing instance context on setup and draw
  • Flexible ways to defining your sketches

Motivation

p5.js in global mode is simple, consice and easy-to-use. However, injecting to the global window makes it less flexible in the modern web environment which you may have multiple pages and components with their own lifecycles, that's the reason we have the instance mode. Unfortunately, it isn't prefect, in the instance mode, you have to prefix every single function with xxx., make it a bit verbose and misaligned with the global mode.

new P5((sketch) => {
  sketch.setup = () => {
    sketch.createCanvas(720, 400)
    sketch.frameRate(30)
  }

  sketch.draw = () => {
    sketch.background(0)
    sketch.stroke(255)
  }
}, document.getElementById('canvas'))

To get rid of it, you may think of destructuring, but it won't work

new P5((sketch) => {
  // NO! you can't!
  const { frameRate, createCanvas } = sketch

  sketch.setup = () => {
    // `this` gets lost
    createCanvas(200, 200)
  }
})

p5i is a wrapper for p5.js to make the API more flexible and friendly to the modern world. It makes the functions in p5 independent from the this context and being destructurable. This makes the instance mode more like the global mode while keeps the ability to be isolated and reuseable. See the following example and the type definitions for more details.

Install

npm i p5i

CDN

<script src="http://unpkg.com/p5i"></script>

Functions will be exposed to the global variable P5I.

Usage

import P5 from 'p5'

const myp5 = new P5((sketch) => {
  let y = 100

  sketch.setup = () => {
    sketch.createCanvas(720, 400)
    sketch.stroke(255)
    sketch.frameRate(30)
  }

  sketch.draw = () => {
    sketch.background(0)
    y = y - 1
    if (y < 0)
      y = sketch.height

    sketch.line(0, y, sketch.width, y)
  }
}, document.getElementById('canvas'))

After

import { p5i } from 'p5i'

let y = 100

function setup({ createCanvas, stroke, frameRate }) {
  createCanvas(720, 400)
  stroke(255)
  frameRate(30)
}

function draw({ background, line, height, width }) {
  background(0)
  y = y - 1
  if (y < 0)
    y = height

  line(0, y, width, y)
}

p5i({ setup, draw }, document.getElementById('canvas'))

Or

import { P5I, p5i } from 'p5i'

const { mount, createCanvas, stroke, frameRate, background, line } = p5i()

let y = 100

function setup() {
  createCanvas(720, 400)
  stroke(255)
  frameRate(30)
}

// with TypeScript
function draw({ height, width }: P5I) {
  background(0)
  y = y - 1
  if (y < 0)
    y = height

  line(0, y, width, y)
}

mount(document.getElementById('canvas'), { setup, draw })

Or

import { p5i } from 'p5i'

const sketch = p5i(() => {
  let y = 100

  return {
    setup({ createCanvas, stroke, frameRate }) {
      createCanvas(720, 400)
      stroke(255)
      frameRate(30)
    },
    draw({ background, height, width, line }) {
      background(0)
      y = y - 1
      if (y < 0)
        y = height

      line(0, y, width, y)
    }
  }
})

// you can mount it later
sketch.mount(document.getElementById('canvas'))

The with keyword:

p5i((sketch) => {
  let y = 100

  with (sketch) {
    function setup() {
      createCanvas(720, 400)
      stroke(255)
      frameRate(30)
    }

    function draw() {
      background(0)
      y = y - 1
      if (y < 0)
        y = height

      line(0, y, width, y)
    }

    return { setup, draw }
  }
}, document.getElementById('canvas'))

Sponsors

This project is part of my Sponsor Program

License

MIT