アニメーション表示するテキストが必要です。おそらく、フェードインして場所を移動することがあります。また、シェイプの最大幅と高さを設定する必要があります。その一部は大きすぎるためです。また、形状がランダムな色ではなく、方法がある場合は、選択できる色の配列を設定できます。テキストをアニメーション化し、キャンバスのJavaScriptを使用して最大幅と高さを設定する方法
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();
var canvas = document.getElementById('artCanvas');
var context = canvas.getContext('2d');
fillBackgroundColor(canvas, context);
drawText(canvas, context, title);
var num = 10;
while(num --> 0) {
\t drawCircle(canvas, context);
\t drawRect(canvas, context);
drawTriangle(canvas, context);
\t \t \t drawShape(canvas, context);
\t \t \t drawCir(canvas, context);
}
}
\t function getBackgroundColor() {
\t return "rgb("+[
\t Math.round(Math.random()*0xFF),
\t Math.round(Math.random()*0xFF),
\t Math.round(Math.random()*0xFF)
].join(",")+")";
}
function fillBackgroundColor(canvas, context) {
var bgColor = getBackgroundColor();
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) {
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 = getBackgroundColor();
context.fill();
}
function drawText(canvas, context, title) {
context.fillStyle = getBackgroundColor();
context.font = 'bold 1em sans-serif';
context.textAlign = 'right';
context.fillText(title, canvas.width - 20, canvas.height - 40);
}
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();
}
\t function drawShape(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);
\t \t context.lineTo(Math.random()*canvas.width, Math.random()*canvas.height);
\t \t context.lineTo(Math.random()*canvas.width, Math.random()*canvas.height);
context.fill();
}
\t
function drawCir(canvas, context) {
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(180), true);
context.fillStyle = getBackgroundColor();
context.fill();
}
// window.onload = function() {
var button = document.getElementById('artButton');
button.onclick = artHandler;
//}
<form>
<input type='button' id='artButton' value='New Masterpiece'>
</form>
<canvas width='1200' height='600' id='artCanvas'></canvas>
'のparseInt(Math.random()*(colorArray.length-1))'との配列から色を選択 - > ' parseInt(Math.random()* colorArray.length) 'または最後のインデックスはMath.randomが[0,1>]の範囲にあるため使用されません。 – K3N