-
Notifications
You must be signed in to change notification settings - Fork 0
/
car.html
162 lines (136 loc) · 6.55 KB
/
car.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame
Remove this if you use the .htaccess -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>index</title>
<meta name="description" content="" />
<meta name="author" content="b.rioscampo" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
<link rel="shortcut icon" href="/favicon.ico" />
<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
<style type="text/css">
canvas {
border: 1px solid red;
}
</style>
</head>
<body>
<div id="container"></div>
<script type="text/javascript" src="src/kinetic.latest.js"></script>
<script type="text/javascript">
window.onload = function() {
function manyBlob(stage, layer) {
var sW = stage.getWidth();
var sH = stage.getHeight();
var s = new Kinetic.Layer({
x: 150,
y: 150
});
var cWidth = 100;
var cHeight = 50;
var car = new Kinetic.Rect({
width: cWidth,
height: cHeight,
fill: '#f50',
offset: [cWidth / 2, cHeight / 2]
});
var wheels = [];
for(var i = 0; i < 4; i++) {
var xpos = (i % 2 << 0) * 60 + 10;
var ypos = (i / 2 << 0) * 55 - 5;
wheels[i] = new Kinetic.Rect({
width: 20,
height: 5,
fill: '#000',
x: xpos - 40,
y: ypos - 22.5,
offset: [10, 2.5]
});
s.add(wheels[i]);
}
s.add(car);
var speedDelay = 1500;
var speedLimit = 10;
var rotSpeed = 30;
var speed = 0;
var direction = 0;
var rotation = 0;
var forward = 0;
var wheelAngle = Math.PI / 16;
var start = 0;
var transition = null;
window.onkeydown = function(e) {
if({37:-1,39:1}[e.which] && {37:-1,39:1}[e.which] != direction) {
direction = {37:-1,39:1}[e.which];
}
if({40:-1,38:1}[e.which] && {40:-1,38:1}[e.which] != forward) {
transition && transition.stop();
transition = null;
if(!start) start = Date.now();
forward = {40:-0.5,38:1}[e.which];
}
}
window.onkeyup = function(e) {
if({37:-1,39:1}[e.which]) {
direction = 0;
wheels[1].attrs.rotation = wheels[3].attrs.rotation = 0;
}
if({40:-1,38:1}[e.which]) {
var rad = rotation / (Math.PI * 180);
transition = s.transitionTo({
x: s.attrs.x + Math.cos(rad) * (speed * forward * speed),
y: s.attrs.y + Math.sin(rad) * (speed * forward * speed),
duration: 0.5,
easing: 'ease-out'
});
speed = 0;
forward = 0;
start = 0;
}
}
var easeInQuart = function (x, t, b, c, d) {
return c*(t/=d)*t*t*t + b;
};
var easeOutQuart = function (x, t, b, c, d) {
return -c * ((t=t/d-1)*t*t*t - 1) + b;
};
var anim = new Kinetic.Animation({
func: function(frame) {
rotation += direction * (speed * 2) * forward;
var elapsed = Date.now() - start;
if(elapsed < speedDelay) {
var value = easeOutQuart(elapsed, elapsed, 0, 1, speedDelay);
speed = value * speedLimit;
}
var rad = rotation / (Math.PI * 180);
var posX = (s.attrs.x + Math.cos(rad) * (speed * forward)) % (sW + cWidth);
var posY = (s.attrs.y + Math.sin(rad) * (speed * forward)) % (sH + cHeight);
var angle = (speed / (Math.PI * 180)) * speedLimit;
wheels[1].attrs.rotation = wheels[3].attrs.rotation = angle * direction;
if(posX < -cWidth) posX = sW;
if(posY < -cHeight) posY = sH;
s.setX(posX);
s.setY(posY);
s.setRotation(rad);
},
node: layer
});
anim.start();
layer.add(s);
stage.add(layer);
}
var stage = new Kinetic.Stage({
width: 800,
height: 480,
container: 'container'
});
var layer = new Kinetic.Layer();
manyBlob(stage, layer);
};
</script>
</body>
</html>