Lines
How do I make an lines?
Experimenting with lines, and spring physics. Inspired by my demo Orbit.. What is a lines? How do you make a lines? This script and codes were developed by Francesco Trillini on 01 September 2022, Thursday.
Lines - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Lines</title> <style> /* NOTE: The styles were added inline because Prefixfree needs access to your styles and they must be inlined if they are on local disk! */ /* Use your mouse/touch to move the lines. */
body { margin: 0px; overflow: hidden;
} </style> <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
</head>
<body> <body></body> <script src='http://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Lines - Script Codes CSS Codes
/* Use your mouse/touch to move the lines. */
body { margin: 0px; overflow: hidden;
}
Lines - Script Codes JS Codes
/* * Copyright MIT © <2013> <Francesco Trillini> * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated * documentation files (the "Software"), to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and * to permit persons to whom the Software is furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
var Lines = {};
;(function(Lines, undefined) { var self = window.Lines || {}, canvas, context, mouse = { x: innerWidth / 2, y: innerHeight / 2 }, lines = [], fade, FPS = 60; // Dat GUI default values var maxLines = 50, size = 0.3, spring = 50, friction = 0.98, trail = fade = 0.03, color = '#0096ff', interactive = true; /* * Settings. */ var Settings = function() { this.maxLines = 50; this.size = 0.3; this.spring = 50; this.friction = 0.98; this.trail = 0.03; this.color = '#0096ff'; this.interactive = true; this.changeLines = function(value) { lines = []; maxLines = value; }; this.changeSize = function(value) { size = value; }; this.changeSpring = function(value) { spring = value; }; this.changeFriction = function(value) { friction = value; }; this.changeTrail = function(value) { trail = value; }; this.changeColor = function(value) { color = value; }; this.enableInteractivity = function(value) { interactive = value; }; }; /* * Init. */ self.init = function() { var settings = new Settings(); var GUI = new dat.GUI(); // Dat GUI main GUI.add(settings, 'maxLines').min(10).max(100).onChange(settings.changeLines); GUI.add(settings, 'size').min(0.1).max(0.5).onChange(settings.changeSize); GUI.add(settings, 'spring').min(10).max(50).onChange(settings.changeSpring); GUI.add(settings, 'friction').min(0.10).max(0.99).onChange(settings.changeFriction); GUI.add(settings, 'trail').min(0.01).max(0.5).onChange(settings.changeTrail); GUI.addColor(settings, 'color').onChange(settings.changeColor).listen(); GUI.add(settings, 'interactive').onChange(settings.enableInteractivity); var body = document.querySelector('body'); canvas = document.createElement('canvas'); canvas.width = innerWidth; canvas.height = innerHeight; canvas.style.backgroundColor = 'rgba(255, 255, 255, 1.0)'; canvas.style.position = 'absolute'; canvas.style.top = 0; canvas.style.bottom = 0; canvas.style.left = 0; canvas.style.right = 0; canvas.style.zIndex = -1; body.appendChild(canvas); // Browser supports canvas? if(!!(self.gotSupport())) { context = canvas.getContext('2d'); // Events if('ontouchstart' in window) canvas.addEventListener('touchmove', self.onTouchMove, false); else canvas.addEventListener('mousemove', self.onMouseMove, false); window.onresize = onResize; // Let's animate our lines self.animate(); } else { console.error('Sorry, your browser sucks alot. Update it soon!'); } }; /* * On resize window event. */ function onResize() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; } /* * Checks if browser supports canvas element. */ self.gotSupport = function() { return canvas.getContext && canvas.getContext('2d'); }; /* * Mouse move event. */ self.onMouseMove = function(event) { event.preventDefault(); mouse.x = event.pageX - canvas.offsetLeft; mouse.y = event.pageY - canvas.offsetTop; }; /* * Touch move event. */ self.onTouchMove = function(event) { event.preventDefault(); mouse.x = event.touches[0].pageX - canvas.offsetLeft; mouse.y = event.touches[0].pageY - canvas.offsetTop; }; /* * Create the lines. */ self.createLines = function() { for(var quantity = 0, len = maxLines; quantity < len; quantity++) { lines.push({ x: self.randomBetween(size, innerWidth - size), y: self.randomBetween(size, innerHeight - size), lastX: mouse.x, lastY: mouse.y, vx: 0, vy: 0, easeX: Math.random() * 0.2 + 0.2, easeY: Math.random() * 0.2 + 0.2, }); } }; /* * Let's animate our orbit. */ self.animate = function() { if(lines.length === 0) self.createLines(); // Logic self.clear(); self.update(); self.render(); requestAnimFrame(self.animate); }; /* * Clear the whole screen, with a little effect of trail. */ self.clear = function() { context.fillStyle = 'rgba(255, 255, 255, ' + fade + ')'; context.fillRect(0, 0, canvas.width, canvas.height); }; /* * Update the animation. */ self.update = function() { fade += (trail - fade) * 0.08; [].forEach.call(lines, function(line, index) { if(!interactive) { var time = new Date().getTime() * 0.001; // Random movement - (thanks to @TaminoMartinius) mouse.x = ~~(canvas.width * 0.5 + canvas.width * 0.5 * Math.sin(time) * Math.tan(Math.sin(time * 1.5))); mouse.y = ~~(canvas.height * 0.5 + canvas.height * 0.5 * Math.sin(time * 2) * Math.tan(Math.cos(time))); } // Last position line.lastX = line.x; line.lastY = line.y; line.x += line.vx; line.y += line.vy; // Spring line.vx += (((mouse.x - line.x) * line.easeX) - line.vx) / spring; line.vy += (((mouse.y - line.y) * line.easeY) - line.vy) / spring; // Friction line.vx *= friction; line.vy *= friction; }); }; /* * Render the animation. */ self.render = function() { [].forEach.call(lines, function(line, index) { context.save(); context.globalAlpha = 1.0; context.strokeStyle = color; context.lineWidth = size; context.lineCap = 'round'; context.lineJoin = 'round'; context.beginPath(); context.moveTo(line.lastX, line.lastY); context.lineTo(line.x, line.y); context.closePath(); context.stroke(); context.restore(); }); }; /* * Useful function for random integer between [min, max]. */ self.randomBetween = function(min, max) { return ~~(Math.random() * (max - min + 1) + min); }; /* * Request new frame by Paul Irish. * 60 FPS. */ window.requestAnimFrame = (function() { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) { window.setTimeout(callback, 1000 / FPS); }; })(); window.addEventListener ? window.addEventListener('load', self.init, false) : window.onload = self.init;
})(Lines);
Developer | Francesco Trillini |
Username | Francext |
Uploaded | September 01, 2022 |
Rating | 4.5 |
Size | 4,499 Kb |
Views | 30,360 |
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 |
Orbit | 4,605 Kb |
Music Player Concept | 381,176 Kb |
Molecules | 4,760 Kb |
Typo | 4,738 Kb |
Tiling Subdivision | 3,937 Kb |
Waviness | 4,500 Kb |
Pulsar | 5,686 Kb |
Concentrics | 4,301 Kb |
TweetLove | 6,715 Kb |
Twitter Unfold Concept | 2,343 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 |
Scifi-style Interative Form | Aaronchuo | 4,566 Kb |
CSS-Flexbox-Demo | Sstiglets | 1,709 Kb |
Portfolio Layout Flex | Lahvjal | 3,489 Kb |
Particle Motion trajectories | Sniejadlik | 5,899 Kb |
Force Counter | Kenlauguico | 2,732 Kb |
CSS Letter animations | Sladix | 2,116 Kb |
Prototype Responsive Homepage | Heyitsolivia | 7,677 Kb |
Mobile first social buttons with no iframe | Alistairtweedie | 3,158 Kb |
A Pen by Eka Risyana | Risyana | 3,705 Kb |
Boom | Mhrjnsa1 | 1,794 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!