Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping SVG ARCTO to HTML Canvas ARCTO

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?

like image 395
AppleGrew Avatar asked Dec 22 '22 11:12

AppleGrew


1 Answers

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:

  • x1/y1: The start position (the position of the last path command)
  • x2/y2: The end position of the arc (the x and y values of this path command)
  • rx/ry: The x and y radius
  • φ: The rotation angle
  • fA: The large arc flag (1 or 0, whether to use the larg or the small arc)
  • fS: The sweep flag (whether to go clockwise or anticlockwise)

The canvas arc uses (center point parameterization):

  • cx/cy: The center point of the ellipse
  • rx/ry: The x and y radius
  • φ: The rotation angle
  • θ1: The start angle of the ellipse (before rotation)
  • Δθ: The angle distance to use of the ellipse (direction depends on the sweep flag fS, you can also calculate the end point θ2 which may be even better)

Convert from SVG to Canvas

This means converting from SVG to canvas you can use the following equations (taken directly from the given url from W3C):

  1. Compute (x1′, y1′) (Equation F.6.5.1)

    Equation F.6.5.1

  2. Compute (cx′, cy′) (Equation F.6.5.2)

    Equation F.6.5.2

    where the + sign is chosen if fA ≠ fS, and the − sign is chosen if fA = fS.

  3. Compute (cx, cy) from (cx′, cy′) (Equation F.6.5.3)

    Equation F.6.5.3

  4. 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

    Equation F.6.5.5

    Equation F.6.5.6

    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

Edit: Modified equations for step 4.

I am now using the following equations for determining θ1 and Δθ:

Modified equation F.6.5.5

Modified Equation F.6.5.6

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.

Convert from Canvas to SVG

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.

  1. Compute (x1', y1') and (x2', y2')

    Compute x1', y1', x2' and y2'

    This is calculating the intersection for the line which is defined by the given angle θ12 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 ≤ θ ≤ π.

  2. Compute (x1, y1) and (x2, y2)

    compute x1, y1, x2 and 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.

  3. 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°.

like image 174
miile7 Avatar answered Jan 13 '23 20:01

miile7