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.

how to draw local image on canvas using react-native-canvas

See original GitHub issue

Hi, Thank you for the package. I have followed instructions in the source and I am not able to draw image on canvas if image’s source is within a project.

here is the code:

import React from 'react';
import { StyleSheet} from 'react-native';
import Canvas, {Image as CanvasImage} from 'react-native-canvas';


export default class App extends React.Component {
  handleCanvas =  (canvas) => {
    const image = new CanvasImage(canvas);
    canvas.width = 100;
    canvas.height = 100;
    
    const context = canvas.getContext('2d');

    image.src = './dys.jpg';

    //'https://image.freepik.com/free-vector/unicorn-background-design_1324-79.jpg'; Note: with this uri everything works well

     image.addEventListener('load', () => {
      debugger
      console.log('image is loaded');
      context.drawImage(image, 0, 0, 100, 100);
    }); 
  }

  render() {
    return (
      <Canvas ref={this.handleCanvas}/>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

Is it possible to set image.src to local path?

Thank you once again. Regards, Zoran

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
shubhamkescommented, Dec 27, 2020

For those struggling to import PNGs in typescript

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

function resolveImage() {
    return require('../assets/image.png');
}
 const handleCanvas = (canvas) => {
     const image = new CanvasImage(canvas);
     const imageUri = RNimage.resolveAssetSource(resolveImage()).uri;
     image.src = imageUri;
     const context = canvas.getContext('2d');
      
     image.addEventListener('load', () => {
         context.drawImage(image, 0, 0)    
     })
 }

4reactions
salmenlinnacommented, Sep 14, 2020

Thanks for this. Without Expo, you can solve it using “resolveAssetSource” like this:

import {Image as RNimage} from 'react-native'
import Canvas, {Image} from 'react-native-canvas'
import imagePng from '../assets/image.png'

const imageUri = RNimage.resolveAssetSource(imagePng).uri 

and then:

image.src = imageUri
image.addEventListener('load', () => {
   ctx.drawImage(image, 0, 0)    
})

Note that resolveAssetSource is method from React Native Image, so you need to import is also, but with a different name.

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - local image won't load on canvas - Stack Overflow
Use require to load images and don't add listeners(like onload, onError etc) in render , do it in componentDidMount. Working demo is here....
Read more >
How to draw images on canvas with React? - Konva
How to draw images on canvas with React ? Navigate...
Read more >
How to Draw on Images in React Native - IMG.LY
In this article, we are shedding light on how to draw on images in React Native with the help of a simple HTML5...
Read more >
[Solved]-How to load Image on canvas in React?-Reactjs
React Native - How to load local image using the uri in Image Component? How to load Image on canvas in React? ......
Read more >
How to use the react-native-canvas.Image function in ... - Snyk
How to use the react-native-canvas.Image function in react-native-canvas · To help you get started, we've selected a few react-native-canvas examples, based on ...
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