I'm making a field of asteroids and try to change colors in two ways. Btw I'm a beginner with this. :)
First is to have all the androids colored randomly and second is two have them colored in lets say 3 different predefined colors.
Thanks guys...
Code:
$(document).ready(function() {
var canvas = $("#myCanvas");
var context = canvas.get(0).getContext("2d");
var canvasWidth = canvas.width();
var canvasHeight = canvas.height();
$(window).resize(resizeCanvas);
var Asteroid = function(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
this.vX = vX;
this.vY = vY;
};
var asteroids = new Array();
for (var i = 0; i < 1000; i++) {
var x = 20+(Math.random()*(canvasWidth-40));
var y = 20+(Math.random()*(canvasHeight-40));
var radius = 2+Math.random()*30;
var vX = Math.random()*4-2;
var vY = Math.random()*4-2;
var color = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
asteroids.push(new Asteroid(x, y, radius, vX, vY, color));
};
function animate() {
context.clearRect(0, 0, canvasWidth, canvasHeight);
//context.fillStyle = "rgb(255, 255, 255)";
var asteroidsLength = asteroids.length;
for (var i = 0; i < asteroidsLength; i++) {
var tmpAsteroid = asteroids[i];
tmpAsteroid.x += tmpAsteroid.vX;
tmpAsteroid.y += tmpAsteroid.vY;
function oneOfManyColors(){
var colors=["#ff6622", "#00ff00", "#0088ff", "#ff0000", "#ff8800"]; //and so on
return colors[ Math.floor(colors.length * Math.random()) ];
}