I need to draw multiple circles inside multiple squares like in the picture below:
I only managed to draw multiple circles inside each other and multiple squares inside each other but cannot figure out how to place them as it is on the picture
This is what I have currently
function drawSquare() {
var canvas = document.getElementById("Square");
if (canvas.getContext) {
var square = canvas.getContext("2d");
square.fillStyle = "red";
square.fillRect(0, 0, 500, 500);
square.fillStyle = "yellow";
square.fillRect(50, 50, 400, 400);
square.fillStyle = "blue";
square.fillRect(100, 100, 300, 300);
}
}
function drawRedCircle() {
var canvas = document.getElementById("Circle");
if (canvas.getContext) {
var circle = canvas.getContext("2d");
circle.beginPath();
circle.arc(250, 250, 125, 0, Math.PI * 2, );
circle.fillStyle = "red";
circle.fill();
circle.stroke();
circle.closePath();
circle.beginPath();
circle.arc(250, 250, 75, 0, Math.PI * 2, );
circle.fillStyle = "yellow";
circle.fill();
circle.stroke();
circle.closePath();
circle.beginPath();
circle.arc(250, 250, 25, 0, Math.PI * 2, );
circle.fillStyle = "blue";
circle.fill();
circle.stroke();
circle.closePath();
}
}
<canvas id="Square" width="500" height="500" style="border: 5px solid #FF0000;">
Your browser does not support the Canvas tag. </canvas>
Could someone please advise me on how to places these circles inside the squares? Pretty please
2
Answers
Just put everything on one canvas.
Currently, you code says, get a canvas with an id of square and put squares on it. Then get a canvas with an id of circle and put circles on it. Instead, just use one canvas and draw both the squares and circles on it.
id="square"
on your canvas Elementconst drawSquare = (xCenter, yCenter, size, color) => {