I've done dynamic objects and patterns before in html, but I came accross with this exercice I am doing on the holidays vacation and can't get it right though I'm sure its pretty simple :)
The exercice asks to create an object "unit" made by 4 circles like in the images bellow, in a 500x500 canvas . Then you have to create a function called patternCircle(n) to make a pattern from that "unit". Notice that n=1 shows the "unit", n=2 2x2, n=3 3x3, n=4 4x4 pattern in the canvas.
I can successfully create the "unit" but when I use my pattern function the overlapping isnt right but the (x,y) of the circles seem correct.
I use three var, i=lines, j=rows, and side(side of the canvas, 500)/n2,
Two for cicles, that create the lines and row but it doesn't create the image I'm looking for.
I really hope you can take a glance at the code and help.
Unit in a pattern of 4x4 - Create function that creates the pattern in the image using the "unit"
var screen, paint;
function inicGraf() {
screen = document.getElementById("screen");
paint = screen.getContext("2d");
}
function circleScreen(x, y, radius, colorLine, colorInside) {
paint.lineWidth = 1;
paint.strokeStyle = colorLine;
paint.fillStyle = colorInside;
paint.beginPath();
paint.arc(x, y, radius, 0, 2.0 * Math.PI);
paint.closePath();
paint.fill();
paint.stroke();
}
function figureCircle(x, y, side) {
var r1 = (side / 2.0),
r2 = (((side / 2.0) * 80.0) / 100.0);
circleScreen(x + r1, y + side, r1, "#449779", "#449779");
circleScreen(x + r1, y + side, r2, "#013D55", "#013D55");
circleScreen(x + side, y + r1, r1, "#E6B569", "#E6B569");
circleScreen(x + side, y + r1, r2, "#AA8D49", "#AA8D49");
circleScreen(x, y + r1, r1, "#E6B569", "#E6B569");
circleScreen(x, y + r1, r2, "#AA8D49", "#AA8D49");
circleScreen(x + r1, y, r1, "#449779", "#449779");
circleScreen(x + r1, y, r2, "#013D55", "#013D55");
}
function patternCircle(n) {
var i, j, side = 500 / n;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
figureCircle(i * side, j * side, side);
}
}
}