-
Notifications
You must be signed in to change notification settings - Fork 0
/
step1-2.html
75 lines (66 loc) · 1.55 KB
/
step1-2.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Mario Game</title>
<link href="./css/game.css" media="screen, projection" rel="stylesheet" type="text/css" />
<!--[if lte IE 8]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<section class="cont">
</section>
<script>
var canvas = null,
context = null,
imgCache = null,
imgs = [{id:'bg',src:'./img/bg.png'},{id:'player',src:'./img/player.png'},{id:'enemy',src:'./img/enemy.png'}];
function init(){
var canvas = document.createElement('canvas');
canvas.height = 400;
canvas.width = 600;
canvas.className = 'hb';
document.querySelector('.cont').appendChild(canvas);
context = canvas.getContext('2d');
imgCache = loadImg(imgs,drawDemo);
}
function loadImg(imgList,callback){
var imgs = {},
cn = 0,
an = imgList.length;
for (var i=0; i<an; i++){
var nubImg = imgList[i];
//var image = imgs[nubImg.id] = new Image;
imgs[nubImg.id] = new Image;
var image = imgs[nubImg.id];
console.log(image);
image.onload = function(){
cn++;
}
image.src = nubImg.src;
var that = this;
if (typeof callback == 'function')
{
function check(){
if (cn >= an)
{
drawDemo();//callback.apply(that,arguments);
}else{
setTimeout(check,100);
}
}
check();
}
}
return imgs;
}
function drawDemo(){
context.drawImage(imgCache['bg'],0,0);
context.drawImage(imgCache['player'],0,60,50,60,400,284,50,60);
context.drawImage(imgCache['enemy'],0,0,50,50,100,294,50,50);
}
window.onload = init;
</script>
</body>
</html>