|
| 1 | +import { Canvas, Text, Rect, CanvasEvent } from '@antv/g'; |
| 2 | +import { Renderer as SVGRenderer } from '@antv/g-svg'; |
| 3 | + |
| 4 | +/** |
| 5 | + * @see https://github.com/antvis/G/issues/1911 |
| 6 | + */ |
| 7 | +export async function issue_1911(context) { |
| 8 | + const { canvas, gui } = context; |
| 9 | + await canvas.ready; |
| 10 | + |
| 11 | + canvas.setRenderer(new SVGRenderer()); |
| 12 | + |
| 13 | + const text = new Text({ |
| 14 | + style: { |
| 15 | + x: 100, |
| 16 | + y: 100, |
| 17 | + text: '这是测试文本This is text', |
| 18 | + textBaseline: 'bottom', |
| 19 | + fontSize: 40, |
| 20 | + textAlign: 'left', |
| 21 | + fill: '#1890FF', |
| 22 | + stroke: '#F04864', |
| 23 | + lineWidth: 5, |
| 24 | + }, |
| 25 | + }); |
| 26 | + const textLine3 = new Text({ |
| 27 | + style: { |
| 28 | + x: 100, |
| 29 | + y: 300, |
| 30 | + text: '这是测试文本2 This is text in line 3', |
| 31 | + textBaseline: 'bottom', |
| 32 | + wordWrap: true, |
| 33 | + wordWrapWidth: 360, |
| 34 | + fontSize: 40, |
| 35 | + textAlign: 'left', |
| 36 | + fill: '#1890FF', |
| 37 | + stroke: '#F04864', |
| 38 | + lineWidth: 5, |
| 39 | + }, |
| 40 | + }); |
| 41 | + const bounds = new Rect({ |
| 42 | + style: { |
| 43 | + width: 0, |
| 44 | + height: 0, |
| 45 | + stroke: 'black', |
| 46 | + lineWidth: 2, |
| 47 | + }, |
| 48 | + }); |
| 49 | + const bounds1 = new Rect({ |
| 50 | + style: { |
| 51 | + width: 0, |
| 52 | + height: 0, |
| 53 | + stroke: 'black', |
| 54 | + lineWidth: 2, |
| 55 | + }, |
| 56 | + }); |
| 57 | + canvas.appendChild(text); |
| 58 | + canvas.appendChild(textLine3); |
| 59 | + canvas.appendChild(bounds); |
| 60 | + canvas.appendChild(bounds1); |
| 61 | + |
| 62 | + canvas.addEventListener(CanvasEvent.AFTER_RENDER, () => { |
| 63 | + const bounding = text.getBounds(); |
| 64 | + const bounding1 = textLine3.getBounds(); |
| 65 | + if (bounding) { |
| 66 | + const { center, halfExtents } = bounding; |
| 67 | + bounds.attr('width', halfExtents[0] * 2); |
| 68 | + bounds.attr('height', halfExtents[1] * 2); |
| 69 | + bounds.setPosition( |
| 70 | + center[0] - halfExtents[0], |
| 71 | + center[1] - halfExtents[1], |
| 72 | + ); |
| 73 | + } |
| 74 | + if (bounding1) { |
| 75 | + const { center, halfExtents } = bounding1; |
| 76 | + bounds1.attr('width', halfExtents[0] * 2); |
| 77 | + bounds1.attr('height', halfExtents[1] * 2); |
| 78 | + bounds1.setPosition( |
| 79 | + center[0] - halfExtents[0], |
| 80 | + center[1] - halfExtents[1], |
| 81 | + ); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + const folder = gui.addFolder('textBaseline'); |
| 86 | + const config = { textBaseline: 'bottom' }; |
| 87 | + folder |
| 88 | + .add(config, 'textBaseline', ['top', 'middle', 'bottom']) |
| 89 | + .onChange((name) => { |
| 90 | + text.attr('textBaseline', name); |
| 91 | + textLine3.attr('textBaseline', name); |
| 92 | + }); |
| 93 | + |
| 94 | + const dxyFolder = folder.addFolder('dx, dy'); |
| 95 | + const dxyConfig = { dx: 0, dy: 0 }; |
| 96 | + dxyFolder.add(dxyConfig, 'dx', -100, 100).onChange((dx) => { |
| 97 | + text.attr('dx', dx); |
| 98 | + textLine3.attr('dx', dx); |
| 99 | + }); |
| 100 | + dxyFolder.add(dxyConfig, 'dy', -100, 100).onChange((dy) => { |
| 101 | + text.attr('dy', dy); |
| 102 | + textLine3.attr('dy', dy); |
| 103 | + }); |
| 104 | +} |
0 commit comments