* 绘制矩形边框背景 * @param ctx * @param style * @param textArray * @param lineHeight * @param canvas * @returns {{width: number, height: number}}
(ctx, style, textArray, lineHeight, canvas)
| 800 | * @returns {{width: number, height: number}} |
| 801 | */ |
| 802 | static drawRect(ctx, style, textArray, lineHeight, canvas) { |
| 803 | let backgroundFill = style.backgroundFill, maxWidth = style.maxWidth - doublePadding; |
| 804 | let width, height = 0, lineCount = 0, lineWidths = []; |
| 805 | //100的宽度,去掉左右两边3padding |
| 806 | textArray.forEach(function (arrText) { |
| 807 | let line = '', isOverMax; |
| 808 | lineCount++; |
| 809 | for (var n = 0; n < arrText.length; n++) { |
| 810 | let textLine = line + arrText[n]; |
| 811 | let metrics = ctx.measureText(textLine); |
| 812 | let textWidth = metrics.width; |
| 813 | if ((textWidth > maxWidth && n > 0) || arrText[n] === '\n') { |
| 814 | line = arrText[n]; |
| 815 | lineCount++; |
| 816 | //有换行,记录当前换行的width |
| 817 | isOverMax = true; |
| 818 | } else { |
| 819 | line = textLine; |
| 820 | width = textWidth; |
| 821 | } |
| 822 | } |
| 823 | if (isOverMax) { |
| 824 | lineWidths.push(maxWidth); |
| 825 | } else { |
| 826 | lineWidths.push(width); |
| 827 | } |
| 828 | }, this); |
| 829 | width = this.getCanvasWidth(lineWidths, maxWidth); |
| 830 | height = lineCount * lineHeight; |
| 831 | height += doublePadding; |
| 832 | canvas.width = width; |
| 833 | canvas.height = height; |
| 834 | ctx.fillStyle = backgroundFill; |
| 835 | ctx.fillRect(0, 0, width, height); |
| 836 | return { |
| 837 | width: width, |
| 838 | height: height |
| 839 | } |
| 840 | } |
| 841 | /** |
| 842 | * 获取自适应的宽度(如果没有超过最大宽度,就用文字的宽度) |
| 843 | * @param lineWidths |
no test coverage detected