Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 227 additions & 0 deletions examples/transform-aware.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Transform-Aware Demo - react-use-measure</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
margin: 0;
padding: 20px;
background: #f5f5f5;
}

.container {
max-width: 800px;
margin: 0 auto;
}

h1 {
text-align: center;
color: #333;
margin-bottom: 40px;
}

.demo-section {
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
margin-bottom: 30px;
}

.demo-box {
width: 40px;
height: 40px;
margin: 20px 0;
border: 2px solid #333;
display: inline-block;
transition: transform 0.3s ease;
}

.demo-box.default {
background: #9cf;
transform: scale(0.75);
transform-origin: 0 0;
}

.demo-box.transform-aware {
background: #fc9;
transform: scale(0.75);
transform-origin: 0 0;
}

.bounds-display {
background: #f8f9fa;
border: 1px solid #e9ecef;
border-radius: 4px;
padding: 15px;
margin-top: 15px;
font-family: 'Monaco', 'Menlo', monospace;
font-size: 14px;
}

.bounds-display h4 {
margin: 0 0 10px 0;
color: #495057;
}

.bounds-display pre {
margin: 0;
color: #6f42c1;
}

.controls {
margin: 20px 0;
}

button {
background: #007bff;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
margin-right: 10px;
}

button:hover {
background: #0056b3;
}

.explanation {
background: #e7f3ff;
border-left: 4px solid #007bff;
padding: 15px;
margin: 20px 0;
}

.explanation h4 {
margin: 0 0 10px 0;
color: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<h1>Transform-Aware Measuring Demo</h1>

<div class="explanation">
<h4>What this demonstrates:</h4>
<p>Both boxes below are 40×40 pixels with <code>transform: scale(0.75)</code>.
The left box uses default measurement (layout box), the right uses transform-aware measurement (visual box).
Notice how they report different dimensions!</p>
</div>

<div class="demo-section">
<h3>Default Mode (layout-based)</h3>
<div class="demo-box default" id="default-box"></div>
<div class="bounds-display">
<h4>Reported Bounds:</h4>
<pre id="default-bounds">Loading...</pre>
</div>
</div>

<div class="demo-section">
<h3>Transform-Aware Mode (visual-based)</h3>
<div class="demo-box transform-aware" id="transform-box"></div>
<div class="bounds-display">
<h4>Reported Bounds:</h4>
<pre id="transform-bounds">Loading...</pre>
</div>
</div>

<div class="demo-section">
<div class="controls">
<button onclick="animateBoxes()">Animate Transform</button>
<button onclick="resetBoxes()">Reset</button>
</div>
<p><em>Click "Animate Transform" to see how transform-aware mode tracks the visual changes in real-time.</em></p>
</div>
</div>

<script type="module">
// Simulate the react-use-measure behavior for demo purposes
function measureElement(element, includeTransforms = false) {
if (includeTransforms) {
// Use getBoundingClientRect for transform-aware measurement
return element.getBoundingClientRect();
} else {
// Simulate layout-based measurement (what ResizeObserver would see)
const rect = element.getBoundingClientRect();
// For demo: simulate that layout box ignores transforms
return {
x: rect.x,
y: rect.y,
width: 40, // Original width
height: 40, // Original height
top: rect.top,
left: rect.left,
right: rect.left + 40,
bottom: rect.top + 40
};
}
}

function updateBounds() {
const defaultBox = document.getElementById('default-box');
const transformBox = document.getElementById('transform-box');
const defaultBounds = document.getElementById('default-bounds');
const transformBounds = document.getElementById('transform-bounds');

const defaultRect = measureElement(defaultBox, false);
const transformRect = measureElement(transformBox, true);

defaultBounds.textContent = JSON.stringify({
width: Math.round(defaultRect.width),
height: Math.round(defaultRect.height),
x: Math.round(defaultRect.x),
y: Math.round(defaultRect.y)
}, null, 2);

transformBounds.textContent = JSON.stringify({
width: Math.round(transformRect.width),
height: Math.round(transformRect.height),
x: Math.round(transformRect.x),
y: Math.round(transformRect.y)
}, null, 2);
}

// Update bounds initially and on window events
updateBounds();
window.addEventListener('resize', updateBounds);
window.addEventListener('scroll', updateBounds);

// Make functions available globally for buttons
window.animateBoxes = function() {
const boxes = document.querySelectorAll('.demo-box');
boxes.forEach(box => {
box.style.transform = 'scale(1.2) rotate(10deg)';
box.style.transition = 'transform 0.5s ease';
});

setTimeout(() => {
boxes.forEach(box => {
box.style.transform = 'scale(0.75)';
});
setTimeout(updateBounds, 100);
}, 500);

updateBounds();
};

window.resetBoxes = function() {
const boxes = document.querySelectorAll('.demo-box');
boxes.forEach(box => {
box.style.transform = 'scale(0.75)';
box.style.transition = 'transform 0.3s ease';
});
updateBounds();
};

// Update bounds periodically to show real-time changes
setInterval(updateBounds, 100);
</script>
</body>
</html>
Loading
Loading