Giter Site home page Giter Site logo

p5-runner's Introduction

p5 runner

A tool for running and exporting p5.js sketches in a modern ESNext environment.

As soon as you make changes to your sketch file, this package will automatically reload the sketch in the browser. The tool also provides a simple API for getting animation frames out of the browser and saving them as PNG files.

Usage

  1. npm init -y
  2. npm install --save p5-runner
  3. Create index.js
import { runSketch } from "p5-runner"

const size = 512
const fps = 50

const { sin, cos, PI } = Math
const r = size / 3

runSketch((p, api) => {
  p.setup = () => {
    p.createCanvas(size, size)
    p.frameRate(fps)

    p.noFill()
    p.stroke("pink")
    p.strokeWeight(15)
  }

  p.draw = () => {
    p.background("aquamarine")

    p.translate(size / 2, size / 2)

    p.beginShape()
    for (let i = 0; i < 48; i++) {
      const v = getPos(p.frameCount - i)
      p.vertex(v.x, v.y)
    }
    p.endShape()

    // if (p.frameCount <= 250) api.saveFrame("out/out.#####.png")
  }

  const getPos = frameCount => {
    const t = frameCount / fps / 2
    const x = sin(t * 2 * PI) * r
    const y = cos(t * 3 * PI) * r
    return p.createVector(x, y)
  }
})
  1. npx p5-runner or npx p5-runner index.js
  2. Go to http://localhost:3000

Exporting your sketch as PNG files

Calling the api.saveFrame() method in your sketch file will save the current animation frame in your project's directory. The default output location is "out/out.#####.png" but you can also provide your own destination path like this: api.saveFrame("renders/sketch01/#####.png") or api.saveFrame("still.png")

p5-runner's People

Contributors

dependabot[bot] avatar

Stargazers

 avatar

Watchers

James Cloos avatar Paul Kretschel avatar  avatar

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.