如何在javascript

时间:2016-06-19 16:45:43

标签: javascript canvas random

使用画布我需要创建4个其他随机形状,以随机大小和随机位置显示。到目前为止,我有随机圈子。但我无法弄清楚如何获得其他形状。

我身上的

<canvas width='600' height='200' id='artCanvas'></canvas>
<form>
  <input type='button' id='artButton' value='New Masterpiece'>
</form>

使用此代码我需要创建多个其他形状,就像随机创建圆圈一样。

<script>
  function makeTitle() {
  //generate the title for your masterpiece
  var lines =[['Meditative','Objective','Reflective'['Ellipses','Tranformation', 'State', 'Emotion', 'Composition']['I', 'II', 'III', 'IV','V']];
  var title = '';
  for (var i=0; i<lines.length; i++) {
    var random = Math.floor(Math.random() * lines[i].length);
    title += lines[i][random] + ' ';
  };
return(title);
}
function artHandler() {
  var title = makeTitle();
  alert(title);
  var canvas = document.getElementById('artCanvas');
  var context = canvas.getContext('2d');
  fillBackgroundColor(canvas, context);
  var colors = ['white', 'yellow', 'blue', 'red'];
  for (var i = 0; i < 20; i++) {
    var color = colors[Math.floor(Math.random() * colors.length)];
    drawCircle(canvas, context, color); 
  }
  drawText(canvas, context, title);
  }
function fillBackgroundColor(canvas, context) {
  var colors = ['white', 'yellow', 'blue', 'red'];
  var bgColor = colors[Math.floor(Math.random() * colors.length)];
  context.fillStyle = bgColor;
  context.fillRect(0, 0, canvas.width, canvas.height);
}
function degreesToRadians(degrees) {
  return (degrees * Math.PI)/180;
}
// Draws a circle at a random location
function drawCircle(canvas, context, color) {
  var radius = Math.floor(Math.random() * 40);
  var x = Math.floor(Math.random() * canvas.width);
  var y = Math.floor(Math.random() * canvas.height);
  context.beginPath();
  context.arc(x, y, radius, 0, degreesToRadians(360), true);
  context.fillStyle = color;
  context.fill();
}
function drawText(canvas, context, title) {
  context.fillStyle = 'black';
  context.font = 'bold 1em sans-serif';
  context.textAlign = 'right';
  context.fillText(title, canvas.width-20, canvas.height-40);
}
window.onload = function() {
var button = document.getElementById('artButton');
button.onclick = artHandler;
}
</script>

1 个答案:

答案 0 :(得分:3)

要生成随机颜色,请使用此功能:

function getBackgroundColor() {
    return "rgb("+[
        Math.round(Math.random()*0xFF),
        Math.round(Math.random()*0xFF),
        Math.round(Math.random()*0xFF)
    ].join()+")";
}

生成随机矩形:

function drawRect(canvas, context) {
    context.fillStyle = getBackgroundColor();
    context.fillRect(Math.random()*canvas.width, Math.random()*canvas.height,Math.random()*100,Math.random()*100);
}

三角:

function drawTriangle(canvas, context) {
    context.fillStyle = getBackgroundColor();

    context.beginPath();
    context.moveTo(Math.random()*canvas.width, Math.random()*canvas.height);
    context.lineTo(Math.random()*canvas.width, Math.random()*canvas.height);
    context.lineTo(Math.random()*canvas.width, Math.random()*canvas.height);
    context.fill();

}

这是你的代码,随机生成器:

https://jsfiddle.net/m06e8Lh4/