|
| 1 | +import { Canvas, Text, CanvasEvent } from '@antv/g'; |
| 2 | +import { Renderer } from '@antv/g-canvas'; |
| 3 | + |
| 4 | +// const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 5 | +// const randomLetter = () => letters.charAt(Math.floor(Math.random() * 26)); |
| 6 | + |
| 7 | +/** |
| 8 | + * @see https://github.com/antvis/G/issues/1910 |
| 9 | + */ |
| 10 | +export async function issue_1910(context: { canvas: Canvas }) { |
| 11 | + const { canvas } = context; |
| 12 | + console.log(canvas); |
| 13 | + |
| 14 | + canvas.setRenderer( |
| 15 | + new Renderer({ |
| 16 | + enableAutoRendering: false, |
| 17 | + }), |
| 18 | + ); |
| 19 | + |
| 20 | + const canvasDom = canvas |
| 21 | + .getContextService() |
| 22 | + .getDomElement() as unknown as HTMLCanvasElement; |
| 23 | + const btnDom = document.createElement('button'); |
| 24 | + btnDom.style.cssText = |
| 25 | + 'position: absolute; top: 0px; left: 600px; width: 120px; height: 32px'; |
| 26 | + btnDom.textContent = 'trigger render'; |
| 27 | + |
| 28 | + canvasDom.parentElement.appendChild(btnDom); |
| 29 | + |
| 30 | + const text1 = new Text({ |
| 31 | + style: { |
| 32 | + x: 100, |
| 33 | + y: 200, |
| 34 | + text: 'A', |
| 35 | + fontSize: 16, |
| 36 | + fill: '#f00', |
| 37 | + zIndex: 1, // zIndex 为 0 或不配置时,一切正常 |
| 38 | + }, |
| 39 | + }); |
| 40 | + |
| 41 | + const text2 = new Text({ |
| 42 | + style: { |
| 43 | + x: 100, |
| 44 | + y: 300, |
| 45 | + text: 'B', |
| 46 | + fontSize: 16, |
| 47 | + fill: '#000', |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + canvas.addEventListener(CanvasEvent.READY, () => { |
| 52 | + canvas.appendChild(text1); |
| 53 | + canvas.appendChild(text2); |
| 54 | + canvas.render(); |
| 55 | + |
| 56 | + btnDom.onclick = () => { |
| 57 | + canvas.removeChild(text1); |
| 58 | + canvas.removeChild(text2); |
| 59 | + console.log('removed'); |
| 60 | + |
| 61 | + canvas.render(); // 添加这行代码后不会出现锯齿,但会有图元丢失问题 |
| 62 | + |
| 63 | + // text1.style.text = randomLetter(); |
| 64 | + text1.style.zIndex = Math.round(Math.random()); |
| 65 | + // text2.style.text = randomLetter(); |
| 66 | + |
| 67 | + canvas.appendChild(text1); |
| 68 | + canvas.appendChild(text2); |
| 69 | + console.log('appended'); |
| 70 | + |
| 71 | + canvas.render(); |
| 72 | + }; |
| 73 | + }); |
| 74 | +} |
0 commit comments