Here are all the drawing functions you can use in the code editor. Simply copy and paste the code into the editor to use them in your drawings.
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 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 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 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 drawLine(x1, y1, x2, y2, color) {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.strokeStyle = color;
ctx.stroke();
}
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 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 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 drawRect(x, y, width, height, color) {
ctx.fillStyle = color;
ctx.fillRect(x, y, width, height);
}
function drawSemicircle(x, y, radius, color) {
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI);
ctx.fillStyle = color;
ctx.fill();
ctx.closePath();
}
function drawSquare(x, y, size, color) {
drawRect(x, y, size, size, color);
}
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 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 randomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function drawText(text, x, y, color) {
ctx.fillStyle = color;
ctx.font = '20px Arial';
ctx.fillText(text, x, y);
}