From :複数行/折り返しテキストのサポート - GitHub
方法 1:
複数行のサポートを取得するには、次を追加します。
MapLabel.prototype.wrapText = function(context, text, x, y, maxWidth, lineHeight) {
var words = text.split(' ');
var line = '';
for(var n = 0; n < words.length; n++) {
var testLine = line + words[n] + ' ';
var metrics = context.measureText(testLine);
var testWidth = metrics.width;
if (testWidth > maxWidth && n > 0) {
context.strokeText(line, x, y);
context.fillText(line, x, y);
line = words[n] + ' ';
y += lineHeight;
}
else {
line = testLine;
}
}
context.strokeText(line, x, y);
context.fillText(line, x, y);
};
drawCanvas_ で変更
if (strokeWeight) {
ctx.lineWidth = strokeWeight;
ctx.strokeText(text, strokeWeight, strokeWeight);
}
ctx.fillText(text, strokeWeight, strokeWeight);
に
if (strokeWeight) {
ctx.lineWidth = strokeWeight;
}
this.wrapText(ctx, text, strokeWeight, strokeWeight, *ADD MAX WIDTH*, *ADD LINEHEIGHT*);
//e.g. this.wrapText(ctx, text, strokeWeight, strokeWeight, 200, 14);
このコードは次の拡張です:
http://www.html5canvastutorials.com/tutorials/html5-canvas-wrap-text-tutorial/
方法 2:
交換:
if (strokeWeight) {
ctx.lineWidth = strokeWeight;
ctx.strokeText(text, strokeWeight, strokeWeight);
}
ctx.fillText(text, strokeWeight, strokeWeight);
と
if (strokeWeight) {
ctx.lineWidth = strokeWeight;
// ctx.strokeText(text, strokeWeight, strokeWeight);
}
// ctx.fillText(text, strokeWeight, strokeWeight);
var lineheight = 15;
var lines = text.split('\n');
for (var i = 0; i<lines.length; i++) {
ctx.fillText(lines[i], strokeWeight, strokeWeight + (i * lineheight));
if (strokeWeight) {
ctx.lineWidth = strokeWeight;
ctx.strokeText(lines[i], strokeWeight, strokeWeight + (i * lineheight));
}
}