123456789101112131415161718192021222324252627282930313233343536373839 |
- function Circle() {
- var x = 10, y = 10, radius = 5;
- var velocity = {x: 1, y: 1};
- this.init = function(canvas) {
- x = Math.floor(canvas.width * Math.random());
- y = Math.floor(canvas.height * Math.random());
- velocity.x = Math.floor(10 * Math.random() - 5);
- velocity.y = Math.floor(10 * Math.random() - 5);
- };
- this.update = function(canvas) {
- x += velocity.x;
- y += velocity.y;
- if(x + radius > canvas.width) {
- x = canvas.width - radius;
- velocity.x *= -1.1;
- }
- if(x < radius) {
- x = radius;
- velocity.x *= -1.1;
- }
- if(y + radius > canvas.height) {
- y = canvas.height - radius;
- velocity.y *= -1.1;
- }
- if(y < radius) {
- y = radius;
- velocity.y *= -1.1;
- }
- velocity.x = Math.max(-5, Math.min(5, velocity.x));
- velocity.y = Math.max(-5, Math.min(5, velocity.y));
- },
- this.draw = function(context) {
- context.strokeStyle = "#000000";
- context.beginPath();
- context.arc(x, y, radius, 0, 2 * Math.PI);
- context.stroke();
- }
- }
|