Closed ps2-controller closed 4 years ago
Figured it out :) accessing context.handleMouseDown
and context.handlePointerMove
gets the job mostly done for me.
Hey @ps2-controller - can you give some more context on what you did to make this work? I'm trying to access the default onDoubleClick
prop of the canvas that you can otherwise access by baking a vanilla canvas tag into a React component. Something like:
<CanvasDraw
ref={(canvasDraw) => (this.saveableCanvas = canvasDraw)}
brushColor={strokeStyle}
brushRadius={8}
imgSrc={path}
lazyRadius={10}
canvasWidth={parseInt(width, 10)}
canvasHeight={parseInt(height, 10)}
disabled={disabled}
onDoubleClick={this.onDoubleClick}
/>
Any idea how I'd go about setting that up so that I can access that method? Note that it works when I pass that onDoubleClick
prop directly to a
Hmm, not sure - My workaround actually involves calling react-canvas-draw's own implemented wrapper functions such as this one
I call that as follows:
const callUnderlyingMethod = () => {
let context = canvasRef.current
// literally empty, as react-canvas-draw's function requires a preventDefault fxn as a param
let someFunction = () => { }
context.handleMouseDown({preventDefault: someFunction})
}
return (
<CanvasDraw
ref={canvasRef}
brushRadius={brushRadius}
brushColor={brushColor}
/>
)
This works for my use-case since I'm using websockets to draw an image based on events instead of the user's manual mouse clicks, so I'm using the same functions react-canvas-draw already exposes. (You can check it out at https://demondoodle.com, but the source code isn't public yet)
However, it looks like you want to go a layer lower and call a directly unexposed method on the canvas itself. I'm not sure how that works with this library. It's especially complicated because react-canvas-draw seems to be using four html canvases under the hood. One slightly tacky option might be wrapping your
<span onDoubleClick={() => this.onDoubleClick()} >
<CanvasDraw
ref={(canvasDraw) => (this.saveableCanvas = canvasDraw)}
brushColor={strokeStyle}
brushRadius={8}
imgSrc={path}
lazyRadius={10}
canvasWidth={parseInt(width, 10)}
canvasHeight={parseInt(height, 10)}
disabled={disabled}
onDoubleClick={this.onDoubleClick}
/>
</span>
Otherwise, not sure :( Hope that's helpful!
That is helpful- thanks a bunch! Ended up wrapping the CanvasDraw component in a div that takes my onDoubleClick custom event. Seems to be working alright.
Awesome, glad to hear it :)
Hi, thanks so much for making this library! I was wondering - is there a way to access methods such as
beginDraw()
on the underlying canvas context? Ideally I'd like to do something like thisreference: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/beginPath