A Pen By
Art Carvajal
Pro

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

` ````
<canvas id="canvas"></canvas>
<p style="position: fixed; top: 0;"></p>
<!-- try clicking! -->
```

` ````
body {
margin: 0;
overflow: hidden;
background-color: taupe;
cursor: crosshair;
}
```

` ````
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
canvas.width = innerWidth;
canvas.height = innerHeight;
window.addEventListener('resize', function(){
canvas.width = innerWidth;
canvas.height = innerHeight;
})
var numFilings = Math.round(innerWidth * 1.5);
var filingLength = 12;
var magWidth = innerWidth / 2;
var magHeight = magWidth / 7;
var mouseX = innerWidth / 2;
var mouseY = innerHeight/2;
var moving = 0;
var magnetVisible = 1;
var poleN = mouseX - magWidth/2 + magHeight/2;
var poleS = mouseX + magWidth/2 - magHeight/2;
function Filing() {
this.x = Math.random() * innerWidth;
this.y = Math.random() * innerHeight;
this.facing = Math.random() * 2;
this.nearN;
this.nearS;
this.length = Math.random();
this.dynamicSpeed = 0.04 + Math.random() * 0.04;
}
Filing.prototype.rotate = function() {
var deltaX, deltaY, theta, pX, bias;
if (this.x < mouseX) {
pX = poleN;
this.nearN = 1;
this.nearS = 0;
} else {
pX = poleS;
this.nearN = 0;
this.nearS = 1;
}
if (this.x < poleN || this.x > poleS) {
deltaX = pX - this.x;
deltaY = mouseY - this.y;
theta = Math.atan(deltaY / deltaX);
if (this.nearS) { theta += Math.PI; }
} else {
bias = Math.abs((this.x - mouseX)/(pX - mouseX));
deltaX = pX - this.x;
deltaY = (mouseY - this.y) * bias;
theta = Math.atan(deltaY / deltaX) + Math.PI;
if (this.nearS) { theta += Math.PI; }
}
this.facing = theta / Math.PI;
};
Filing.prototype.drawNormal = function() {
ctx.strokeStyle = this.nearN ? '#f00' : '#00f';
var x1 = this.x + Math.cos(Math.PI * this.facing) * filingLength;
var y1 = this.y + Math.sin(Math.PI * this.facing) * filingLength;
var x2 = this.x + Math.cos(Math.PI * (this.facing + 1)) * filingLength;
var y2 = this.y + Math.sin(Math.PI * (this.facing + 1)) * filingLength;
ctx.beginPath();
ctx.moveTo(x1,y1);
ctx.lineTo(x2,y2);
ctx.stroke();
};
Filing.prototype.drawDynamic = function() {
this.length + this.dynamicSpeed >= 1 ? (this.length = 0,
this.x = Math.random() * innerWidth,
this.y = Math.random() * innerHeight,
this.rotate()
) : this.length += this.dynamicSpeed;
var lightness = 60 * this.length;
if (this.nearN) {
ctx.strokeStyle = 'hsl(0,100%,' + lightness + '%)';
var x1 = this.x + Math.cos(Math.PI * this.facing) * (filingLength * this.length * 2);
var y1 = this.y + Math.sin(Math.PI * this.facing) * (filingLength * this.length * 2);
ctx.beginPath();
ctx.moveTo(this.x,this.y);
ctx.lineTo(x1,y1);
ctx.stroke();
} else {
ctx.strokeStyle = 'hsl(240,100%,' + lightness + '%)';
ctx.strokeStyle = this.nearN ? 'hsl(0,100%,' + lightness + '%)' : 'hsl(240,100%,' + lightness + '%)';
var x1 = this.x - Math.cos(Math.PI * this.facing) * (filingLength * this.length * 2);
var y1 = this.y - Math.sin(Math.PI * this.facing) * (filingLength * this.length * 2);
ctx.beginPath();
ctx.moveTo(this.x, this.y);
ctx.lineTo(x1, y1);
ctx.stroke();
}
};
var filings = [];
for (var i = 0; i < numFilings; i++) {
filings.push(new Filing);
filings[i].rotate();
}
document.addEventListener('mousemove', function(e){
e.preventDefault();
moving = 1;
mouseX = e.clientX;
mouseY = e.clientY;
poleN = mouseX - magWidth/2 + magHeight/2;
poleS = mouseX + magWidth/2 - magHeight/2;
})
document.addEventListener('touchmove', function(e) {
e.preventDefault();
mouseX = e.touches[0].pageX;
mouseY = e.touches[0].pageY;
moving = 1;
poleN = mouseX - magWidth/2 + magHeight/2;
poleS = mouseX + magWidth/2 - magHeight/2;
}, false);
document.addEventListener('click', function(e){
e.preventDefault();
magnetVisible = magnetVisible ? 0 : 1;
})
function drawMagnet() {
ctx.fillStyle = '#c00';
ctx.fillRect(mouseX - magWidth/2, mouseY - magHeight/2, magWidth/2, magHeight);
ctx.fillStyle = "#ddd";
ctx.fillRect(mouseX, mouseY - magHeight/2, magWidth/2, magHeight);
}
function drawAttrPoints() {
ctx.fillStyle = '#fff';
ctx.fillRect(poleN - 1, mouseY - 1, 2, 2);
ctx.fillRect(poleS - 1, mouseY - 1, 2, 2);
}
function animate() {
if (magnetVisible) { ctx.clearRect(0, 0, innerWidth, innerHeight); }
else {
ctx.fillStyle = 'rgba(0, 0, 0, 0.6)';
ctx.fillRect(0, 0, innerWidth, innerHeight);
}
for (var i = 0; i < filings.length; i++) {
if (magnetVisible) { filings[i].drawNormal(); }
else { filings[i].drawDynamic(); }
if (moving) { filings[i].rotate(); }
}
if (mouseX && magnetVisible) {
drawMagnet();
} else {
drawAttrPoints();
}
moving = 0;
window.requestAnimationFrame(animate);
}
animate();
```

999px

Loading
..................

Alt F
Opt F
Find & Replace

Also see: Tab Triggers