简体   繁体   中英

React Native canvas in WebView

I'm trying to code a "paint" Android app with React Native 0.17.

I have a Canvas component (strongly inspired by react-native-canvas ):

import React from 'react-native';
import Paint from '../utils/paint'

var {
  View,
  WebView
} = React;

const Canvas = React.createClass({

  render() {
    return (
      <WebView
        html={Paint}
        style={this.props.style}
        javaScriptEnabledAndroid={true}
      />
    );
  },

});

export default Canvas;

and a Paint component that I use in the html props of the WebView in the Canvas component above.

This component is nothing more than a very long string containing HTML and JS that come from this tuto: creating-a-paint-application-with-html5-canvas

export default `<html>
  <head></head>
  <body>
    <h1 id='toto'>TATA</h1>

    <br />

    <div id="sketch">
      <canvas id="paint" style='border: 1px solid black'></canvas>
    </div>

    <script>
      (function() {
        var canvas = document.querySelector('#paint');
        var ctx = canvas.getContext('2d');

        var sketch = document.querySelector('#sketch');
        var sketch_style = getComputedStyle(sketch);
        canvas.width = parseInt(sketch_style.getPropertyValue('width'));
        canvas.height = parseInt(sketch_style.getPropertyValue('height'));

        var mouse = {x: 0, y: 0};

        var start_events = ["mousedown", "touchstart"];
        var move_events = ["mousemove", "touchmove"];
        var end_events = ["mouseup", "touchend"];

        /* Mouse Capturing Work */
        move_events.forEach(function(event) {
          canvas.addEventListener(event, function(e) {
            mouse.x = e.pageX - this.offsetLeft;
            mouse.y = e.pageY - this.offsetTop;
          }, false);
        });

        /* Drawing on Paint App */
        ctx.lineWidth = 5;
        ctx.lineJoin = 'round';
        ctx.lineCap = 'round';
        ctx.strokeStyle = 'back';

        start_events.forEach(function(event) {
          canvas.addEventListener(event, function(e) {
            ctx.beginPath();
            ctx.moveTo(mouse.x, mouse.y);

            move_events.forEach(function(me){ canvas.addEventListener(me, onPaint, false); });
          }, false);
        });


        end_events.forEach(function(event) {
          canvas.addEventListener(event, function(e) {
            move_events.forEach(function(me){ canvas.removeEventListener(me, onPaint, false); });
          }, false);
        });


        var onPaint = function() {
          ctx.lineTo(mouse.x, mouse.y);
          ctx.stroke();
        };

      }());
    </script>
  </body>
</html>`;

My HTML/JS works well in a brower (Chrome) but unfortunately I'm not able to draw anything in my React Native app.

Can someone help me? Which events should I listen in React Native?

ES6 template string seems to break when you comment some lines of JS in your <script> tag.

Try remove them:

export default `<html>
  <head></head>
  <body>
    <h1 id='toto'>TATA</h1>

    <br />

    <div id="sketch">
      <canvas id="paint" style='border: 1px solid black'></canvas>
    </div>

    <script>
      (function() {
        var canvas = document.querySelector('#paint');
        var ctx = canvas.getContext('2d');

        var sketch = document.querySelector('#sketch');
        var sketch_style = getComputedStyle(sketch);
        canvas.width = parseInt(sketch_style.getPropertyValue('width'));
        canvas.height = parseInt(sketch_style.getPropertyValue('height'));

        var mouse = {x: 0, y: 0};

        var start_events = ["mousedown", "touchstart"];
        var move_events = ["mousemove", "touchmove"];
        var end_events = ["mouseup", "touchend"];

        move_events.forEach(function(event) {
          canvas.addEventListener(event, function(e) {
            mouse.x = e.pageX - this.offsetLeft;
            mouse.y = e.pageY - this.offsetTop;
          }, false);
        });

        ctx.lineWidth = 5;
        ctx.lineJoin = 'round';
        ctx.lineCap = 'round';
        ctx.strokeStyle = 'back';

        start_events.forEach(function(event) {
          canvas.addEventListener(event, function(e) {
            ctx.beginPath();
            ctx.moveTo(mouse.x, mouse.y);

            move_events.forEach(function(me){ canvas.addEventListener(me, onPaint, false); });
          }, false);
        });


        end_events.forEach(function(event) {
          canvas.addEventListener(event, function(e) {
            move_events.forEach(function(me){ canvas.removeEventListener(me, onPaint, false); });
          }, false);
        });


        var onPaint = function() {
          ctx.lineTo(mouse.x, mouse.y);
          ctx.stroke();
        };

      }());
    </script>
  </body>
</html>`;

react-native-canvas current maintainer here, to people coming here looking for a canvas solution for React Native: I want to let you know react-native-canvas today is a stable solution which is very different from the code injection hack it used to be.

@flyskywhy/react-native-gcanvas is a C++ native canvas component based on gpu opengl glsl shader, runs well on Android and iOS. You can see Performance Test Result , and an easily <canvas/> ported example react-native-particles-bg runs on Android, iOS and Web.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM