4 <title>HTML5 Test</title>
5 <link rel="stylesheet" href="http://sfpg.seesaa.net/styles-index.css" type="text/css" />
6 <script type="text/javascript" src="http://sfpg.up.seesaa.net/scripts/jquery-1.4.4.min.js"></script>
7 <style type="text/css">
11 <script type="text/javascript">
13 var _gaq = _gaq || [];
14 _gaq.push(['_setAccount', 'UA-15457703-1']);
15 _gaq.push(['_setDomainName', '.seesaa.net']);
16 _gaq.push(['_trackPageview']);
19 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
20 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
21 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
28 <canvas id="disp01" width="320" height="240" ></canvas>
31 <button type="button" id="start" disabled="true">開始</button>
32 <button type="button" id="stop" disabled="true" >停止</button>
34 <script type="text/javascript">
42 var ctx_main = $("#disp01")[0].getContext("2d");
43 var screenWidth = $("#disp01")[0].width;
44 var screenHeight = $("#disp01")[0].height;
46 var backBuffer = $("<canvas>")[0];
47 backBuffer.width = width + 160;
48 backBuffer.height = height + 120;
49 var ctx = backBuffer.getContext("2d");
51 var renderTimerId = undefined;
52 var mainTimerId = undefined;
56 function Ball(x, y, radius, xSpeed, ySpeed,index,color) {
68 // Ball.prototype.init = function (x, y, radius,xSpeed,ySpeed,index) {
71 // this.xSpeed = xSpeed;
72 // this.ySpeed = ySpeed;
73 // this.radius = radius;
74 // this.index = index;
75 // this.enable = true;
78 Ball.prototype.move = function () {
81 this.x += this.xSpeed;
83 if ((this.x + this.radius) > width) {
84 this.x = width - this.radius;
85 this.xSpeed = -(this.xSpeed - 1.0);
88 if (this.x < this.radius) {
89 this.x = this.radius; // -this.x;
90 this.xSpeed = -(this.xSpeed + 1.0);
93 this.y += this.ySpeed;
95 if ((this.y + this.radius) > height) {
96 this.y = height - this.radius;
97 this.ySpeed = -this.ySpeed + 1.0;
100 if (this.y < this.radius) {
101 this.y = this.radius;
102 this.ySpeed = -this.ySpeed;
106 Ball.prototype.draw = function (ctx) {
107 var fillStyle = ctx.fillStyle;
108 ctx.globalAlpha = 0.5;
109 ctx.fillStyle = this.color;
111 ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
114 ctx.fillStyle = fillStyle;
115 ctx.globalAlpha = 1.0;
119 var balls = new Array(0);
123 this.array = new Array(0);
129 // indexの位置のタスクを置き換える
130 setNextTask: function (index, func) {
131 this.array[index] = func;
134 pushTask : function (func)
136 for (var i = 0; i < this.length; ++i) {
137 if (this.array[i] == null) {
138 this.array[i] = func;
142 this.array.push(func);
145 getArray: function () {
150 this.array.length = 0;
154 var tasks = new Tasks();
157 $(window).ready(function () {
158 ctx.fillStyle = "blue";
159 ctx.fillRect(0, 0, backBuffer.width, backBuffer.height);
160 ctx.setTransform(1, 0, 0, 1, sx, sy);
161 ctx.fillStyle = "white";
162 ctx.font = "12px 'MS ゴシック'"; ;
163 ctx.fillText("ボールを表示します。", 0, 20, 240);
165 ctx_main.drawImage(backBuffer, sx, sy, width, height, 0, 0, screenWidth, screenHeight);
166 var color = ["#ff0000","#ff3800","#ff6000","#ff8800"];
169 $("#start").click(function () {
170 // renderTimerId = setInterval(render, 100);
171 tasks.pushTask(init);
173 for (var i = 0; i < 200; ++i) {
174 var rad = 10 + Math.random() * 10;
177 Math.random() * (width - rad * 2) + rad,
178 Math.random() * (height - rad * 2) + rad,
180 (Math.random() * 5 + 5) * (Math.random() >= 0.5 ? -1:1),
181 Math.random() * 4 + 9,
183 color[(Math.random() * 4)|0]
187 mainTimerId = setTimeout(processMain, 50);
188 $("#stop")[0].disabled = false;
189 $("#start")[0].disabled = true;
193 $("#stop").click(function () {
194 // if (renderTimerId != undefined) {
195 // clearInterval(renderTimerId);
196 // renderTimerId = undefined;
198 if (mainTimerId != undefined) {
199 clearTimeout(mainTimerId);
200 mainTimerId = undefined;
205 $("#stop")[0].disabled = true;
206 $("#start")[0].disabled = false;
209 // 準備ができたので開始ボタンを押せるようにする
210 $("#start")[0].disabled = false;
218 ctx_main.drawImage(backBuffer, sx, sy, width, height, 0, 0, screenWidth, screenHeight);
224 var ellapsedTime = 0;
225 function processMain() {
226 var startTime = new Date().getTime();
229 $(tasks.getArray()).each(function (taskIndex) {
236 ctx.fillStyle = "black";
237 ctx.fillRect(0, 0, width, height);
238 var endTime = new Date().getTime();
239 ellapsedTime = endTime - startTime;
240 period = 32 - (ellapsedTime) % 32;
241 mainTimerId = setTimeout(processMain, period);
245 function init(taskIndex)
247 ctx.fillStyle = "black";
248 ctx.fillRect(0, 0, width, height);
250 tasks.setNextTask(taskIndex, addCount);
251 tasks.pushTask(ballAction);
255 function addCount() {
256 var txt = "処理時間:" + ellapsedTime + "ms";
257 ctx.textBaseline = "top";
258 var textWidth = ctx.measureText(txt);
259 ctx.fillStyle = "black";
260 ctx.fillRect(0, 0, textWidth.width, 12);
261 ctx.fillStyle = "white";
262 ctx.fillText(txt, 0, 0, textWidth.width);
266 function ballAction() {
267 $(balls).each(function () {