-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflowerDesign.js
More file actions
73 lines (52 loc) · 2.04 KB
/
flowerDesign.js
File metadata and controls
73 lines (52 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import {randomCellPosition, cellToPx} from "./utils.js";
import {xCells, yCells} from "./config.js";
const TAU = Math.PI * 2; // 1 tour complet
const friction = 0.95; // ralentit: ~95%/seconde
let speed = 6; // tours/s au début
export function setNewGridFlowerPosition(canvas) {
// Random positon of Flower (en pixel)
let flowerGridPosition = { x:0, y:0 };
flowerGridPosition = {
x: randomCellPosition(xCells),
y: randomCellPosition(yCells)
};
return flowerGridPosition;
}
export function slowDownFlowerRotation(flowerAngle) {
speed *= friction;
return (flowerAngle + TAU * speed / 60) % TAU;
}
export function reinitializeSpeedFlowerRotation() {
speed = 3;
}
// draw Flower
export function drawFlower(ctx, flowerGridPosition, flowerAngle) {
// Diameter < 20px (size of a cell)
const petalR = 4; // petal radius in px
const centerR = 3; // center radius in px
const offset = 5; // distance from center to each petal in px
// position of the center of the flower (in pixels)
const px = cellToPx(flowerGridPosition.x);
const py = cellToPx(flowerGridPosition.y);
// Animation (= rotation)
ctx.save(); // save canvas state
ctx.translate(px, py); // (0,0) becomes the flower’s center
ctx.rotate(flowerAngle); // rotate around the center
// 5 Petals (relative coordinates to flower's center)
for (let i = 0; i < 5; i++) {
ctx.fillStyle = "#ffd34d";
const a = i * (2 * Math.PI / 5); // 360° / 5 = 72°
// petal's center coordinates from the flower's center
const pxr = Math.cos(a) * offset;
const pyr = Math.sin(a) * offset;
ctx.beginPath();
ctx.arc(pxr, pyr, petalR, 0, Math.PI * 2);
ctx.fill();
}
// Coeur
ctx.fillStyle = "#ff0000"; // jaune
ctx.beginPath();
ctx.arc(0, 0, centerR, 0, Math.PI * 2);
ctx.fill();
ctx.restore(); // Cancels the rotation/translation for the rest of the drawing.
}