123456789101112131415161718192021222324252627282930313233343536373839404142 |
- function Triangle() {
- var x = 10, y = 10, base = 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 + base > canvas.width) {
- x = canvas.width - base;
- velocity.x *= -1.1;
- }
- if(x - base < 0) {
- x = base;
- velocity.x *= -1.1;
- }
- if(y + base * 2 > canvas.height) {
- y = canvas.height - (base * 2);
- velocity.y *= -1.1;
- }
- if(y < 0) {
- y = 0;
- 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.moveTo(x, y);
- context.lineTo(x - base, y + base * 2);
- context.lineTo(x + base, y + base * 2);
- context.lineTo(x, y);
- context.stroke();
- }
- }
|