Skip to content

Instantly share code, notes, and snippets.

@sxywu
Last active July 28, 2016 20:26
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 sxywu/269e3ab4053c08ac122ee2a9db0d61a2 to your computer and use it in GitHub Desktop.
Save sxywu/269e3ab4053c08ac122ee2a9db0d61a2 to your computer and use it in GitHub Desktop.
DS July: Code 3
license: gpl-3.0
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.js'></script>
<style>
svg {
width: 1200px;
height: 10000px;
}
</style>
</head>
<body>
<svg>
</svg>
<script>
var padding = 20;
var svg = d3.select('svg')
.append('g')
.attr('transform', 'translate(' + [padding, padding] + ')');
var flowerSize = 300;
var petalPaths = [[
'M0,0',
"C50,50 50,100 0,100",
"C-50,100 -50,50 0,0"
],
[
'M-35,0',
'C-25,25 25,25 35,0',
'C50,25 25,75 0,100',
'C-25,75 -50,25 -35,0'
],
[
'M0,0',
'C50,40 50,70 20,100',
'L0,85',
'L-20,100',
'C-50,70 -50,40 0,0'
],
[
'M0,0',
'C50,25 50,75 0,100',
'C-50,75 -50,25 0,0'
]];
var numPetals = _.range(1, 11);
var sections = svg.selectAll('g.section')
.data(numPetals).enter().append('g')
.classed('section', true)
.attr('transform', function(d, i) {
return 'translate(0,' + (i * flowerSize) + ')';
});
var flowers = sections.selectAll('g.flower')
.data(function(d) {
return _.map(petalPaths, function(path) {
return {
numPetals: d,
path: path
}
});
}).enter().append('g')
.classed('flower', true)
.attr('transform', function(d, i) {
var x = (i % 4) * flowerSize;
var y = Math.floor(i / 4) * flowerSize;
return 'translate(' + x + ',' + y + ')';
});
flowers.selectAll('path')
.data(function(d) {
// for each flower, draw the petal 6 times
return _.times(d.numPetals, function(i) {
return {
angle: (360/d.numPetals) * i,
path: d.path
}
});
}).enter().append('path')
.attr('stroke', '#000')
.attr('stroke-width', 2)
.attr('fill', 'none')
.attr('d', function(d) {return d.path})
.attr('transform', function(d) {
var cx = flowerSize / 2;
var cy = flowerSize / 2;
return 'translate(' + [cx, cy] +
')rotate(' + [d.angle] + ')';
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment