JSFiddle - React, Tailwind, and code Playground
by Darby Rathbone
HTML
<canvas id='canvas' width=500 height=500></canvas>
JavaScript
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var ball;
var t;
var t0;
var dt;
var animId;
var numBalls = 10;
var animTime = 50000; // duration of animation
window.onload = init;
var gravity =new Vector2D(0,1);
function Vector2D(x, y) {
this.x = x;
this.y = y;
return this;
}
// PUBLIC METHODS
Vector2D.prototype = {
lengthSquared: function () {
return this.x * this.x + this.y * this.y;
},
length: function () {
return Math.sqrt(this.lengthSquared());
},
clone: function () {
return new Vector2D(this.x, this.y);
},
negate: function () {
this.x = -this.x;
this.y = -this.y;
},
normalize: function () {
var length = this.length();
if (length > 0) {
this.x /= length;
this.y /= length;
}
return this.length();
},
add: function (vec) {
return new Vector2D(this.x + vec.x, this.y + vec.y);
},
incrementBy: function (vec) {
this.x += vec.x;
this.y += vec.y;
return this;
},
subtract: function (vec) {
return new Vector2D(this.x - vec.x, this.y - vec.y);
},
decrementBy: function (vec) {
this.x -= vec.x;
this.y -= vec.y;
},
scaleBy: function (k) {
this.x *= k;
this.y *= k;
return this;
},
dotProduct: function (vec) {
return this.x * vec.x + this.y * vec.y;
}
};
// STATIC METHODS
Vector2D.distance = function (vec1, vec2) {
return (vec1.subtract(vec2)).length();
}
Vector2D.angleBetween = function (vec1, vec2) {
return Math.acos(vec1.dotProduct(vec2) / (vec1.length() * vec2.length()));
}
function Particle(mass, charge) {
if (typeof (mass) === 'undefined') mass = 1;
if (typeof (charge) === 'undefined') charge = 0;
this.mass = mass;
this.charge = charge;
this.x = 0;
this.y = 0;
this.vx = 0;
this.vy = 0;
}
Particle.prototype = {
...