Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a random color in my CreateJS shape?

I want to have a random color where "Crimson" is defined

    var stage = new createjs.Stage("demoCanvas");

    var circle = new createjs.Shape();
    circle.graphics.beginFill("Crimson").drawCircle(0, 0, 50);
    circle.x = 100;
    circle.y = 100;
    stage.addChild(circle);
    stage.update();
like image 643
Jim Peeters Avatar asked Jan 31 '15 18:01

Jim Peeters


People also ask

How do I generate a random color in CSS?

var randomColor = Math. floor(Math. random()*16777215).

How do I generate a random color in TypeScript?

Introduction. Here I show a random color generator for a div using TypeScript and change the color of the div at regular intervals of time using TypeScript. We use the setInterval method in this example. The setInterval method creates a timer that calls the specified function at the specified interval in milliseconds.


1 Answers

beginFill accepts any color, also hex, so you just have to generate a random hex color

var stage  = new createjs.Stage("demoCanvas");
var circle = new createjs.Shape();
var color  = '#'+(Math.random()*0xFFFFFF<<0).toString(16);

circle.graphics.beginFill(color).drawCircle(0, 0, 50);
circle.x = 100;
circle.y = 100;
stage.addChild(circle);
stage.update();
like image 136
adeneo Avatar answered Oct 25 '22 02:10

adeneo