question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

image must be initialized with a canvas instance

See original GitHub issue
<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>

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
kaidojcommented, 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);
2reactions
neohuijicommented, Jul 28, 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>
)

Read more comments on GitHub >

github_iconTop Results From Across the Web

'react-native-canvas' return `Error: Image must be initialized ...
I read some issues on github, but It does not work for me. Here's my code : import Canvas, { Image as CanvasImage...
Read more >
Image must be initialized with a Canvas instance`-React Native
Coding example for the question 'react-native-canvas' return `Error: Image must be initialized with a Canvas instance`-React Native.
Read more >
Image Manipulation with Fabric.js and Canvas - Pieces.app
As soon as the canvas is initialized, you can select objects, drag them around, scale them, rotate them, and group them in order...
Read more >
ImageData() - Web APIs - MDN Web Docs
An unsigned long representing the width of the image. ... Initializing ImageData with an array ... const canvas = document.
Read more >
React Native with Canvas rendering - Questions - Babylon.js
I've been working on code to render panorama images in React Native. ... the camera's rotation quaternion is initialized correctly. camera.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found