-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphics.ts
More file actions
104 lines (88 loc) · 2.63 KB
/
Graphics.ts
File metadata and controls
104 lines (88 loc) · 2.63 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import Point3D from "./mathematics/Point3D";
export class Graphics {
constructor(
private context: CanvasRenderingContext2D,
public width: number,
public height: number,
public options3D = { distance: 800 }) {
}
clear() {
this.context.clearRect(0, 0, this.width, this.height);
}
drawLine(x1, y1, x2, y2) {
this.context.beginPath();
this.context.moveTo(x1, y1);
this.context.lineTo(x2, y2);
this.context.closePath();
this.context.stroke();
}
stroke(color: string, width = 1) {
this.context.strokeStyle = color;
this.context.lineWidth = width;
}
fillCircle3D(x, y, z, r = 5) {
const p = this.transform3D(x, y, z);
this.fillCircle(p.x, p.y, r)
}
drawCircle(x, y, r) {
this.context.save();
this.context.beginPath();
this.context.arc(x, y, r, 0, Math.PI * 2);
this.context.closePath();
this.context.stroke();
this.context.restore();
}
fill(color: string) {
this.context.fillStyle = color;
}
fillCircle(x, y, r) {
this.context.beginPath();
this.context.arc(x, y, r, -2 * Math.PI, 2 * Math.PI);
this.context.closePath();
this.context.fill();
}
fillText(x: number, y: number, text) {
this.context.font = '12px tahoma';
this.context.fillText(text, x, y)
}
scale3D(z: number) {
return (z + 1000) / 800
}
transform3D(x: number, y: number, z: number) {
const a = Math.atan2(y, x)
const f = Math.sqrt(x * x + y * y) * this.scale3D(z);
return {
x: (this.width / 2) + Math.cos(a) * f,
y: (this.height / 2) + Math.sin(a) * f
}
}
beginPath() {
this.context.beginPath()
}
moveTo3D(x: number, y: number, z: number) {
const point = this.transform3D(x, y, z);
this.context.moveTo(point.x, point.y);
}
lineTo3D(x: number, y: number, z: number) {
const point = this.transform3D(x, y, z);
this.context.lineTo(point.x, point.y);
}
path() {
this.context.stroke();
}
drawPoint3D(x: number, y: number, z: number) {
this.context.save();
this.moveTo3D(x, y, z);
this.context.beginPath();
this.context.arc(0, 0, 6, 0, Math.PI * 2);
this.context.fill();
this.context.restore();
}
line3D(p1: Point3D, p2: Point3D) {
this.context.save();
this.context.beginPath();
this.moveTo3D(p1.x, p1.y, p1.z);
this.lineTo3D(p2.x, p2.y, p2.z);
this.context.restore();
}
}