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

CanvasRenderingContext2D - add missing properties #19859

Merged
merged 29 commits into from
Sep 1, 2022

Conversation

hamishwillee
Copy link
Collaborator

@hamishwillee hamishwillee commented Aug 23, 2022

Missing from CanvasRenderingContext2D API docs

  • letterSpacing
  • fontKerning
  • fontStretch
  • fontVariantCaps
  • textRendering
  • wordSpacing
  • roundRect()
  • isContextLost()
    • HTMLCanvasElement: contextlost event
    • HTMLCanvasElement: contextrestored event
  • reset()

Docs work being done as part of #18772

Adds new docs with live examples. Everything experimental except for fontkerning property.

@github-actions github-actions bot added the Content:WebAPI Web API docs label Aug 23, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Aug 23, 2022

Preview URLs

Flaws

Note! 11 documents with no flaws that don't need to be listed. 🎉

URL: /en-US/docs/Web/API/HTMLCanvasElement
Title: HTMLCanvasElement
on GitHub
Flaw count: 1

  • macros:
    • /en-US/docs/Web/API/HTMLCanvasElement/mozPrintCallback does not exist

URL: /en-US/docs/Web/API/CanvasRenderingContext2D/wordSpacing
Title: CanvasRenderingContext2D.wordSpacing
on GitHub
Flaw count: 1

  • broken_links:
    • Can't resolve https://developer.mozilla.org/en-US/docs/Web/CSS/word-spacing

External URLs

URL: /en-US/docs/Web/API/HTMLCanvasElement
Title: HTMLCanvasElement
on GitHub

No new external URLs


URL: /en-US/docs/Web/API/HTMLCanvasElement/contextlost_event
Title: HTMLCanvasElement: contextlost event
on GitHub

No new external URLs


URL: /en-US/docs/Web/API/HTMLCanvasElement/contextrestored_event
Title: HTMLCanvasElement: contextrestored event
on GitHub

No new external URLs


URL: /en-US/docs/Web/API/CanvasRenderingContext2D
Title: CanvasRenderingContext2D
on GitHub

No new external URLs


URL: /en-US/docs/Web/API/CanvasRenderingContext2D/fontVariantCaps
Title: CanvasRenderingContext2D.fontVariantCaps
on GitHub

No new external URLs


URL: /en-US/docs/Web/API/CanvasRenderingContext2D/fontKerning
Title: CanvasRenderingContext2D.fontKerning
on GitHub

No new external URLs


URL: /en-US/docs/Web/API/CanvasRenderingContext2D/wordSpacing
Title: CanvasRenderingContext2D.wordSpacing
on GitHub

No new external URLs


URL: /en-US/docs/Web/API/CanvasRenderingContext2D/textRendering
Title: CanvasRenderingContext2D.textRendering
on GitHub

No new external URLs


URL: /en-US/docs/Web/API/CanvasRenderingContext2D/isContextLost
Title: CanvasRenderingContext2D.isContextLost()
on GitHub

No new external URLs


URL: /en-US/docs/Web/API/CanvasRenderingContext2D/letterSpacing
Title: CanvasRenderingContext2D.letterSpacing
on GitHub

No new external URLs


URL: /en-US/docs/Web/API/CanvasRenderingContext2D/fontStretch
Title: CanvasRenderingContext2D.fontStretch
on GitHub

No new external URLs


URL: /en-US/docs/Web/API/CanvasRenderingContext2D/reset
Title: CanvasRenderingContext2D.reset()
on GitHub

No new external URLs


URL: /en-US/docs/Web/API/CanvasRenderingContext2D/roundRect
Title: CanvasRenderingContext2D.roundRect()
on GitHub

No new external URLs

(this comment was updated 2022-09-01 14:26:23.001359)

@hamishwillee hamishwillee force-pushed the canvasrenderingcontext2d_updates branch from 39f12f7 to d982908 Compare August 26, 2022 00:57
@hamishwillee hamishwillee marked this pull request as ready for review August 26, 2022 00:58
@hamishwillee hamishwillee requested a review from a team as a code owner August 26, 2022 00:58
@hamishwillee hamishwillee requested review from Elchi3 and teoli2003 and removed request for a team August 26, 2022 00:58
Copy link
Member

@Elchi3 Elchi3 left a comment

Choose a reason for hiding this comment

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

How great roundRect is!! 🤯 Finally!

Great content work here, love it! I had some minor things and one example that doesn't work for me.

Also, I looked up if this is everything new in Canvas 2D and it seems that there is also a new reset() method. Would be cool to document it as well. Doesn't need to be in this PR, though.


## Examples

In this example we display the text "Hello World" using each of the supported values of the `fontStretch` property.
Copy link
Member

@Elchi3 Elchi3 Aug 26, 2022

Choose a reason for hiding this comment

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

This example doesn't show different text renderings to me (in Chrome).

Like in CSS, should a new FontFace be added to the document first?

let ff = new FontFace('myfont', url);
ff.stretch = "condensed";
document.fonts.add(ff);

