(name: string, color: string)
| 43 | } |
| 44 | |
| 45 | function createLabelTexture(name: string, color: string): LabelTexture | null { |
| 46 | const canvas = document.createElement("canvas"); |
| 47 | const ctx = canvas.getContext("2d"); |
| 48 | if (!ctx) { |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | ctx.font = TEXTURE_FONT; |
| 53 | const text = fitText(ctx, name, TEXTURE_MAX_TEXT_WIDTH); |
| 54 | const textWidth = Math.ceil(ctx.measureText(text).width); |
| 55 | const logicalWidth = Math.max( |
| 56 | 1, |
| 57 | textWidth + TEXTURE_PADDING_X * 2 + TEXTURE_STROKE_WIDTH * 2, |
| 58 | ); |
| 59 | const logicalHeight = |
| 60 | TEXTURE_FONT_SIZE + TEXTURE_PADDING_Y * 2 + TEXTURE_STROKE_WIDTH * 2; |
| 61 | const pixelRatio = |
| 62 | typeof window === "undefined" |
| 63 | ? 1 |
| 64 | : Math.min(window.devicePixelRatio || 1, 2); |
| 65 | |
| 66 | canvas.width = Math.ceil(logicalWidth * pixelRatio); |
| 67 | canvas.height = Math.ceil(logicalHeight * pixelRatio); |
| 68 | canvas.style.width = `${logicalWidth}px`; |
| 69 | canvas.style.height = `${logicalHeight}px`; |
| 70 | |
| 71 | ctx.scale(pixelRatio, pixelRatio); |
| 72 | ctx.font = TEXTURE_FONT; |
| 73 | ctx.textAlign = "center"; |
| 74 | ctx.textBaseline = "middle"; |
| 75 | ctx.lineJoin = "round"; |
| 76 | ctx.lineWidth = TEXTURE_STROKE_WIDTH; |
| 77 | ctx.strokeStyle = "rgba(0, 0, 0, 0.9)"; |
| 78 | ctx.fillStyle = color; |
| 79 | |
| 80 | const x = logicalWidth / 2; |
| 81 | const y = logicalHeight / 2; |
| 82 | ctx.strokeText(text, x, y); |
| 83 | ctx.fillText(text, x, y); |
| 84 | |
| 85 | const texture = new THREE.CanvasTexture(canvas); |
| 86 | texture.colorSpace = THREE.SRGBColorSpace; |
| 87 | texture.minFilter = THREE.LinearFilter; |
| 88 | texture.magFilter = THREE.LinearFilter; |
| 89 | texture.generateMipmaps = false; |
| 90 | texture.needsUpdate = true; |
| 91 | |
| 92 | return { |
| 93 | texture, |
| 94 | width: logicalWidth, |
| 95 | height: logicalHeight, |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | function NodeLabelSprite({ node }: { node: GraphNode }) { |
| 100 | const label = useMemo( |
no test coverage detected