jonigata commited on
Commit
c8da9c0
·
1 Parent(s): 38122ef

ignore key and wheel event when cursor is not on the canvas

Browse files
Files changed (1) hide show
  1. static/poseEditor.js +13 -0
static/poseEditor.js CHANGED
@@ -377,6 +377,13 @@ function getCanvasPosition(event) {
377
  return [x, y];
378
  }
379
 
 
 
 
 
 
 
 
380
  function forEachMarkedNodes(fn) {
381
  for (let i = 0; i < dragMarks.length; i++) {
382
  for (let j = 0; j < dragMarks[i].length; j++) {
@@ -505,6 +512,8 @@ function handleMouseUp(event) {
505
  function ModifyDragRange(delta) { dragRange = Math.max(dragRangeDelta, Math.min(512, dragRange + delta)); }
506
 
507
  document.addEventListener('wheel', function(event) {
 
 
508
  event.preventDefault();
509
  const deltaY = event.deltaY;
510
  if (deltaY < 0) {ModifyDragRange(-dragRangeDelta);}
@@ -515,6 +524,8 @@ document.addEventListener('wheel', function(event) {
515
  }, {passive: false});
516
 
517
  document.addEventListener("keydown", (event) => {
 
 
518
  if (event.code == "BracketLeft") { ModifyDragRange(-dragRangeDelta); }
519
  if (event.code == "BracketRight") { ModifyDragRange(dragRangeDelta); }
520
  keyDownFlags[event.code] = true;
@@ -522,6 +533,8 @@ document.addEventListener("keydown", (event) => {
522
  event.preventDefault();
523
  });
524
  document.addEventListener("keyup", (event) => {
 
 
525
  keyDownFlags[event.code] = false;
526
  if (event.ctrlKey && event.code == "KeyE") {
527
  addPerson();
 
377
  return [x, y];
378
  }
379
 
380
+ function isMouseOnCanvas(event) {
381
+ const rect = canvas.getBoundingClientRect();
382
+ const x = event.clientX - rect.left;
383
+ const y = event.clientY - rect.top;
384
+ return 0 <= x && x <= canvas.width && 0 <= y && y <= canvas.height;
385
+ }
386
+
387
  function forEachMarkedNodes(fn) {
388
  for (let i = 0; i < dragMarks.length; i++) {
389
  for (let j = 0; j < dragMarks[i].length; j++) {
 
512
  function ModifyDragRange(delta) { dragRange = Math.max(dragRangeDelta, Math.min(512, dragRange + delta)); }
513
 
514
  document.addEventListener('wheel', function(event) {
515
+ if (!isMouseOnCanvas(event)) {return;}
516
+
517
  event.preventDefault();
518
  const deltaY = event.deltaY;
519
  if (deltaY < 0) {ModifyDragRange(-dragRangeDelta);}
 
524
  }, {passive: false});
525
 
526
  document.addEventListener("keydown", (event) => {
527
+ if (!isMouseOnCanvas(event)) {return;}
528
+
529
  if (event.code == "BracketLeft") { ModifyDragRange(-dragRangeDelta); }
530
  if (event.code == "BracketRight") { ModifyDragRange(dragRangeDelta); }
531
  keyDownFlags[event.code] = true;
 
533
  event.preventDefault();
534
  });
535
  document.addEventListener("keyup", (event) => {
536
+ if (!isMouseOnCanvas(event)) {return;}
537
+
538
  keyDownFlags[event.code] = false;
539
  if (event.ctrlKey && event.code == "KeyE") {
540
  addPerson();