Coder Social home page Coder Social logo

understand-svg-arcs's Introduction

Understanding SVG arcs

SVG paths can be daunting at first, but upon closer examination, most of the commands can be understood pretty intuitively.

The syntax of the arc command, however, seems to be an exception. This repo provides some resources that hopefully make its syntax clearer.

Explanation

The awkward syntax of the arc command is due to it being designed to match the structure of the other SVG path commands, all of which consist of one or more pairs of (x,y) coordinates, the last one being the coordinates of the new "current point" of the "pen" (to follow the analogy of a plotter).

However, just like a circle is defined more intuitively by specifying the center and radius, an elliptical arc is also best understood if defined by its center, the minor and major radius, the starting angle and the angular span it covers. The fact that SVG requires a different way to specify an arc is what makes it weird and cumbersome to understand.

The best way to think of SVG's "endpoint parametrization" is as providing parameters to a constraint solver: the arc has to follow an ellipse with a given width/height ratio (x-radius vs. y-radius), with a given rotationโ€ , and that passes through the start point given at the end of the preceding path command, and the end point given at the end of the arc command.

โ€ Note that the rotation maintains the start and end points, whereas a rotation with the transform attribute (outside the path description) would cause the entire path, including the start and end points, to be rotated.

These restrictions (with a few adjustments to prevent unsolvable constraints) result in two possible ellipses, and four corresponding elliptical arcs connecting the two endpoints. The large arc and sweep flags (specified as 0 or 1) then provide disambiguation between these four arc possibilities, giving a unique, unambiguous (but highly unintuitive) arc parametrization.

Practical example

The live demo page (screenshot below), showcases a few variations of an arc command with the same starting and ending point (100,200 and 250,150), along with the ellipses that are used for their construction, as a practical example that may help understand the textual description above.

screenshot

Resources

The demo above takes inspiration from the excellent diagram in the OReilly Commons wiki.

The SVG implementation of this diagram was inspired by Jakob Jenkov's excellent svg paths tutorial, with some help from his svg markers tutorial for the arrow heads. He also made a video version of the svg paths tutorial (content about arcs starts at 2:52), which helps visualize the effects of the arc command parameters.

Another nice video explanation is this one by Glenn Howes (content about arcs starts at 10:42). The iOS app used in the video seems to be an interesting way to experiment with arcs interactively.

understand-svg-arcs's People

Contributors

waldyrious avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

barionleg

understand-svg-arcs's Issues

Make the live demo interactive

Perhaps there could be a single main diagram with controls (sliders?) for each parameter, and maybe the current images could work as buttons to set up specific presets (combinations of parameters).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.