Built with blockbuilder.org
Last active
October 31, 2016 17:01
-
-
Save clhenrick/a51dc05589a967ea8d1aa40719b21cec to your computer and use it in GitHub Desktop.
D3 Canvas color transition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<style> | |
html, body { margin:0;position:relative; width: 100%; height: 100%; background: #222 } | |
canvas { position: absolute; display: block; top: calc(50% - 200px); left: calc(50% - 200px);} | |
</style> | |
</head> | |
<body> | |
<canvas id="canvas" width=400 height=400></canvas> | |
<script> | |
var canvas = d3.select("#canvas"); | |
var context = canvas.node().getContext('2d'); | |
var i = d3.interpolateLab('red', 'purple'); | |
var setColor = function setColor(val) { | |
context.clearRect(0,0,400,400); | |
context.fillStyle = val; | |
context.arc(200, 200, 200, 0, 2 * Math.PI, true); | |
context.fill(); | |
context.closePath(); | |
} | |
var x = 0; | |
var t = d3.interval(function() { | |
if (x < 1) { | |
x += 0.01; | |
setColor(i(x)); | |
} else { | |
x = 0; | |
} | |
}, 100); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment