javascript - How to fillStyle all circles in different color in canvas: red, yellow, blue and green? -
i looking way fillstyle of circles in 4 colors: #00ce66
, #e3bf37
, #cc5543
, #a1b7c2
. want circles show in way: 25% of circles red, 25% circles yellow… etc. on canvas while page loaded.
when added math.random()
, put inside colors, circles black. please :-)
(function() { var width, height, largeheader, canvas, ctx, circles, target, animateheader = true; // main initheader(); addlisteners(); function initheader() { width = window.innerwidth; height = window.innerheight; target = {x: 0, y: height}; largeheader = document.getelementbyid('large-header'); largeheader.style.height = height+'px'; canvas = document.getelementbyid('canvas'); canvas.width = width; canvas.height = height; ctx = canvas.getcontext('2d'); // create particles circles = []; for(var x = 0; x < width*0.2; x++) { var c = new circle(); circles.push(c); } animate(); } // event handling function addlisteners() { window.addeventlistener('scroll', scrollcheck); window.addeventlistener('resize', resize); } function scrollcheck() { if(document.body.scrolltop > height) animateheader = false; else animateheader = true; } function resize() { width = window.innerwidth; height = window.innerheight; largeheader.style.height = height+'px'; canvas.width = width; canvas.height = height; } function animate() { if(animateheader) { ctx.clearrect(0,0,width,height); for(var in circles) { circles[i].draw(); } } requestanimationframe(animate); } // canvas manipulation function circle() { var _this = this; // constructor (function() { _this.pos = {}; init(); console.log(_this); })(); function init() { _this.pos.x = math.random()*width; _this.pos.y =-50; _this.alpha = 0.1+math.random()*0.9; _this.scale = 0.1+math.random()*1; _this.velocity = math.random(); } this.draw = function() { if(_this.alpha <= 0) { init(); } _this.pos.y -= -_this.velocity; _this.alpha -= 0.0005; ctx.beginpath(); ctx.arc(_this.pos.x, _this.pos.y, _this.scale*10, 0, 2 * math.pi, false); ctx.fillstyle = 'rgba(16,239,173,'+ _this.alpha+')'; ctx.fill(); }; } })();
you can create array 4 colors , choose random 1 @ creation of each circle.
here code circle function:
function circle() { var _this = this; // constructor (function() { _this.pos = {}; init(); console.log(_this); })(); function init() { _this.pos.x = math.random()*width; _this.pos.y =-50; _this.alpha = 0.1+math.random()*0.9; _this.scale = 0.1+math.random()*1; _this.velocity = math.random(); var colors = [[0,206,102], [227, 191, 55], [204,85,67], [161,183, 194]]; var random_index = math.floor(0 + (math.random() * ((3 + 1) - 0))); _this.color = colors[random_index]; } this.draw = function() { if(_this.alpha <= 0) { init(); } _this.pos.y -= -_this.velocity; _this.alpha -= 0.0005; ctx.beginpath(); ctx.arc(_this.pos.x, _this.pos.y, _this.scale*10, 0, 2 * math.pi, false); var c = _this.color; ctx.fillstyle = 'rgba('+c[0]+','+c[1]+','+c[2]+','+ _this.alpha+')'; ctx.fill(); }; }
Comments
Post a Comment