Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom postMessage crashes react-native-canvas #66

Closed
peacechen opened this issue Jul 5, 2018 · 3 comments
Closed

custom postMessage crashes react-native-canvas #66

peacechen opened this issue Jul 5, 2018 · 3 comments
Labels

Comments

@peacechen
Copy link

I'm porting a mini-game which uses canvas. This requires user interaction, so I injected a touch handler that posts a message to pass the event back to the RN side.

    this.onkeypress = 'window.addEventListener("touchstart", function (e) { window.postMessage("touchstart") } );';
    canvas.webview.injectJavaScript(this.onkeypress);

However, handleMessage() in Canvas.js tries to parse this as JSON which crashes.

    var data = JSON.parse(e.nativeEvent.data);

I tried passing in serialized JSON, but the rest of the code needs id, meta, and payload fields in a specific way which the app has no visibility into. I got it to work by wrapping the JSON.parse in a try-catch block, and if it catches, pass data directly to listeners and skip the rest including the bus call.

Would you be interested in a PR? Or is there a better way to accomplish this?

@iddan iddan added the question label Jul 7, 2018
@iddan
Copy link
Owner

iddan commented Jul 7, 2018

Hey! Thanks for giving React Native Canvas a try.
You shouldn't count on <Canvas /> being a WebView as it might change in the future. You should manage all user interaction events on the RN side. To do so wrap the <Canvas /> element with a <TouchableWithoutFeedback /> to get touch events.
Let me know if that solves your problem.

@iddan iddan closed this as completed Jul 8, 2018
@peacechen
Copy link
Author

I did try wrapping it in TouchableWithoutFeedback but no touch events would fire. I didn't dig down into exactly what was going on, but I'm guessing the WebView is swallowing the events. Strange, but that's what I observed.

@iddan
Copy link
Owner

iddan commented Jul 10, 2018

Just wrap the <Canvas /> component with a <View /> that is with the same dimensions as it is and that should fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants