Coder Social home page Coder Social logo

Comments (10)

salmenlinna avatar salmenlinna commented on August 16, 2024 6

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.

from react-native-canvas.

shubhamkes avatar shubhamkes commented on August 16, 2024 6

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)    
     })
 }

from react-native-canvas.

zokicpokic avatar zokicpokic commented on August 16, 2024 5

Hi @iddan , thank you for asking.

I was able to solve this by

  const asset = Expo.Asset.fromModule(require('./images/bckg.jpg'));
  image.src = asset.uri ;

Then:
image.addEventListener('load', function() {
context.drawImage(image, 0, 0, canvas.width, canvas.height);
}

I am using create react native app and Expo, linking FS appeared not easy going approach since linking and Expo are not much compatible.

But I will keep using react-native-canvas since it is most straightforward and achievable for 2D rendering.
However, now I am wondering should I have to include load listener for each image I want to drive on let's say one scene, or there is another way to obtain image ref. and draw it directly on canvas.

Thank you once again.
Regards,
Zoran

from react-native-canvas.

zokicpokic avatar zokicpokic commented on August 16, 2024 1

Thank you iddan.
I will try as you have suggested.

Sincerely,
Zoran

from react-native-canvas.

shov avatar shov commented on August 16, 2024 1

@zokicpokic solution works just fine, thanks!

import {Asset} from 'expo-asset'
import {Image as CanvasImage} from 'react-native-canvas'

async loadImage(localPath: string): Promise<CanvasImage> {
  const i = new CanvasImage(canvas)
  const asset = Asset.fromModule(require(localPath))
  i.src = asset.uri
  
  return await new Promise(r => {
      i.addEventListener('load', () => {
          r(i)
      })
  })
}

from react-native-canvas.

iddan avatar iddan commented on August 16, 2024

I would use React Native FS:

import RNFS from 'react-native-fs';
const imageSrc = `${RNFS.MainBundlePath}/${FILE_PATH}`

from react-native-canvas.

iddan avatar iddan commented on August 16, 2024

Hey @zokicpokic, were you able to solve this problem?

from react-native-canvas.

iddan avatar iddan commented on August 16, 2024

I think once an image is loaded you can utilise it in the same canvas.

from react-native-canvas.

ShaoGongBra avatar ShaoGongBra commented on August 16, 2024

RNimage.resolveAssetSource It doesn't work on the release

from react-native-canvas.

ShaoGongBra avatar ShaoGongBra commented on August 16, 2024

${RNFS.MainBundlePath} Output undefined on Android

from react-native-canvas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.