|
@@ -28,7 +28,7 @@ export default class Painter {
|
|
|
this._doClip(this.data.borderRadius, width, height);
|
|
|
if (!bg) {
|
|
|
// 如果未设置背景,则默认使用白色
|
|
|
- this.ctx.setFillStyle("#fff");
|
|
|
+ this.ctx.fillStyle = "#fff";
|
|
|
this.ctx.fillRect(-(width / 2), -(height / 2), width, height);
|
|
|
} else if (
|
|
|
bg.startsWith("#") ||
|
|
@@ -36,7 +36,7 @@ export default class Painter {
|
|
|
bg.toLowerCase() === "transparent"
|
|
|
) {
|
|
|
// 背景填充颜色
|
|
|
- this.ctx.setFillStyle(bg);
|
|
|
+ this.ctx.fillStyle = bg;
|
|
|
this.ctx.fillRect(-(width / 2), -(height / 2), width, height);
|
|
|
} else {
|
|
|
// 背景填充图片
|
|
@@ -77,8 +77,8 @@ export default class Painter {
|
|
|
const r = Math.min(borderRadius.toPx(), width / 2, height / 2);
|
|
|
// 防止在某些机型上周边有黑框现象,此处如果直接设置 setFillStyle 为透明,在 Android 机型上会导致被裁减的图片也变为透明, iOS 和 IDE 上不会
|
|
|
// setGlobalAlpha 在 1.9.90 起支持,低版本下无效,但把 setFillStyle 设为了 white,相对默认的 black 要好点
|
|
|
- this.ctx.setGlobalAlpha(0);
|
|
|
- this.ctx.setFillStyle("white");
|
|
|
+ this.ctx.globalAlpha = 0;
|
|
|
+ this.ctx.fillStyle = "white";
|
|
|
this.ctx.beginPath();
|
|
|
this.ctx.arc(
|
|
|
-width / 2 + r,
|
|
@@ -117,7 +117,7 @@ export default class Painter {
|
|
|
) {
|
|
|
this.ctx.clip();
|
|
|
}
|
|
|
- this.ctx.setGlobalAlpha(1);
|
|
|
+ this.ctx.globalAlpha = 1;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -194,8 +194,9 @@ export default class Painter {
|
|
|
width = view.css.width ? view.css.width.toPx() : textLength;
|
|
|
// 计算行数
|
|
|
const calLines = Math.ceil(textLength / width);
|
|
|
- const lines =
|
|
|
- view.css.maxLines < calLines ? view.css.maxLines : calLines;
|
|
|
+ const maxLines = view.css.maxLines;
|
|
|
+ const lines = maxLines < calLines ? maxLines : calLines;
|
|
|
+
|
|
|
let lineHeight = view.css.lineHeight
|
|
|
? view.css.lineHeight.toPx()
|
|
|
: view.css.fontSize.toPx();
|
|
@@ -203,6 +204,7 @@ export default class Painter {
|
|
|
height = lineHeight * lines;
|
|
|
extra = {
|
|
|
lines: lines,
|
|
|
+ maxLines: maxLines || 0,
|
|
|
lineHeight: lineHeight,
|
|
|
};
|
|
|
break;
|
|
@@ -344,29 +346,43 @@ export default class Painter {
|
|
|
}
|
|
|
this.ctx.save();
|
|
|
const { width, height, extra } = this._preProcess(view);
|
|
|
- this.ctx.setFillStyle(view.css.color || "black");
|
|
|
- const { lines, lineHeight } = extra;
|
|
|
+ this.ctx.fillStyle = view.css.color || "black";
|
|
|
+ const { lines, lineHeight, maxLines } = extra;
|
|
|
const preLineLength = Math.round(view.text.length / lines);
|
|
|
let start = 0;
|
|
|
let alreadyCount = 0;
|
|
|
+
|
|
|
+ // 先获取一个文字的宽度
|
|
|
+ let alphabetStart = 0;
|
|
|
+ let alphabet = view.text.substr(alphabetStart, alphabetStart + 1);
|
|
|
+ let alphabetWith = this.ctx.measureText(alphabet).width;
|
|
|
+ while (!alphabetWith) {
|
|
|
+ alphabet = view.text.substr(++alphabetStart, alphabetStart + 1);
|
|
|
+ alphabetWith = this.ctx.measureText(alphabet).width;
|
|
|
+ }
|
|
|
+
|
|
|
for (let i = 0; i < lines; ++i) {
|
|
|
- alreadyCount = preLineLength;
|
|
|
+ alreadyCount =
|
|
|
+ maxLines > 0 ? Number(width / alphabetWith) : preLineLength;
|
|
|
let text = view.text.substr(start, alreadyCount);
|
|
|
let measuredWith = this.ctx.measureText(text).width;
|
|
|
// 如果测量大小小于width一个字符的大小,则进行补齐,如果测量大小超出 width,则进行减除
|
|
|
- // 如果已经到文本末尾,也不要进行该循环
|
|
|
+ // 如果已经到文本末尾,也不要进行该循
|
|
|
while (
|
|
|
start + alreadyCount <= view.text.length &&
|
|
|
(width - measuredWith > view.css.fontSize.toPx() ||
|
|
|
- measuredWith > width)
|
|
|
+ (measuredWith > width && !isStop))
|
|
|
) {
|
|
|
if (measuredWith < width) {
|
|
|
+ console.log("+++++++++", view.css.fontSize);
|
|
|
text = view.text.substr(start, ++alreadyCount);
|
|
|
} else {
|
|
|
if (text.length <= 1) {
|
|
|
// 如果只有一个字符时,直接跳出循环
|
|
|
break;
|
|
|
}
|
|
|
+ console.log("---------", view.css.fontSize);
|
|
|
+
|
|
|
text = view.text.substr(start, --alreadyCount);
|
|
|
}
|
|
|
measuredWith = this.ctx.measureText(text).width;
|
|
@@ -384,6 +400,7 @@ export default class Painter {
|
|
|
text += "...";
|
|
|
measuredWith = this.ctx.measureText(text).width;
|
|
|
}
|
|
|
+ console.log("ssss====", text, measuredWith);
|
|
|
this.ctx.setTextAlign(view.css.align ? view.css.align : "left");
|
|
|
let x;
|
|
|
switch (view.css.align) {
|
|
@@ -435,7 +452,7 @@ export default class Painter {
|
|
|
_drawAbsRect(view) {
|
|
|
this.ctx.save();
|
|
|
const { width, height } = this._preProcess(view);
|
|
|
- this.ctx.setFillStyle(view.css.backgroundColor || "#FFFFFF");
|
|
|
+ this.ctx.fillStyle = view.css.backgroundColor || "#FFFFFF";
|
|
|
this.ctx.fillRect(-(width / 2), -(height / 2), width, height);
|
|
|
this.ctx.restore();
|
|
|
this._doBorder(view, width, height);
|