xxxxxxxxxx
135
let flowers = function (p) {
p.setup = function () {
p.createCanvas(400, 400);
};
p.draw = function () {
p.background(220);
info.petal_fill = p.colorRange(12);
p.drawFlower(200, 200, info);
};
p.drawFlower = function (x, y, data) {
p.push();
function drawPetals(cn) {
// Draw petal function
function drawPetal(cn) {
p.push();
p.fill(pFill[cn]);
p.noStroke();
p.triangle(0, 0, A.x, A.y, B.x, B.y);
p.stroke(data.petal_stroke);
p.strokeWeight(data.petal_stroke_weight);
p.arc(C.x, C.y, tipRad * 2, tipRad * 2, 1.5 * p.PI, 2.5 * p.PI, p.OPEN);
p.line(0, 0, A.x, A.y);
p.line(0, 0, B.x, B.y);
p.pop();
} // End drawPetal
p.push();
let pFill = data.petal_fill;
let deltaAngle = p.TWO_PI / data.nbrPetals,
hda = deltaAngle / 2;
let pRad = data.petal_radius;
let aB = p.TWO_PI - hda,
aA = p.TWO_PI + hda,
A = p.createVector(pRad * p.cos(hda), pRad * p.sin(hda)),
B = p.createVector(A.x, -A.y),
C = p.createVector(A.x, 0),
tipRad = A.y;
for (let i = 0, n = 0; i < data.nbrPetals; i++) {
drawPetal(n++ % pFill.length);
p.rotate(deltaAngle);
}
p.pop();
} // End drawPetals
function drawFace() {
function drawMouth() {
p.push();
//p.translate(0, -data.face_radius * 0.1);
p.stroke(data.mouth_stroke);
p.strokeWeight(data.mouth_stroke_weight);
p.fill(data.mouth_fill);
let r0 = face_radius * 0.6,
r1 = face_radius * 0.2,
r3 = face_radius * 0.65;
p.arc(0, 0, r0 * 2, r1 * 2, p.PI, p.TWO_PI, p.OPEN);
p.arc(0, 0, r0 * 2, r3 * 2, 0, p.PI, p.OPEN);
p.pop();
} // End drawMouth
function drawEyes() {
function drawEye(ex, ey, eang) {
p.push();
p.translate(ex, ey);
p.rotate(eang);
p.noStroke();
p.fill(data.eye_fill[0]);
p.ellipse(0, 0, eye_radius * 2, eye_radius * 1.4);
p.fill(data.eye_fill[1]);
p.ellipse(
0.3 * eye_radius,
-0.18 * eye_radius,
0.9 * eye_radius,
0.5 * eye_radius
);
p.ellipse(
-0.45 * eye_radius,
0.15 * eye_radius,
0.7 * eye_radius,
0.4 * eye_radius
);
p.pop();
} // End draw eye
drawEye(-0.35 * face_radius, -0.45 * face_radius, -0.2*p.PI);
drawEye(0.35 * face_radius, -0.45 * face_radius, 0.2*p.PI);
} // End of draw eyes
let face_radius = data.petal_radius * 0.5,
eye_radius = face_radius * 0.18;
p.push();
p.stroke(data.face_stroke);
p.strokeWeight(data.face_stroke_weight);
p.fill(data.face_fill);
p.ellipse(0, 0, face_radius * 2, face_radius * 2);
drawMouth();
drawEyes();
p.pop();
} // End drawFace
p.push();
p.translate(x, y);
drawPetals();
drawFace();
p.pop();
};
let info = {
petal_radius: 120,
nbrPetals: 12,
petal_fill: [p.color(255, 200, 200), p.color(255, 255, 200)],
petal_stroke: p.color(0),
petal_stroke_weight: 4,
face_fill: p.color(255, 255, 0),
face_stroke: p.color(0),
face_stroke_weight: 4,
mouth_fill: p.color(255, 32, 32),
mouth_stroke: p.color(0),
mouth_stroke_weight: 4,
eye_fill: [p.color(0), p.color(200)],
};
p.colorRange = function(n){
p.push();
p.colorMode(p.HSB,360,100,100);
let c = [], delta = 360/n;
for(let i=0; i < n;i++){
c.push(p.color(i*delta,80,100));
}
p.pop();
return c;
}
};
let sketch = new p5(flowers);