Reactive Dot Grid - Canvas
How do I make an reactive dot grid - canvas?
Just some more canvas tomfoolery.. What is a reactive dot grid - canvas? How do you make a reactive dot grid - canvas? This script and codes were developed by Kyle Brumm on 07 January 2023, Saturday.
Reactive Dot Grid - Canvas - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Reactive Dot Grid - Canvas</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> <canvas id="canvas"></canvas> <script src="js/index.js"></script>
</body>
</html>
Reactive Dot Grid - Canvas - Script Codes CSS Codes
body { overflow: hidden; cursor: none;
}
Reactive Dot Grid - Canvas - Script Codes JS Codes
// Canvas
const c = document.getElementById('canvas');
const ctx = c.getContext('2d');
// Variables
let [cw, ch] = [window.innerWidth, window.innerHeight];
let [lastX, lastY] = [null, null];
let dots = [];
let resizeTimer;
// Settings
let [DOT_SMALL, DOT_LARGE] = [1, 20];
const HOVER_RADIUS = 60;
const DOT_DECAY = 0.15;
let [NUM_ROWS, NUM_COLS] = [Math.ceil(ch/DOT_LARGE), Math.ceil(cw/DOT_LARGE)];
let NUM_DOTS = NUM_ROWS*NUM_COLS;
// requestAnimationFrame fallback
window.requestAnimFrame = (function() { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame || function( callback ){ window.setTimeout(callback, 1000 / 60); };
})();
//--------------------------------------------------------------------
// Create a new dot
function Dot(radius, x, y, hue) {
this.radius = radius;
this.x = x;
this.y = y;
this.hue = hue; this.inHoverRadius = false;
}
// Update a dot
Dot.prototype.update = function() { // Draw the dot
ctx.beginPath();
ctx.arc( this.x, this.y, this.radius, 0, Math.PI * 2, false );
ctx.closePath();
// Style it up
ctx.fillStyle = `hsla(${this.hue}, 100%, 50%, 1)`;
ctx.fill(); // Check the mouses proximity this.checkProximity(); // Decay the radius this.decayRadius();
};
// Check the dots proximity
Dot.prototype.checkProximity = function() { let dist = DOT_SMALL; // Check if the mouse is on the canvas if (lastX && lastY) { // Get proximity let dX = this.x - lastX; let dY = this.y - lastY; dist = Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2)); // Update dot radius if (dist >= HOVER_RADIUS) { this.radius = this.radius; this.inHoverRadius = false; } else { // this.radius = DOT_SMALL/2 + ((DOT_LARGE*2) - (DOT_LARGE*(dist/100))); // this.radius = DOT_SMALL/2 + (DOT_LARGE*((HOVER_RADIUS-dist)/10)); // this.radius = DOT_SMALL/2 + (DOT_LARGE - (DOT_LARGE*dist/HOVER_RADIUS)); this.radius = DOT_SMALL/2 + (DOT_LARGE*2 - (DOT_LARGE*dist/HOVER_RADIUS)); this.inHoverRadius = true; } } else { this.inHoverRadius = false; }
}
// Check the dots radius decay
Dot.prototype.decayRadius = function() { if (!this.inHoverRadius && this.radius > DOT_SMALL) { this.radius = this.radius - DOT_DECAY; }
}
// Add a new dot
function addDot(radius, x, y, hue) { radius = radius || DOT_SMALL/2; x = x; y = y;
hue = hue || Math.floor(Math.random() * (360 - 1 + 1)) + 1; // Create the new dot let dot = new Dot(radius, x, y, hue); // Add the dot to the array dots.push(dot);
}
// Add our initial dots
function init() { dots = []; // Add the dots for (let row = DOT_LARGE; row <= ch; row += DOT_LARGE*2) { for (let col = DOT_LARGE; col <= cw; col += DOT_LARGE*2) { addDot(DOT_SMALL, col, row); } } draw();
}
// Clear the canvas and draw the new frame
function draw() {
ctx.clearRect(0, 0, cw, ch); // Update the dots for (let i = 0; i < dots.length; i++) {
dots[i].update();
} // Add helper text // ctx.fillStyle = 'black'; // ctx.textAlign='center'; // ctx.font = '12px Arial'; // ctx.fillText('Move you mouse around and watch the dots change.', (cw / 2), 15); requestAnimFrame(draw);
}
// Update the size of the canvas
function resizeCanvas() { [cw, ch] = [window.innerWidth, window.innerHeight]; // Update canvas size [c.width, c.height] = [cw, ch]; // Update rows, cols, and number of dots [NUM_ROWS, NUM_COLS] = [Math.ceil(ch/(DOT_LARGE*2))+1, Math.ceil(cw/(DOT_LARGE*2))]; NUM_DOTS = NUM_ROWS*NUM_COLS; clearTimeout(resizeTimer); resizeTimer = setTimeout(function() { init(); }, 250);
}
//--------------------------------------------------------------------
// Set mouse coordinates
c.addEventListener('mousemove', (e) => { [lastX, lastY] = [e.offsetX, e.offsetY];
}, false);
c.addEventListener('mouseout', (e) => { [lastX, lastY] = [null, null];
}, false);
// Click events
c.addEventListener('click', (e) => { // Update the dots for (let i = 0; i < dots.length; i++) {
dots[i].radius = DOT_LARGE/2;
}
}, false);
// Window resize
window.addEventListener('resize', resizeCanvas, false);
//--------------------------------------------------------------------
// Initialize the fun
resizeCanvas();
init();
Developer | Kyle Brumm |
Username | kjbrum |
Uploaded | January 07, 2023 |
Rating | 4 |
Size | 3,287 Kb |
Views | 4,048 |
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 |
CSS Only Dropdown | 2,655 Kb |
3D Orbiting Split Images | 5,806 Kb |
Stacked Cards Hover Effects | 4,514 Kb |
_toolbox.scss | 15,452 Kb |
Dotted Menu Concept | 3,327 Kb |
Simple Tabs Concept | 4,038 Kb |
Subtle Menu Animation | 2,923 Kb |
Vue.js Lazy Loading | 3,620 Kb |
Wavy Loading Animations | 12,777 Kb |
Stylish Brand Buttons | 8,832 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 |
Nested table email layout | Massimo-cassandro | 2,355 Kb |
Basic HTML5 Structure | YuvarajTana | 1,289 Kb |
Tile Animation Thing | Frxnz | 4,332 Kb |
Gradients | Karpovsystems | 2,394 Kb |
A Pen by aleen42 | Aleen42 | 11,473 Kb |
Hmmm... | Rk007 | 4,848 Kb |
Prism | Pyrografix | 2,843 Kb |
A Pen by Tosh | Panev | 2,586 Kb |
A form arranged using automatic placement. | Vikasford | 2,103 Kb |
TestTube CSS | EZPK | 2,710 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!