Skip to content

Instantly share code, notes, and snippets.

@chabb
Last active March 9, 2018 05:40
Show Gist options
  • Select an option

  • Save chabb/aad15b37306fb15ea037a5ebd8f2ceb6 to your computer and use it in GitHub Desktop.

Select an option

Save chabb/aad15b37306fb15ea037a5ebd8f2ceb6 to your computer and use it in GitHub Desktop.
Animation with async/await
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<script src="http://d3js.org/d3.v4.min.js" charset="utf-8" type="text/javascript"></script>
</head>
<style>
html,body {
height: 100%;
width: 100%;
margin: 0;
}
</style>
<script>
async function start() {
// some crap
d3.transition.prototype.end = function() {
return new Promise((resolve, reject) => {
this.on("interrupt", reject).on("end", resolve);
});
};
const w = 500;
const h = 500;
const r = 20;
const t = 1500;
const svg = d3.select('svg');
const circle = svg.append("circle").attr("r", r).attr("cx", w / 4).attr("cy", h / 4);
while (true) {
console.log('1');
await circle.transition().duration(t).attr("cy", h * 3 / 4).end();
console.log('2');
await circle.transition().duration(t).attr("cx", w * 3 / 4).end();
console.log('3');
await circle.transition().duration(t).attr("cy", h * 1 / 4).end();
console.log('4');
await circle.transition().duration(t).attr("cx", w * 1 / 4).end();
}
}
</script>
<body onload="start()">
<svg width="500" height="500"></svg>
</body>
<html/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment