在进行此实验时,我意识到randomColor函数只会为填充的正方形生成黑色。 谁能向我解释我的错误在哪里以及可能的解决方法?
<!DOCTYPE html>
<html>
<head>
<title>Recipe: Drawing a square</title>
<script src="easel.js"></script>
<script type="text/javascript">
var canvas;
var stage;
var squareSide = 70;
var squareOutline = 5;
var max_rgb_color_value = 255;
var gray = createjs.Graphics.getRGB(20, 20, 20);
function init() {
var rows = 5;
var columns = 6;
var squarePadding = 10;
canvas = document.getElementById('myCanvas');
stage = new createjs.Stage(canvas);
for(var i=0;i<rows*columns;i++){
var square = drawSquare();
square.x = (squareSide+squarePadding) * (i%columns);
square.y = (squareSide+squarePadding) * Math.floor(i/columns);
stage.addChild(square);
stage.update();
}
}
function drawSquare() {
var shape = new createjs.Shape();
var graphics = shape.graphics;
var color = randomColor();
graphics.setStrokeStyle(squareOutline);
graphics.beginStroke(gray);
graphics.beginFill(color);
graphics.rect(squareOutline, squareOutline, squareSide, squareSide);
return shape;
}
function randomColor(){
var color = Math.floor(Math.random()*createjs.max_rgb_color_number);
var color2 = Math.floor(Math.random()*createjs.max_rgb_color_number);
var color3 = Math.floor(Math.random()*createjs.max_rgb_color_number);
return createjs.Graphics.getRGB(color, color2, color3);
}
</script>
</head>
<body onload="init()">
<canvas id="myCanvas" width="960" height="600"></canvas>
</body>
</html>
答案 0 :(得分:0)
我拥有我们发布的每个CreateJS构建,据我所知,从来没有max_rgb_color_number
。我不知道您在示例中的位置。
您可以将该变量替换为255(或0xFF)。 RGB颜色的每个通道为0-255。
获得随机RGB颜色的另一种简便方法是
var color = "#" + (Math.random() * 0xFFFFFF | 0).toString(16);
干杯。