How to use p5 - 10 common examples

To help you get started, we’ve selected a few p5 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github piratefsh / p5js-art / app / index.js View on Github external
p.translate(0, height-50);
        // stroke(random(100,200),random(100,200),0)

        for(let i = 0; i < systems.length; i++){
            const options = systems[i];
            options.p = p;
            console.log(options)
            const l = new LSystem(options);
            p.translate(distanceX, 0)
            l.run(iterations[i]);
        }
    }
}

// set global functions for p5
const p5Instance = new p5(sketch)
github winkerVSbecks / sketchbook / flocking.js View on Github external
function separate(boids, boid) {
  const [count, direction] = boids.reduce(
    ([count, direction], otherBoid) => {
      const d = boid.position.dist(otherBoid.position);
      if (d > 0 && d < CONFIG.desiredSeparation) {
        // Calculate vector pointing away from neighbour
        const diff = Vector.sub(boid.position, otherBoid.position)
          .normalize()
          .div(d); // Weight by distance
        return [count + 1, direction.add(diff)];
      }
      return [count, direction];
    },
    [0, new Vector(0, 0)],
  );

  return count > 0
    ? direction
        .div(count) // average
        .normalize()
        .mult(boid.maxSpeed)
        .sub(boid.velocity)
        .limit(boid.maxForce)
    : direction;
}
github winkerVSbecks / sketchbook / flocking.js View on Github external
function seek(target, boid) {
  // A vector pointing from the boid location to the target
  const desired = Vector.sub(target, boid.position);
  // Normalize and scale to maximum speed
  desired.normalize();
  desired.mult(boid.maxSpeed);
  // Steer = Desired - Velocity
  const steer = Vector.sub(desired, boid.velocity);
  // Limit to maximum steering force
  steer.limit(boid.maxForce);
  return steer;
}
github winkerVSbecks / sketchbook / flocking.js View on Github external
function seek(target, boid) {
  // A vector pointing from the boid location to the target
  const desired = Vector.sub(target, boid.position);
  // Normalize and scale to maximum speed
  desired.normalize();
  desired.mult(boid.maxSpeed);
  // Steer = Desired - Velocity
  const steer = Vector.sub(desired, boid.velocity);
  // Limit to maximum steering force
  steer.limit(boid.maxForce);
  return steer;
}
github ml5js / ml5-library / examples / es6 / lstm / src / index.js View on Github external
This is a port of Daniel Shiffman Nature of Code: Intelligence and Learning
Original Repo: https://github.com/shiffman/NOC-S17-2-Intelligence-Learning

Cristóbal Valenzuela
https://github.com/cvalenzuela/p5deeplearn
===
*/

import p5 from 'p5';
import 'p5/lib/addons/p5.dom';
import { lstm } from './lstm';

let textInput, tempSlider, lengthSlider, maxlen = 40,
  waiting = false;

let sketch = new p5((p) => {

  p.setup = () => {
    p.noCanvas();

    // Grab the DOM elements
    textInput = p.select('#textInput');
    lengthSlider = p.select('#lenSlider');
    tempSlider = p.select('#tempSlider');

    // Run generate anytime something changes
    textInput.input(generate);
    lengthSlider.input(generate);
    tempSlider.input(generate);
  };

  let generate = () => {
github nitin42 / Elements-of-physics / simulator / components / hoc.js View on Github external
getCanvas = () =>
      new p5(p => {
        this.instance = p

        this.renderer(p)

        // Do some extra stuff here with processing
        this.props.extras && this.props.extras(p)
      }, this.wrapper)
github nitin42 / Elements-of-physics / src / module-build.js View on Github external
(_this.getCanvas = function() {
          return new p5(function(p) {
            _this.instance = p

            _this.renderer(p)

            // Do some extra stuff here with processing
            _this.props.extras && _this.props.extras(p)
          }, _this.wrapper)
        }),
        _temp)),
github winkerVSbecks / sketchbook / flocking.js View on Github external
([count, direction], otherBoid) => {
      const d = boid.position.dist(otherBoid.position);
      if (d > 0 && d < CONFIG.desiredSeparation) {
        // Calculate vector pointing away from neighbour
        const diff = Vector.sub(boid.position, otherBoid.position)
          .normalize()
          .div(d); // Weight by distance
        return [count + 1, direction.add(diff)];
      }
      return [count, direction];
    },
    [0, new Vector(0, 0)],
github zero-to-mastery / visual-music / src / vendor / sketch.js View on Github external
p.setup = function() {
        // Canvas setup
        p.background(7, 11, 21);
        canvas = p.createCanvas(width, height);
        fft = new p5.FFT(0.9, 256); // for the waves
        amplitude = new p5.Amplitude(); // for the ellipses
        p.noFill();
        p.stroke(0, 100);
        initAudioStream();
    };
github ccorcos / circle / src / sketch.js View on Github external
p.setup = () => {
      this.canvas = p.createCanvas(window.innerWidth, window.innerHeight);

      if (this.state.mode === "mic") {
        this.mic = new p5.AudioIn();
        this.mic.start();
        this.fft = new p5.FFT();
        this.fft.setInput(this.mic);
      } else {
        // using a song file
        this.song.setVolume(1.0);
        this.song.playMode("restart");
        this.song.play();
        this.fft = new p5.FFT();
        this.fft.setInput(this.song);
      }
    };

p5

[![npm version](https://badge.fury.io/js/p5.svg)](https://www.npmjs.com/package/p5)

LGPL-2.1
Latest version published 12 days ago

Package Health Score

95 / 100
Full package analysis