How can I apply an SVGMatrix to an array of points

2020-05-06 06:28发布

Are there built-in libraries to multiply a vector of points by an SVGMatrix?

I have an SVG drawing that has been scaled, and I want to annotate that drawing in its original coordinate system with a line that has a fixed width in screen space. (I.e. the line should not change width when zooming in or out, but lines in the image do, of course.) So, my approach is to transform the image inside a , and then take my array of points and apply the same transformation, then create a new path object at the root level using these transformed points.

I'm looking for the cleanest way to do this.

1条回答
再贱就再见
2楼-- · 2020-05-06 07:04

The svg element has methods from creating matrix objects and point objects. The matrix object has methods for matrix operations (e.g. multiply, translate, scale, etc). The point object has method to apply matrix transform.

For example...

var svg = document.getElementById("mySvg");
var matrix1 = svg.createSVGMatrix();
var matrix2 = matrix1.translate(2, 3);
var point1 = svg.createSVGPoint();
point1.x = 1;
point1.y = 1;
var point2 = point1.matrixTransform(matrix2);

Documentation for the matrix and point objects can be found at...

http://www.w3.org/TR/SVG/single-page.html#coords-InterfaceSVGPoint http://www.w3.org/TR/SVG/single-page.html#coords-InterfaceSVGMatrix

查看更多
登录 后发表回答