-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP06-canvas-draw.js
502 lines (349 loc) · 13.1 KB
/
P06-canvas-draw.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*================================================
PART 06: Drawing on the canvas
It's time to draw our ships and asteroids on
the screen with the canvas API.
https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial
The first thing to do is to get the context, this
is where all canvas methods are stored.
We will use the '2d' context that allow us to draw
lines, arcs, images, rectangles, etc.
=================================================*/
var canvas = document.querySelector('.screen');
var ctx = canvas.getContext('2d');
/*================================================
The width and height tells the number of pixels
the browser will calculate for the canvas.
=================================================*/
canvas.width = 256;
canvas.height = 256;
/*================================================
We need a camera object with a few values.
The position on top of the player ship, the
rotation poiting down (angle in radians) and
the lens XY values for pan and Z for zoom.
=================================================*/
var cameraOffset = 5;
var camera = {
position: { x: player.ship.x, y: player.ship.y - cameraOffset, z: 10 },
rotation: { x: -Math.PI * 0.9, y: 0, z: 0 },
lens: { x: canvas.width / 2, y: canvas.height / 2, z: 120 }
};
/*================================================
Now let's create our "draw" global variable to
hold our beautyful art.
=================================================*/
var draw = {
/*================================================
To create custom RGB (red, green and blue ) colors
we will use the customization values.
If you are not familiar with RGB take a look here:
https://en.wikipedia.org/wiki/RGB_color_model
Let's use the attack for our red channel, the
speed for the green and the control for the blue.
Since the customization values go from 0 to 100
we need to scale them.
=================================================*/
color: function (p) {
var r = p.attack * 2.5;
var g = p.speed * 2.5;
var b = p.control * 2.5;
return 'rgba('+r+','+g+','+b+', 1)';
},
/*================================================
With our color function we can now create a
function to draw a single colored line.
First we project each point to our camera
then we use the canvas API to stroke our line.
We will only draw the line if the points are
inside out screen space.
=================================================*/
line: function (color, start, end, width=1) {
var s = draw.projectToCamera(start);
var e = draw.projectToCamera(end);
/*================================================
To avoid clipping ships on the edge of the screen
we use an offset value.
=================================================*/
var o = 25;
var inScreen = function (p) {
return (p.x > -o && p.x < canvas.width + o &&
p.y > -o && p.y < canvas.height + o);
};
/*================================================
We want all our lines ends to be rounded so let's
set the line cap value to round.
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineCap
=================================================*/
if (inScreen(s) && inScreen(e)){
ctx.lineCap = "round";
ctx.lineWidth = width;
ctx.strokeStyle = color;
ctx.beginPath();
ctx.moveTo(s.x, s.y);
ctx.lineTo(e.x, e.y);
ctx.stroke();
ctx.closePath();
}
}, /* close draw.line function */
/*================================================
Easy peasy, now let's make a circle. As before,
fist we project the center point. Let's also make
our circles default line width equal to one pixel
and our fill color transparent.
=================================================*/
circle: function (color, circle, width=1, fill='#0000') {
var c = draw.projectToCamera({
x: circle.x,
y: circle.y
});
/*================================================
Not let's calculate the radius projection.
=================================================*/
var radiusPoint = draw.projectToCamera({
x: circle.x + circle.r, y: circle.y
});
c.r = radiusPoint.x - c.x;
/*================================================
And let's use the canvas API to draw an full arc.
=================================================*/
if (c.r >= 0) {
ctx.lineWidth = width;
ctx.fillStyle = fill;
ctx.strokeStyle = color;
ctx.beginPath();
ctx.arc(c.x, c.y, c.r, 0, Math.PI*2);
ctx.stroke();
ctx.fill();
ctx.closePath();
}
}, /* close draw.circle function */
/*================================================
That was too easy, come on. Let's make something
fancier, shall we? Let's make a function that will
draw in 3D! All we gotta do is to scale the size
of our 3D points, I mean stars, along their Z axis.
Then we can just use the fill rectangle function.
=================================================*/
dot: function (color, p3d) {
/*================================================
Our stars Z value are between 8 and -8
We want our stars size to stay between 1.5 and 0.5
We invert the sign and divide by world.height to
get a factor between 0 and 1. Now we just jave to
subtract the factor from the maximum desired value.
=================================================*/
var factor = -p3d.z / 8;
var size = 1.5 - factor;
var p = draw.projectToCamera(p3d);
ctx.fillStyle = color;
ctx.fillRect(p.x, p.y, size, size);
}, /* close draw.dot function */
/*================================================
Now you are wondering how that project function
works right? If you never heard about a projection
don't worry, I got you covered.
https://en.wikipedia.org/wiki/3D_projection
But if you think that's too much math, just skip
all this camera details and move forward.
If you are still with me, let's dig in.
I'll store the camera for brevity.
=================================================*/
projectToCamera: function (p) {
var c = camera;
/*================================================
If our point don't have a Z axis value, let's
assume it's equal to 0.
=================================================*/
if (!p.z) p.z = 0;
/*================================================
First we pre-store our camera rotation cosines
to avoid recalculating them.
=================================================*/
var sin = {
x: Math.sin(c.rotation.x),
y: Math.sin(c.rotation.y),
z: Math.sin(c.rotation.z),
};
var cos = {
x: Math.cos(c.rotation.x),
y: Math.cos(c.rotation.y),
z: Math.cos(c.rotation.z)
};
/*================================================
Now we store the distance between the camera and
the point, that's a simple subtraction.
=================================================*/
var a = {
x: p.x - c.position.x,
y: p.y - c.position.y,
z: p.z - c.position.z
};
/*================================================
We need to project each distance, for that we need
our old friend Euler. More specifically his works
on how to orient rigid bodies.
If you love maths and wanna dive real deep, go on:
https://en.wikipedia.org/wiki/Euler_angles
If you think you already are too deep then just
try to keep this image in your mind:
https://en.wikipedia.org/wiki/File:EulerProjections.svg
=================================================*/
var szx = sin.z*a.x,
szy = sin.z*a.y,
czx = cos.z*a.x,
cyz = cos.y*a.z,
czx = cos.z*a.x,
czy = cos.z*a.y,
szyczx = szy+czx,
czyszx = czy-szx;
var d = {
x: (cos.y*(szyczx))-(sin.y*a.z),
y: (sin.x*(cyz+(sin.y*(szyczx)))+(cos.x*(czyszx))),
z: (cos.x*(cyz+(sin.y*(szyczx)))-(sin.x*(czyszx))),
};
/*================================================
That was the most complex part of this function.
I'll admit that it's hard to grasp. But if you
are still here, that means you've understood it
a little better right?
Now that everything is projected we still need to
scale the distances with our camera zoom.
This will make the illusion that our stars further
away from the camera are moving slower.
To do that we are gonig to need a scale factor.
=================================================*/
var factor = c.lens.z / d.z;
/*================================================
If our "d.z" value is equal to zero we will have a
math problem. It's not possible to divide by zero:
https://en.wikipedia.org/wiki/Division_by_zero
That will happen when the point has the same
Z value as the camera zoom.
In our game this wont happen because our camera
Z value is greater then all other values.
But just to keep our code error free, let's
avoid this scenario with a simple if statement
and just set our factor to zero when that
condition occour. That's how the engineers
solve problems ( deal with it mathematicians >:D )
=================================================*/
if (d.z == 0) factor = 0;
/*================================================
Now we can calculate our 2d point by this factor.
The last thing to do is add our lens XY positions
to pan the image to the center of the screen.
=================================================*/
var d2 = {
x: (factor * d.x) + c.lens.x,
y: (factor * d.y) + c.lens.y
};
/*================================================
Now we can return our projected point to be drawn.
=================================================*/
return d2;
}, /* close draw.projectToCamera function */
/*=================================================
Let's create a function to keep the player ship
on the middle of our screen.
=================================================*/
moveCamera: function () {
/*=================================================
We move the camera by copying our ship position,
this way our ship will always be on the center
of the screen while playing.
=================================================*/
if (!player.actionInput.editing) {
camera.position.x = player.ship.x;
camera.position.y = player.ship.y - cameraOffset;
}
/*=================================================
And we animate our starfield by moving our camera
to the left.
=================================================*/
else {
camera.position.x += 0.05;
world.limit(camera.position);
}
}, /* close draw player function */
/*================================================
To create the infinity illusion we will create 8
mirrored images. For this to work we have to make
sure our camera is far enough from the original.
It will look like this:
M M M
M O M
M M M
I see a loop there ( and some mother issues :D )
So the middle image is our origin. In our loop,
that's when x and y are equal to 0.
=================================================*/
loopXY: function (callback) {
loop( -1, 2, function (x) {
loop( -1, 2, function (y) {
callback(x, y);
});
});
},
/*================================================
Now we can draw mirrored dots, circles and lines.
=================================================*/
dotMirrored: function (color, p) {
draw.loopXY(function (x, y) {
var mp = {
x: p.x + (x * world.width),
y: p.y + (y * world.height),
z: p.z
};
draw.dot(color, mp);
});
},
/*================================================
That's a great effect, we will use it on our stars.
Let's use it on our circles as well. We will use
this function to draw our bullets and asterois.
We will just pass along the line color, width and
fill color values.
=================================================*/
circleMirrored: function (color, c, width, fill) {
draw.loopXY(function (x, y) {
var mc = {
x: c.x+(x*world.width),
y: c.y+(y*world.height),
r: c.r
};
draw.circle(color, mc, width, fill);
});
},
/*================================================
We also need mirrored lines to apply this effect
on our ships. For brevity start = s and end = e
=================================================*/
lineMirrored: function (color, s, e, width) {
draw.loopXY(function (x, y) {
var start = {
x: s.x+(x*world.width),
y: s.y+(y*world.height),
z: s.z
};
var end = {
x: e.x+(x*world.width),
y: e.y+(y*world.height),
z: e.z
};
draw.line(color, start, end, width);
}); /* close loopXY function */
}, /* close draw.lineMirrored function */
/*================================================
Last we have simple function that just clears out
all our canvas screen with the clearRect method.
=================================================*/
clear: function () {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
}; /* close draw global var */
/*================================================
That was the longest lesson, if you made this far
you the next ones will be easy. Next we are going
to use this functions to draw a parallax starfield.
What are you waiting for? Go to "P07-tv-stars.js"
=================================================*/