-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
245 lines (170 loc) · 6.4 KB
/
app.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
class BlochSphereVisualiser {
constructor(canvasId) {
var that = this;
this.lastDrawTimestamp = 0;
window.addEventListener("mousedown", function (e) { that.mouseDown(e); });
window.addEventListener("mouseup", function (e) { that.mouseDown(e); });
window.addEventListener("mousemove", function (e) { that.mouseDown(e); });
this.canvasId = canvasId;
this.alpha = 70;
this.beta = 0;
this.cx = 1000;
this.cy = 1000;
this.sphereRadius = 800;
this.sphereRotation = 20;
this.stateAlpha = 140;
this.stateBeta = 45;
this.time = 0;
}
__start__() {
var that = this;
this.canvas = document.getElementById(this.canvasId);
this.context = this.canvas.getContext("2d");
this.context.imageSmoothingQuality = "high";
requestAnimationFrame(function (timestamp) { that.__loop__(timestamp); });
}
__loop__(timestamp) {
var delta = timestamp - this.lastDrawTimestamp;
if (delta > 30) {
this.draw();
this.lastDrawTimestamp = timestamp;
this.time += delta;
}
var that = this;
requestAnimationFrame(function (timestamp) { that.__loop__(timestamp); });
}
mouseDown(e) { }
mouseUp(e) { }
mouseMove(e) { }
projectPoint(x, y, z) {
var ca = Math.cos(this.alpha);
var sa = Math.sin(this.alpha);
var cb = Math.cos(this.beta);
var sb = Math.sin(this.beta);
var x2 = x;
var y2 = ca * y + sa * z;
var z2 = - sa * y + ca * z;
var x3 = cb * x2 - sb * z2;
var y3 = y2;
var z3 = sb * x2 + cb * z2;
var x4 = x3 + this.cx;
var y4 = y3 + this.cy;
return { "x": x4, "y": y4 };
}
getSurfaceVector(theta, phi) {
var thetaR = 2 * Math.PI * theta / 360;
var phiR = 2 * Math.PI * phi / 360;
var x1 = this.sphereRadius * Math.sin(thetaR) * Math.cos(phiR);
var y1 = this.sphereRadius * Math.sin(thetaR) * Math.sin(phiR);
var z1 = this.sphereRadius * Math.cos(thetaR);
return { "x": x1, "y": y1, "z": z1 };
}
getLineOfLatitude(latitude) {
var path = [];
for (var i = 0; i <= 360; i += 5) {
var point1 = this.getSurfaceVector(latitude, i);
var point2 = this.projectPoint(point1.x, point1.y, point1.z);
path.push(point2);
}
return path;
}
drawLineOfLatitude(context, latitude, colour = "black") {
var path = this.getLineOfLatitude(latitude);
this.drawPath(context, path, colour);
}
drawPath(context, path, colour = "black") {
context.strokeStyle = colour;
context.lineWidth = 3;
context.beginPath();
context.moveTo(path[0].x, path[0].y);
for (var i = 1; i < path.length; i++) {
context.lineTo(path[i].x, path[i].y);
}
context.stroke();
}
fillPath(context, path, colour = "black") {
context.fillStyle = colour;
context.beginPath();
context.moveTo(path[0].x, path[0].y);
for (var i = 1; i < path.length; i++) {
context.lineTo(path[i].x, path[i].y);
}
context.fill();
}
drawLine(context, x1, y1, x2, y2, colour = "black") {
context.strokeStyle = colour;
context.lineWidth = 3;
context.beginPath();
context.moveTo(x1, y1);
context.lineTo(x2, y2);
context.stroke();
}
drawArrowHead(context, x, y, ux, uy, colour = "black") {
var path = []
var w = 22;
var h = 40;
var u = this.getNormalVector(ux, uy);
var n = this.getNormalVector(u.x, u.y);
path.push({ "x": x, "y": y });
path.push({ "x": x + u.x * w + n.x * h, "y": y + u.y * w + n.y * h });
path.push({ "x": x + n.x * h * 0.8, "y": y + n.y * h * 0.8 });
path.push({ "x": x + u.x * -w + n.x * h, "y": y + u.y * -w + n.y * h });
path.push({ "x": x, "y": y });
this.fillPath(context, path, colour);
}
drawState(context) {
var r = 30 * this.time / 1000;
var v1 = this.getSurfaceVector(this.stateAlpha, this.stateBeta + this.sphereRotation + r);
var v1p = this.projectPoint(v1.x, v1.y, v1.z);
this.drawLine(context, this.cx, this.cy, v1p.x, v1p.y);
context.fillStyle = "black";
context.beginPath();
context.arc(v1p.x, v1p.y, 10, 0, 360);
context.fill();
}
getUnitVector(x, y) {
var m = Math.sqrt(x * x + y * y);
return { "x": x / m, "y": y / m };
}
getNormalVector(x, y) {
var theta = 2 * Math.PI * 90 / 360;
var x2 = Math.cos(theta) * x - Math.sin(theta) * y;
var y2 = Math.sin(theta) * x - Math.cos(theta) * y;
return { "x": x2, "y": y2 };
}
draw() {
var c = this.context;
c.fillStyle = "white";
c.fillRect(0, 0, 2000, 2000);
for (var i = 10; i < 180; i += 10) {
this.drawLineOfLatitude(c, i, "lightgrey");
}
var r = 30 * this.time / 1000;
var v1 = this.getSurfaceVector(180, 0 + this.sphereRotation + r);
var v1p = this.projectPoint(v1.x, v1.y, v1.z);
var v1u = this.getUnitVector(v1p.x - this.cx, v1p.y - this.cy);
this.drawLine(c, this.cx, this.cy, v1p.x, v1p.y);
this.drawArrowHead(c, v1p.x, v1p.y, v1u.x, v1u.y);
var v2 = this.getSurfaceVector(90, 0 + this.sphereRotation + r);
var v2p = this.projectPoint(v2.x, v2.y, v2.z);
var v2u = this.getUnitVector(v2p.x - this.cx, v2p.y - this.cy);
this.drawLine(c, this.cx, this.cy, v2p.x, v2p.y);
this.drawArrowHead(c, v2p.x, v2p.y, v2u.x, v2u.y);
var v3 = this.getSurfaceVector(90, 90 + this.sphereRotation + r);
var v3p = this.projectPoint(v3.x, v3.y, v3.z);
var v3u = this.getUnitVector(v3p.x - this.cx, v3p.y - this.cy);
this.drawLine(c, this.cx, this.cy, v3p.x, v3p.y);
this.drawArrowHead(c, v3p.x, v3p.y, v3u.x, v3u.y);
this.drawState(c);
c.strokeStyle = "black";
c.lineWidth = 3;
c.beginPath();
c.arc(this.cx, this.cy, this.sphereRadius, 0, 360);
c.stroke();
this.drawLineOfLatitude(c, 90);
}
}
function loadVisualiser() {
var visualiser = new BlochSphereVisualiser("maincanvas");
visualiser.__start__();
}