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 Troy Taillefer <tj...@gmail.com> on 2015/09/01 16:00:15 UTC
Pan to center of element need correct coordinate conversion
Hi all,
Loving batik a lot of cool features. I have tried a lot of different things
reading the mailing lists
but I can not figure out giving an svg dom element how to get JSVGCanvas to
pan to center of that element. I will show my current attempt which
appeared to partially work on a given svg on a certain computer fails
miserably in other cases.
SVGLocatable textLocatable = (SVGLocatable) textToColor;
SVGLocatable parentLocatable = (SVGLocatable)
textToColor.getParentNode();
SVGRect bBox = textLocatable.getBBox();
// get center of text bbox
float domX = bBox.getX() + bBox.getWidth() / 2;
float domY = bBox.getY() + bBox.getHeight() / 2;
// This close to works it is off center a bit low
on the screen
// it is either due to not taking into accunt the
local matrix
// as well as the parent matrix or a miss
calculation of the
// canvas center
SVGPoint svgPoint = rootElement.createSVGPoint();
svgPoint.setX(domX);
svgPoint.setY( domY );
SVGMatrix parentMatrix = parentLocatable.getCTM();
SVGMatrix localMatrix = textLocatable.getCTM();
SVGPoint screenPoint =
svgPoint.matrixTransform(parentMatrix);
AffineTransform tx =
AffineTransform.getTranslateInstance
(-screenPoint.getX(),
-screenPoint.getY());
Dimension canvasSize = customSvgCanvas.getSize();
tx.preConcatenate(AffineTransform.getTranslateInstance
(canvasSize.width/2, canvasSize.height/2));
AffineTransform rt = (AffineTransform)
customSvgCanvas.getInitialTransform().clone();
rt.preConcatenate(tx);
customSvgCanvas.setRenderingTransform(rt);
The structure of this svg is
svg
|- g
|- a
|- text
I want to pan to center of the text giving this text svg dom node how do I
properly convert the coordinates and pan so that this text node appears in
the center of the jsvgcanvas ?
thanks
Troy