-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvasSurface.html
296 lines (268 loc) · 7.71 KB
/
canvasSurface.html
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<!DOCTYPE html>
<html>
<head>
<style>
body {
text-align: center;
}
</style>
</head>
<body>
<!-- <div class="main">
<canvas id="canvas"></canvas>
</div> -->
<script>
var constants = {
canvasWidth : 600,
canvasHeight : 600,
leftArrow : 37,
upArrow : 38,
rightArrow : 39,
downArrow : 40,
xMin : -9,
xMax : 9,
yMin : -9,
yMax : 9,
xDelta : 0.2,
yDelta : 0.2,
colorMap : ['#060', '#090', '#0c0', '#0f0', '#9fo','#9c0', '#990', '#960', '#930', '#900', '#c00'],
pointWidth : 2,
dTheta : 0.05,
surfaceScale : 24
};
var X = 0;
var Y = 1;
var Z = 2;
var controlKeyPressed = false; // Shared between processKeyDown();
var surface = new Surface(); // A set of points
function point (x, y, z) {
return [x, y, z];
}
function Surface () {
this.points = [];
return this;
}
Surface.prototype.equation = function (x, y) {
var d = Math.sqrt(x * x + y * y);
return 4 * (Math.sin(d) / d);
}
Surface.prototype.sortByXZIndex = function (a, b) {
return a[Z] - b[Z];
}
Surface.prototype.generate = function () {
var i = 0;
for (var x = constants.xMin; x <= constants.xMax; x += constants.xDelta) {
for (var y = constants.yMin; y <= constants.yMax; y+= constants.yDelta) {
this.points[i] = point(x, y, this.equation(x, y));
i++;
}
}
// console.log(this.points.length);
};
Surface.prototype.color = function () {
var z;
this.zMin = this.zMax = this.points[0][Z];
for (var i = 0; i < this.points.length; i++) {
z = this.points[i][Z];
if (z < this.zMin) {
this.zMin = z;
}
if (z > this.zMax) {
this.zMax = z;
}
}
var zDelta = Math.abs(this.zMax - this.zMin) / constants.colorMap.length;
// console.log(zDelta);
for (var i = 0; i < this.points.length; i++) {
this.points[i].color = constants.colorMap[Math.floor((this.points[i][Z] - this.zMin) / zDelta)];
// console.log(Math.floor((this.points[i][Z] - this.zMin) - zDelta));
}
};
Surface.prototype.draw = function () {
var myCanvas = document.getElementById('myCanvas');
var ctx = myCanvas.getContext('2d');
this.points = this.points.sort(Surface.sortByXZIndex);
var point;
for (var i = 0; i < this.points.length; i++) {
point = this.points[i];
ctx.fillStyle = point.color;
// console.log(point.color)
ctx.fillRect(point[X] * constants.surfaceScale, point[Y] * constants.surfaceScale, constants.pointWidth, constants.pointWidth);
}
};
// Surface.prototype.multi = function (R) {
// var px = 0, py = 0, pz = 0, sum;
// var p = this.points;
// for (var v = 0; v < p.length; v++) {
// px = p[v][X];
// py = p[v][Y];
// pz = p[v][Z];
// for (var row = 0; row < 3; row++) {
// sum = (R[row][X] * px) + (R[row][Y] * pz) + (R[row][Z] * pz);
// p[v][row] = sum;
// }
// }
// }
Surface.prototype.multi = function(R)
/*
Assumes that R is a 3 x 3 matrix and that this.points (i.e., P) is a 3 x n matrix. This method performs P = R * P.
*/
{
var Px = 0, Py = 0, Pz = 0; // Variables to hold temporary results.
var P = this.points; // P is a pointer to the set of surface points (i.e., the set of 3 x 1 vectors).
var sum; // The sum for each row/column matrix product.
for (var V = 0; V < P.length; V++) // For all 3 x 1 vectors in the point list.
{
Px = P[V][X], Py = P[V][Y], Pz = P[V][Z];
for (var Rrow = 0; Rrow < 3; Rrow++) // For each row in the R matrix.
{
sum = (R[Rrow][X] * Px) + (R[Rrow][Y] * Py) + (R[Rrow][Z] * Pz);
P[V][Rrow] = sum;
}
}
}
Surface.prototype.yRotate = function (sign) {
var R = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
];
var sign = sign*constants.dTheta;
R[0][0] = Math.cos( sign );
R[0][1] = 0;
R[0][2] = Math.sin( sign );
R[1][0] = 0;
R[1][1] = 1;
R[1][2] = 0;
R[0][0] = -Math.sin( sign );
R[0][1] = 0;
R[0][2] = Math.cos( sign );
this.multi(R);
this.erase();
this.draw();
};
Surface.prototype.zRotate = function (sign) {
var R = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
];
var sign = sign*constants.dTheta;
R[0][0] = Math.cos( sign );
R[0][1] = -Math.sin( sign );
R[0][2] = 0;
R[1][0] = Math.sin( sign );
R[1][1] = Math.cos( sign );
R[1][2] = 0;
R[0][0] = 0
R[0][1] = 0;
R[0][2] = 1;
this.multi(R);
this.erase();
this.draw();
};
/*Surface.prototype.xRotate = function (sign) {
var R = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
];
var sign = sign * constants.dTheta;
R[0][0] = 1;
R[0][1] = 0;
R[0][2] = 0;
R[1][0] = 0;
R[1][1] = Math.cos( sign );
R[1][2] = -Math.sin( sign );
R[0][0] = 0;
R[0][1] = Math.sin( sign );
R[0][2] = Math.cos( sign );
this.multi(R);
this.erase();
this.draw();
};*/
Surface.prototype.xRotate = function(sign)
/*
Assumes "sign" is either 1 or -1, which is used to rotate the surface "clockwise" or "counterclockwise".
*/
{
var Rx = [ [0, 0, 0],
[0, 0, 0],
[0, 0, 0] ]; // Create an initialized 3 x 3 rotation matrix.
Rx[0][0] = 1;
Rx[0][1] = 0; // Redundant but helps with clarity.
Rx[0][2] = 0;
Rx[1][0] = 0;
Rx[1][1] = Math.cos( sign*constants.dTheta );
Rx[1][2] = -Math.sin( sign*constants.dTheta );
Rx[2][0] = 0;
Rx[2][1] = Math.sin( sign*constants.dTheta );
Rx[2][2] = Math.cos( sign*constants.dTheta );
this.multi(Rx); // If P is the set of surface points, then this method performs the matrix multiplcation: Rx * P
this.erase(); // Note that one could use two canvases to speed things up, which also eliminates the need to erase.
this.draw();
}
Surface.prototype.erase = function () {
var myCanvas = document.getElementById('myCanvas');
var ctx = myCanvas.getContext('2d');
ctx.clearRect(-constants.canvasWidth / 2, -constants.canvasHeight / 2, myCanvas.width, myCanvas.height);
}
function appendCanvasElement () {
var canvasElement = document.createElement('canvas');
canvasElement.width = constants.canvasWidth;
canvasElement.height = constants.canvasHeight;
canvasElement.id = 'myCanvas';
// canvasElement.getContext('2d').translate(constants.canvasWidht / 2, constants.canvasHeight / 2);
canvasElement.getContext('2d').translate(constants.canvasWidth / 2, constants.canvasHeight / 2);
document.body.appendChild(canvasElement);
}
function processKeyDown (evt) {
if (evt.ctrlKey) {
switch (evt.keyCode) {
case constants.upArrow :
evt.preventDefault();
break;
case constants.downArrow :
evt.preventDefault();
break;
case constants.rightArrow :
evt.preventDefault();
surface.zRotate(-1);
break;
case constants.leftArrow :
evt.preventDefault();
surface.zRotate(1);
break;
}
return;
}
switch (evt.keyCode) {
case constants.upArrow :
surface.xRotate(1);
evt.preventDefault();
break;
case constants.downArrow :
evt.preventDefault();
surface.xRotate(-1);
break;
case constants.rightArrow :
evt.preventDefault();
surface.yRotate(-1);
break;
case constants.leftArrow :
evt.preventDefault();
surface.yRotate(1);
break;
}
}
function onloadInit () {
appendCanvasElement();
surface.draw();
document.addEventListener('keydown', processKeyDown, false);
}
surface.generate();
surface.color();
window.addEventListener('load', onloadInit, false);
</script>
</body>
</html>