Colorful Wanderers

Developer
Size
3,142 Kb
Views
28,336

How do I make an colorful wanderers?

What is a colorful wanderers? How do you make a colorful wanderers? This script and codes were developed by Satchmorun on 28 August 2022, Sunday.

Colorful Wanderers Previews

Colorful Wanderers - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Colorful Wanderers</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <canvas id="canvas"></canvas> <script src="js/index.js"></script>
</body>
</html>

Colorful Wanderers - Script Codes CSS Codes

body { margin: 0; padding: 0; overflow: hidden;
}

Colorful Wanderers - Script Codes JS Codes

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
'floor|ceil|random|round|abs|sqrt|PI|atan2|sin|cos|pow|max|min|hypot' .split('|') .forEach(function(p) { window[p] = Math[p]; });
var TAU = PI*2;
function r(n) { return random()*n; }
function rrng(lo, hi) { return lo + r(hi-lo); }
function rint(lo, hi) { return lo + floor(r(hi - lo + 1)); }
function choose(args) { return args[rint(0, args.length-1)]; }
/*---------------------------------------------------------------------------*/
var W, H, frame, t0, time, DPR;
function resize() { var w = innerWidth; var h = innerHeight; DPR = devicePixelRatio || 1; canvas.style.width = w+'px'; canvas.style.height = h+'px'; W = canvas.width = w * DPR; H = canvas.height = h * DPR;
}
function loop(t) {
// console.log(1000/(t-t0)); t0 = t; frame = requestAnimationFrame(loop); draw(); time++;
}
function pause() { cancelAnimationFrame(frame); frame = frame ? null : requestAnimationFrame(loop);
}
function reset() { cancelAnimationFrame(frame); resize(); ctx.clearRect(0, 0, W, H); init(); time = 0; frame = requestAnimationFrame(loop);
}
/*---------------------------------------------------------------------------*/
function Vector(x, y) { this.x = x; this.y = y;
}
Vector.add = function(a, b) { return new Vector(a.x + b.x, a.y + b.y); };
Vector.sub = function(a, b) { return new Vector(a.x - b.x, a.y - b.y); };
Vector.mul = function(a, n) { return new Vector(a.x * n, a.y * n); };
Vector.div = function(a, n) { return new Vector(a.x / n, a.y / n); };
Vector.prototype.clone = function() { return new Vector(this.x, this.y);
};
Vector.prototype.add = function(other) { this.x += other.x; this.y += other.y; return this;
};
Vector.prototype.sub = function(other) { this.x -= other.x; this.y -= other.y; return this;
};
Vector.prototype.mul = function(n) { this.x *= n; this.y *= n; return this;
};
Vector.prototype.div = function(n) { this.x /= n; this.y /= n; return this;
}
Vector.prototype.mag = function() { var x = this.x, y = this.y; return sqrt(x*x + y*y);
};
Vector.prototype.normalize = function() { return this.div(this.mag());
};
Vector.prototype.constrain = function(n) { var m = this.mag(); return m > n ? this.mul(n/m) : this;
};
Vector.prototype.zero = function() { this.x = 0; this.y = 0;
};
function vec(x, y) { return new Vector(x, y);
}
/*---------------------------------------------------------------------------*/
function clamp(val, lo, hi) { if (val < lo) return lo; if (val > hi) return hi; return val;
}
function FlowField(resolution) { this.resolution = resolution; this.rows = ceil(H / resolution); this.cols = ceil(W / resolution); this.field = new Array(this.cols); for (var x = 0; x < this.cols; x++) { this.field[x] = new Array(this.rows); for (var y = 0; y < this.rows; y++) { var t = r(TAU); this.field[x][y] = vec(cos(t), sin(t)); } }
}
FlowField.prototype.lookup = function(v) { var y = floor(clamp(v.y/this.resolution, 0, this.rows-1)); var x = floor(clamp(v.x/this.resolution, 0, this.cols-1)); return this.field[x][y];
};
/*---------------------------------------------------------------------------*/
function Particle(loc, vel, max, col) { this.loc = loc; this.vel = vel; this.max = max; this.col = col; this.acc = vec(0, 0);
}
Particle.prototype.applyForce = function(force) { this.acc.add(force);
};
Particle.prototype.follow = function(field) { var force = field.lookup(this.loc); var steer = Vector.mul(force, this.max).sub(this.vel).constrain(0.1); this.applyForce(steer);
};
Particle.prototype.seek = function(target, maxf) { var desired = Vector.sub(target, this.loc); desired.normalize().mul(this.max); var steer = Vector.sub(desired, this.vel).constrain(maxf); this.applyForce(steer);
};
Particle.prototype.move = function() { this.vel.add(this.acc).constrain(this.max); ctx.beginPath(); ctx.moveTo(this.loc.x, this.loc.y); this.loc.add(this.vel); ctx.lineTo(this.loc.x, this.loc.y); ctx.strokeStyle = color(this.loc); ctx.stroke(); this.acc.zero();
};
function color(v) { var h = floor(360 * ((v.x / W) + (v.y / H))/2); var s = '50%'; var l = '33%'; return 'hsla('+h+','+s+','+l+',0.2)';
}
Particle.prototype.wrap = function() { var x = this.loc.x, y = this.loc.y; if (x < 0) this.loc.x = W; else if (x > W) this.loc.x = 0; if (y < 0) this.loc.y = H; else if (y > H) this.loc.y = 0; return this;
};
/*---------------------------------------------------------------------------*/
var P;
var particles;
var F;
var field;
function init() { P = W+H; particles = new Array(P); for (var i = 0; i < W; i++) { particles[i] = new Particle(vec(i, H/2), vec(0,0), DPR*4); } for (var i = 0; i < H; i++) { particles[W+i] = new Particle(vec(W/2, i), vec(0,0), DPR*4); } field = new FlowField(DPR*floor(min(W,H)/50));
}
function draw() { for (var i = 0; i < P; i++) { var p = particles[i]; p.wrap(); p.follow(field); p.move(); }
}
/*---------------------------------------------------------------------------*/
document.onclick = pause;
document.ondblclick = reset;
reset();
Colorful Wanderers - Script Codes
Colorful Wanderers - Script Codes
Home Page Home
Developer Satchmorun
Username satchmorun
Uploaded August 28, 2022
Rating 4.5
Size 3,142 Kb
Views 28,336
Do you need developer help for Colorful Wanderers?

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!

Satchmorun (satchmorun) Script Codes
Name
A Gooey Cursor
A Study in Red
Just playing around
Lines
Circular
Wanderers in the Dark
Signals.
Patchwork
Rectangles
F1
Create amazing captions 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!