Skip to content

Commit fb4d6d6

Browse files
author
Andy Bayer
committed
divide
1 parent 7388029 commit fb4d6d6

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: Spin
33
layout: food
44
description: Spin
55
meta: Spin
6+
permalink: /feed/hourglass
67
---
78

89
Spin

_feed/2022-11-15-divide.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
title: Divide
3+
layout: food
4+
description: Divide
5+
meta: Divide
6+
---
7+
8+
Divide
9+
10+
<section class="container">
11+
<div id="parent" style="width: 400px; height: 400px" data-depth="1"></div>
12+
<script src="/feed/divide.js"></script>
13+
</section>

_feed/divide.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
(() => {
2+
const maxDepth = 20;
3+
const parent = document.currentScript.parentElement.querySelector("#parent");
4+
const listen = (container) => {
5+
container.addEventListener("mouseover", (e) => {
6+
e.stopPropagation();
7+
divide(container);
8+
});
9+
};
10+
listen(parent);
11+
const map = (value, minA, maxA, minB, maxB) => {
12+
return ((value - minA) / (maxA - minA)) * (maxB - minB) + minB;
13+
};
14+
const divide = (parent) => {
15+
if (parseInt(parent.dataset.depth) > maxDepth) {
16+
return;
17+
}
18+
const children = new Array(4).fill().map(() => {
19+
const div = document.createElement("div");
20+
const depth = parseInt(parent.dataset.depth) + 1;
21+
div.dataset.depth = depth;
22+
const value = map(depth, 0, maxDepth, 255, 0);
23+
div.style = `width: 50%; height: 50%; float: left; background-color: rgb(${
24+
(Math.random() * value) | 0
25+
}, ${(Math.random() * value) | 0}, ${(Math.random() * value) | 0})`;
26+
listen(div);
27+
return div;
28+
});
29+
parent.replaceChildren(...children);
30+
};
31+
divide(parent);
32+
})();

0 commit comments

Comments
 (0)