ARCTO in SVG specification is quite different from the one we have in Canvas. I have a use case where I will have the data as per SVG spec but I need to draw that on Canvas.
I tried this but I guess my geometry is weak. Can you please help?
I had the same problem so I came across this post. The Implementation Requirements Appendix of the W3C SVG definition tells exactly how to convert form (they call it) end point parameterization to the center parameterization and back:
The SVG arc (end point parameterization) is described by:
x
and y
values of this path command)The canvas arc uses (center point parameterization):
This means converting from SVG to canvas you can use the following equations (taken directly from the given url from W3C):
Compute
(x1′, y1′)
(Equation F.6.5.1)Compute
(cx′, cy′)
(Equation F.6.5.2)where the + sign is chosen if fA ≠ fS, and the − sign is chosen if fA = fS.
Compute
(cx, cy)
from(cx′, cy′)
(Equation F.6.5.3)Compute θ1 and Δθ (Equations F.6.5.5 and F.6.5.6)
Edit: I am now using other equations, have a look at the bottom
where θ1 is fixed in the range −360° < Δθ < 360° such that:
if fS = 0, then Δθ < 0,
else if fS = 1, then Δθ > 0.
In other words, if fS = 0 and the right side of (F.6.5.6) is greater than 0, then subtract 360°, whereas if fS = 1 and the right side of (F.6.5.6) is less than 0, then add 360°. In all other cases leave it as is.
Copyright © 16 August 2011 World Wide Web Consortium, (MIT, ERCIM, Keio, Beihang). http://www.w3.org/Consortium/Legal/2015/doc-license
I am now using the following equations for determining θ1 and Δθ:
This is simply the vectors between the start and end point of the arc and the center point. The φ is subtracted because of the angle is calculated before rotation. You may just leave this away if needed.
I received wrong results of the given equations but this may also be a bug in my implementation. When trying to find the bug I was thinking about what W3C is doing here. I was looking on how to calculate the angles and this was the first thing I thought about. This is leading to the correct results for me.
I also ran into problems when using the W3C equations when converting back. This may be because of changing the angles. For getting from Canvas to SVG you need to convert the start and end angles (θ1 and θ2 = θ1 + Δθ) together with the center point to the intersections of the arc. Those are the start and end points of the SVG arc.
Compute (x1', y1')
and (x2', y2')
This is calculating the intersection for the line which is defined by the given angle θ1/θ2 in the rotated coordinate system. For the x coordinate the + sign should be chosen, when the -π/2 ≤ θ ≤ π/2. The + sign for the y coordinate should be chosen when 0 ≤ θ ≤ π.
Compute (x1, y1)
and (x2, y2)
The x and y coordinate of the start and end points can then be calculated by rotating back the rotation angle φ and translating the vector to the center of the ellipse.
Find the flags
The flags can easily be determined: fA is 1 if Δθ is greater than 180°, fS is 1 if Δθ is greater than 0°.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With