Skip to content

Instantly share code, notes, and snippets.

@walkerjeffd
Created May 12, 2017 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walkerjeffd/cc8388c64572499963096bc72ca7fd13 to your computer and use it in GitHub Desktop.
Save walkerjeffd/cc8388c64572499963096bc72ca7fd13 to your computer and use it in GitHub Desktop.
Spinning Ovals
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
.stop-left {
stop-color: RGBA(0, 192, 191, 0.5); /* Indigo */
}
.stop-right {
stop-color: RGBA(44, 161, 252, 0.5); /* Teal */
}
.outlined {
fill: none;
stroke: url(#mainGradient);
stroke-width: 25;
}
</style>
</head>
<body>
<script>
var width = 960,
height = 500;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
var svgDefs = svg.append('defs');
var mainGradient = svgDefs.append('linearGradient')
.attr('id', 'mainGradient');
// Create the stops of the main gradient. Each stop will be assigned
// a class to style the stop using CSS.
mainGradient.append('stop')
.attr('class', 'stop-left')
.attr('offset', '0');
mainGradient.append('stop')
.attr('class', 'stop-right')
.attr('offset', '1');
var oval1 = svg.append("ellipse")
.classed("outlined", true)
.attr("cx", width / 2)
.attr("cy", height / 2)
.attr("rx", 110)
.attr("ry", 100);
var oval2 = svg.append("ellipse")
.classed("outlined", true)
.attr("cx", width / 2)
.attr("cy", height / 2)
.attr("rx", 110)
.attr("ry", 100);
d3.timer(function (elapsed) {
var speed = 1500,
offset = 75,
omega1 = elapsed % speed / speed * 360,
omega2 = elapsed % speed / speed * 360 - offset;
oval1.attr("transform", "rotate(" + omega1 + "," + width/2 + "," + height/2 + ")")
oval2.attr("transform", "rotate(" + omega2 + "," + width/2 + "," + height/2 + ")")
})
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment