-
Notifications
You must be signed in to change notification settings - Fork 22.5k
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
CanvasRenderingContext2D - add missing properties #19859
Conversation
39f12f7
to
d982908
Compare
There was a problem hiding this 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.
files/en-us/web/api/canvasrenderingcontext2d/fontstretch/index.md
Outdated
Show resolved
Hide resolved
|
||
## Examples | ||
|
||
In this example we display the text "Hello World" using each of the supported values of the `fontStretch` property. |
There was a problem hiding this comment.
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";
}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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^
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
files/en-us/web/api/canvasrenderingcontext2d/roundrect/index.md
Outdated
Show resolved
Hide resolved
files/en-us/web/api/htmlcanvaselement/contextlost_event/index.md
Outdated
Show resolved
Hide resolved
files/en-us/web/api/htmlcanvaselement/contextlost_event/index.md
Outdated
Show resolved
Hide resolved
files/en-us/web/api/htmlcanvaselement/contextrestored_event/index.md
Outdated
Show resolved
Hide resolved
files/en-us/web/api/htmlcanvaselement/contextrestored_event/index.md
Outdated
Show resolved
Hide resolved
Co-authored-by: Florian Scholz <fs@florianscholz.com>
There was a problem hiding this 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
.
files/en-us/web/api/htmlcanvaselement/contextrestored_event/index.md
Outdated
Show resolved
Hide resolved
files/en-us/web/api/htmlcanvaselement/contextlost_event/index.md
Outdated
Show resolved
Hide resolved
Co-authored-by: Florian Scholz <fs@florianscholz.com>
Co-authored-by: Florian Scholz <fs@florianscholz.com>
Thanks @hamishwillee and @Elchi3 , looks great! 🙌🏻 Is there anything else outstanding on this one? It looks ready to merge from my side. |
I think this is good. It's just that we need to work a bit on |
Great, Hamish probably has the context on it, but I'm happy to pick that up as a follow-up. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! 👍🏻
The functions |
Well spotted @teoli2003, I've added cleanup PR here: #20210 |
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. |
* 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>
Missing from
CanvasRenderingContext2D
API docsletterSpacing
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.