-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.js
62 lines (48 loc) · 1.01 KB
/
Game.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
function Game()
{
this.initCanvas();
this.draw();
}
Game.prototype.test = function(e)
{
console.log("test");
console.log(e.keyCode);
}
Game.prototype.update = function()
{
}
Game.prototype.gameLoop = function()
{
// this.update();
console.log("gameLoop");
window.requestAnimationFrame(game.gameLoop);
}
Game.prototype.initCanvas = function()
{
canvas= document.createElement("canvas");
/*ctx is the context that we will draw on*/
ctx = canvas.getContext("2d");
document.body.appendChild(canvas);
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
Game.prototype.draw = function()
{
ctx.fillStyle = rgb(255,0,0);
ctx.fillRect(0,0,100,100);
}
/*function for rgb for convenience*/
function rgb(r, g, b)
{
return 'rgb('+clamp(Math.round(r),0,255)+', '+clamp(Math.round(g),0,255)+', '+clamp(Math.round(b),0,255)+')';
}
/*helper function*/
function clamp(value, min, max)
{
if(max<min) {
var temp = min;
min = max;
max = temp;
}
return Math.max(min, Math.min(value, max));
}