Determine width of string in HTML5 canvas
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm drawing text in an HTML5 canvas on top of an image (a meme generator). I want to calculate a font size so that the string will span the entire width of the canvas. So basically, is there a way in JavaScript to determine what font size I should use for some string of a certain font for a given width? 回答1: The fillText() method has an optional fourth argument, which is the max width to render the string. MDN's documentation says... maxWidth Optional; the maximum width to draw. If specified, and the string is computed to be wider than this