folk-canvas/demo/html-ink.html

74 lines
1.8 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ink</title>
<style>
html {
height: 100%;
}
body {
min-height: 100%;
position: relative;
margin: 0;
}
button {
margin: 0.5rem;
}
</style>
</head>
<body>
<button on:click="DRAW">Draw</button>
<script type="module">
import '../src/standalone/folk-shape.ts';
import '../src/standalone/folk-ink.ts';
const drawButton = document.querySelector('button');
async function draw(e) {
if (e.target === drawButton) return;
// Stop the default focus and pointer capture of geometry elements
e.preventDefault();
e.stopPropagation();
const ink = document.createElement('folk-ink');
document.body.appendChild(ink);
await ink.draw(e);
const rect = ink.getPathBox();
ink.points = ink.points.map(([x, y, p]) => [x - rect.x, y - rect.y, p]);
const geometry = document.createElement('folk-shape');
geometry.x = rect.x;
geometry.y = rect.y;
geometry.height = rect.height;
geometry.width = rect.width;
geometry.appendChild(ink);
document.body.appendChild(geometry);
}
let isDrawing = false;
document.addEventListener('click', (e) => {
if (e.target !== drawButton) return;
isDrawing = !isDrawing;
if (isDrawing) {
document.addEventListener('pointerdown', draw, { capture: true });
drawButton.textContent = 'Drawing';
} else {
document.removeEventListener('pointerdown', draw, { capture: true });
drawButton.textContent = 'Draw';
}
});
</script>
</body>
</html>