-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflower.html
More file actions
228 lines (211 loc) · 13.8 KB
/
flower.html
File metadata and controls
228 lines (211 loc) · 13.8 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<!DOCTYPE html>
<html lang="en">
<meta content="utf-8" charset="utf-8">
<title>
aa
</title>
<header>
<style>
body {
width: 100vw;
height: 100vh;
}
.flower {
border: dashed 1vh;
position: relative;
width: 20vh;
height: 20vh;
left: 40vh;
top: 40vh;
}
.petal {
position: absolute;
width: calc(var(--radius) * 2);
height: calc(var(--radius) * 2);
border-radius: 50.0%;
background: transparent;
overflow: hidden;
transform-origin: '';
transform: rotate(var(--rotate-deg));
mix-blend-mode: screen;
}
.petal:before {
content: "";
width: inherit;
height: inherit;
border-radius: inherit;
position: absolute;
background: linear-gradient(135deg, rgb(0, 0, 255), rgb(255, 255, 255));
left: calc(2 * var(--radius) - var(--petal-width));
}
input {
position: relative;
height: 10vh;
width: 20vw;
}
</style>
</header>
<body>
<div class="flower" id = "flower"> </div>
<div style="display: flex; flex-direction: column;margin-top: 5vh;">
<label for="petal-length">Length</label>
<input class="flower-control" id="petal-length" type="range" min="1" max="30" value="20" step="1""/>
<label for="petal-width">Width</label>
<input class="flower-control" id="petal-width" type="range" min="1" max="30" value="1" step="1"/>
<label for="petal-num">Num</label>
<input class="flower-control" id="petal-num" type="range" min="1" max="72" value="3" step="1"/>
<label for="petal-alpha">Alpha</label>
<input class="flower-control" id="petal-alpha" type="range" min="0" max="1" value="1" step="0.001"/>
<label for="flower-alpha">Range</label>
<input class="flower-control" id="flower-range" type="range" min="0" max="360" value="180" step="1"/>
</div>
</body>
<script>
function bloom(petalLength, petalWidth, petalNum, alpha, flowerRangeDegree, aa = undefined) {
// Basic Pythagorean theorem
// (Radius - Width/2)^2 + (Length/2)^2 = Radius^2
// Radius = ((Width/2)^2 + (Length/2)^2) / Width = (Width^2 + Length^2) / (4 * Width)
let radius = Number((petalLength * petalLength + petalWidth * petalWidth) / (4 * petalWidth));
let flower = document.getElementById('flower')
flower.style.width = `${ 2 * petalLength}vh`;
flower.style.height = `${ 2 * petalLength}vh`;
flower.style.left = `${50 - petalLength}vh`;
flower.style.top = `${50 - petalLength}vh`;
let middleToSides = false?1:-1; // middleToSides: 1; sidesToMiddle: -1
let odd = petalNum % 2; // Avilaible when var middleToSides is not undifined
let bias = Number(flowerRangeDegree === 360?1:0); // 360deg flower range will have a bias value "1"
// let middlePetal = Math.ceil(petalNum / 2); // Deprecated
let clockwise = false?1:-1; // Clockwise: 1, Anti-clockwise: -1
let synchronous = false // Petals move synchronously:true or one-by-one:false.
let beginOffset = (1 - bias) * (-clockwise * flowerRangeDegree / 2);
let degreeUnit = flowerRangeDegree / (Number(petalNum) + 1.0 - bias);
let actualRotateDegree = 0;
let rotateDegree = function(clockwise, i, degreeUnit, flowerRangeDegree, petalNum, middleToSides, odd, bias, alpha, synchronous) {
if (undefined === middleToSides) {
if (synchronous)
return Number(clockwise * i * degreeUnit * alpha);
return clockwise * Math.min(Number(i * degreeUnit), Math.max(0, Number((flowerRangeDegree - (1 - bias) * degreeUnit) * alpha - bias * degreeUnit)));
} else {
if (synchronous)
return i * degreeUnit * alpha;
return Math.min(Number(i * degreeUnit), Math.max(0, Number((Math.ceil((petalNum - 1) / 2) - bias + Number(bias * petalNum % 2) + 1 + bias*(1 - petalNum % 2)) * alpha * degreeUnit - degreeUnit -1*(middleToSides - 1)/2 * degreeUnit)));
}
}
let opacity = function(i , alpha, petalNum, bias, synchronous, middleToSides, degreeUnit) {
if (undefined === middleToSides) {
if (synchronous)
return 1 / (1 + Math.exp(-30 * alpha + 10)) + "";
let tmpAlpha = petalNum * alpha - Number(i) - Number(bias) + 1.0;
return Math.min(1, Math.max(0, tmpAlpha)) + "";
} else {
if (synchronous) {
return 1 / (1 + Math.exp(-30 * alpha + 10)) + "";
}
let tmpAlpha = ((Math.ceil((petalNum - 1) / 2) - bias + Number(bias * petalNum % 2) + 1 + bias*(1 - petalNum % 2)) * alpha) - i + -1*(middleToSides - 1)/2
return Math.min(1, Math.max(0, tmpAlpha)) + "";
}
}
if (middleToSides === undefined) {
for (let i = 0 + (1 - bias); i < Number(petalNum) + (1 - bias); i++) {
// let op = opacity(i , alpha, petalNum, bias, synchronous, middleToSides, degreeUnit);
// if ("0" === op) continue;
let petal = document.createElement('div');
petal.className = 'petal';
actualRotateDegree = Number(beginOffset + rotateDegree(clockwise, i, degreeUnit, flowerRangeDegree, petalNum, middleToSides, odd, bias, alpha, synchronous));
petal.style.setProperty('--radius', `${radius}vh`);
petal.style.setProperty('--rotate-deg', `${actualRotateDegree}deg`);
petal.style.setProperty('--petal-width', `${petalWidth}vh`);
let transformOriginX = 2 * radius - petalWidth / 2 + "vh";
let transformOriginY = radius + petalLength / 2 + "vh";
petal.style.transformOrigin = `${transformOriginX} ${transformOriginY}`;
petal.style.left = `${Number(petalWidth ) / 2.0 + Number(petalLength) - 2.0 * radius}vh`;
petal.style.top = `${Number(petalLength) / 2.0 - radius}vh`;
petal.style.opacity = opacity(i , alpha, petalNum, bias, synchronous, middleToSides, degreeUnit);
flower.appendChild(petal);
}
} else {
if (-1 === middleToSides && 1 === bias) throw Error("Not supported! SideToMiddle and 360deg range are not compatible!")
beginOffset = 1 === middleToSides?0:flowerRangeDegree / 2;
if (1 === petalNum % 2 || 1 === bias){
let petal = document.createElement('div');
petal.className = 'petal';
actualRotateDegree = Number(0 * beginOffset + rotateDegree(clockwise, 0, degreeUnit, flowerRangeDegree, petalNum, middleToSides, odd, bias, alpha, synchronous));
petal.style.setProperty('--radius', `${radius}vh`);
petal.style.setProperty('--rotate-deg', `${actualRotateDegree}deg`);
petal.style.setProperty('--petal-width', `${petalWidth}vh`);
let transformOriginX = 2 * radius - petalWidth / 2 + "vh";
let transformOriginY = radius + petalLength / 2 + "vh";
petal.style.transformOrigin = `${transformOriginX} ${transformOriginY}`;
petal.style.left = `${Number(petalWidth ) / 2.0 + Number(petalLength) - 2.0 * radius}vh`;
petal.style.top = `${Number(petalLength) / 2.0 - radius}vh`;
petal.style.opacity = opacity(1===middleToSides?0:petalNum / 2, alpha, petalNum, bias, synchronous, middleToSides, degreeUnit);
flower.appendChild(petal);
}
for (let i = 1; i <= Math.ceil((petalNum - 1) / 2) - bias + Number(bias * petalNum % 2); i++) {
let petal = document.createElement('div');
petal.className = 'petal';
actualRotateDegree = Number(middleToSides * beginOffset + rotateDegree(clockwise, i, degreeUnit, flowerRangeDegree, petalNum, middleToSides, odd, bias, alpha, synchronous));
petal.style.setProperty('--radius', `${radius}vh`);
petal.style.setProperty('--rotate-deg', `${actualRotateDegree}deg`);
petal.style.setProperty('--petal-width', `${petalWidth}vh`);
let transformOriginX = 2 * radius - petalWidth / 2 + "vh";
let transformOriginY = radius + petalLength / 2 + "vh";
petal.style.transformOrigin = `${transformOriginX} ${transformOriginY}`;
petal.style.left = `${Number(petalWidth ) / 2.0 + Number(petalLength) - 2.0 * radius}vh`;
petal.style.top = `${Number(petalLength) / 2.0 - radius}vh`;
petal.style.opacity = opacity(i, alpha, petalNum, bias, synchronous, middleToSides, degreeUnit);
flower.appendChild(petal);
// ---------------------------------------------------------------
petal = document.createElement('div');
petal.className = 'petal';
actualRotateDegree = Number(-middleToSides * beginOffset + -1 * rotateDegree(clockwise, i, degreeUnit, flowerRangeDegree, petalNum, middleToSides, odd, bias, alpha, synchronous));
petal.style.setProperty('--radius', `${radius}vh`);
petal.style.setProperty('--rotate-deg', `${actualRotateDegree}deg`);
petal.style.setProperty('--petal-width', `${petalWidth}vh`);
transformOriginX = 2 * radius - petalWidth / 2 + "vh";
transformOriginY = radius + petalLength / 2 + "vh";
petal.style.transformOrigin = `${transformOriginX} ${transformOriginY}`;
petal.style.left = `${Number(petalWidth ) / 2.0 + Number(petalLength) - 2.0 * radius}vh`;
petal.style.top = `${Number(petalLength) / 2.0 - radius}vh`;
petal.style.opacity = opacity(i, alpha, petalNum, bias, synchronous, middleToSides, degreeUnit);;
flower.appendChild(petal);
}
if (1 === bias && 0 === petalNum % 2) {
let petal = document.createElement('div');
petal.className = 'petal';
actualRotateDegree = Number(beginOffset + rotateDegree(clockwise, petalNum / 2, degreeUnit, flowerRangeDegree, petalNum, middleToSides, odd, bias, alpha, synchronous));
actualRotateDegree = petalNum / 2 * degreeUnit
petal.style.setProperty('--radius', `${radius}vh`);
petal.style.setProperty('--rotate-deg', `${actualRotateDegree}deg`);
petal.style.setProperty('--petal-width', `${petalWidth}vh`);
let transformOriginX = 2 * radius - petalWidth / 2 + "vh";
let transformOriginY = radius + petalLength / 2 + "vh";
petal.style.transformOrigin = `${transformOriginX} ${transformOriginY}`;
petal.style.left = `${Number(petalWidth ) / 2.0 + Number(petalLength) - 2.0 * radius}vh`;
petal.style.top = `${Number(petalLength) / 2.0 - radius}vh`;
petal.style.opacity = opacity(petalNum / 2, alpha, petalNum, bias, synchronous, middleToSides, degreeUnit);;
flower.appendChild(petal);
}
}
}
[...document.getElementsByClassName('flower-control')].forEach(controller => {
controller.addEventListener('input', function(e) {
let petalLength = document.getElementById('petal-length').value;
let petalWidth = document.getElementById('petal-width' ).value;
let petalNum = document.getElementById('petal-num' ).value;
let petalAlpha = document.getElementById('petal-alpha' ).value;
let flowerRangeDegree = document.getElementById('flower-range').value;
[...document.getElementById('flower').children].forEach(petal => {
petal.remove();
})
bloom(petalLength, petalWidth, petalNum, Number(petalAlpha), Number(flowerRangeDegree));
})
})
let petalLength = document.getElementById('petal-length').value;
let petalWidth = document.getElementById('petal-width' ).value;
let petalNum = document.getElementById('petal-num' ).value;
let petalAlpha = document.getElementById('petal-alpha' ).value;
let flowerRangeDegree = document.getElementById('flower-range').value;
bloom(petalLength, petalWidth, petalNum, Number(petalAlpha), Number(flowerRangeDegree));
</script>
</html>