Skip to content

Instantly share code, notes, and snippets.

@dskjal
Last active September 5, 2016 04:33
Show Gist options
  • Select an option

  • Save dskjal/968c1994c35565d81310514ab26fea0a to your computer and use it in GitHub Desktop.

Select an option

Save dskjal/968c1994c35565d81310514ab26fea0a to your computer and use it in GitHub Desktop.
HTML5 の Canvas でテキストの折り返し
var canvasWidthPx = 640;
var text = "sample text sample text sample text sample text";
var textWidthPx = context.measureText(text).width;
var charWidthPx = Math.ceil(textWidthPx / text.length);
var nWordInLine = Math.floor(canvasWidthPx / charWidthPx);
var nLine = Math.ceil(text.length / nWordInLine);
var wordHeightPx = 60;
for (var i = 0; i < nLine; ++i) {
var render_text = text.substr(i*nWordInLine, (i+1)*nWordInLine);
context.fillText(render_text, 0, 200+(wordHeightPx*i));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment