stacking transforms in D3

旧街凉风 提交于 2020-01-02 05:32:07

问题


I have an SVG element with a transform already applied to it (this could be a single translate, or a combination of multiple transforms). I'm trying to apply additional transform to it. Problem is that this transform could be applied repeatedly and needs to stack with the existing transform state (so appending additional transforms to the end is not practical). Looking through d3 API, I didn't notice anything that lets me apply a relative transform (although I must admit, I'm not that familiar with advanced features of d3). Manually parsing the current transform string and then computing the same transformation matrix that SVG already does behind the scenes for free seems silly, is there a better way?

For example, if existing element already has the following attribute:

transform="translate(30) rotate(45 50 50)"

And I invoke this transformation logic twice, wanting to offset the element by 1 pixel in each dimension each time, I would need to parse and process both, the translate and rotate calls, because the new translations cannot be applied prior to rotation.


回答1:


I've actually been thinking that there should be a special function for this, similar to the way the classed() function handles adding and remove certain classes without messing up the other ones.

However, until that happens, just access the existing attribute and then concatenate strings:

selection.attr("transform", function(d){
        return this.getAttribute("transform") +
                     " translate(30) rotate(45 50 50)";
    });

You could also use d3.select(this).attr("transform") but the raw javascript should work and saves a function call.

Just be careful not to mix up attribute transforms with style transforms.




回答2:


As Lars pointed out in a comment, d3.transform will generate the transform from the string. Applying that to my original problem, I can do the following:

element.attr('transform', d3.transform('translate(30) rotate(45 50 50) translate(1,1) translate(1,1)').toString())


来源:https://stackoverflow.com/questions/21126127/stacking-transforms-in-d3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!