moved qdb here because matt is lazy
[public/www-new.git] / pub / qdb / res / themes / default / js / excanvas / examples / example1.html
1 <!--\r
2         Copyright 2006 Google Inc.\r
3 \r
4         Licensed under the Apache License, Version 2.0 (the "License");\r
5         you may not use this file except in compliance with the License.\r
6         You may obtain a copy of the License at\r
7 \r
8           http://www.apache.org/licenses/LICENSE-2.0\r
9 \r
10         Unless required by applicable law or agreed to in writing, software\r
11         distributed under the License is distributed on an "AS IS" BASIS,\r
12         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13         See the License for the specific language governing permissions and\r
14         limitations under the License.\r
15 -->\r
16 <html>\r
17 <head>\r
18         <title>ExplorerCanvas Example 1</title>\r
19         <!--[if IE]><script type="text/javascript" src="../excanvas.js"></script><![endif]-->\r
20         <script type="text/javascript">\r
21                 var canvas, ctx;\r
22                 var particles = [];\r
23                 var NUM_PARTICLES = 20;\r
24 \r
25                 function Particle() {\r
26                         this.x = Math.random() * canvas.width;\r
27                         this.y = Math.random() * canvas.height;\r
28 \r
29                         this.xvel = Math.random() * 5 - 2.5;\r
30                         this.yvel = Math.random() * 5 - 2.5;\r
31                 }\r
32 \r
33                 Particle.prototype.update = function() {\r
34                         this.x += this.xvel;\r
35                         this.y += this.yvel;\r
36 \r
37                         this.yvel += 0.1;\r
38 \r
39                         if (this.x > canvas.width || this.x < 0) {\r
40                                 this.xvel = -this.xvel;\r
41                         }\r
42 \r
43                         if (this.y > canvas.height || this.y < 0) {\r
44                                 this.yvel = -this.yvel;\r
45                         }\r
46                 }\r
47 \r
48                 function loop() {\r
49                         ctx.clearRect(0, 0, canvas.width, canvas.height);\r
50 \r
51                         for(var i = 0; i < NUM_PARTICLES; i++) {\r
52                                 particles[i].update();\r
53 \r
54                                 ctx.beginPath();\r
55                                 ctx.moveTo(particles[i].x, particles[i].y);\r
56                                 ctx.lineTo(particles[i].x - particles[i].xvel,\r
57                                                                          particles[i].y - particles[i].yvel);\r
58                                 ctx.stroke();\r
59                                 ctx.closePath();\r
60                         }\r
61 \r
62                         setTimeout(loop, 10);\r
63                 }\r
64 \r
65                 function load() {\r
66                         canvas = document.getElementById("cv");\r
67                         ctx = canvas.getContext("2d");\r
68 \r
69                         for(var i = 0; i < NUM_PARTICLES; i++) {\r
70                                 particles[i] = new Particle();\r
71                         }\r
72 \r
73                         ctx.lineWidth = "2";\r
74                         ctx.strokeStyle = "rgb(255, 255, 255)";\r
75                         loop();\r
76                 }\r
77         </script>\r
78         <style>\r
79                 body {\r
80                         background-color:black;\r
81                         margin:50px;\r
82                         text-align:center;\r
83                 }\r
84 \r
85                 canvas {\r
86                         border:1px solid #444;\r
87                 }\r
88         </style>\r
89 </head>\r
90 <body onload="load();">\r
91         <canvas id="cv" width="400" height="300"></canvas>\r
92 </body>\r
93 </html>