folk-canvas/website/canvas/moveBefore.html

108 lines
3.0 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>moveBefore</title>
<style>
html {
width: 100%;
height: 100%;
position: fixed;
overflow: hidden;
}
body {
min-height: 100%;
position: relative;
margin: 0;
display: flex;
}
.container {
width: 50%;
height: 100vh;
border: 2px dashed #ccc;
transition: background-color 0.3s;
}
.container:has(folk-shape) {
background-color: rgba(0, 255, 0, 0.1);
}
folk-shape {
background: rgb(187, 178, 178);
}
.warning-banner {
position: fixed;
top: 0;
left: 0;
right: 0;
background-color: #fff3cd;
color: #856404;
border: 1px solid #ffeeba;
padding: 12px;
text-align: center;
z-index: 1000;
font-family: system-ui, -apple-system, sans-serif;
}
</style>
</head>
<body>
<div class="container" id="left"></div>
<div class="container" id="right"></div>
<folk-shape x="100" y="100" width="50" height="50"></folk-shape>
<folk-shape x="100" y="200" width="50" height="50"></folk-shape>
<folk-shape x="100" y="300" width="50" height="50" rotation="45"></folk-shape>
<script type="module">
import '../../lib/standalone/folk-shape.ts';
// Check for moveBefore support
if (!Element.prototype.moveBefore) {
const warning = document.createElement('div');
warning.className = 'warning-banner';
warning.innerHTML =
'⚠️ Note: moveBefore() API requires Chrome Canary with <code>chrome://flags/#atomic-move</code> enabled';
document.body.insertAdjacentElement('afterbegin', warning);
console.warn('Note: moveBefore() API requires Chrome Canary with chrome://flags/#atomic-move enabled');
}
// TODO: investigate / figure out how to avoid the need for this
let isMoving = false;
document.addEventListener('transform', (e) => {
if (isMoving) return; // Prevent recursive calls
const shape = e.target;
if (!shape.moveBefore) {
return;
}
const rect = shape.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const containers = document.querySelectorAll('.container');
try {
isMoving = true;
for (const container of containers) {
const containerRect = container.getBoundingClientRect();
if (centerX >= containerRect.left && centerX <= containerRect.right) {
container.moveBefore(shape, container.firstChild);
return;
}
}
if (shape.parentElement !== document.body) {
document.body.moveBefore(shape, document.body.firstChild);
}
} finally {
isMoving = false;
}
});
</script>
</body>
</html>