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

image must be initialized with a canvas instance #133

Closed
sergey-kompaniets opened this issue Jun 14, 2019 · 12 comments
Closed

image must be initialized with a canvas instance #133

sergey-kompaniets opened this issue Jun 14, 2019 · 12 comments

Comments

@sergey-kompaniets
Copy link

sergey-kompaniets commented Jun 14, 2019

<View
        onStartShouldSetResponder={() => true}
        onResponderStart={this.mouseDownListener}
        onResponderMove={this.mouseMoveListener}
        onResponderRelease={this.mouseUpListener}
        style={{
          // flexGrow: 1,
          height: canvasHeight,
          alignItems: "center",
          marginTop: (3 * h) / 100
        }}
      >
        
        <View>
          <View
            style={{
              height: canvasHeight,
              width: bgWidth
              // backgroundColor: "green"
            }}
          >
            <Image
              source={bgImg}
              style={{
                // flexGrow: 1,
                width: null,
                height: "100%"
              }}
              resizeMode="contain"
            />
          </View>
          <View
            style={{
              height: canvasHeight,
              width: bgWidth,
              position: "absolute",
              alignItems: "center"
            }}
          >
            <Canvas
              ref={this.handleCanvas}
              style={
                {
                  // backgroundColor: "red"
                }
              }
            />
          </View>
        </View>
      </View>
@iddan iddan added the question label Jun 18, 2019
@iddan
Copy link
Owner

iddan commented Jun 18, 2019

Make sure to create CanvasImage like:

handleCanvas = canvas => {
    const image = new CanvasImage(canvas);
}

@sergey-kompaniets
Copy link
Author

sergey-kompaniets commented Jun 24, 2019

handleCanvas = canvas => {
    const { canvasHeight, canvasWidth, imgSrc } = this.props;

    const image = new CanvasImage(canvas);

    canvas.height = canvasHeight;
    canvas.width = canvasWidth;
    const ctx = canvas.getContext("2d");
    const rectX = 0;
    const rectY = 0;
    const rectWidth = canvasWidth;
    const rectHeight = canvasHeight;

    ctx.lineJoin = "round";

    image.src = `${RNFS.MainBundlePath}/assets/${imgSrc}`;

    image.addEventListener(
      "load",
      (drawImageCanvas = () => {
        ctx.drawImage(image, rectX, rectY, rectWidth, rectHeight);
      })
    );

    this.setState({ canvas: canvas });
  };

@sergey-kompaniets
Copy link
Author

Make sure to create CanvasImage like:

handleCanvas = canvas => {
    const image = new CanvasImage(canvas);
}

This error show me after Actions.pop() or replace

@sergey-kompaniets
Copy link
Author

sergey-kompaniets commented Jun 25, 2019

@iddan, please help! image.removeEventListener() not working

@iddan
Copy link
Owner

iddan commented Jun 26, 2019

removeEventListener() is not implemented currently

@sergey-kompaniets
Copy link
Author

sergey-kompaniets commented Jun 26, 2019

@iddan, how can I fix this problem?

@sergey-kompaniets
Copy link
Author

@iddan, please, I need help

@iddan
Copy link
Owner

iddan commented Jul 3, 2019

You can implement it if you'd like: take a look at how addEventListener() is implemented.

@neohuiji
Copy link

neohuiji commented Jul 27, 2019

Hi, @iddan, I alse have this problem. I got Error: Image must be initialized with a Canvas instance.
Here is my code :

import Canvas, { Image as CanvasImage } from 'react-native-canvas';
import RNFS from 'react-native-fs';


    const _handleCanvas = (canvas) => {
        const image = new CanvasImage(canvas);
        canvas.width = 500;
        canvas.height = 500;
        image.src = `${RNFS.MainBundlePath}/${viewerData[0].path}`;
        console.log('image src: ', image.src);
        // the log here is  `undefined/../../images/3.jpg`
        const context = canvas.getContext('2d');
        image.addEventListener('load', () => {
            context.drawImage(image, 0, 0, 500, 500);
        });
    };

    return ( 
      <View>
            <Canvas ref={_handleCanvas} />
      </View>
)

@iddan
Copy link
Owner

iddan commented Jul 28, 2019

Needs investigation. Currently, I'm not available.

@kaidoj
Copy link

kaidoj commented Feb 2, 2020

Canvas is not initialized when calling new CanvasImage. Something to do with render method of react-native. Seems I solved the problem with little workaround. Checking if canvas is set before CanvasImage does its own check.

handleCanvas = (canvas) => {
    if (!(canvas instanceof Canvas)) {
      return;
    }

    const image = new CanvasImage(canvas);

@iddan
Copy link
Owner

iddan commented Feb 3, 2020

Oh right, refs in React can be null. Make sure to check that before using a canvas instance!

@iddan iddan closed this as completed Feb 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants