Bug 225859 - Canvas does not respect line cap behaviour for zero-length paths
Summary: Canvas does not respect line cap behaviour for zero-length paths
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: Canvas (show other bugs)
Version: Safari 14
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Said Abou-Hallawa
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2021-05-17 00:29 PDT by Pierpaolo Follia
Modified: 2022-09-27 03:59 PDT (History)
9 users (show)

See Also:


Attachments
Output of linked example (8.88 KB, image/png)
2021-05-17 00:29 PDT, Pierpaolo Follia
no flags Details
Patch (5.41 KB, patch)
2021-05-24 19:01 PDT, Said Abou-Hallawa
sabouhallawa: review?
ews-feeder: commit-queue-
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Pierpaolo Follia 2021-05-17 00:29:38 PDT
Created attachment 428813 [details]
Output of linked example

When drawing a zero-length path in canvas using 'round' or 'square' line-cap, the output does not respect the correct behavior (the stroke should consist of a full circle/square centered at the path's point)

Live example here:

https://codepen.io/madchicken/pen/WNpxBXj

The top line is done drawing a zero-length path, the bottom one using a path of length 1px.
Comment 1 Radar WebKit Bug Importer 2021-05-24 00:30:17 PDT
<rdar://problem/78388953>
Comment 2 Said Abou-Hallawa 2021-05-24 12:17:23 PDT
WebKit drawing is different from the drawing of Chrome and FireFox. But I can't find the specs for this degenerate case.

We call moveTo() to a point and then we call lineTo() to the same point. So are we supposed to draw a dot at this point? Or should we just assume we are still at the same point and nothing is drawn?
Comment 3 Pierpaolo Follia 2021-05-24 12:34:54 PDT
I don't think this can be considered a specification, but this is what I was able to find:

https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-linecap#round

The round value indicates that at the end of each subpath the stroke will be extended by a half circle with a diameter equal to the stroke width. On a zero length subpath, the stroke consists of a full circle centered at the subpath's point.
Comment 4 Pierpaolo Follia 2021-05-24 12:44:13 PDT
Maybe this is more accurate:

https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-linecap

At point 9:

Caps: Each point has a flat edge perpendicular to the direction of the line coming out of it. This is then augmented according to the value of style's lineCap. The "butt" value means that no additional line cap is added. The "round" value means that a semi-circle with the diameter equal to style's lineWidth width must additionally be placed on to the line coming out of each point. The "square" value means that a rectangle with the length of style's lineWidth width and the width of half style's lineWidth width, placed flat against the edge perpendicular to the direction of the line coming out of the point, must be added at each point.

Seems that the zero-length path should have the semi-circle (or half-square) added to the line coming out of each point.
Comment 5 Said Abou-Hallawa 2021-05-24 19:01:08 PDT
Created attachment 429610 [details]
Patch
Comment 6 Said Abou-Hallawa 2021-05-24 19:18:33 PDT
These WPT tests 

imported/w3c/web-platform-tests/html/canvas/element/path-objects/2d.path.stroke.prune.arc.html
imported/w3c/web-platform-tests/html/canvas/element/path-objects/2d.path.stroke.prune.closed.html
imported/w3c/web-platform-tests/html/canvas/element/path-objects/2d.path.stroke.prune.curve.html
imported/w3c/web-platform-tests/html/canvas/element/path-objects/2d.path.stroke.prune.line.html

are going to fail with this patch. They are currently passing with Safari but failing for the other browsers. See

https://wpt.fyi/results/html/canvas/element/path-objects/2d.path.stroke.prune.arc.html?label=experimental&label=master&aligned
https://wpt.fyi/results/html/canvas/element/path-objects/2d.path.stroke.prune.closed.html?label=experimental&label=master&aligned
https://wpt.fyi/results/html/canvas/element/path-objects/2d.path.stroke.prune.curve.html?label=experimental&label=master&aligned
https://wpt.fyi/results/html/canvas/element/path-objects/2d.path.stroke.prune.rect.html?label=experimental&label=master&aligned
Comment 7 Simon Fraser (smfr) 2021-05-24 19:18:38 PDT
Comment on attachment 429610 [details]
Patch

If the path has a non-opaque color, should we see all the points stack up? Do the other browsers do this? Maybe it should be a WPT test.
Comment 8 Pierpaolo Follia 2022-09-27 03:59:53 PDT
Hi, is there any news on this one? Thanks.