简体   繁体   中英

d3js - SVG circle in smooth perpetual motion

I am trying to animate a cloud of circles with d3js. What I'd like to get is a random smooth perpetual motion where all the circle "gravitate" around their initial position.

So far, I got this: http://jsfiddle.net/vnr1f9f8/6/

The problem here is that the motion is not smooth at all. The circles move then stops and starts again.

How can I fix this?

Here is the d3 code I used:

    var svg = d3.select('body').append('svg')
            .attr("width", 960)
            .attr("height", 480);

var circleContainer = [];

for (var i = 0; i < 10; i++) {
    var originX = 100 * (1 + 2.5 * Math.random()),
        originY = 80 * (1 + 2.5 * Math.random());

    circleContainer[i] = svg.append('circle')
            .attr('class', 'circle-' + i)
            .attr('cx', originX)
            .attr('cy', originY)
            .attr('originX', originX)
            .attr('originY', originY)
            .attr('r', 20)
            .attr('fill','red');
}


transition();

function transition() {
    for (var i = 0; i < 10; i++) {
        circleContainer[i].transition()
            .duration(1000)
            .attr('cx',  circleContainer[i].attr('originX') * (1 + Math.random()/10))
            .attr('cy',  circleContainer[i].attr('originY') * (1 + Math.random()/10))
            .each("end", transition)
            .ease("linear");
    }
}

You can add .ease("linear") to get it to be smooth. Here is jsfiddle - http://jsfiddle.net/cuckovic/vnr1f9f8/3/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM