From 8747d1890433ddacad1005a64d16922daa4f419f Mon Sep 17 00:00:00 2001 From: CPPAlien Date: Wed, 25 Jul 2018 14:54:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20align=20=E5=AF=B9=E5=A4=9A?= =?UTF-8?q?=E8=A1=8C=E6=96=87=E5=AD=97=E7=9A=84=E5=AF=B9=E9=BD=90=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pen.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/pen.js b/lib/pen.js index 874b7fa..a109ef2 100644 --- a/lib/pen.js +++ b/lib/pen.js @@ -266,7 +266,19 @@ export default class Painter { text += '...'; measuredWith = this.ctx.measureText(text).width; } - const x = -(width / 2); + this.ctx.setTextAlign(view.css.align ? view.css.align : 'left'); + let x; + switch (view.css.align) { + case 'center': + x = 0; + break; + case 'right': + x = (width / 2); + break; + default: + x = -(width / 2); + break; + } const y = -(height / 2) + (i === 0 ? view.css.fontSize.toPx() : (view.css.fontSize.toPx() + i * lineHeight)); if (view.css.textStyle === 'stroke') { this.ctx.strokeText(text, x, y, measuredWith);