Loading... ## 实例 在画布上写一段 40 像素的文本,使用的字体是 "Arial": JavaScript: ``` var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.font="40px Arial"; ctx.fillText("Hello World",10,50); ``` [亲自试一试](https://www.w3school.com.cn/tiy/t.asp?f=eg_html5_canvas_font) ## 定义和用法 font 属性设置或返回画布上文本内容的当前字体属性。 font 属性使用的语法与 [CSS font 属性](https://www.w3school.com.cn/cssref/pr_font_font.asp "CSS font 属性")相同。 | 默认值: | 10px sans-serif | | ------------------- | --------------------------------------------------- | | JavaScript 语法: | context.font="italic small-caps bold 12px arial"; | ### 属性值 | 值 | 描述 | | ----------------------------- | -------------------------------------------------------------------------------------------------------- | | *font-style* | 规定字体样式。可能的值:* normal* italic* oblique | | *font-variant* | 规定字体变体。可能的值:* normal* small-caps | | *font-weight* | 规定字体的粗细。可能的值:* normal* bold* bolder* lighter* 100* 200* 300* 400* 500* 600* 700* 800* 900 | | *font-size* / *line-height* | 规定字号和行高,以像素计。 | | *font-family* | 规定字体系列。 | | caption | 使用标题控件的字体(比如按钮、下拉列表等)。 | | icon | 使用用于标记图标的字体。 | | menu | 使用用于菜单中的字体(下拉列表和菜单列表)。 | | message-box | 使用用于对话框中的字体。 | | small-caption | 使用用于标记小型控件的字体。 | | status-bar | 使用用于窗口状态栏中的字体。 | [HTML5 Canvas 参考手册](https://www.w3school.com.cn/tags/html_ref_canvas.asp) 最后修改:2021 年 07 月 11 日 © 允许规范转载 打赏 赞赏作者 微信 赞 0 如果觉得我的文章对你有用,请随意赞赏
此处评论已关闭