-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubbles.html
205 lines (182 loc) · 6.13 KB
/
bubbles.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>HTML5 Diagnostic</title>
<link href="css/bubbles.css" media="all" rel="stylesheet" type="text/css"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>
<style>
body {
background: black;
}
#report {
color: white;
font: bold 2em arial, sans-seriff;
}
</style>
<script langauage="javascript" type="text/javascript">
//Globals
var canvas = null;
var WIDTH = null;
var HEIGHT = null;
var FS = "<br>";
var actionObjs = [];
var MAX_TIME_TO_LIVE = 80; //2 seconds at 40fps
function randomColor() {
var colorArray = ['FFFF00','00CC00','0066FF','CC0000','6600FF'];
var color = colorArray[Math.floor(Math.random() * colorArray.length)];
return color;
}
function Action(I) {
I = I || {};
//I.color = 'FFFF00';
I.color = randomColor();
I.radius = 10;
I.timeAlive = 0;
I.active = true;
I.draw = function() {
canvas.fillStyle = this.color;
canvas.strokeStyle = this.color;
var lowerLimit = (WIDTH > HEIGHT ? HEIGHT : WIDTH);
var lw = Math.ceil(lowerLimit / 45);
canvas.lineWidth = lw;
canvas.beginPath();
canvas.arc(I.x,I.y,I.radius,0,Math.PI*2,true);
canvas.closePath();
canvas.stroke();
};
I.update = function() {
I.timeAlive += 1;
I.radius += 1;
if(I.timeAlive > MAX_TIME_TO_LIVE){
I.active = false;
}
};
return I;
}
function logMsg(msg){
var reportDiv = document.getElementById('report');
var reportText = reportDiv.innerHTML;
var msgArray = reportText.split(FS);
var timeStamp = new Date();
msg = timeStamp.getTime() + ' ' + msg;
var MAX = msgArray.length > 5 ? 5 : msgArray.length;
reportText = msg + FS;
for(var i=0;i<MAX;i++) {
if(i>0){
reportText += msgArray[i-1] + FS;
}
}
reportDiv.innerHTML = reportText;
}
function resize(evt){
var newWidth = window.innerWidth;
var newHeight = window.innerHeight;
var canvasElement = document.getElementById('canvasId');
canvasElement.width = newWidth - 20;
canvasElement.height = newHeight - 20;
//Update the constants so other methods don't have to refetch the dom element
WIDTH = canvasElement.width;
HEIGHT = canvasElement.height;
}
function touchMoveHandler(evt) {
/*
var msg = 'Touch move: ';
for(var i = 0; i < evt.touches.length; i++) {
var touch = evt.touches[i];
//msg += touch.identifier + '(x,y) = ('+touch.clientX+','+touch.clientY+')';
if(i<evt.touches.length) {
msg += ', ';
}
}
logMsg(msg);
*/
}
function touchStartHandler(evt) {
//var msg = 'Touch start: ';
for(var i = 0; i < evt.changedTouches.length; i++) {
var touch = evt.changedTouches[i];
//msg += touch.identifier + '(x,y) = ('+touch.clientX+','+touch.clientY+')';
actionObjs.push(Action({
x: touch.clientX,
y: touch.clientY
}));
}
//logMsg(msg);
}
function mousedownHandler(evt) {
actionObjs.push(Action({
x: evt.offsetX,
y: evt.offsetY
}));
}
function clickHandler(evt){
//This gets fired after touchstart and gets processed as a click button 0
logMsg('Click: button = '+evt.button);
}
function update() {
actionObjs.forEach(function(actionObj) {
actionObj.update();
});
//Remove any inactive fireworks
actionObjs = actionObjs.filter(function(actionObj) {
return actionObj.active;
});
}
function draw() {
canvas.clearRect(0,0,WIDTH,HEIGHT);
actionObjs.forEach(function(actionObj) {
actionObj.draw();
});
}
//Create a common way to request the animation frame
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback ) {
window.setTimeout(callback, 1000/60);
};
})();
function startEventLoop() {
/*
setInterval(function() {
update();
draw();
}, 40);
*/
//Typical this is set to 60fps
requestAnimFrame(startEventLoop);
update();
draw();
}
function init(){
var canvasElement = document.getElementById('canvasId');
if(!canvasElement.getContext){
logMsg("Canvas: No support");
return;
} else {
canvas = canvasElement.getContext('2d');
}
canvasElement.addEventListener('touchstart', touchStartHandler, false);
//canvasElement.addEventListener('touchmove', touchMoveHandler, false);
canvasElement.addEventListener('mousedown', mousedownHandler, false);
resize();
window.addEventListener('resize', resize, false);
window.addEventListener('orientationchange', resize, false);
//canvasElement.addEventListener('click', clickHandler, false);
//Prevent user dragging here to keep the canvas stationary
document.body.addEventListener('touchmove', function(event) {
event.preventDefault();
}, false);
startEventLoop();
}
</script>
</head>
<body onLoad="init()">
<canvas id='canvasId' width='600px' height='400px' style="border: 1px solid red"></canvas>
<div id="report">
</div>
</body>
</html>