folk-canvas/demo/sticky-html-rope.html

77 lines
2.1 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Wiggly</title>
<style>
html {
height: 100%;
}
body {
min-height: 100%;
position: relative;
margin: 0;
}
folk-shape {
background-color: black;
border-radius: 10%;
}
folk-rope {
display: block;
position: absolute;
inset: 0 0 0 0;
pointer-events: none;
}
button {
margin: 1rem;
background-color: black;
font-size: 2rem;
border-radius: 5px;
}
</style>
</head>
<body>
<button id="wind">☁️</button>
<folk-shape id="box1" x="100" y="100" width="30" height="30"></folk-shape>
<folk-shape id="box2" x="300" y="105" width="30" height="30"></folk-shape>
<folk-shape id="box3" x="200" y="150" width="30" height="30"></folk-shape>
<folk-shape id="box4" x="400" y="100" width="30" height="30"></folk-shape>
<folk-rope source="#box1" target="#box2"></folk-rope>
<folk-rope source="#box1" target="#box3"></folk-rope>
<folk-rope source="#box1" target="#box4"></folk-rope>
<script type="module">
import '../src/standalone/folk-shape.ts';
import '../src/standalone/folk-rope.ts';
let timeout = -1;
let isBlowing = false;
const ropes = document.querySelectorAll('folk-rope');
const randomInt = (multiplier, adjust = 0) => Math.floor(Math.random() * multiplier) + adjust;
const updateGravity = (gravity) => ropes.forEach((rope) => (rope.gravity = gravity));
function simulateWind() {
updateGravity({ x: randomInt(5000), y: randomInt(5000) });
timeout = setTimeout(simulateWind, randomInt(1000, 200));
}
wind.addEventListener('click', () => {
if (isBlowing) {
clearTimeout(timeout);
updateGravity({ x: 0, y: 3000 });
wind.textContent = '☁️';
} else {
simulateWind();
wind.textContent = '🌬️';
}
isBlowing = !isBlowing;
});
</script>
</body>
</html>