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.

You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.

If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.

You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.

+ add another resource

If active, Pens will autosave every 30 seconds after being saved once.

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Visit your global Editor Settings.

` ````
<canvas id="c"></canvas>
```

` ````
body, html, canvas {
margin: 0px;
padding: 0px;
position: absolute;
cursor: none;
}
canvas {
background-color: #343435;
}
```

` ````
var partNum = 100;
//particle number - change it!
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function(callback){
window.setTimeout(callback, 1000 / 60);
};
})();
function between(min, max) {
return Math.random() * (max - min) + min;
}
var c = document.getElementById('c');
var ctx = c.getContext('2d');
//context and id of canvas
var w = window.innerWidth;
var h = window.innerHeight;
//width and height of canvas
c.width = w;
c.height = h;
//setting the width and height for canvas
var mouse = {
x: w / 2,
y: h / 2,
r: 25,
mass: 3,
vx: 0,
vy: 0
};
//mouse position
document.addEventListener('mousemove', function(e){
mouse.x = e.clientX || e.pageX;
mouse.y = e.clientY || e.pageY
}, false);
//finding the mouse position
var particles = [];
setTimeout(function() {
for(i = 0; i < partNum; i++) {
particles.push(new particle);
}
}, 1111)
//the particle function
function particle() {
this.x = between(c.width/2 - 30, c.width/2 + 30);
this.y = between(c.height/2 - 30, c.height/2 + 30);
this.vx = 0;
this.vy = 0;
this.r = Math.random() * 28;
this.mass = this.r;
var r = '#20a2aa';
var o = '#ffae14';
var y = '#ef4138';
var array = [r, o, y];
this.color = array[Math.floor(Math.random() * 3)];
}
function draw() {
requestAnimFrame(draw);
ctx.fillStyle = 'rgba(52, 52, 53, 1)';
ctx.fillRect(0, 0, c.width, c.height);
ctx.beginPath();
ctx.fillStyle = 'rgba(255, 255, 255, 1)';
ctx.arc(mouse.x, mouse.y, mouse.r, Math.PI * 2, false);
ctx.fill();
for(t = 0; t < particles.length; t++) {
var p = particles[t];
ctx.beginPath();
ctx.fillStyle = p.color;
ctx.arc(p.x, p.y, p.r, Math.PI * 2, false);
ctx.fill();
//the context of the particle(s)
p.x+=p.vx;
p.y+=p.vy;
if(p.y <= p.r) {
p.vy *= -1;
p.y = p.r;
}
if(p.y >= c.height - p.r) {
p.vy *= -0.3;
p.y = c.height - p.r;
}
if(p.x < 0 + p.r) {
p.vx *= -1;
p.x = p.r;
}
if(p.x > c.width - p.r) {
p.vx *= -1;
p.x = c.width - p.r;
}
if(p.r < 3) {
p.color = 'white';
};
for(j = 0; j < particles.length; j++) {
var pp = particles[j];
distance(p, pp);
}
distanceM(mouse, p);
}
}
function distance(p1, p2) {
var dist,
dx = p1.x - p2.x,
dy = p1.y - p2.y;
dist = Math.sqrt(dx*dx + dy*dy);
var minDist = p1.r + p2.r;
if(dist <= minDist) {
newVelX1 = (p1.vx * (p1.mass - p2.mass) + (2 * p2.mass * p2.vx)) / (p1.mass + p2.mass);
newVelY1 = (p1.vy * (p1.mass - p2.mass) + (2 * p2.mass * p2.vy)) / (p1.mass + p2.mass);
newVelX2 = (p2.vx * (p2.mass - p1.mass) + (2 * p1.mass * p1.vx)) / (p1.mass + p2.mass);
newVelY2 = (p2.vy * (p2.mass - p1.mass) + (2 * p1.mass * p1.vy)) / (p1.mass + p2.mass);
var depth_x = dx * (dist / minDist -1);
var depth_y = dy * (dist / minDist -1);
p1.x -= depth_x;
p1.y -= depth_y;
p2.x += depth_x;
p2.y += depth_y;
p1.vx = newVelX1 * 0.9;
p1.vy = newVelY1 * 0.9;
p2.vx = newVelX2 * 0.9;
p2.vy = newVelY2 * 0.9;
}
}
function distanceM(m, p2) {
var dist,
dx = m.x - p2.x,
dy = m.y - p2.y;
dist = Math.sqrt(dx*dx + dy*dy);
var minDist = m.r + p2.r;
if(dist <= minDist) {
newVelX1 = (m.vx * (m.mass - p2.mass) + (2 * p2.mass * p2.vx)) / (m.mass + p2.mass);
newVelY1 = (m.vy * (m.mass - p2.mass) + (2 * p2.mass * p2.vy)) / (m.mass + p2.mass);
newVelX2 = (p2.vx * (p2.mass - m.mass) + (2 * m.mass * m.vx)) / (m.mass + p2.mass);
newVelY2 = (p2.vy * (p2.mass - m.mass) + (2 * m.mass * m.vy)) / (m.mass + p2.mass);
var depth_x = dx * (dist / minDist -1);
var depth_y = dy * (dist / minDist -1);
p2.x += depth_x;
p2.y += depth_y;
p2.vx = newVelX2 * 0.9;
p2.vy = newVelY2 * 0.9;
}
}
draw();
```

999px

Also see: Tab Triggers