-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas5.html
More file actions
181 lines (181 loc) · 4.71 KB
/
canvas5.html
File metadata and controls
181 lines (181 loc) · 4.71 KB
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
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript"
src="jquery.js">
</script>
<script type="text/javascript">
window.onload = function() {
var vector2d=function(x,y){
var vec={
vx:x,
vy:y,
scale:function(scale){
vec.vx*=scale;
vec.vy*=scale;
},
add:function(vec2){
vec.vx*=vec2.vx;
vec.vy*=vec2.vy;
},
negate:function(){
vec.vx=-vec.vx;
vec.vy=-vec.vy;
},
normalize:function(){
len=vec.vx*vec.vx+vec.vy*vec.vy;
if(len){
vec.vx/=len;
vec.vy/=len;
}
return len;
},
length:function(){
return Math.sqrt(vec.vx*vec.vx+vec.vy*vec.vy);
},
lengthsq:function(){
return vec.vx*vec.vx+vec.vy*vec.vy;
},
rotate: function (angle) {
var vx = vec.vx,
vy = vec.vy,
cosVal = Math.cos(angle),
sinVal = Math.sin(angle);
vec.vx = vx * cosVal - vy * sinVal;
vec.vy = vx * sinVal + vy * cosVal;
},
// toString() is a utility function for displaying the vector as text,
// a useful debugging aid.
toString: function () {
return '(' + vec.vx.toFixed(3) + ',' + vec.vy.toFixed(3) + ')';
}
};
return vec;
};
var gameObjects=[],
canvas=document.getElementById('canvas'),
ctx=canvas.getContext('2d');
var cannonBall=function(x,y,vector){
var gravity=0;
var that={
x:x,
y:y,
removeMe:false,
move:function(){
vector.vy+=gravity;
gravity+=.1;
that.x =that.x+vector.vx;
that.y =that.y+vector.vy;
if(that.y>canvas.height-150)
that.removeMe=true;
},
draw: function () {
ctx.beginPath();
ctx.arc(that.x, that.y, 5, 0, Math.PI * 2, true);
ctx.fill();
ctx.closePath();
}
};
return that;
};
var cannon=function(x,y)
{
var mx=0,
my=0,
angle=0,
that={
x:x,
y:y,
angle:0,
removeMe:false,
move:function(){
//alert((my-that.y)/(mx-that.x));
angle=Math.atan2((my-that.y),(mx-that.x));
},
draw:function()
{
ctx.save();
ctx.lineWidth=2;
ctx.translate(that.x,that.y);
ctx.rotate(angle);
ctx.strokeRect(0,-5,50,10);
ctx.moveTo(0,0);
ctx.beginPath();
ctx.arc(0,0,15,0,Math.PI*2,true);
ctx.fill();
ctx.closePath();
ctx.restore();
}
};
// When mouse is clicked, fire a cannonball.
canvas.onmousedown = function (event) {
// Create a vector from cannon postion in direction of mouse.
//alert(mx-that.x, my-that.y);
var vec = vector2d(mx-that.x, -my-that.y);
vec.normalize(); // Make it unit length.
vec.scale(25); // Scale it up to 25 units per frame.
// Create a new cannonball, and add it to the gameObjects list.
gameObjects.push(cannonBall(that.x, that.y, vec));
};
// Keep a note of the mouse position over the canvas.
canvas.onmousemove = function (event) {
var bb = canvas.getBoundingClientRect();
mx = (event.clientX - bb.left);
my = (event.clientY - bb.top);
};
return that;
};
var drawSkyAndGrass = function (){
ctx.save();
// Set transparency.
ctx.globalAlpha = 0.4;
// Create a CanvasGradient object in linGrad.
// The gradient line is defined from the top to the bottom of the canvas.
var linGrad = ctx.createLinearGradient(0, 0, 0, canvas.height);
// Start off with sky blue at the top.
linGrad.addColorStop(0, '#00BFFF');
// Fade to white in the middle.
linGrad.addColorStop(0.5, 'white');
// Green for the top of the grass.
linGrad.addColorStop(0.5, '#55dd00');
// Fade to white at the bottom.
linGrad.addColorStop(1, 'white');
// Use the CanvasGradient object as the fill style.
ctx.fillStyle = linGrad;
// Finally, fill a rectangle the same size as the canvas.
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.restore();
};
// Add an initial cannon to the game objects list.
gameObjects.push(cannon(50,canvas.height-150));
var vec = vector2d(12, -15);
gameObjects.push(cannonBall(30, 300, vec));
// This is the main loop that moves and draws everything.
setInterval( function() {
drawSkyAndGrass();
//alert(gameObjects.length);
// Here, we loop through all the object in the gameObjects[]
// Array. As each object is found, it is drawn, moved, and then
// added to the gameObjectsFresh[] array,UNLESS it has its removeMe flag
// set. gameObjectsFresh[] is then copied into gameObjects[] ready for
// the next frame. gameObjects[] will now not contain any removed
// objects, and they will disappear, as nothing references them anymore.
gameObjectsFresh = [];
for(var i=0;i<gameObjects.length;i++) {
gameObjects[i].move();
gameObjects[i].draw();
if ( gameObjects[i].removeMe === false) {
gameObjectsFresh.push(gameObjects[i]);
}
};
gameObjects = gameObjectsFresh;
},30);
};
</script>
</head>
<body>
<canvas id = "canvas" width = "640" height = "480" style="border:1px solid">
No HTML5 Canvas detected!
</canvas>
</body>
</html>