2021-05-09 13:04:42 +00:00
|
|
|
// Some helpers for drawing SVGs.
|
2021-06-24 08:18:14 +00:00
|
|
|
import { getSweep } from 'utils'
|
2021-05-09 13:04:42 +00:00
|
|
|
|
|
|
|
// General
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
export default class Svg {
|
|
|
|
static ellipse = (A: number[], r: number): string => {
|
|
|
|
return `M ${A[0] - r},${A[1]}
|
2021-05-09 13:04:42 +00:00
|
|
|
a ${r},${r} 0 1,0 ${r * 2},0
|
|
|
|
a ${r},${r} 0 1,0 -${r * 2},0 `
|
2021-06-21 21:35:28 +00:00
|
|
|
}
|
2021-05-09 13:04:42 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
static moveTo = (v: number[]): string => {
|
|
|
|
return `M ${v[0]},${v[1]} `
|
|
|
|
}
|
2021-05-09 13:04:42 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
static lineTo = (v: number[]): string => {
|
|
|
|
return `L ${v[0]},${v[1]} `
|
|
|
|
}
|
2021-05-09 13:04:42 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
static line = (a: number[], ...pts: number[][]): string => {
|
|
|
|
return Svg.moveTo(a) + pts.map((p): string => Svg.lineTo(p)).join()
|
|
|
|
}
|
2021-05-09 13:04:42 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
static hLineTo = (v: number[]): string => {
|
|
|
|
return `H ${v[0]},${v[1]} `
|
|
|
|
}
|
2021-05-09 13:04:42 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
static vLineTo = (v: number[]): string => {
|
|
|
|
return `V ${v[0]},${v[1]} `
|
|
|
|
}
|
2021-05-09 13:04:42 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
static bezierTo = (A: number[], B: number[], C: number[]): string => {
|
|
|
|
return `C ${A[0]},${A[1]} ${B[0]},${B[1]} ${C[0]},${C[1]} `
|
|
|
|
}
|
2021-05-09 13:04:42 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
static arcTo = (C: number[], r: number, A: number[], B: number[]): string => {
|
|
|
|
return [
|
|
|
|
Svg.moveTo(A),
|
|
|
|
'A',
|
|
|
|
r,
|
|
|
|
r,
|
|
|
|
0,
|
|
|
|
getSweep(C, A, B) > 0 ? '1' : '0',
|
|
|
|
0,
|
|
|
|
B[0],
|
|
|
|
B[1],
|
|
|
|
].join(' ')
|
|
|
|
}
|
2021-05-09 13:04:42 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
static closePath = (): string => {
|
|
|
|
return 'Z'
|
|
|
|
}
|
2021-05-09 13:04:42 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
static rectTo = (A: number[]): string => {
|
|
|
|
return ['R', A[0], A[1]].join(' ')
|
|
|
|
}
|
2021-05-09 13:04:42 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
static getPointAtLength = (
|
|
|
|
path: SVGPathElement,
|
|
|
|
length: number
|
|
|
|
): number[] => {
|
|
|
|
const point = path.getPointAtLength(length)
|
|
|
|
return [point.x, point.y]
|
|
|
|
}
|
2021-05-09 13:04:42 +00:00
|
|
|
}
|