Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3 transitions , less cpu usage?

I'm currently using d3 transitions to animate graphs. Unfortunately these transitions make the page to redraw continuously, as a result cpu is always around 100%.

d3Element.attr("transform", "translate(" + this.someDistance + ")")
                                   .attr("d", linePath)
                                   .transition()
                                   .ease("linear")
                                   .duration(animationDuration)
                                   .attr("transform", "translate(" + 0 + ")");

I have already found a simple solution to this problem and I will share it with you but I would like to know if there is any better way to do it with d3.

like image 583
George Sofianos Avatar asked Oct 21 '22 12:10

George Sofianos


1 Answers

What I'm actually doing to reduce cpu usage is to limit the frames per second.

This is a part of the code :

var start = d3.transform( "translate(" + this.startPoint.x  + "," + this.startPoint.y + ")");
var stop  = d3.transform("translate(" + this.stopPoint.x   + "," + this.stopPoint.y  + ")");
var interpolate = d3.interpolateTransform(start,stop); 

   var animation_interval = window.setInterval(function(){

         frame++;
         // Get transform Value and aply it to the DOM
         var transformValue = interpolate(frame/(self.fps * self.duration));
         self.d3Selector.attr("transform", transformValue);

         // Check if animation should stop
         if(frame >= self.fps * self.duration || self.stopFlag){
               window.clearInterval(animation_interval);
               return;
         }


   },intervalTime);
like image 130
George Sofianos Avatar answered Oct 30 '22 15:10

George Sofianos