how to draw local image on canvas using react-native-canvas
See original GitHub issueHi, 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:
- Created 5 years ago
- Reactions:3
- Comments:10 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For those struggling to import PNGs in typescript
Thanks for this. Without Expo, you can solve it using “resolveAssetSource” like this:
and then:
Note that resolveAssetSource is method from React Native Image, so you need to import is also, but with a different name.