Exactly as the title says, I'm trying to add a random color parameter (as a 4th parameter) to a pre-existing function. I have tried many things but nothing has been working.
That is what I'm working with. I know, it's not efficient but I'm a beginner. It's a smiley face. I need to add a random color parameter so that only the first 'fillStyle' gets changed but the rest of the colors remain the same. How can I achieve this?
var canvas = document.getElementById("cnv"),
context = canvas.getContext('2d'),
width = 500,
height = 500;
function pintaSmiley(cX, cY, s) {
"use strict";
context.beginPath();
context.fillStyle = "yellow";
context.arc(cX, cY, 100 * s, 0, Math.PI * 2, true);
context.closePath();
context.fill();
context.lineWidth = 2;
context.stroke();
context.fillStyle = "black";
context.moveTo(55, 50);
context.beginPath();
context.fillStyle = "white";
context.arc(cX - 35 * s, cY - 38 * s, 16 * s, 0, Math.PI * 2, true);
context.closePath();
context.fill();
context.lineWidth = 2;
context.stroke();
context.moveTo(103, 49);
context.beginPath();
context.fillStyle = "white";
context.arc(cX + 35 * s, cY - 38 * s, 16 * s, 0, Math.PI * 2, true);
context.closePath();
context.fill();
context.lineWidth = 2;
context.stroke();
context.moveTo(55, 50);
context.beginPath();
context.fillStyle = "black";
context.arc(cX - 32 * s, cY - 43 * s, 9 * s, 0, Math.PI * 2, true);
context.closePath();
context.fill();
context.moveTo(103, 49);
context.beginPath();
context.fillStyle = "black";
context.arc(cX + 38 * s, cY - 43 * s, 9 * s, 0, Math.PI * 2, true);
context.closePath();
context.fill();
context.moveTo(105, 75);
context.beginPath();
context.fillStyle = 'rgba(0, 0, 0, 1)';
context.arc(cX, cY + 10 * s, 60 * s, 0, Math.PI, false);
context.closePath();
context.fill();
}
for (i = 0; i < 94; i = i + 1) {
centerX = Math.floor((Math.random() * width) + 1);
centerY = Math.floor((Math.random() * height) + 1);
size = 15;
size = size / 100;
pintaSmiley(centerX, centerY, size);
}
pintaSmiley(centerX, centerY, size * 14);
centerX = Math.floor((Math.random() * width) + 1);
centerY = Math.floor((Math.random() * height) + 1);
pintaSmiley(centerX, centerY, size + 0.1);
centerX = Math.floor((Math.random() * width) + 1);
centerY = Math.floor((Math.random() * height) + 1);
pintaSmiley(centerX, centerY, size + 0.2);
centerX = Math.floor((Math.random() * width) + 1);
centerY = Math.floor((Math.random() * height) + 1);
pintaSmiley(centerX, centerY, size + 0.3);
centerX = Math.floor((Math.random() * width) + 1);
centerY = Math.floor((Math.random() * height) + 1);
pintaSmiley(centerX, centerY, size + 0.4);
centerX = Math.floor((Math.random() * width) + 1);
centerY = Math.floor((Math.random() * height) + 1);
pintaSmiley(centerX, centerY, size + 0.5);
<canvas id="cnv"><</canvas>