Comments (8)
I needed this too, so I built it! #87
from react-canvas-draw.
@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.
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.
Mind sharing a bit more about how you rigged it up?
from react-canvas-draw.
@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.
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.
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.
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)
- Insert label
- it seems it does not supports react 17+ versions? HOT 2
- background image while fetching from AWS S3 in google chrome does not work from localhost HOT 1
- Support for pressure considered? HOT 4
- The "ref" issue HOT 1
- Cannot read properties of undefined (reading 'getPointerCoordinates') HOT 2
- [Feature Request] Resize pointer on zoom
- getDataURL parameters useBgImage doesn't seems to work HOT 11
- Scrolling while drawing throws an error
- Accidental duplicate issue
- Cannot read properties of undefined (reading 'getPointerCoordinates') when setting a small canvasWidth HOT 3
- Invariant Violation: View config getter callback for component `canvas` must be a function (received `undefined`). Make sure to start component names with a capital letter.
- ... HOT 1
- Support for React 18 & typescript HOT 3
- Property 'getDataURL' does not exist on type 'CanvasDraw' HOT 1
- Feature request: callbacks for common events
- Animation events continue after loading new data
- Postion canvas always static
- Uncaught TypeError: Cannot read property 'getPointerCoordinates' of undefined. HOT 1
- apply clear reactangle
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-canvas-draw.