-
Notifications
You must be signed in to change notification settings - Fork 2
/
css3d_rotate.html
101 lines (84 loc) · 3.14 KB
/
css3d_rotate.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
<!-- vim: sw=4 ts=4 expandtab smartindent ft=javascript
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>demo</title>
<style> document, body { margin: 0px; padding: 0px; overflow: hidden; } </style>
</head>
<body>
<div
style="z-index:1;margin:1vh;position:absolute;top:0px;left:0px;display:flex;flex-direction:column;"
id="inputs"
>
<div>
<input type="text" value="3" id="size_input" style="margin:0.2vh;"/>
<label for="size_input">size</label>
</div>
<div>
<input type="text" value="60" id="count_input" style="margin:0.2vh;"/>
<label for="count_input">count</label>
</div>
<div>
<input type="text" value="360" id="perspective_input" style="margin:0.2vh;"/>
<label for="perspective_input">perspective</label>
</div>
</div>
<script>"use strict";
for (const input of document.querySelector('#inputs').querySelectorAll('input')) {
input.oninput = init;
}
const parent = document.createElement('div');
parent.id = 'carousel-holder'
let divs;
function init() {
parent.innerHTML = '';
parent.style.width = '100vw';
parent.style.height = '100vh';
parent.style.perspective = parseInt(document.querySelector('input#perspective_input').value) + 'px';
document.body.appendChild(parent);
const size_vh = parseInt(document.querySelector('input#size_input').value);
const count = parseInt(document.querySelector('input#count_input').value);
divs = [];
for (let i = 0; i < count; i++) {
const div = document.createElement('div');
div.style.width = size_vh + 'vh';
div.style.height = size_vh + 'vh';
div.style.backgroundColor = 'gray';
div.style.display = 'flex';
div.style.justifyContent = 'center';
div.style.alignItems = 'center';
div.style.fontSize = size_vh*0.5 + 'vh';
div.textContent += "🤔";
divs.push(div);
parent.appendChild(div);
}
}
window.onload = () => {
document.body.style.backgroundColor = 'white';
init();
requestAnimationFrame(function frame(dt) {
requestAnimationFrame(frame);
const size_vh = parseInt(document.querySelector('input#size_input').value);
for (let i = 0; i < divs.length; i++) {
const t = i / divs.length;
const ANGLE_RANGE = Math.PI * 2;
const angle = (t + dt*0.000025) * ANGLE_RANGE;
let y = Math.cos(angle);
let z = Math.sin(angle);
/* NOTE: Math.atan2(y, z) is tempting, but you get upside down quads */
const tan_angle = -Math.atan2(y, -z);
y *= window.innerHeight*0.3;
z *= window.innerHeight*0.3;
divs[i].style.position = 'absolute';
divs[i].style.top = (y + window.innerHeight*0.5) + 'px';
divs[i].style.left = `calc(${window.innerWidth*0.5}px - ${size_vh*0.5}vh)`;
divs[i].style.transform = `translateZ(${-z}px) rotateX(${tan_angle}rad)`;
divs[i].style.zIndex = -~~z;
}
});
}
</script>
</body>
</html>