-
Notifications
You must be signed in to change notification settings - Fork 609
/
EllipseElement.ts
64 lines (59 loc) · 1.32 KB
/
EllipseElement.ts
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
import { RenderingContext2D } from '../types'
import { BoundingBox } from '../BoundingBox'
import { PathElement } from './PathElement'
export class EllipseElement extends PathElement {
type = 'ellipse'
path(ctx: RenderingContext2D) {
const KAPPA = 4 * ((Math.sqrt(2) - 1) / 3)
const rx = this.getAttribute('rx').getPixels('x')
const ry = this.getAttribute('ry').getPixels('y')
const cx = this.getAttribute('cx').getPixels('x')
const cy = this.getAttribute('cy').getPixels('y')
if (ctx && rx > 0 && ry > 0) {
ctx.beginPath()
ctx.moveTo(cx + rx, cy)
ctx.bezierCurveTo(
cx + rx,
cy + (KAPPA * ry),
cx + (KAPPA * rx),
cy + ry,
cx,
cy + ry
)
ctx.bezierCurveTo(
cx - (KAPPA * rx),
cy + ry,
cx - rx,
cy + (KAPPA * ry),
cx - rx,
cy
)
ctx.bezierCurveTo(
cx - rx,
cy - (KAPPA * ry),
cx - (KAPPA * rx),
cy - ry,
cx,
cy - ry
)
ctx.bezierCurveTo(
cx + (KAPPA * rx),
cy - ry,
cx + rx,
cy - (KAPPA * ry),
cx + rx,
cy
)
ctx.closePath()
}
return new BoundingBox(
cx - rx,
cy - ry,
cx + rx,
cy + ry
)
}
getMarkers() {
return null
}
}