CanvasRenderingContext2D 方法 fillText() , part of the Canvas 2D API, draws a text string at the specified coordinates, filling the string's characters with the current fillStyle . An optional parameter allows specifying a maximum width for the rendered text, which the 用户代理 will achieve by condensing the text or by using a lower font size.

This method draws directly to the canvas without modifying the current path, so any subsequent fill() or stroke() calls will have no effect on it.

The text is rendered using the font and text layout configuration as defined by the font , textAlign , textBaseline ,和 direction 特性。

To draw the outlines of the characters in a string, call the context's strokeText() 方法。

句法

CanvasRenderingContext2D.fillText(text, x, y [, maxWidth]);
					

参数

text
DOMString specifying the text string to render into the context. The text is rendered using the settings specified by font , textAlign , textBaseline ,和 direction .
x

The x-axis coordinate of the point at which to begin drawing the text, in pixels.

y

The y-axis coordinate of the point at which to begin drawing the text, in pixels.

maxWidth 可选

The maximum number of pixels wide the text may be once rendered. If not specified, there is no limit to the width of the text. However, if this value is provided, the user agent will adjust the kerning, select a more horizontally condensed font (if one is available or can be generated without loss of quality), or scale down to a smaller font size in order to fit the text in the specified width.

返回值

undefined .

范例

Drawing filled text

This example writes the words "Hello world" using the fillText() 方法。

HTML

First, we need a canvas to draw into. This code creates a context 400 pixels wide and 150 pixels across.

<canvas id="canvas" width="400" height="150"></canvas>
					

JavaScript

The JavaScript code for this example follows.

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.font = '50px serif';
ctx.fillText('Hello world', 50, 90);
					

This code obtains a reference to the <canvas> , then gets a reference to its 2D graphics context.

With that in hand, we set the font to 50-pixel-tall "serif" (the user's default serif font), then call fillText() to draw the text "Hello world," starting at the coordinates (50, 90).

结果

Restricting the text size

This example writes the words "Hello world," restricting its width to 140 pixels.

HTML

<canvas id="canvas" width="400" height="150"></canvas>
					

JavaScript

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.font = '50px serif';
ctx.fillText('Hello world', 50, 90, 140);
					

结果

规范

规范 状态 注释
HTML 实时标准
The definition of 'CanvasRenderingContext2D.fillText' in that specification.
实时标准

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
fillText Chrome Yes Edge 12 Firefox 3.5 IE 9 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改:
  1. CanvasRenderingContext2D
  2. 特性
    1. canvas
    2. currentTransform
    3. direction
    4. fillStyle
    5. filter
    6. font
    7. globalAlpha
    8. globalCompositeOperation
    9. imageSmoothingEnabled
    10. imageSmoothingQuality
    11. lineCap
    12. lineDashOffset
    13. lineJoin
    14. lineWidth
    15. miterLimit
    16. shadowBlur
    17. shadowColor
    18. shadowOffsetX
    19. shadowOffsetY
    20. strokeStyle
    21. textAlign
    22. textBaseline
  3. 方法
    1. addHitRegion()
    2. arc()
    3. arcTo()
    4. beginPath()
    5. bezierCurveTo()
    6. clearHitRegions()
    7. clearRect()
    8. clip()
    9. closePath()
    10. createImageData()
    11. createLinearGradient()
    12. createPattern()
    13. createRadialGradient()
    14. drawFocusIfNeeded()
    15. drawImage()
    16. drawWidgetAsOnScreen()
    17. drawWindow()
    18. ellipse()
    19. fill()
    20. fillRect()
    21. fillText()
    22. getImageData()
    23. getLineDash()
    24. getTransform()
    25. isPointInPath()
    26. isPointInStroke()
    27. lineTo()
    28. measureText()
    29. moveTo()
    30. putImageData()
    31. quadraticCurveTo()
    32. rect()
    33. removeHitRegion()
    34. resetTransform()
    35. restore()
    36. rotate()
    37. save()
    38. scale()
    39. scrollPathIntoView()
    40. setLineDash()
    41. setTransform()
    42. stroke()
    43. strokeRect()
    44. strokeText()
    45. transform()
    46. translate()
  4. HTML DOM 相关页面
    1. BeforeUnloadEvent
    2. DOMStringMap
    3. ErrorEvent
    4. GlobalEventHandlers
    5. HTMLAnchorElement
    6. HTMLAreaElement
    7. HTMLAudioElement
    8. HTMLBRElement
    9. HTMLBaseElement
    10. HTMLBaseFontElement
    11. HTMLBodyElement
    12. HTMLButtonElement
    13. HTMLCanvasElement
    14. HTMLContentElement
    15. HTMLDListElement
    16. HTMLDataElement
    17. HTMLDataListElement
    18. HTMLDialogElement
    19. HTMLDivElement
    20. HTMLDocument
    21. HTMLElement
    22. HTMLEmbedElement
    23. HTMLFieldSetElement
    24. HTMLFormControlsCollection
    25. HTMLFormElement
    26. HTMLFrameSetElement
    27. HTMLHRElement
    28. HTMLHeadElement
    29. HTMLHeadingElement
    30. HTMLHtmlElement
    31. HTMLIFrameElement
    32. HTMLImageElement
    33. HTMLInputElement
    34. HTMLIsIndexElement
    35. HTMLKeygenElement
    36. HTMLLIElement
    37. HTMLLabelElement
    38. HTMLLegendElement
    39. HTMLLinkElement
    40. HTMLMapElement
    41. HTMLMediaElement
    42. HTMLMetaElement
    43. HTMLMeterElement
    44. HTMLModElement
    45. HTMLOListElement
    46. HTMLObjectElement
    47. HTMLOptGroupElement
    48. HTMLOptionElement
    49. HTMLOptionsCollection
    50. HTMLOutputElement
    51. HTMLParagraphElement
    52. HTMLParamElement
    53. HTMLPictureElement
    54. HTMLPreElement
    55. HTMLProgressElement
    56. HTMLQuoteElement
    57. HTMLScriptElement
    58. HTMLSelectElement
    59. HTMLShadowElement
    60. HTMLSourceElement
    61. HTMLSpanElement
    62. HTMLStyleElement
    63. HTMLTableCaptionElement
    64. HTMLTableCellElement
    65. HTMLTableColElement
    66. HTMLTableDataCellElement
    67. HTMLTableElement
    68. HTMLTableHeaderCellElement
    69. HTMLTableRowElement
    70. HTMLTableSectionElement
    71. HTMLTemplateElement
    72. HTMLTextAreaElement
    73. HTMLTimeElement
    74. HTMLTitleElement
    75. HTMLTrackElement
    76. HTMLUListElement
    77. HTMLUnknownElement
    78. HTMLVideoElement
    79. HashChangeEvent
    80. 历史
    81. ImageData
    82. 定位
    83. MessageChannel
    84. MessageEvent
    85. MessagePort
    86. Navigator
    87. NavigatorGeolocation
    88. NavigatorID
    89. NavigatorLanguage
    90. NavigatorOnLine
    91. NavigatorPlugins
    92. PageTransitionEvent
    93. Plugin
    94. PluginArray
    95. PopStateEvent
    96. PortCollection
    97. PromiseRejectionEvent
    98. RadioNodeList
    99. Transferable
    100. ValidityState
    101. Window
    102. WindowBase64
    103. WindowEventHandlers
    104. WindowTimers