Created
July 21, 2018 19:42
-
-
Save izebit/031f16bff5eb124d4c94e427b1475658 to your computer and use it in GitHub Desktop.
mouse-walker fanny animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#this example uses processing.js library | |
var maxDir = new PVector(width,height); | |
var maxMag = maxDir.mag(); | |
var Mover = function() { | |
this.position = new PVector(width/2, height/2); | |
this.velocity = new PVector(0, 0); | |
this.acceleration = new PVector(0, 0); | |
}; | |
Mover.prototype.update = function() { | |
var mouse = new PVector(mouseX, mouseY); | |
var dir = PVector.sub(mouse, this.position); | |
var closeness = (maxMag - dir.mag())/maxMag; | |
dir.normalize(); | |
dir.mult(closeness); | |
this.acceleration = dir; | |
this.velocity.add(this.acceleration); | |
this.velocity.limit(5); | |
this.position.add(this.velocity); | |
}; | |
Mover.prototype.display = function() { | |
stroke(0); | |
strokeWeight(2); | |
fill(127); | |
ellipse(this.position.x, this.position.y, 48, 48); | |
}; | |
Mover.prototype.checkEdges = function() { | |
if (this.position.x > width) { | |
this.position.x = 0; | |
} else if (this.position.x < 0) { | |
this.position.x = width; | |
} | |
if (this.position.y > height) { | |
this.position.y = 0; | |
} else if (this.position.y < 0) { | |
this.position.y = height; | |
} | |
}; | |
var mover = new Mover(); | |
draw = function() { | |
background(255, 255, 255); | |
mover.update(); | |
mover.checkEdges(); | |
mover.display(); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment