Metaballs
How do I make an metaballs?
What is a metaballs? How do you make a metaballs? This script and codes were developed by Khalkeus on 11 November 2022, Friday.
Metaballs - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>metaballs</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <canvas id = "canvas"></canvas> <script src="js/index.js"></script>
</body>
</html>
Metaballs - Script Codes CSS Codes
body, html{ width: 100%; height: 100%; margin: 0px; padding: 0px; overflow: hidden;
}
canvas{ width: 100%; height: 100%;
}
Metaballs - Script Codes JS Codes
var canvas, ctx, metaball, mouse;
window.onload = function () { canvas = document.getElementById("canvas"); canvas.width = window.innerWidth; canvas.height = window.innerHeight; mouse = [0, 0]; ctx = canvas.getContext("2d"); metaball = new Metaball(30, ctx, canvas.width, canvas.height); render();
}
window.onresize = function(){ canvas.width = window.innerWidth; canvas.height = window.innerHeight; metaball.width = canvas.width; metaball.height = canvas.height;
}
window.onmousemove = function(e){ mouse[0] = e.clientX; mouse[1] = e.clientY;
}
function render(){ ctx.fillStyle = "black"; ctx.fillRect(0, 0, canvas.width, canvas.height); metaball.update(canvas.width, canvas.height, 10); requestAnimationFrame(render);
}
function ball(x, y){ this.center = [x, y]; this.direction = [Math.random()*5-2.5, Math.random()*5-2.5]; this.weight = (Math.random() + .2);
}
function Metaball(size, ctx, width, height){ this.ctx = ctx; this.balls = []; this.threshold = .0005; this.size = size; this.width = width; this.height = height; this.init = function(size){ for(var i = 0; i < size; i++){ this.balls.push(new ball(Math.random() * this.width, Math.random() * this.height)); } } this.init(this.size); this.draw = function(){ } this.update = function(width, height, increment){ this.ctx.fillStyle = "white"; for(var x = 0; x < width; x += increment){ for(var y = 0; y < height; y += increment){ var s = 0; for(var i = 0; i < this.balls.length; i++){ s += 1/((x - this.balls[i].center[0])*(x - this.balls[i].center[0]) + (y - this.balls[i].center[1])*(y - this.balls[i].center[1]))*this.balls[i].weight; } s += 1/((x - mouse[0])*(x - mouse[0]) + (y - mouse[1])*(y - mouse[1])) * 5; if(s < this.threshold){ this.ctx.fillRect(x, y, 1, 1); } } } this.ctx.fillStyle = "red"; for(var i = 0; i < this.balls.length; i++){ this.balls[i].center[0] += this.balls[i].direction[0]; this.balls[i].center[1] += this.balls[i].direction[1]; if(this.balls[i].center[0] < -100){ this.balls[i].center[0] = this.width + 100; }else if(this.balls[i].center[1] > this.width + 100){ this.balls[i].center[0] = -100; } if(this.balls[i].center[1] < -100){ this.balls[i].center[1] = this.height + 100; }else if(this.balls[i].center[1] > this.height + 100){ this.balls[i].center[1] = -100; } } }
}
Developer | Khalkeus |
Username | khalkeus |
Uploaded | November 11, 2022 |
Rating | 3 |
Size | 2,277 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 |
Capybaras | 2,775 Kb |
Waves | 4,380 Kb |
Warpspeed | 2,409 Kb |
Color Name Game | 3,245 Kb |
Boids ii | 3,358 Kb |
Live pixel sorting | 2,314 Kb |
Boids iii | 2,947 Kb |
Boids i | 3,564 Kb |
Particle fire | 3,152 Kb |
Css loaders ii | 2,294 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 |
Intake Form Page 2 | Ijantje | 4,983 Kb |
Wave Lines | Mikehobizal | 4,023 Kb |
A simple log in form made with css | Mayurelbhar | 2,160 Kb |
Pure CSS albums gallery | Renaudtertrais | 2,978 Kb |
A Pen by Brendan Skousen | Bskousen | 2,954 Kb |
Sidebar Thing | Jonambas | 2,779 Kb |
JavaScript constructors | Simboonlong | 2,415 Kb |
Rainbow Drops | Csbarnes | 2,365 Kb |
JS Beispiel getElementsByClassName 3 | HSZG-Frontend-Kurs | 1,988 Kb |
Light Switch | Bartuc | 4,933 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!