-
Notifications
You must be signed in to change notification settings - Fork 1
/
arbol.html
132 lines (120 loc) · 3.71 KB
/
arbol.html
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="icon" type="image/png" sizes="32x32" href="/assets/favicon-32x32.png">
<link rel="manifest" href="/site.webmanifest">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="theme-color" content="#ffffff">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="/style.css" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
<title>Pythagoras tree</title>
<style>
html, body {
height: 100%;
margin: 0;
background-color: #C0FFB2;
overflow: hidden;
}
body {
display: flex;
justify-content: center;
align-items: flex-end;
}
.root {
position: relative;
width: 5em;
height: 10em;
/* initial values */
--level: 0;
--max-level: 1;
--sizea: calc(var(--wa, 50) * 1%);
--sizeb: calc(var(--wb, 50) * 1%);
--dega: calc(var(--ra, 0) * 1deg);
--degb: calc(var(--rb, 0) * 1deg);
}
.root div {
position: absolute;
bottom: 100%;
}
.root, .root div {
background-color: hsl(
calc(288 - 219 * var(--level) / var(--max-level)),
calc(58% + 1% * var(--level)),
calc(17% + 34% * var(--level) / var(--max-level))
);
}
.root div:first-child {
width: var(--sizea);
height: var(--sizea);
transform: rotate(var(--dega));
transform-origin: 0 100%;
left: 0;
}
.root div:last-child {
width: var(--sizeb);
height: var(--sizeb);
transform: rotate(var(--degb));
transform-origin: 100% 100%;
right: 0;
}
</style>
</head>
<body>
<div class="root" data-max-level="9"></div>
<script>
(function() {
const root = document.querySelector('.root');
if (!root) return false;
const { sqrt, pow, atan, sin, PI } = Math;
const max = root.getAttribute('data-max-level') || 8;
root.style.setProperty('--max-level', max);
function build(root, level) {
level && setTimeout(() => {
let nodes = (max === level)
? [root] : root.querySelectorAll('[node]');
[].forEach.call(nodes, n => {
n.removeAttribute('node');
n.innerHTML =
`<div style="--level: ${ max - level + 1 }" node></div>`
.repeat(2);
});
build(root, --level);
}, 500);
}
function compute(x, y, w, h) {
const r = x / w ;
const d = (1 - y / h) * sin(PI / 3);
const d2 = pow(d, 2);
const deg = 180 / PI;
return {
'--wa': sqrt(d2 + pow(r, 2)) * 100,
'--wb': sqrt(d2 + pow((1 - r), 2)) * 100,
'--ra': atan(d / r) * -deg,
'--rb': atan(d / (1 - r)) * deg
}
}
function update(e) {
const props = compute(
e.clientX || (e.changedTouches && e.changedTouches[0] || e).pageX,
e.clientY || (e.changedTouches && e.changedTouches[0] || e).pageY,
window.innerWidth, window.innerHeight
);
Object.keys(props).forEach(n => (
root.style.setProperty(n, props[n])
));
}
['touchmove', 'touchstart', 'mousemove']
.forEach(n => document.addEventListener(n, update));
build(root, max);
}());
</script>
<footer class="foot-right">
<a href="/">🏡 VOLVER 🏡</a>
<a href="https://github.com/yuanchuan/css-fractal">
<span class="fab fa-github" aria-hidden="true"></span>ver codigo
</a>
</footer>
</body>
</html>