* 获取文字标注对应的canvas * @param style * @returns {{canvas: *, width: number, height: number}}
(style)
| 739 | * @returns {{canvas: *, width: number, height: number}} |
| 740 | */ |
| 741 | static getCanvas(style) { |
| 742 | let canvas; |
| 743 | if (style.canvas) { |
| 744 | if (document.querySelector("#" + style.canvas)) { |
| 745 | canvas = document.getElemntById(style.canvas); |
| 746 | } else { |
| 747 | canvas = this.createCanvas(style); |
| 748 | } |
| 749 | } else { |
| 750 | //不存在canvas,当前feature |
| 751 | canvas = this.createCanvas(style); |
| 752 | style.canvas = canvas.id; |
| 753 | } |
| 754 | canvas.style.display = "none"; |
| 755 | var ctx = canvas.getContext("2d"); |
| 756 | //行高 |
| 757 | let lineHeight = Number(style.font.replace(/[^0-9]/ig, "")); |
| 758 | let textArray = style.text.split('\r\n'); |
| 759 | let lenght = textArray.length; |
| 760 | //在改变canvas大小后再绘制。否则会被清除 |
| 761 | ctx.font = style.font; |
| 762 | let size = this.drawRect(ctx, style, textArray, lineHeight, canvas); |
| 763 | this.positionY = padding; |
| 764 | if (lenght > 1) { |
| 765 | textArray.forEach(function (text, i) { |
| 766 | if (i !== 0) { |
| 767 | this.positionY = this.positionY + lineHeight; |
| 768 | } |
| 769 | this.canvasTextAutoLine(text, style, ctx, lineHeight, size.width); |
| 770 | }, this); |
| 771 | } else { |
| 772 | this.canvasTextAutoLine(textArray[0], style, ctx, lineHeight, size.width); |
| 773 | } |
| 774 | return { |
| 775 | canvas: canvas, |
| 776 | width: size.width, |
| 777 | height: size.height |
| 778 | }; |
| 779 | } |
| 780 | /** |
| 781 | * 创建当前feature对应的canvas |
| 782 | * @param {Object} style |
no test coverage detected