function StarShape() { 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 < 0) { x = radius; velocity.x *= -1.1; } if(y + radius * 2 > canvas.height) { y = canvas.height - (radius * 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) { var distance = radius / 5; context.strokeStyle = "#000000"; context.beginPath(); context.moveTo(x, y - radius); context.lineTo(x + 3 * distance, y + radius); context.lineTo(x - radius, y - 2 * distance); context.lineTo(x + radius, y - 2 * distance); context.lineTo(x - 3 * distance, y + radius); context.lineTo(x, y - radius); context.stroke(); }; }