You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Johan Brelet <jo...@gmail.com> on 2009/06/05 12:02:15 UTC
Modify a SVGMAtrix
Hi all,
I'm trying to work with matrix. What I'd like is to get the transformation
matrix of a shape compared to root of SVG, and remove only "translate"
transformations. Here is how I'm currently doing it :
//svg = SVGSVGElement
//myshape = a shape (rect, circle, ...)
SVGMatrix matrix = myshape.getTransformToElement(svg);
//Only take into account "translate" transformations, e.g. matrix.e (tx) and
matrix.f (ty)
matrix.setA(1);
matrix.setB(0);
matrix.setC(0);
matrix.setD(1);
But the matrix doesn't seems to be updated (a b c and d are kept
unchanged)...
Am I doing something wrong?
Johan
Re: Modify a SVGMAtrix
Posted by Johan Brelet <jo...@gmail.com>.
Thanks for your help.
In fact I finally chose to convert the SVG DOM Elements into awt Shapes.
So now I can use the SVGMatrix returned by getTransformToElement() method to
create an AffineTransform instance.
Then I use AffineTransform.createTransformedShape() method.
Here is the code:
SVGSVGElement svg= document.getRootElement();
SVGMatrix matrix = (SVGMatrix) element.getTransformToElement(svg);
AffineTransform shapeTransform = new AffineTransform(matrix.getA(),
matrix.getB(), matrix.getC(), matrix.getD(), matrix.getE(), matrix.getF());
GraphicsNode shapeNode = (GraphicsNode)
bridgeContext.getGraphicsNode(element);
Shape shape = ((ShapeNode) shapeNode).getShape();
Shape transformedShape = shapeTransform.createTransformedShape(shape);
Regards.
2009/6/19 <th...@kodak.com>
>
> Paul Wellner Bou <pa...@purecodes.org> wrote on 06/19/2009 02:22:02 AM:
>
> > [...] with other (SVG)Elements, I am doing this in plain xml attributes:
> >
> > String transformMatrix= "matrix(" + mat.getA() + " " + mat.getB() +
> > " " + mat.getC() + " " + mat.getD() + " " + mat.getE() + " "
> > + mat.getF() + ")";
> > myElement.setAttribute("transform", transformMatrix);
>
> > Johan Brelet <jo...@gmail.com> wrote on 06/05/2009 06:02:15 AM:
>
> >> I'm trying to work with matrix. What I'd like is to get the
> >> transformation matrix of a shape compared to root of SVG, and remove
> >> only "translate" transformations. Here is how I'm currently doing it :
> >>
> >> SVGMatrix matrix = myshape.getTransformToElement(svg);
>
> Paul is pointing in the right direction. The matrix returned
> from getTransformToElement is not a changeable matrix. So modifying it
> does not change the document in any way.
>
> > //Only take into account "translate" transformations, e.g. matrix.e
> > (tx) and matrix.f (ty)
> > matrix.setA(1);
> > matrix.setB(0);
> > matrix.setC(0);
> > matrix.setD(1);
> >
> > But the matrix doesn't seems to be updated (a b c and d are kept
> unchanged)...
> > Am I doing something wrong?
>
> You need to copy the matrix into one you create from the SVG root
> element. Then you need
> to set the matrix on something (similar to what Paul suggests).
>
Re: Modify a SVGMAtrix
Posted by th...@kodak.com.
Paul Wellner Bou <pa...@purecodes.org> wrote on 06/19/2009 02:22:02 AM:
> [...] with other (SVG)Elements, I am doing this in plain xml attributes:
>
> String transformMatrix= "matrix(" + mat.getA() + " " + mat.getB() +
> " " + mat.getC() + " " + mat.getD() + " " + mat.getE() + " "
> + mat.getF() + ")";
> myElement.setAttribute("transform", transformMatrix);
> Johan Brelet <jo...@gmail.com> wrote on 06/05/2009 06:02:15 AM:
>> I'm trying to work with matrix. What I'd like is to get the
>> transformation matrix of a shape compared to root of SVG, and remove
>> only "translate" transformations. Here is how I'm currently doing it :
>>
>> SVGMatrix matrix = myshape.getTransformToElement(svg);
Paul is pointing in the right direction. The matrix returned
from getTransformToElement is not a changeable matrix. So modifying it
does not change the document in any way.
> //Only take into account "translate" transformations, e.g. matrix.e
> (tx) and matrix.f (ty)
> matrix.setA(1);
> matrix.setB(0);
> matrix.setC(0);
> matrix.setD(1);
>
> But the matrix doesn't seems to be updated (a b c and d are kept
unchanged)...
> Am I doing something wrong?
You need to copy the matrix into one you create from the SVG root
element. Then you need
to set the matrix on something (similar to what Paul suggests).
Re: Modify a SVGMAtrix
Posted by Paul Wellner Bou <pa...@purecodes.org>.
Hi,
I searched for this, too and spent hours, but did not find anything. For
SVGPoints there is matrixTransform(), as documented at the bottom of
http://xmlgraphics.apache.org/batik/using/scripting/java.html. But with
other (SVG)Elements, I am doing this in plain xml attributes:
String transformMatrix= "matrix(" + mat.getA() + " " + mat.getB() +
" " + mat.getC() + " " + mat.getD() + " " + mat.getE() + " "
+ mat.getF() + ")";
myElement.setAttribute("transform", transformMatrix);
Another way would be to create a SVGPoint, transform it to the
position/rotation you want and copy the transform attribute to your
other element. Not tried though...
If there is a better and nicer way... Would be great.
Regards
Paul.
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org