JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.

Any URL's added here will be added as `<script>`

s in order, and run *before* the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

` ````
let attractor;
let mover;
function setup(){
const width = window.innerWidth;
const height = window.innerHeight;
createCanvas(width,height);
mover = new Mover();
attractor = new Attractor();
}
function draw() {
background(51);
let force = attractor.calculateAttraction(mover);
mover.applyForce(force);
mover.update();
attractor.display();
mover.display();
}
function mouseMoved() {
attractor.handleHover(mouseX, mouseY);
}
function mousePressed() {
attractor.handlePress(mouseX, mouseY);
}
function mouseDragged() {
attractor.handleHover(mouseX, mouseY);
attractor.handleDrag(mouseX, mouseY);
}
function mouseReleased() {
attractor.stopDragging();
}
function windowResized() {
setup()
}
class Mover {
constructor() {
this.position = createVector(400, 50);
this.velocity = createVector(1, 0);
this.acceleration = createVector(0, 0);
this.mass = 1;
}
applyForce(force) {
// Divide the vector by a scalar ( in this instance mass force vector x y both divided by mass i.e 4 2 -> 2 1 )
let f = p5.Vector.div(force, this.mass);
// add to acceleration
this.acceleration.add(f);
}
update() {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.acceleration.mult(0);
}
display() {
stroke(0);
strokeWeight(2);
fill(255, 127);
ellipse(this.position.x, this.position.y, this.mass * 16, this.mass * 16);
}
checkEdges() {
if (this.position.x > width) {
this.position.x = width;
this.velocity.x *= -1;
} else if (this.position.x < 0) {
this.velocity.x *= -1;
this.position.x = 0;
}
if (this.position.y > height) {
this.velocity.y *= -1;
this.position.y = height;
}
}
}
class Attractor {
constructor() {
this.position = createVector(width / 2, height / 2);
this.mass = 20;
this.G = 1;
this.dragOffset = createVector(0, 0);
this.dragging = false;
this.rollover = false;
}
calculateAttraction(m) {
let force = p5.Vector.sub(this.position, m.position);
// the size of the vector
let distance = force.mag();
distance = constrain(distance, 5, 25);
force.normalize();
// F = G * m1 * m2 / r squared
let strength = (this.G * this.mass * m.mass) / (distance * distance)
// magnitude * direction
force.mult(strength);
return force
}
// Method to display
display() {
ellipseMode(CENTER);
strokeWeight(4);
stroke(0);
if (this.dragging) {
fill(50);
} else if (this.rollover) {
fill(100);
} else {
fill(175, 200);
}
ellipse(this.position.x, this.position.y, this.mass * 2, this.mass * 2);
}
// The methods below are for mouse interaction
handlePress(mx, my) {
let d = dist(mx, my, this.position.x, this.position.y);
if (d < this.mass) {
this.dragging = true;
this.dragOffset.x = this.position.x - mx;
this.dragOffset.y = this.position.y - my;
}
}
handleHover(mx, my) {
let d = dist(mx, my, this.position.x, this.position.y);
if (d < this.mass) {
this.rollover = true;
} else {
this.rollover = false;
}
}
stopDragging() {
this.dragging = false;
}
handleDrag(mx, my) {
if (this.dragging) {
this.position.x = mx + this.dragOffset.x;
this.position.y = my + this.dragOffset.y;
}
}
}
```

999px

Shift F
Opt F
Find & Replace

Also see: Tab Triggers