JSFiddle - React, Tailwind, and code Playground
by askhflajsf
HTML
<!DOCTYPE html>
<meta charset="utf-8">
<title>Canvas Swarm</title>
<style>
canvas {
position: absolute;
top: 0;
}
</style>
<div id="fps">FPS: <span>?</span></div>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var data = d3.range(500).map(function() {
return {xloc: 0, yloc: 0, xvel: 0, yvel: 0};
});
var width = 600,
height = 400,
angle = 2 * Math.PI;
var x = d3.scale.linear()
.domain([-5, 5])
.range([0, width]);
var y = d3.scale.linear()
.domain([-5, 5])
.range([0, height]);
var time0 = Date.now(),
time1;
var canvas = d3.select("body").append("canvas")
.attr("width", width)
.attr("height", height);
var context = canvas.node().getContext("2d");
context.fillStyle = "#ccc";
context.strokeStyle = "#fff";
d3.timer(function() {
context.clearRect(0, 0, width, height);
data.forEach(function(d) {
d.xloc += d.xvel;
d.yloc += d.yvel;
d.xvel += 0.4 * (Math.random() - .5) - 0.1 * d.xvel - 0.1 * d.xloc;
d.yvel += 0.4 * (Math.random() - .5) - 0.1 * d.yvel - 0.1 * d.yloc;
context.beginPath();
context.arc(x(d.xloc), y(d.yloc), Math.min(1 + 1000 * Math.abs(d.xvel * d.yvel), 10), 0, angle);
context.fill();
context.stroke();
});
});
</script>