Last active
September 5, 2016 04:33
-
-
Save dskjal/968c1994c35565d81310514ab26fea0a to your computer and use it in GitHub Desktop.
HTML5 の Canvas でテキストの折り返し
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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