0

I'd like to allow zooming on a canvas (excalidraw API) using only the mouse wheel like on google map , without pressing the ctrl key.

Here are two things that I tried :

useEffect(() => {
    const handleWheel = (event) => {
      if (!ctrlSimulate) return;
      event.preventDefault();
      event.stopPropagation();
      const wheelEvent = new WheelEvent("wheel", {
        ...event,
        deltaY:event.deltaY,
        ctrlKey: true
      });
      console.log(event.target)
      event.target.dispatchEvent(wheelEvent);
    };
    if (ctrlSimulate) {
      window.addEventListener("wheel", handleWheel);
    } else {
      window.removeEventListener("wheel", handleWheel);
    }
    return () => {
      window.removeEventListener("wheel", handleWheel);
    };
  }, [ctrlSimulate]);

or


  useEffect(() => {
    const handleWheel = (event) => {
      if (!ctrlSimulate) return;
      event.preventDefault();
      event.stopPropagation();
      const wheelEvent = new WheelEvent("wheel", {
        ...event,
        deltaY:event.deltaY,
        ctrlKey: true
      });
      console.log(event.target)
      event.target.dispatchEvent(wheelEvent);
    };
    const el = document.querySelector("div")
    el.onwheel=handleWheel;
    
  }, [ctrlSimulate]);

It's zooming but not exactly on my cursor which is very frustrating because I think it should Any help is appreciated !

0

2 Answers 2

0

I'm pretty sure it's because the default event of the wheel is happening even though I have a preventDefault

Sign up to request clarification or add additional context in comments.

1 Comment

This is not an answer, rather a comment, or an addition to the question.
0

Ok done by adding capture: true to the event listener

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.