folk-canvas/website/canvas/event-propagators-with-devi...

120 lines
3.4 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Event Propagator w/ device gravity</title>
<style>
html {
height: 100%;
}
body {
min-height: 100%;
position: relative;
margin: 0;
overscroll-behavior: none;
}
folk-shape {
background-color: black;
border-radius: 5px;
color: white;
padding: 0px 5px;
z-index: 10;
}
button {
margin: 1rem;
background-color: black;
border-radius: 5px;
color: white;
}
</style>
</head>
<body>
<button onclick="enableGravity()">Enable Gravity</button>
<p>Alpha: <span id="alpha">0</span></p>
<p>Beta: <span id="beta">0</span></p>
<p>Gamma: <span id="gamma">0</span></p>
<p>Gravity: <span id="gravity">0, 3000</span></p>
<folk-shape id="box1" x="100" y="100" width="30" height="30"></folk-shape>
<folk-shape id="box2" x="200" y="350">Hello World</folk-shape>
<folk-event-propagator
source="#box1"
target="#box2"
trigger="click"
expression="textContent: to.textContent + '!'"
></folk-event-propagator>
<!-- <folk-shape id="box3" x="350" y="200" width="30" height="30"></folk-shape>
<folk-shape id="box4" x="500" y="250" width="30" height="30"></folk-shape>
<folk-event-propagator
source="#box3"
target="#box4"
trigger="transform"
expression="y: from.x,
rotation: from.x"
></folk-event-propagator> -->
<script type="module">
import {requestAnimationFrame} from '@lib'
import '@labs/standalone/folk-shape.ts';
import '@labs/standalone/folk-event-propagator.ts';
document.addEventListener('touchmove', event => { event.preventDefault() }, { passive: false })
const ropes = Array.from(document.querySelectorAll('folk-event-propagator'));
let orientationEvent;
/**
*
* 0 -> 0, 3000
* 270 -> 3000, 0
*/
function tick() {
requestAnimationFrame(tick);
if (orientationEvent === undefined) return;
window.alpha.textContent = orientationEvent.alpha.toFixed(2);
window.beta.textContent = orientationEvent.beta.toFixed(2);
window.gamma.textContent = orientationEvent.gamma.toFixed(2);
// if (event.alpha) {
// // ropes.forEach(rope => { rope.gravity = { x: 0, y: 0} })
// }
}
tick();
function onDeviceOrientation(e) {
orientationEvent = e;
}
window.enableGravity = async function enableGravity() {
console.log('requesting')
if (DeviceOrientationEvent.requestPermission) {
try {
const permission = await DeviceOrientationEvent.requestPermission();
console.log('permission', permission)
if (permission === "granted") {
window.addEventListener("deviceorientation", onDeviceOrientation);
console.log('registered')
} else {
console.warn('Permission for device orientation rejected', permission)
}
} catch (e) {
console.error(e);
}
} else {
window.addEventListener("deviceorientation", onDeviceOrientation);
}
}
</script>
</body>
</html>