Skip to content

Instantly share code, notes, and snippets.

@doc22940
Forked from veltman/README.md
Created May 10, 2020 22:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doc22940/d8e15de1bbf0b12683d6af47c8ef9f8c to your computer and use it in GitHub Desktop.
Save doc22940/d8e15de1bbf0b12683d6af47c8ef9f8c to your computer and use it in GitHub Desktop.
Canvas animation to video
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<canvas width="960" height="500"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.3/d3.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>
<script>
var canvas = document.querySelector("canvas"),
context = canvas.getContext("2d"),
width = +canvas.width,
height = +canvas.height;
var projection = d3.geoOrthographic().scale(195).translate([width / 2, height / 2]).precision(0.1);
var path = d3.geoPath().projection(projection).context(context);
d3.json("/mbostock/raw/4090846/world-110m.json", function(err, world) {
var land = topojson.feature(world, world.objects.land),
mesh = topojson.mesh(world, world.objects.countries, function(a, b) {
return a !== b;
});
var data = [],
stream = canvas.captureStream(),
recorder = new MediaRecorder(stream, { mimeType: "video/webm" });
var timer = d3.timer(function(t) {
t = t / 3000;
if (t < 1) {
draw(t);
} else {
recorder.stop();
timer.stop();
}
});
recorder.ondataavailable = function(event) {
if (event.data && event.data.size) {
data.push(event.data);
}
};
recorder.onstop = () => {
var url = URL.createObjectURL(new Blob(data, { type: "video/webm" }));
d3.select("canvas").remove();
d3.select("body")
.append("video")
.attr("src", url)
.attr("controls", true)
.attr("autoplay", true);
};
recorder.start();
function draw(t) {
projection.rotate([360 * t]);
context.lineWidth = 1;
context.fillStyle = "#fff";
context.fillRect(0, 0, width, height);
context.strokeStyle = "#222";
context.beginPath();
path({ type: "Sphere" });
context.stroke();
context.fillStyle = "#222";
context.beginPath();
path(land);
context.fill();
context.strokeStyle = "#fff";
context.beginPath();
path(mesh);
context.stroke();
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment