Skip to content

Commit 9f90930

Browse files
committed
modularized functions, passed variable, removed global
1 parent d82af48 commit 9f90930

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

js/secrets.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ if(isMobile && !isOtherMediaQuery) {
2020
radius = Math.floor(Math.max(two.width, two.height) / Math.max(rows, cols)) / 2;
2121
}
2222
var shapes = makeFlowers();
23+
var flowers = [];
2324

2425
for (var r = 0; r < rows; r++) {
2526
// even rows have an offset of 0.5
@@ -35,8 +36,10 @@ for (var r = 0; r < rows; r++) {
3536
}
3637
}
3738
var hi = k /(cols - 1);
38-
var shape = pickFlower();
39+
var shape = pickFlower(shapes);
3940
shape.translation.set(hi * two.width, vi * two.height);
41+
flowers.push(shape);
42+
two.add(shape);
4043
}
4144
}
4245
two.update();
@@ -49,7 +52,7 @@ function roseMath(v, k, t) {
4952
}
5053

5154
function makeFlowers() {
52-
var shapes = [];
55+
var flowers = [];
5356
var resolution = 240; // every flower has 240 points
5457
for (var k = 4; k < 20; k++) {
5558
var points = [];
@@ -58,26 +61,26 @@ function makeFlowers() {
5861
roseMath(points[j], k, Math.PI * 2 * j / resolution);
5962
}
6063
// Create shape
61-
var shape = new Two.Path(points, true, true);
62-
shapes.push(shape);
64+
var flower = new Two.Path(points, true, true);
65+
flowers.push(flower);
6366
}
64-
return shapes;
67+
return flowers;
6568
}
6669

67-
function pickFlower() {
68-
var s = Math.floor(Math.random() * shapes.length);
69-
var shape = shapes[s].clone();
70+
function pickFlower(flowers) {
71+
var f = Math.floor(Math.random() * flowers.length);
72+
var flower = flowers[f].clone();
7073
// Style the shape
7174
var colors = ['tomato', 'lightsalmon', 'floralwhite', 'orangered',
7275
'gold', 'red', 'darkorange'];
7376
var color = colors[Math.floor(Math.random() * colors.length)];
74-
shape.stroke = color;
75-
shape.fill = color;
76-
shape.linewidth = 4;
77-
shape.cap = 'round';
78-
shape.rotation = Math.floor(Math.random() * 4) * Math.PI / 2 + Math.PI / 4;
77+
flower.stroke = color;
78+
flower.fill = color;
79+
flower.linewidth = 4;
80+
flower.cap = 'round';
81+
flower.rotation = Math.floor(Math.random() * 4) * Math.PI / 2 + Math.PI / 4;
7982
// For animation later on
80-
shape.step = (Math.floor(Math.random() * 8) / 8) * Math.PI / 60;
81-
shape.step *= Math.random() > 0.5 ? - 1 : 1;
82-
return shape;
83+
flower.step = (Math.floor(Math.random() * 8) / 8) * Math.PI / 60;
84+
flower.step *= Math.random() > 0.5 ? - 1 : 1;
85+
return flower;
8386
}

0 commit comments

Comments
 (0)