document.fonts.ready.then(() => {
ctx.font = '25px myfont';
ctx.fontStretch = "condensed";
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks @Elchi3
Yes, clearly the default font is not a variable font, so this can't work. So yes we need to add a font. Unfortunately I am completely stumped as to how to do this in a live example (and I think it is worth doing in a live example).

I tried having the font in the same folder as the docs but I get 404 errors (it isn't served). I tried using links from the interactive examples, but they fail with CORS errors.

The "best" way to do this would be to use a CDN served font with appropriate allow headers. I tried this with fonts.gstatic.com but I can't find a woff file that contains variable fonts. For example, this doesn't vary:

let font_file = new FontFace('LeagueMonoVariable', 'url(https://fonts.gstatic.com/s/leaguespartan/v6/kJEqBuEW6A0lliaV_m88ja5TwvZwLZk.woff2');
font_file.load().then( () => {

  document.fonts.add(font_file);
  // font loaded successfully!
  const ctx = canvas.getContext('2d');
  ctx.font = '36px "LeagueMonoVariable"'

// Default (normal)
ctx.fillText(`Hello world (default: ${ctx.fontStretch})`, 5, 20);
...

What I'd like to do, because I think it would be generally useful, is use the google font API with the font loading API. However passing in the URLs suggested for CSS just gives you link parsing errors.

Any ideas?

PS Our CSS Font loading API docs are "very thin". Not enough examples, and some may be incorrect - for example https://developer.mozilla.org/en-US/docs/Web/API/FontFace/load looks to me to not be adding the font to the document, and I think that is a precondition.
In general it isn't clear what you need to do - I THINK you load() the font, wait for it to complete, then add it to the document and wait for it to be ready, but none of that is demonstrated.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@Elchi3 So I think we're good except for fontstretch, on which I am stumped for the above reasons.

Copy link
Member

Choose a reason for hiding this comment

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

@bsmth See these messages above^

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, tracking this improvement in #20208

Copy link
Collaborator

Choose a reason for hiding this comment

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

Re: using fancy fonts in live samples, please see mdn/yari#5727.

Copy link
Member

@Elchi3 Elchi3 left a comment

Choose a reason for hiding this comment

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

Fantastic work! Found two more nits but all reviewed and ready to go except fontStretch.

bsmth and others added 3 commits September 1, 2022 14:08
Co-authored-by: Florian Scholz <fs@florianscholz.com>
Co-authored-by: Florian Scholz <fs@florianscholz.com>
@bsmth
Copy link
Member

bsmth commented Sep 1, 2022

Thanks @hamishwillee and @Elchi3 , looks great! 🙌🏻

Is there anything else outstanding on this one? It looks ready to merge from my side.

@bsmth bsmth mentioned this pull request Sep 1, 2022
11 tasks
@Elchi3
Copy link
Member

Elchi3 commented Sep 1, 2022

I think this is good. It's just that we need to work a bit on fontStretch as it only really works with variable fonts. I would be okay with putting that work to a separate PR, though.

@bsmth
Copy link
Member

bsmth commented Sep 1, 2022

I would be okay with putting that work to a separate PR, though.

Great, Hamish probably has the context on it, but I'm happy to pick that up as a follow-up.

Copy link
Member

@bsmth bsmth left a comment

Choose a reason for hiding this comment

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

Thank you! 👍🏻

@Elchi3 Elchi3 merged commit 5acd7d7 into mdn:main Sep 1, 2022
@teoli2003
Copy link
Contributor

teoli2003 commented Sep 1, 2022

The functions DrawRect() and DrawCirclestart with capital letters, they shouldn't. @bsmth, can you create a follow-up PR?

@bsmth
Copy link
Member

bsmth commented Sep 1, 2022

The functions DrawRect() and DrawCirclestart with capital letters, they shouldn't. @bsmth, can you create a follow-up PR?

Well spotted @teoli2003, I've added cleanup PR here: #20210

@hamishwillee hamishwillee deleted the canvasrenderingcontext2d_updates branch September 1, 2022 23:23
@hamishwillee
Copy link
Collaborator Author

Thanks guys! Yes, I'm still trying to get the variable fonts thing working as a live example (yes, I could do it as an interactive one, but I prefer live). The easy fix would be mdn/yari#5727 but I am also trying to get Google Fonts to work here.

goshdarnheck pushed a commit to goshdarnheck/content that referenced this pull request Sep 7, 2022
* CanvasRenderingContext2D.letterSpacing - add doc

* Add letterSpacing to canvasrenderingcontext2d

* Add cross link to wordspacing

* CanvasRenderingContext2D.wordSpacing - add doc

* CanvasRenderingContext2D.fontStretch - add doc

* fontStretch - minor layout update

* CanvasRenderingContext2D.fontVariantCaps - add doc

* fontVariantCaps - fix typo

* fontVariantCaps - typos

* CanvasRenderingContext2D.textRednering - add doc

* CanvasRenderingContext2D.fontKerning - add doc

* font kerning, fontstretch tidy

* CanvasRenderingContext2D.roundedRect() - add doc

* Fix up parent doc to link to the sub docs

* CanvasRenderingContext2D.fontKerning - not experimental now

* Fix up new additions experimental status

* Apply suggestions from code review

Co-authored-by: Florian Scholz <fs@florianscholz.com>

* Canvas - losing/regaining context

* Update files/en-us/web/api/htmlcanvaselement/contextlost_event/index.md

* Add reset()

* Apply suggestions from code review

Co-authored-by: Florian Scholz <fs@florianscholz.com>

* Switch generic context events first

* isContextLost() - add example

* CanvasRenderingContext2D.reset() - examples plus more words

* contextlost/restored updates

* Fix link to preventDefault()

Co-authored-by: Florian Scholz <fs@florianscholz.com>

* Apply fix from reviewer feedback

Co-authored-by: Florian Scholz <fs@florianscholz.com>

Co-authored-by: Florian Scholz <fs@florianscholz.com>
Co-authored-by: Brian Thomas Smith <brian@smith.berlin>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:WebAPI Web API docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants