/
canvas.tsx
79 lines (67 loc) · 2.09 KB
/
canvas.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { CellCanvasRendererProps, HeatMapDatum } from './types'
export const renderRect = <Datum extends HeatMapDatum>(
ctx: CanvasRenderingContext2D,
{
cell: { x, y, width, height, color, borderColor, opacity, labelTextColor, label },
borderWidth,
enableLabels,
theme,
}: CellCanvasRendererProps<Datum>
) => {
ctx.save()
ctx.globalAlpha = opacity
ctx.fillStyle = color
if (borderWidth > 0) {
console.log(borderWidth)
ctx.strokeStyle = borderColor
ctx.lineWidth = borderWidth
}
ctx.fillRect(x - width / 2, y - height / 2, width, height)
if (borderWidth > 0) {
ctx.strokeRect(x - width / 2, y - height / 2, width, height)
}
if (enableLabels) {
ctx.fillStyle = labelTextColor
ctx.font = `${theme.labels.text.fontWeight ? `${theme.labels.text.fontWeight} ` : ''}${
theme.labels.text.fontSize
}px ${theme.labels.text.fontFamily}`
ctx.textAlign = 'center'
ctx.textBaseline = 'middle'
ctx.fillText(label, x, y)
}
ctx.restore()
}
export const renderCircle = <Datum extends HeatMapDatum>(
ctx: CanvasRenderingContext2D,
{
cell: { x, y, width, height, color, borderColor, opacity, labelTextColor, label },
borderWidth,
enableLabels,
theme,
}: CellCanvasRendererProps<Datum>
) => {
ctx.save()
ctx.globalAlpha = opacity
const radius = Math.min(width, height) / 2
ctx.fillStyle = color
if (borderWidth > 0) {
ctx.strokeStyle = borderColor
ctx.lineWidth = borderWidth
}
ctx.beginPath()
ctx.arc(x, y, radius, 0, 2 * Math.PI)
ctx.fill()
if (borderWidth > 0) {
ctx.stroke()
}
if (enableLabels) {
ctx.fillStyle = labelTextColor
ctx.font = `${theme.labels.text.fontWeight ? `${theme.labels.text.fontWeight} ` : ''}${
theme.labels.text.fontSize
}px ${theme.labels.text.fontFamily}`
ctx.textAlign = 'center'
ctx.textBaseline = 'middle'
ctx.fillText(label, x, y)
}
ctx.restore()
}