Giter Club home page Giter Club logo

Comments (8)

markbiddlecom avatar markbiddlecom commented on May 17, 2024 1

I needed this too, so I built it! #87

from react-canvas-draw.

petedejoy avatar petedejoy commented on May 17, 2024 1

@markbiddlecom the hero we don't deserve! Thanks a ton- will definitely be checking out your PR and giving it a shot as soon as I have a chance.

from react-canvas-draw.

martin056 avatar martin056 commented on May 17, 2024

I needed this as well. Manged to do it by monkey patching the getPointerPos method. It doesn't look like a long-term solution so I'd be really happy to have it as an exposed API 👍

from react-canvas-draw.

petedejoy avatar petedejoy commented on May 17, 2024

Mind sharing a bit more about how you rigged it up?

from react-canvas-draw.

martin056 avatar martin056 commented on May 17, 2024

@petedejoy It's a combination with the react-zoom-pan-pinch package.

I basically change/monkey patch the getPointerPos method when I store the canvas object ref:

  patchCanvas = (canvasObj, scale) => {
    if (canvasObj) {
      this.canvasObj = canvasObj;

      // TODO: This is copy-pasted from the package. The only change is we divide the result by the scale from the zoom.
      this.canvasObj.getPointerPos = e => {
        const rect = this.canvasObj.canvas.interface.getBoundingClientRect();

        // use cursor pos as default
        let clientX = e.clientX;
        let clientY = e.clientY;

        // use first touch if available
        if (e.changedTouches && e.changedTouches.length > 0) {
          clientX = e.changedTouches[0].clientX;
          clientY = e.changedTouches[0].clientY;
        }

        // return mouse/touch position inside canvas
        return {
          x: (clientX - rect.left) / scale,
          y: (clientY - rect.top) / scale
        };
      };
    }
  };

// ...
// in render
// ..

<TransformComponent>
  <CanvasDraw ref={canvasObj => this.patchCanvas(canvasObj, scale)} />
</TransformComponent>

If the CanvasDraw expects such prop, this can be safely added to the real getPointerPos method I think. Do you think PR with such a thing is okay?

from react-canvas-draw.

petedejoy avatar petedejoy commented on May 17, 2024

Ahh, that's interesting! I'll try to follow along and implement a similar function when I have a chance. I'm not sure how much of a lift it'd be to PR your patchCanvas()as a default prop of some kind, but it's definitely something I would put to use if that's of any influence.

from react-canvas-draw.

elimauskopf avatar elimauskopf commented on May 17, 2024

Hey @martin056, how did you access the scale prop from Transform wrapper? I'm just not sure how scale is defined your example

from react-canvas-draw.

embiem avatar embiem commented on May 17, 2024

The hero we don't deserve indeed. Merged his PR finally (sorry for the delays), so this feature will be part of v1.2.0

from react-canvas-draw.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.