Skip to content

Instantly share code, notes, and snippets.

@jarek-foksa
Last active May 25, 2019 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarek-foksa/fd67b58472492f722576bdbfa62e68b0 to your computer and use it in GitHub Desktop.
Save jarek-foksa/fd67b58472492f722576bdbfa62e68b0 to your computer and use it in GitHub Desktop.
canvas issue
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="node_modules/xel/themes/material.css">
<script src="node_modules/xel/xel.min.js"></script>
<style>
body, html{
height: 100%;
width: 100%;
}
</style>
<title>Test</title>
</head>
<body>
<x-menu id="context-menu">
<x-menuitem value="undo">
<x-label>Undo</x-label>
</x-menuitem>
<x-menuitem value="redo">
<x-label>Redo</x-label>
</x-menuitem>
</x-menu>
<canvas id="canvas"></canvas>
<script>
let canvas = document.querySelector("#canvas");
let contextMenu = document.querySelector("#context-menu");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
contextMenu.openAtPoint(event.clientX, event.clientY);
});
canvas.addEventListener("click", () => {
if (contextMenu.opened) {
contextMenu.close();
}
});
contextMenu.addEventListener("click", async (event) => {
let item = event.target.closest("x-menuitem");
if (item) {
console.log("Clicked menu item: ", item.value);
await item.whenTriggerEnd;
contextMenu.close();
}
});
canvas.addEventListener("pointermove", () => {
console.log("Moved pointer");
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment