Skip to content

Commit 1347bb8

Browse files
committed
modularized code to accomodate the body resize event
1 parent b9af6f2 commit 1347bb8

File tree

1 file changed

+50
-52
lines changed

1 file changed

+50
-52
lines changed

js/2secrets.js

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,73 @@
1-
var height = $('body').height();
2-
var width = $('body').width();
3-
// var mobileSize = 200;
4-
// var desktopSize = 150;
5-
var radius = 0, rows = 0, cols = 0, size = 150;
1+
var shapes;
2+
main();
63

7-
// Define rows, colnums, and shape radius based on document size
8-
var isMobile = window.matchMedia("only screen and (max-width: 850px)").matches ||
9-
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
10-
var isOtherMediaQuery = window.matchMedia("only screen and (min-width:1080px)").matches;
11-
if(isMobile && !isOtherMediaQuery) {
12-
size = 200;
13-
} else {
14-
size = 150;
15-
}
4+
function main() {
5+
var height = $('body').height();
6+
var width = $('body').width();
167

17-
rows = Math.floor(height / size);
18-
cols = Math.floor(width / size);
19-
radius = Math.floor(Math.max(width, height) / Math.max(rows, cols)) / 2;
8+
var size = getShapeSize();
9+
var rows = Math.floor(height / size);
10+
var cols = Math.floor(width / size);
11+
var radius = Math.floor(Math.max(width, height) / Math.max(rows, cols)) / 2;
2012

21-
console.log(rows);
22-
console.log(cols);
13+
makeGrid(rows, cols);
14+
shapes = makeFlowers(radius);
15+
addSvgsToCells(size);
16+
}
2317

24-
for (var r = 0; r < rows; r++) {
25-
var rowId = "row" + r;
26-
var row = $("<div/>").addClass("row").attr("id", rowId).appendTo('body');
27-
var vi = r / (rows - 1);
28-
for (var c = 0; c < cols; c++) {
29-
var cellId = "cell" + ((r * rows) + c);
30-
$(row).append('<div class="cell" id="' + cellId + '"></div>');
18+
function getShapeSize() {
19+
var size;
20+
var isMobile = window.matchMedia("only screen and (max-width: 850px)").matches ||
21+
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
22+
var isOtherMediaQuery = window.matchMedia("only screen and (min-width:1080px)").matches;
23+
if(isMobile && !isOtherMediaQuery) {
24+
size = 200;
25+
} else {
26+
size = 150;
3127
}
28+
return size;
3229
}
3330

31+
function addSvgsToCells(size) {
32+
$(".cell").each(function (index, object) {
33+
var two = new Two({
34+
width: size + 20,
35+
height:size + 20
36+
}).appendTo(object);
37+
var shape = pickFlower(shapes);
38+
shape.translation.set(two.width / 2, two.height / 2);
39+
two.add(shape);
40+
two.update();
41+
});
42+
}
3443

35-
var flowers = [];
36-
var shapes = makeFlowers();
37-
38-
$(".cell").each(function (index, object) {
39-
var two = new Two({
40-
width: size + 20,
41-
height:size + 20
42-
}).appendTo(object);
43-
var shape = pickFlower(shapes);
44-
shape.translation.set(two.width / 2, two.height / 2);
45-
two.add(shape);
46-
two.update();
47-
flowers.push(shape);
48-
});
49-
50-
//
51-
// for (var f in flowers) {
52-
// var flower = flowers[f];
53-
// $(flower._renderer.elem)
54-
// .click(function(e) {
55-
// flower.fill = "blue";
56-
// })
57-
// }
44+
function makeGrid(rows, cols) {
45+
$('body').empty();
46+
for (var r = 0; r < rows; r++) {
47+
var rowId = "row" + r;
48+
var row = $("<div/>").addClass("row").attr("id", rowId).appendTo('body');
49+
var vi = r / (rows - 1);
50+
for (var c = 0; c < cols; c++) {
51+
var cellId = "cell" + ((r * rows) + c);
52+
$(row).append('<div class="cell" id="' + cellId + '"></div>');
53+
}
54+
}
55+
}
5856

59-
function roseMath(v, k, t) {
57+
function roseMath(radius, v, k, t) {
6058
v.x = radius * Math.cos(k * t) * Math.cos(t);
6159
v.y = radius * Math.cos(k * t) * Math.sin(t);
6260
return v;
6361
}
6462

65-
function makeFlowers() {
63+
function makeFlowers(radius) {
6664
var flowers = [];
6765
var resolution = 240; // every flower has 240 points
6866
for (var k = 4; k < 20; k++) {
6967
var points = [];
7068
for (var j = 0; j < resolution; j++) {
7169
points[j] = new Two.Anchor();
72-
roseMath(points[j], k, Math.PI * 2 * j / resolution);
70+
roseMath(radius, points[j], k, Math.PI * 2 * j / resolution);
7371
}
7472
// Create shape
7573
var flower = new Two.Path(points, true, true);

0 commit comments

Comments
 (0)