Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
Confluence
For you

Programming
Results will update as you type.
  • Tiling and Repetition
  • Reactors
  • Programming Basics: Parametric and Generative Graphic Design 2016
  • Archive
  • High Scores
  • Artificial Neural Network
  • Alternatives to the Processing IDE
  • p5.js Programming
    • p5.js Introduction
    • p5.js Variables
    • p5.js Random Numbers
    • p5.js SVG + Images
    • p5.js WebGL
    • p5.js Classes and Objects
    • p5.js Events and Functions
    • p5.js Loops
    • p5.js Coordinates
    • P5js Nested Loops
    • p5.js Animation Exercise 1
    • p5.js Animation Exercise 2
    • p5.js Conditionals
    • p5.js Arrays and Lists
    • p5.js Simple Collision Detection
    • p5.js Reactors
    • p5.js Tiling and Repetition
    • p5.js Vectors
    • p5.js Animation Solution with Objects
    • p5.js Easing
    • p5.js Perlin Noise
    • p5.js Particle System
    • p5.js Sound
    • p5j.s Typography
    • P5js Archive
  • Programming in Processing (java)

/
p5.js Animation Exercise 1
Updated Sep 01, 2021

p5.js Animation Exercise 1

Analytics

Loading data...

In this simple program, we animate an ellipse to always chase a point defined by the mouse. The ellipse only ever moves a percentage of the distance between it and the target destination in each step. The p5.Vector object is used to handle the coordinates and the simple math operations.

Exercise 

  1. Add an additional ellipse that follows the mouse at a slower rate than the first ellipse. 

  2. Modify the code so that the ellipse always follows the mouse position.  

  3. Replace with the ellipse with another shape (like a triangle or even a car) , and keep it pointed towards the mouse.

 

var pos; var targetPos; var mousePos; function setup() { createCanvas(1000, 600); fill(0); pos = createVector(0, 0); targetPos = createVector(0, 0); mousePos = createVector(width/2, height/2); } function draw() { background(255); targetPos.set(mousePos); targetPos.sub(pos); // find the vector between mouse position and ellipse position targetPos.mult(0.2); // each step in the animation will move this percentage of the distance to the target position pos.add(targetPos); ellipse(pos.x, pos.y, 60, 60); } function mousePressed() { mousePos.set(mouseX, mouseY); }



Exercise Solution Using Arrays

var pos = []; var targetPos = []; var mousePos = []; var arrayLength = 5; function setup() { createCanvas(1000, 600); fill(0); for (var i = 0; i< arrayLength; i++) { pos[i] = createVector(0, 0); targetPos[i] = createVector(0, 0); } mousePos = createVector(width/2, height/2); } function draw() { mousePos.set(mouseX, mouseY); background(255); for (var i = 0; i < pos.length; i++) { targetPos[i].set(mousePos); targetPos[i].sub(pos[i]); // find the vector between mouse position and ellipse position targetPos[i].mult((i*0.02) + 0.05); // each step in the animation will move this percentage of the distance to the target position pos[i].add(targetPos[i]); fill(125,255,23); ellipse(pos[i].x, pos[i].y, 60, 60); } }


Exercise Solution Using Arrays and Objects

let shape = []; var arrayLength = 60; function setup() { createCanvas(1000, 600); fill(0); for (var i= 0; i < arrayLength; i++) { shape[i] = new myShape((i*0.02) + 0.02); } } function draw() { background(255); for (var i= 0; i < shape.length; i++) { shape[i].update(); } } class myShape { constructor(_speed) { this.pos = createVector(0,0); this.targetPos = createVector(0,0); this.mousePos = createVector(width/2, height/2); this.speed = _speed; } update() { this.mousePos.set(mouseX, mouseY); this.targetPos.set(this.mousePos); this.targetPos.sub(this.pos); // find the vector between mouse position and ellipse position this.targetPos.mult(this.speed); // each step in the animation will move this percentage of the distance to the target position this.pos.add(this.targetPos); pushMatrix(); translate(this.pos.x, this.pos.y); rotate(calcAngle(this.pos, this.mousePos)); triangle(0,0,-5,20,5,20); popMatrix(); } calcAngle(p1, p2) { return -atan2(p1.x - p2.x,p1.y - p2.y); } }

Related content

p5.js Animation Exercise 2
p5.js Animation Exercise 2
Programming
More like this
p5.js Introduction
p5.js Introduction
Programming
More like this
p5.js Vectors
p5.js Vectors
Programming
More like this
p5.js Events and Functions
p5.js Events and Functions
Programming
More like this
p5.js Particle System
p5.js Particle System
Programming
More like this
p5.js Coordinates
p5.js Coordinates
Programming
More like this
{"serverDuration": 20, "requestCorrelationId": "be2f50fe32904231a874b2f5e87f0872"}