-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotAndCold.js
103 lines (87 loc) · 4.05 KB
/
hotAndCold.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
// JavaScript Document
var canvas = document.getElementById("theCanvas");
if(canvas.getContext){
var ctx = document.getElementById('theCanvas').getContext('2d');
var background = new Image();
background.src = 'http://i246.photobucket.com/albums/gg93/micintexp/Pixel_Art%20Training/64_64_zps7bdeaf29.jpg';
//background
background.onload = function(){//don't do anytihng until bacground is loaded
console.log("background image loaded");
//keep track of bf coordinates
var backgroundInfo = {
'x':0,
'y':0
};
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(background, 64, 64);
backgroundInfo.x = 64;
backgroundInfo.y = 64;
function move(x,y) {
//console.log("moving");
var loopTimer;
xGoal = backgroundInfo.x + x;
console.log("xGoal: " + xGoal);
yGoal = backgroundInfo.y + y;
console.log("yGoal: " + yGoal);
//if animation has not completed
function animate(){
if(backgroundInfo.x === xGoal && backgroundInfo.y === yGoal){//if we hit the distance goal
clearTimeout(loopTimer);
console.log("congratulations, the animation completed!");
}
//else
else {
//save, do something, restore
ctx.save();
ctx.clearRect(0, 0, canvas.width, canvas.height);
//console.log("background started at: " + backgroundInfo.x + " " + backgroundInfo.y);
backgroundInfo.x += x/32;
backgroundInfo.y += y/32;
console.log("background at: " + backgroundInfo.x + " " + backgroundInfo.y);
//TODO: if the backgrond is going to start moing off-screen, we need to do something idfferent
ctx.drawImage(background, backgroundInfo.x, backgroundInfo.y);//draw the background to the new location
ctx.restore();//restore canvas back to original location
var loopTimer = setTimeout(animate, 1);//function to call to iterate through the animation, and the speed at which the animation will take to complete
}
};
animate();
};
//event listener for key press
document.addEventListener('keydown', keyDownAction, false);
function keyDownAction(e) {//handle directions
e = e || window.event;
switch (e.keyCode) {
case 37://left arrow
console.log("left");
//document.removeEventListener('keydown', keyDownAction, false);
move(-64,0);
break;
case 38://up arrow
//document.removeEventListener('keydown', keyDownAction, false);
move(0, -64);
break;
case 39://right arrow
//document.removeEventListener('keydown', keyDownAction, false);
move(64, 0);
break;
case 40://down arrow
//document.removeEventListener('keydown', keyDownAction, false);
move(0, 64);
break;
default:
break;
}
};
};
}
else console.log("not compatible with canvas");
/*
function move(direction) {
//save, do something, restore
window.setInterval(
ctx.save();
ctx.translate(1, 0);
ctx.drawImage(background, 0, 0);
ctx.restore();
}
*/