Skip to content

Trying to use React Native Canvas with Gesture Responder in Views #174

@joshiatharva

Description

@joshiatharva

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>
  );
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions