javascript - Is there a performance difference between SVG attributes and styles? -
i'm working large svg particle animation involve lots of little elements. 1000. browsers seem struggle when animate many elements, i'm looking ways optimize code better, smoother performance (especially firefix).
my main question whether there difference between, example, setting element's stroke
property attribute, or as style:
<circle r="0.15" cx="84" cy="782" stroke-width="6" stroke="transparent" style="fill: #ffffff;"></circle>
vs
<circle r="0.15" cx="84" cy="782" stroke-width="6" stroke="transparent" style="stroke:transparent; stroke-width:6; fill: #ffffff;"></circle>
i'm using d3.js add bunch of these , make them move around... , 1000 appears limit before things clunky in chrome. firefox worse.
if have other performance suggestions they're welcome.
update request:
here how create nodes:
var makenode = function(coeficient, x, y) { coeficient = coeficient || 1; return { radius: (math.random() * coeficient ).tofixed(2), cx: function() { return x || math.round(math.random()*width) }, cy: function() { return y || math.round(math.random()*height) } } }; var nodes1 = d3.range(300).map( function(){ return makenode(1.9); } ); var nodes2 = d3.range(700).map( function(){ return makenode(.6); } ); // var nodes2 = []; var svg = d3.select('#sky_svg'); var group1 = svg.append('g').attr("class", "group1"); var group2 = svg.append('g').attr("class", "group2"); var addnodes = function(group, nodes) { (var i=0; i<nodes.length; i++){ var node = nodes[i]; var circle = group.append('circle'); circle .attr("r", node.radius ) .attr("cx", node.cx ) .attr("cy", node.cy ) .attr("stroke-width", 8 ) .attr("stroke", "transparent") .style("fill", "#ffffff"); } } addnodes( group1, nodes1 ); addnodes( group2, nodes2 );
and how i'm animating them:
function orbit( target, ease, duration ) { /* other easing options here: https://github.com/mbostock/d3/wiki/transitions#wiki-d3_ease */ ease = ease || 'linear'; duration = duration || 40000; target .transition() .duration( duration ) .ease( ease ) .attrtween("transform", rottween) .each('end', function(){ orbit( group1, false ); } ); function rottween() { var = d3.interpolate(0, 360); return function(t) { return "rotate(" + i(t) + ","+width/2+","+height/2+")"; }; } } orbit( group1, 'sin-in', orbitspeed ); orbit( group2, 'sin-in', orbitspeed*2.5 );
i'm not married implementation. original design used force layout firefox struggled really hard load , ran terribly refactored code use simple rotation (which works smoothly in ff).
circles have baseval
cx,cy,r. has been suggested accessing these attributes, via baseval
. more effecient tnan using setattribute
or attr
(in d3). e.g.
mycircle.cx.baseval.value=mycx mycircle.cy.baseval.value=mycy mycircle.r.baseval.value=myr
Comments
Post a Comment