The aim of this puzzle: Animate the radius and fill color changes using .transition()
Walkthrough of the solution: To animate an attribute change, you use .transition()
. The order of .transition()
and .attr()
is important. You want the .attr()
calls come after the .transition()
, but they should still be attached. Like this: kernels.transition().attr().attr()
.
NOT these:
kernels.transition();
kernels.attr();
kernels.attr().transition()
That means you should select the kernels
inside the code block {}
, and then add .transition()
. That will attach the .transition()
directly to the kernels
so that it comes before the .attr()
calls.
Sample code solution:
(Tap below to reveal)
function heatUp() {
kernels.transition().attr('r', 30).attr('fill', 'white');
}
kernels.on('click', heatUp);
JavaScript Concepts: Code Block (function)
D3 Concepts: .attr('r',)
, .attr('fill',)
, .on('click',...)
, .transition()
Additional Code (hidden code that runs before the puzzle’s code):
function gauss(scale,iter=6) {
return scale*Array(iter).fill().map(i=>Math.random()).reduce((a,c)=>a+c)/iter;
}
for (let i=0; i<50; i++) {
svg.append('circle')
.attr('r',8)
.attr('cx',gauss(window.innerWidth))
.attr('cy',gauss(window.innerHeight))
.attr('fill','#E89800')
.attr('stroke','grey')
.attr('stroke-width',0.2);
}
let kernels = d3.selectAll('circle');