Skip to content
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

Add conic-gradient #6661

Merged
merged 18 commits into from
May 18, 2021
Merged

Add conic-gradient #6661

merged 18 commits into from
May 18, 2021

Conversation

yiyix
Copy link
Contributor

@yiyix yiyix commented May 7, 2021

Copy link
Member

@Kaiido Kaiido left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spotted a few typos while reading.

source Show resolved Hide resolved
source Show resolved Hide resolved
source Show resolved Hide resolved
source Show resolved Hide resolved
source Show resolved Hide resolved
source Show resolved Hide resolved

It follows the same rendering rule as CSS <span>conic-gradient</span>. The <var>position</var> is
the center defined by <var>x</var> and <var>y</var> in the coordinate space. This function is
equivalent to CSS conic-gradient(from <var>startAngle</var> deg at <var>x</var> px <var>y</var> px, <var>angular-color-stop-list</var>),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the convention (e.g. in https://html.spec.whatwg.org/#flow-content-3:is-modal) is to wrap CSS expressions in single quotes '.

source Show resolved Hide resolved
source Show resolved Hide resolved
source Show resolved Hide resolved
equivalent to CSS conic-gradient(from <var>startAngle</var> deg at <var>x</var> px <var>y</var> px, <var>angular-color-stop-list</var>),
where <var>angular-color-stop-list</var> is to be applied to the <code>CanvasGradient</code> by
calling <span data-x="dom-canvasgradient-addColorStop">addColorStop</span>.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does anything need to be said about the coordinate spaces involved? Like, are CSS pixels the same as the canvas pixels?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it helps the clarification, i can add something. I see CSS pixel defined and used in Canvas, (ex: actualBoundingBoxRight). I can may add it in the previous paragraph where i explain what is x and y

<p>The <dfn method for="CanvasFillStrokeStyles"><code
data-x="dom-context-2d-createConicGradient">createConicGradient(<var>startAngle</var>, <var>x</var>,
<var>y</var>)</code></dfn> method takes three arguments, the first argument, <var>startAngle</var>,
represents the angle in degree at which the gradient begins, and the last two arguments,
Copy link
Member

@Kaiido Kaiido May 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see where it has been defined that angle should be in degree.
In #5431 (comment) @nt1m was asking if this should be in radians or degrees, but it seems there hasn't been any response in that thread, and given current Nightly still uses radians in the implementation, I guess they haven't been made aware of that decision to use degrees.

For what it's worth, if it's still time to have this discussion, I feel as a web-author that having the angle in degrees is very confusing: If I'm not mistaken, it is the only place in the whole canvas API that we would have an angle expressed in degrees instead of in radians. So if we want to play with that angle and e.g the transformation of the context, or the start/end angles of an ellipse, we'd have to convert between degrees and radians, not to mention the mental gymnastic of remembering where to use what...
If the goal was to have a simpler mapping from CSS, then note that only the <angle> 0 can have the unit omitted, and that CSS has a rad unit.

Note that there is already a discrepancy with arc (and ellipse), where both startAngle and endAngle are relative to the x-axis, while CSS conic gradients asks that "0deg points to the top of the page", but that one sounds a bit less dramatic to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(@Kaiido to be clear, now is definitely the time. Thanks for raising this! See also https://whatwg.org/working-mode#changes if you want to get more familiar with the process in general.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for radians. I'm not an expert, but all the JS APIs I know of use (e.g. the Math functions) use radians, and canvas should definitely be internally consistent.

The inconistency between axes does seem problematic though. Would it make sense to make the mapping (startAngle + Math.PI / 2) radians so that it matches other canvas APIs? We could also even add a counterclockwise boolean like those APIs have.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented this many months ago and cannot remember why I chose degrees. I think I was trying to match some CSS behaviour.
I also would prefer radians, personally. +1 to rotating everything by PI/2 as well to match arc and ellipse.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strong agree that an angle number without additional type info should be interpreted as radians, coming from a JS API.

Argh at the mismatch of canvas api start angle. CSS copied from SVG, using "bearing angles". I think we should match existing canvas precedent here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am so glad we have this discussion here now before i change more Canvas API.

+1 for radians and rotating everything by PI/2. Canvas API should be consistent. Changing spec. I will also change our implementation to map this.

@domenic domenic added addition/proposal New features or enhancements topic: canvas labels May 12, 2021
Copy link
Member

@domenic domenic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed some editorial tweaks. It now LGTM!

However I noticed that the tests at https://github.com/web-platform-tests/wpt/pull/24299/files seem rather basic. They all use zero angle, so they would not detect the change we made here to radians or to offsetting by pi/2. And if I am reading them right, they all test only a single color (0,255,0,255), even though they use multiple color stops. Can we improve the test coverage here?

@yiyix
Copy link
Contributor Author

yiyix commented May 13, 2021

I pushed some editorial tweaks. It now LGTM!

However I noticed that the tests at https://github.com/web-platform-tests/wpt/pull/24299/files seem rather basic. They all use zero angle, so they would not detect the change we made here to radians or to offsetting by pi/2. And if I am reading them right, they all test only a single color (0,255,0,255), even though they use multiple color stops. Can we improve the test coverage here?

Due to this change, I updated the tests accordingly -> https://chromium-review.googlesource.com/c/chromium/src/+/2891049
I modified the tests a bit, so it's testing for positive/negative angle rotation and 1 test for no-rotation with several colorstop.

I will also add another test for argument validation. What are the other tests you would suggest? I am happy to address them in a later cl.

@domenic
Copy link
Member

domenic commented May 13, 2021

I'm still not seeing any tests that actually test the gradient-ness: every pixel test is testing against the same 0,255,0,255 value. So if I am understanding the tests correctly, an implementation would be able to pass these tests if it only ever used the first color stop in the color stop list.

Are there tests that I am missing which test more than one color? Or am I misunderstanding the test suite?

I do see a test under third_party/blink/web_tests/fast/canvas/conic-gradient.html that seems like it'd be good, but it's an internal Blink test, not a web platform test. Maybe it could be converted into a reftest?

@yiyix
Copy link
Contributor Author

yiyix commented May 14, 2021

I'm still not seeing any tests that actually test the gradient-ness: every pixel test is testing against the same 0,255,0,255 value. So if I am understanding the tests correctly, an implementation would be able to pass these tests if it only ever used the first color stop in the color stop list.

Are there tests that I am missing which test more than one color? Or am I misunderstanding the test suite?

I do see a test under third_party/blink/web_tests/fast/canvas/conic-gradient.html that seems like it'd be good, but it's an internal Blink test, not a web platform test. Maybe it could be converted into a reftest?

Thank you for the feedbacks! I added much more tests in https://chromium-review.googlesource.com/c/chromium/src/+/2896865

@domenic
Copy link
Member

domenic commented May 14, 2021

Excellent, that's perfect! I'm happy to merge this now. I'll give this until Monday in case @annevk or anyone else wants to have a final look, then I'll press the merge button.

@yiyix
Copy link
Contributor Author

yiyix commented May 15, 2021

Added the new tests pull request in the description. web-platform-tests/wpt#29002

Copy link
Member

@annevk annevk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modulo nits this seems okay. At some point we also need to modernize these method definitions.

<dd>
<p>Returns a <code>CanvasGradient</code> object that represents a
conic gradient that paints clockwise along the rotation around the center
represented by the arguments.</p>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: indentation and wrapping is off and misses an end tag.

data-x="dom-context-2d-createConicGradient">createConicGradient(<var>startAngle</var>, <var>x</var>,
<var>y</var>)</code></dfn> method takes three arguments, the first argument, <var>startAngle</var>,
represents the angle in radians at which the gradient begins, and the last two arguments,
(<var>x</var>, <var>y</var>), represent the center of the gradient in <span data-x="'px'">CSS pixels</span>.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: exceeds a 100 columns.

@domenic domenic merged commit cf2dab0 into whatwg:main May 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements topic: canvas
Development

Successfully merging this pull request may close these issues.

6 participants