Repository navigation
Expand file tree
/
Copy path22-autolayout.htm
More file actions
113 lines (77 loc) · 3.28 KB
/
Copy path22-autolayout.htm
File metadata and controls
113 lines (77 loc) · 3.28 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!DOCTYPE html>
<html lang="en">
<head>
<title>Many Boxes in a Row</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- LIBRARY FILES: basic.css and basic.js come first, your own code after them -->
<link rel="preload" href="../basic/font/open-sans/OpenSans-Regular.ttf" as="font" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="../basic/basic.css">
<script src="../basic/basic.js" type="text/javascript" charset="utf-8"></script>
<script>
/*
AUTOLAYOUT: MANY BOXES IN A ROW
A for loop creates 10 boxes inside an HGroup. The group puts them side by side, with a gap
between them: no left or top is written for any box.
Click a box: the boxes up to it are filled, like a progress or a rating.
- HGroup({ gap }) ... endGroup() -> a row: the objects created between the two lines go into it.
- hug: 1 -> the group is as big as its content (here: the 10 boxes).
- for (let i = 0; i < BOX_COUNT; i++) -> runs its code BOX_COUNT times; i is 0, 1, 2 ... 9.
- group.children -> the objects inside a group, in their order.
TRY: change BOX_COUNT, BOX_SIZE or GAP; or write VGroup instead of HGroup for the row.
*/
// *** VARIABLES:
const BOX_COUNT = 10;
const BOX_SIZE = 24;
const GAP = 4;
const COLOR_FILLED = "orangered";
const COLOR_EMPTY = "lightgray";
let filledCount = 7;
let grpBoxes, lblCount;
// *** START:
const start = function () {
page.color = "white";
// GROUP: A column in the middle of the page: the boxes, and the count under them
VGroup({ gap: 16 });
// GROUP: The row of boxes
grpBoxes = HGroup({ hug: 1, gap: GAP });
for (let i = 0; i < BOX_COUNT; i++) {
createStepBox(i);
}
endGroup();
// LABEL: How many boxes are filled
lblCount = Label({
text: "",
fontSize: 16,
textColor: Black(0.6),
});
endGroup();
paintBoxes();
};
// *** FUNCTIONS:
// index: 0 for the first box, 1 for the second...
const createStepBox = function (index) {
// BOX: No position: the group places it after the box before it.
const box = Box({
width: BOX_SIZE,
height: BOX_SIZE,
round: 4,
cursor: "pointer",
});
box.on("click", function () {
filledCount = index + 1;
paintBoxes();
});
};
// The boxes before filledCount are filled, the others are empty.
const paintBoxes = function () {
grpBoxes.children.forEach(function (box, index) {
box.color = (index < filledCount) ? COLOR_FILLED : COLOR_EMPTY;
});
lblCount.text = filledCount + " / " + BOX_COUNT;
};
</script>
</head>
<body>
</body>
</html>