-
Notifications
You must be signed in to change notification settings - Fork 170
Closed
Labels
Description
Hi all,
I'm trying to use React Native Canvas to build a Canvas tool for drawing DFAs. I'm trying
to create this from the Javascript version (excerpt below).
var canvas = document.getElementById('canvas');
var touchX, touchY;
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
}
function drawCircle(ctx, x, y, size) {
ctx.beginPath();
ctx.arc(x, y, size, 0, Math.PI*2, false);
ctx.stroke();
}
However, in React Native the ctx variable only exists within the handleCanvas() method in the example given in the repo, whereas I want to use GestureResponder to execute drawCircle() on any touch on the . How can I store/handle the value of ctx to be able to draw outside handleCanvas(), so I can execute drawCircle() on any touch on the view. The RN code is given below:
handleCanvas = (canvas) => {
var ctx = canvas.getContext("2d");
}
drawCircle = (evt) => {
ctx.beginPath();
ctx.arc(evt.pageX, evt.pageY, 12, 0, Math.PI*2, true);
ctx.stroke();
}
render() {
return (
<View onStartShouldSetResponder={(evt) => true} onResponderRelease=
{this.drawCircle}>
<Canvas ref={this.handleCanvas} />
</View>
);
}