/ Function to draw a circle
function drawCircle(x, y, radius, color) {
ctx.beginPath();
ctx.arc(x, y, radius, 0, 2 * Math.PI);
ctx.fillStyle = color;
ctx.fill();
ctx.closePath();
}
// Function to draw a curve
function drawCurve(x1, y1, x2, y2, x3, y3, color) {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.quadraticCurveTo(x2, y2, x3, y3);
ctx.strokeStyle = color;
ctx.stroke();
}
// Function to draw a heptagon
function drawHeptagon(x, y, size, color) {
ctx.beginPath();
for (let i = 0; i < 7; i++) {
const angle = (Math.PI / 7) * (2 * i + 1) + Math.PI / 2;
const px = x + size * Math.cos(angle);
const py = y + size * Math.sin(angle);
if (i === 0) {
ctx.moveTo(px, py);
} else {
ctx.lineTo(px, py);
}
}
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
}
// Function to draw a hexagon
function drawHexagon(x, y, size, color) {
ctx.beginPath();
for (let i = 0; i < 6; i++) {
const angle = (Math.PI / 3) * i + Math.PI / 6;
const px = x + size * Math.cos(angle);
const py = y + size * Math.sin(angle);
if (i === 0) {
ctx.moveTo(px, py);
} else {
ctx.lineTo(px, py);
}
}
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
}
// Function to draw a line
function drawLine(x1, y1, x2, y2, color) {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.strokeStyle = color;
ctx.stroke();
}
// Function to draw an octagon
function drawOctagon(x, y, size, color) {
ctx.beginPath();
for (let i = 0; i < 8; i++) {
const angle = (Math.PI / 4) * i + Math.PI / 8;
const px = x + size * Math.cos(angle);
const py = y + size * Math.sin(angle);
if (i === 0) {
ctx.moveTo(px, py);
} else {
ctx.lineTo(px, py);
}
}
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
}
// Function to draw an oval
function drawOval(x, y, radiusX, radiusY, rotation, color) {
ctx.beginPath();
ctx.ellipse(x, y, radiusX, radiusY, rotation, 0, 2 * Math.PI);
ctx.fillStyle = color;
ctx.fill();
ctx.closePath();
}
// Function to draw a pentagon
function drawPentagon(x, y, size, color) {
ctx.beginPath();
for (let i = 0; i < 5; i++) {
const angle = (Math.PI / 2) + (i * 2 * Math.PI / 5);
const px = x + size * Math.cos(angle);
const py = y + size * Math.sin(angle);
if (i === 0) {
ctx.moveTo(px, py);
} else {
ctx.lineTo(px, py);
}
}
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
}
// Function to draw a rectangle
function drawRect(x, y, width, height, color) {
ctx.fillStyle = color;
ctx.fillRect(x, y, width, height);
}
// Function to draw a semicircle
function drawSemicircle(x, y, radius, color) {
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI);
ctx.fillStyle = color;
ctx.fill();
ctx.closePath();
}
// Function to draw a square
function drawSquare(x, y, size, color) {
drawRect(x, y, size, size, color);
}
// Function to draw a star
function drawStar(cx, cy, spikes, outerRadius, innerRadius, color) {
let rot = Math.PI / 2 * 3;
let x = cx;
let y = cy;
let step = Math.PI / spikes;
ctx.beginPath();
ctx.moveTo(cx, cy – outerRadius);
for (let i = 0; i < spikes; i++) {
x = cx + Math.cos(rot) * outerRadius;
y = cy + Math.sin(rot) * outerRadius;
ctx.lineTo(x, y);
rot += step;
x = cx + Math.cos(rot) * innerRadius;
y = cy + Math.sin(rot) * innerRadius;
ctx.lineTo(x, y);
rot += step;
}
ctx.lineTo(cx, cy – outerRadius);
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
}
// Function to draw a triangle
function drawTriangle(x1, y1, x2, y2, x3, y3, color) {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.lineTo(x3, y3);
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
}
// Function to generate a random color
function randomColor() {
const letters = ‘0123456789ABCDEF’;
let color = ‘#’;
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
// Function to draw text
function drawText(text, x, y, color) {
ctx.fillStyle = color;
ctx.font = ’20px Arial’;
ctx.fillText(text, x, y);
}