Previously, I asked how to obtain the real coordinates (d) of a path when a transformation is applied, finally I decided to investigate the transformation formulas, I have already seen the translation, and now I am seeing the scaling (scale), I managed to do a scaling Basically the formula is:
x’ = x * sx
y’ = y * sy
Where:
x is equal to the x coordinate.
y is equal to the y coordinate.
sx or/and sy is equal to the scale factor.
x’ or/and y' is equal to the new size or coordinate.
But I can’t get the scale factor to increase or move the coordinates based on pixels, that is, assuming I have the following svg:
<svg width="300" height="300" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<rect x="0" y="0" width="100" height="100" fill="red" />
<path id="element" d="M5, 5 L50, 5 L50, 45 L5, 45 Z" fill="none" stroke="white" />
</svg>
The d attribute (command) of <path id="my-path" d="M5, 5 L50, 5 L50, 45 L5, 45 Z" fill="none" stroke="white" />, If I wanted to scale this path using a scale factor of 2, it would be:
M = 5 * 2, 5 * 2
M = 10, 10
L = 50 * 2, 5 * 2
L = 100, 10
L = 50 * 2, 45 * 2
L = 100, 90
L = 5 * 2, 45 * 2
L = 10, 90
// result
M10, 10 L100, 10 L100, 90 L10, 90
But, going back to the beginning, how do I make it scale based on pixels? Playing around, I managed to guess that if the scale factor is:
1.1 = 15px -> therefore, the total width and height of the path will grow every 15px.
1.01 = 0.5px -> therefore, the total width and height of the path will grow every 0.5px.
But, as I mentioned, I guess it… Is there a formula to do this conversion? I want it to scale to 1px, 2px, 2.5px, etc, etc, for now I only need 1px.
I hope you have understood what I want to do, I hope you can help me.
Note: the svg is being added directly to the HTML.


