

Preserve the size of the descendant elements when scaling the parent element
source link: https://www.codesd.com/item/preserve-the-size-of-the-descendant-elements-when-scaling-the-parent-element.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Preserve the size of the descendant elements when scaling the parent element
I have an XHTML page with SVG embedded into it as an <svg>
element. I need to implement scaling, but there is a requirement that inner <text>
elements must not scale. An obvious solution is
<svg ...>
<!-- Root <g> to programmatically `setAttribute("transform", ...)` -->
<g transform="scale(0.5)">
<!-- Various inner elements here -->
<!-- Here is a text element.
Additional <g> is to apply text position which
*must* depend on the scaling factor set above -->
<g transform="translate(100 50)">
<!-- Apply the opposite scaling factor.
Must be done for every `<text>` element on each change of the scaling factor... -->
<text x="0" y="0" transform="scale(2)">Hello, World!</text>
</g>
</g>
</svg>
Is there a better solution than that? Maybe, there is a way to "reset" the resulting scaling factor on inner <text>
elements? The code must work in Firefox and Chrome.
UPD. There is also an option to place text elements outside the element being scaled and manually control text elements' positions. It avoids visual glitches appearing on the text because of scaling. Currently I use this method.
There is transform="ref(svg)"
which is defined in SVG Tiny 1.2. To the best of my knowledge this is not implemented in any browsers except Opera (Presto).
<svg xmlns="http://www.w3.org/2000/svg" font-size="24" text-rendering="geometricPrecision">
<!-- Root <g> to programmatically `setAttribute("transform", ...)` -->
<text x="0" y="1em" stroke="gray" fill="none">Hello, World!</text>
<g transform="scale(0.5) rotate(25)">
<!-- Various inner elements here -->
<!-- Here is a text element.
Additional <g> is to apply text position which
*must* depend on the scaling factor set above -->
<g transform="translate(100 50)">
<!-- Apply the opposite scaling factor.
Must be done for every `<text>` element on each change of the scaling factor... -->
<text x="0" y="1em" transform="ref(svg)">Hello, World!</text>
</g>
</g>
</svg>
In the above example the text should appear where the gray outline is (in the top-left corner). No rotation or scaling should be applied to the element that has transform="ref(svg)"
, for the purposes of transformations it's as if it was a direct child of the root svg element.
Recommend
-
35
README.md ...
-
27
Descendant X custom ROM for the Xiaomi Mi A1 and Mi 9 adds “Guardia,” a background permission monitorWe may earn a commission for purchases m...
-
2
Webdriver how to find the parent field set element of a given element using Xpath advertisements I have a HTML like the one below
-
10
New issue Add fast path to is_descendant_of #91043
-
10
[S][CORAL] [February] Descendant 12 ny-hardcore Senior Member
-
7
Best approach to raising the visibility of the method in a descendant class advertisements Best way to elaborate on the que...
-
10
How to get the position of an element relative to the parent using jQuery 3557 views 2 years ago jQuery Use...
-
8
A PC closed beta is scheduled for this fall
-
13
The First Descendant will be the debut game to support the HDR10+ GAMING standard...
-
7
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK