|
| 1 | +import { create, select } from 'd3-selection'; |
| 2 | +import { ZoomTransform } from 'd3-zoom'; |
| 3 | +import { getStyles } from './style'; |
| 4 | +import { loadSvgElements } from './svg'; |
| 5 | +import { SimulationNode } from './types'; |
| 6 | + |
| 7 | +export function loadCanvas( |
| 8 | + { width, height, deviceScale }: ReturnType<typeof getStyles>, |
| 9 | + scaledToDevice: boolean, |
| 10 | + canvasElement?: HTMLCanvasElement, |
| 11 | +) { |
| 12 | + const element = canvasElement ? select(canvasElement) : create('canvas'); |
| 13 | + |
| 14 | + const appliedWidth = scaledToDevice ? width * deviceScale : width; |
| 15 | + const appliedHeight = scaledToDevice ? height * deviceScale : height; |
| 16 | + |
| 17 | + element.attr('width', appliedWidth); |
| 18 | + element.attr('height', appliedHeight); |
| 19 | + |
| 20 | + const context = element.node()?.getContext('2d'); |
| 21 | + |
| 22 | + if (scaledToDevice) { |
| 23 | + context?.scale(deviceScale, deviceScale); |
| 24 | + } |
| 25 | + |
| 26 | + return { element, context }; |
| 27 | +} |
| 28 | + |
| 29 | +export interface DrawFrameArgs { |
| 30 | + canvas: ReturnType<typeof loadCanvas>; |
| 31 | + svgElements: ReturnType<typeof loadSvgElements>; |
| 32 | + style: ReturnType<typeof getStyles>; |
| 33 | + zoomTransform: ZoomTransform; |
| 34 | + uniqueNodeColors?: string[]; |
| 35 | +} |
| 36 | + |
| 37 | +export function drawFrame({ |
| 38 | + canvas: { element, context }, |
| 39 | + svgElements: { nodeObjects, linkObjects }, |
| 40 | + uniqueNodeColors, |
| 41 | + zoomTransform, |
| 42 | + style, |
| 43 | +}: DrawFrameArgs) { |
| 44 | + if (!context) return {}; |
| 45 | + |
| 46 | + context.save(); |
| 47 | + |
| 48 | + context.clearRect( |
| 49 | + 0, |
| 50 | + 0, |
| 51 | + Number(element.attr('width')), |
| 52 | + Number(element.attr('height')), |
| 53 | + ); |
| 54 | + context.translate(zoomTransform.x, zoomTransform.y); |
| 55 | + context.scale(zoomTransform.k, zoomTransform.k); |
| 56 | + |
| 57 | + linkObjects.each(function (link) { |
| 58 | + if (link.source.x && link.source.y && link.target.x && link.target.y) { |
| 59 | + context.beginPath(); |
| 60 | + context.strokeStyle = style.linkColor; |
| 61 | + |
| 62 | + context.moveTo(link.source.x, link.source.y); |
| 63 | + context.lineTo(link.target.x, link.target.y); |
| 64 | + |
| 65 | + context.stroke(); |
| 66 | + context.closePath(); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + const mapColorToNode = !!uniqueNodeColors; |
| 71 | + const nodeColorMap: Record<string, SimulationNode> = {}; |
| 72 | + nodeObjects.each(function (n, i) { |
| 73 | + if (n.x && n.y) { |
| 74 | + const radius = Number(select(this).attr('r')); |
| 75 | + const nodeFill = mapColorToNode ? uniqueNodeColors[i] : style.nodeColor; |
| 76 | + |
| 77 | + context.beginPath(); |
| 78 | + context.fillStyle = nodeFill; |
| 79 | + context.arc(n.x, n.y, radius, 0, Math.PI * 2); |
| 80 | + context.fill(); |
| 81 | + context.closePath(); |
| 82 | + |
| 83 | + const name = n.name.split('.md')[0]; |
| 84 | + |
| 85 | + context.beginPath(); |
| 86 | + context.fillStyle = style.titleColor; |
| 87 | + context.fillText( |
| 88 | + name, |
| 89 | + n.x - context.measureText(name).width / 2, |
| 90 | + n.y + radius + style.nodeTitlePadding, |
| 91 | + ); |
| 92 | + context.fill(); |
| 93 | + context.closePath(); |
| 94 | + |
| 95 | + if (mapColorToNode) { |
| 96 | + nodeColorMap[nodeFill] = n; |
| 97 | + } |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + context.restore(); |
| 102 | + |
| 103 | + return nodeColorMap; |
| 104 | +} |
0 commit comments