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="640" height="480" ></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 * 2;
48 backBuffer.height = height * 2;
49 var ctx = backBuffer.getContext("2d");
51 var renderTimerId = undefined;
52 var mainTimerId = undefined;
55 function Ball(x, y, radius) {
65 Ball.prototype.init = function (x, y, radius,xSpeed,ySpeed) {
74 Ball.prototype.move = function () {
77 this.x += this.xSpeed;
79 if ((this.x + this.radius) > width) {
80 this.x = width - this.radius; // (this.x - backBuffer.width);
81 this.xSpeed = -(this.xSpeed - 1.0);
84 if (this.x < this.radius) {
85 this.x = this.radius; // -this.x;
86 this.xSpeed = -(this.xSpeed + 1.0);
89 this.y += this.ySpeed;
91 if ((this.y + this.radius) > height) {
92 this.y = height - this.radius; // -(this.y + this.radius - backBuffer.height);
93 this.ySpeed = -this.ySpeed + 1.0;
97 Ball.prototype.draw = function (ctx) {
98 var fillStyle = ctx.fillStyle;
99 ctx.fillStyle = "blue";
101 ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
104 ctx.fillStyle = fillStyle;
111 this.array = new Array(0);
117 // indexの位置のタスクを置き換える
118 setNextTask: function (index, func) {
119 this.array[index] = func;
122 pushTask : function (func)
124 for (var i = 0; i < this.length; ++i) {
125 if (this.array[i] == null) {
126 this.array[i] = func;
130 this.array.push(func);
133 getArray: function () {
138 this.array.length = 0;
142 var tasks = new Tasks();
145 $(window).ready(function () {
146 ctx.fillStyle = "blue";
147 ctx.fillRect(0, 0, backBuffer.width, backBuffer.height);
148 ctx.setTransform(1, 0, 0, 1, sx, sy);
149 ctx.fillStyle = "white";
150 ctx.font = "12px 'MS ゴシック'"; ;
151 ctx.fillText("ボールを表示します。", 0, 20, 240);
152 ctx_main.drawImage(backBuffer, sx, sy, width, height, 0, 0, screenWidth, screenHeight);
155 $("#start").click(function () {
156 // renderTimerId = setInterval(render, 100);
157 tasks.pushTask(init);
158 ball = new Ball(100, 100, 10);
159 mainTimerId = setTimeout(processMain, 50);
160 $("#stop")[0].disabled = false;
161 $("#start")[0].disabled = true;
165 $("#stop").click(function () {
166 // if (renderTimerId != undefined) {
167 // clearInterval(renderTimerId);
168 // renderTimerId = undefined;
170 if (mainTimerId != undefined) {
171 clearTimeout(mainTimerId);
172 mainTimerId = undefined;
177 $("#stop")[0].disabled = true;
178 $("#start")[0].disabled = false;
181 // 準備ができたので開始ボタンを押せるようにする
182 $("#start")[0].disabled = false;
192 // スクロール・拡大してメイン画面に転送
194 ctx_main.drawImage(backBuffer, r, sy, width, height, 0, 0, screenWidth, screenHeight);
196 ctx_main.drawImage(backBuffer, r, sy, backBuffer.width - r, height, 0, 0, (backBuffer.width - r) * 2, screenHeight);
197 ctx_main.drawImage(backBuffer, 0, sy, width - (backBuffer.width - r), height, (backBuffer.width - r) * 2, 0, (width - (backBuffer.width - r)) * 2, screenHeight);
204 var ellapsedTime = 0;
205 function processMain() {
206 var startTime = new Date().getTime();
209 $(tasks.getArray()).each(function (taskIndex) {
216 ctx.fillStyle = "black";
217 ctx.fillRect(0, 0, width, height);
218 var endTime = new Date().getTime();
219 ellapsedTime = endTime - startTime;
220 period = 32 - (ellapsedTime) % 32;
221 mainTimerId = setTimeout(processMain, period);
225 function init(taskIndex)
227 ctx.fillStyle = "black";
228 ctx.fillRect(0, 0, width, height);
230 tasks.setNextTask(taskIndex, addCount);
231 tasks.pushTask(ballAction);
235 function addCount() {
236 var txt = "処理時間:" + ellapsedTime + "ms";
237 ctx.textBaseline = "top";
238 var textWidth = ctx.measureText(txt);
239 ctx.fillStyle = "black";
240 ctx.fillRect(0, 0, textWidth.width, 12);
241 ctx.fillStyle = "white";
242 ctx.fillText(txt, 0, 0, textWidth.width);
246 function ballAction() {