-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Syntax to support units & calculations in path data strings #72
Comments
Could we also have Also, should |
Interesting idea @jarek-foksa. I'd wondered about some way to access pi as a constant, hadn't thought about infinity. I assume this is so that you can do things like animate curves to straight lines by stretching control points or arc radii? As far as the DOM methods, I would want to see two separate functions, one to get the "defined" value (with units, percentages, etc.), and one to get the current "used" value (everything converted to user units / presumed degree angles). |
As far as I'm concerned, the let inf = 1000000; // pseudo-infinity
let d = `
M ${-inf}, ${-inf}
L ${inf * 2}, ${-inf}
L ${inf * 2}, ${inf * 2}
L ${-inf}, ${inf * 2}
Z
M ${x}, ${y}
L ${x}, ${y + height}
L ${x + width}, ${y + height}
L ${x + width}, ${y}
Z
`; |
You can't actually make that assumption, as it would shut out use of any future CSS unit that has a dash. We don't have plans at the moment to do such a thing, but we reserve the right to in the future. It also shuts out the ability to do math with keywords, which we do plan to allow in the future. Also makes it hard to include CSS functions, particularly var() and attr(), which are extremely useful for paths. In general, continuing to define things in ways that don't naturally work with CSS is a non-starter. They're neither future- nor present-friendly. If we're going to overhaul path syntax, I propose we do it wholesale. Current path syntax is an attempt at balancing hand-authorability against binary size, and it fails badly at both as a result: path syntax is borderline illegible unless carefully formatted, and some of the commands like arcs and curves are, for most people, impossible to write without having docs up; while on the other hand the syntax is still an ASCII format mostly composed of base-10 digits and punctuation, using ~4 bits of every 2 bytes in the string. It would be much better to design something proper for the camps: for humans, design a more verbose, easy to read/write format (borrowing syntax from CSS so it interoperates well), and then if we need to, design a compact binary format, likely embedded in SVG as base64. This would be interconvertable, with some loss of authorability (the binary format would have single canonical forms of each segment type, for example, losing details of what variant the authoring format used) but no change in behavior. (This is similar to Wasm - a compact binary format with a well-defined "decompilation" to a Lisp-like human authoring format.) |
Just stumbling over this, I realize that my proposal in #340 may be what @tabatkins suggested for human readability, although it's doing it outside the |
Not really, your #340 suggestion is far more verbose. I'm suggesting something more like just "path is a sequence of CSS functions for each command", so you get the full range of CSS values available to you. |
Okay, that would be w3c/csswg-drafts#5674 then, I guess. |
Path data (and also polygon/polyline points) can currently only be specified in terms of user units. Percentage-based coordinates, lengths, and combinations thereof are not supported.
The basic shapes support geometric parameters with units. By making them CSS properties in SVG 2, they will also support lengths defined in terms of
calc()
functions. Basic shapes can therefore only be represented as paths by converting them to the current used value (in px) for each length. A path data string cannot represent the flexible nature of a basic shape defined using percentages or font-relative units.For polygons and polylines, there is the potential to synchronise with the CSS shapes
polygon()
syntax. This syntax allows units, percentages, andcalc
functions, albeit with more stringent rules (compared to SVG) for whitespace and commas.For path data, however, there are complications:
This second aspect can actually be a good thing: since path data is not going to be parsed by CSS rules, we can create a syntax that works for this case rather than re-using CSS syntax.
As an initial proposal, I suggest introducing parentheses around any value that needs to be computed. The value inside the parentheses represents a single number in the basic path data syntax.
(1in)
,(3mm)
(0.25turn)
,(30deg)
(10%)
(100/3)
(100%-10px)
,(1em/3)
,(0.5turn + 5deg)
((1em + 1ex)/5)
Note that we'd specifically define that "units" will only ever consist of a string of letters or the
%
character, so we could avoid CSS calc's fussy rules about whitespace around mathematical operators.So, the following rounded rectangle:
would be equivalent to the following path data:
I think this strikes the necessary balance between concise & readable while ensuring that there is always an unambiguous parser interpretation. But I am of course open to other suggestions.
I'd expect the new syntax to be defined in the dedicated SVG Paths module. It would then be incorporated by reference in all the specs that now or in future use SVG path notation, including Canvas 2D context and CSS Shapes.
The text was updated successfully, but these errors were encountered: