Neon spring trails
How do I make an neon spring trails?
Simple springs attached to the mouse position. . What is a neon spring trails? How do you make a neon spring trails? This script and codes were developed by Pimskie on 13 June 2022, Monday.
Neon spring trails - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Neon spring trails</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <script src='http://rawgit.com/josephg/noisejs/master/perlin.js'></script> <script src="js/index.js"></script>
</body>
</html>
Neon spring trails - Script Codes CSS Codes
html,
body { margin: 0; padding: 0; overflow: hidden;
}
canvas { display: block; background: #000;
}
Neon spring trails - Script Codes JS Codes
const distanceBetween = (x1, y1, x2, y2) => Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
const clamp = (value, min, max) => Math.max(min, Math.min(value, max));
const randomBetween = (min, max) => ~~(Math.random() * (max - min + 1)) + min;
noise.seed(Math.random());
class Spring { constructor(x, y, k, damp) { this.x = x; this.y = y; this.k = k; this.damp = damp; this.velX = 0; this.velY = 0; this.length = 0; this.width = 1; } update(destX, destY, pivotX, pivotY) { this.velX += -this.k * (this.x - destX); this.velX *= this.damp; this.velY += -this.k * (this.y - destY); this.velY *= this.damp; let newX = this.x + this.velX; let newY = this.y + this.velY; this.length = ~~distanceBetween(this.x, this.y, newX, newY); this.x = newX; this.y = newY; }
}
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
// all set in `setStage`
let canvasWidth;
let canvasHeight;
let canvasMidX;
let canvasMidY;
let distanceMid;
let radiusX;
let radiusY;
let destination = { x: 0, y: 0
};
let rafId = null;
let tick = 0;
let autoMovement = true;
let automationAngle = 0;
let springs = [];
let spring = null;
// define all viewport related vars
// and resize canvas
const setStage = () => { canvasWidth = window.innerWidth; canvasHeight = window.innerHeight; canvasMidX = canvasWidth >> 1; canvasMidY = canvasHeight >> 1; distanceMid = distanceBetween(0, 0, canvasMidX, canvasMidY); radiusX = distanceMid * 0.25; radiusY = distanceMid * 0.25; destination.x = canvasMidX; destination.y = canvasMidY; canvas.width = canvasWidth; canvas.height = canvasHeight;
}
// update destination to mouse position
const updateDestination = (e) => { destination.x = e.offsetX; destination.y = e.offsetY;
}
// create springs with random settings
const createSprings = () => { const x = canvasMidX; const y = canvasMidY; let numSprings = 300; while (numSprings--) { const k = Math.random() * (0.04) + 0.08; const damp = Math.random() * (0.04) + 0.9; const spring = new Spring(x, y, k, damp); springs.push(spring); }
}
const clearStage = () => { ctx.globalCompositeOperation = 'destination-out'; ctx.fillStyle = 'rgba(0, 0, 0, 0.02)'; ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height); ctx.globalCompositeOperation = 'lighter';
}
const loop = () => { clearStage(); // update position automaticly, based on perlin noise if (autoMovement) { destination.x = canvasMidX + (Math.cos(automationAngle) * radiusX); destination.y = canvasMidY + (Math.sin(automationAngle) * radiusY); destination.x = clamp(destination.x, 0, canvasWidth); destination.y = clamp(destination.y, 0, canvasHeight); automationAngle += noise.perlin2(tick, tick) * 0.25; radiusX += noise.perlin2(destination.x, destination.y) * 20; radiusY += noise.perlin2(destination.x, destination.y) * 20; } // define the color, based on position and perlin const mouseDistance = distanceBetween(canvasMidX, canvasMidY, destination.x, destination.y); const r = ~~(255 * (distanceMid - mouseDistance) / distanceMid); const g = ~~(255 * (mouseDistance / distanceMid)); const b = ~~Math.abs(255 * (noise.perlin2(tick, tick))); const color = `rgba(${r}, ${g}, ${b}, 0.1)`; springs.forEach((spring) => { ctx.beginPath(); ctx.strokeStyle = color; ctx.lineWidth = spring.width; ctx.moveTo(spring.x, spring.y); spring.update(destination.x, destination.y, canvasMidX, canvasMidY); ctx.lineTo(spring.x, spring.y); ctx.stroke(); ctx.closePath(); }); tick += 0.005; rafId = requestAnimationFrame(loop);
}
canvas.addEventListener('mouseover', e => { autoMovement = false;
});
canvas.addEventListener('mouseout', e => { autoMovement = true;
});
canvas.addEventListener('mousemove', updateDestination);
window.addEventListener('resize', setStage);
document.body.appendChild(canvas);
// fire it up
setStage();
createSprings();
loop();
Developer | Pimskie |
Username | pimskie |
Uploaded | June 13, 2022 |
Rating | 4.5 |
Size | 3,214 Kb |
Views | 40,480 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Planet attractors | 4,548 Kb |
Particles Clifford attractor | 3,448 Kb |
Color circles | 3,544 Kb |
Aizawa Attractor | 3,609 Kb |
Glitching image - USA style | 4,733 Kb |
Exploding hamburger - and more | 7,785 Kb |
A Pen by pimskie | 3,699 Kb |
Apollonian gasket | 9,268 Kb |
Duplicate geometry | 4,190 Kb |
Circles pattern | 3,342 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Flex Chart | James_zedd | 4,111 Kb |
Underlined form fields | Mitchdot | 2,323 Kb |
Plotting Points with D3.js | Laurakelly | 31,996 Kb |
Vue.js Starter | Andymerskin | 1,268 Kb |
Wikipedia Viewer | Thomasvaeth | 2,549 Kb |
Form Labels | Bartuc | 2,717 Kb |
Simple Buttons | Haydenmills | 1,750 Kb |
Toolbar | Onsen | 5,414 Kb |
Blog | Rottingroom | 1,430 Kb |
Animated Donut Chart | Jplhomer | 3,808 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!