回答編集履歴

1

追記

2019/02/06 08:30

投稿

moredeep
moredeep

スコア1507

test CHANGED
@@ -6,26 +6,70 @@
6
6
 
7
7
  ```
8
8
 
9
- var lineFeed_fillText = function(_obj){
9
+ var lineFeed_fillText = function(_obj){
10
10
 
11
- // 文字列を改行文字で分割
11
+ // 文字列を改行文字で分割
12
12
 
13
- var lines = _obj.text.split('\n');
13
+ var lines = _obj.text.split('\n');
14
14
 
15
15
 
16
16
 
17
- // 各行の縦位置を調整するための処理、なぜ幅(width)が使われているのかは不明
17
+ // 各行の縦位置を調整するための処理、なぜ幅(width)が使われているのかは不明
18
18
 
19
- var lh = _obj.context.measureText("あ").width + _obj.lineHeight;
19
+ var lh = _obj.context.measureText("あ").width + _obj.lineHeight;
20
20
 
21
21
 
22
22
 
23
- lines.forEach(function(line, i){
23
+ lines.forEach(function(line, i){
24
24
 
25
- // 分割された文字列を印字
25
+ // 分割された文字列を印字
26
26
 
27
- _obj.context.fillText(line, _obj.x, _obj.y + lh * i);
27
+ _obj.context.fillText(line, _obj.x, _obj.y + lh * i);
28
28
 
29
- });
29
+ });
30
30
 
31
31
  ```
32
+
33
+
34
+
35
+ ---
36
+
37
+ 文字列の折り返し方は、参考に挙げているサイトの`multilineText`を呼び出せばよいと思います。といっても、「文字列の配列の各要素を配列に」とか頭がこんがらがってしまうかもしれないので書いてしまうと、
38
+
39
+ ```
40
+
41
+ /*
42
+
43
+ lines.forEach(function(line, i){
44
+
45
+ // 分割された文字列を印字
46
+
47
+ _obj.context.fillText(line, _obj.x, _obj.y + lh * i);
48
+
49
+ });
50
+
51
+ */
52
+
53
+ // ↑削除 ↓追加
54
+
55
+ var lineNumber = 0;
56
+
57
+ lines.forEach(function(line, i){
58
+
59
+ // 改行で分割された文字列をさらに等幅に分割
60
+
61
+ var monospacedLines = multilineText(_obj.context, line, width/*任意の幅*/);
62
+
63
+ monospacedLines.forEach(function(monospacedLine, i){
64
+
65
+ // 等幅文字列を印字
66
+
67
+ _obj.context.fillText(monospacedLine, _obj.x, _obj.y + lh * lineNumber++);
68
+
69
+ });
70
+
71
+ });
72
+
73
+ ```
74
+
75
+ こんな感じではないかと思います。