Metaballs

Developer
Size
2,277 Kb
Views
30,360

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 Previews

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;	}	}	}
}
Metaballs - Script Codes
Metaballs - Script Codes
Home Page Home
Developer Khalkeus
Username khalkeus
Uploaded November 11, 2022
Rating 3
Size 2,277 Kb
Views 30,360
Do you need developer help for Metaballs?

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!

Khalkeus (khalkeus) Script Codes
Name
Capybaras
Waves
Warpspeed
Color Name Game
Boids ii
Live pixel sorting
Boids iii
Boids i
Particle fire
Css loaders ii
Create amazing love letters with AI!

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!