Comments (10)
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.
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.
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.
Thank you iddan.
I will try as you have suggested.
Sincerely,
Zoran
from react-native-canvas.
@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.
I would use React Native FS:
import RNFS from 'react-native-fs';
const imageSrc = `${RNFS.MainBundlePath}/${FILE_PATH}`
from react-native-canvas.
Hey @zokicpokic, were you able to solve this problem?
from react-native-canvas.
I think once an image is loaded you can utilise it in the same canvas.
from react-native-canvas.
RNimage.resolveAssetSource It doesn't work on the release
from react-native-canvas.
${RNFS.MainBundlePath} Output undefined on Android
from react-native-canvas.
Related Issues (20)
- getcontext of a canvas gives TypeError: canvas.getContext is not a function HOT 2
- Difference in drawImage coordinates and getImageData/putImageData coordinates HOT 2
- why Why did I use getImageData() for more than 1 minute? HOT 2
- Canvas remain black on windows
- How do you get fillRect to occupy full dimensions? HOT 4
- ImageData/getImageData/putImageData strange behaviour HOT 2
- error HOT 2
- New architecture support for fabric HOT 1
- require cycle node_modules\react-native-canvas HOT 1
- canvas.loadFromJSON missing in react-native-canvas
- Error: Unable to resolve module ./CanvasRenderingContext2D HOT 1
- Not able to add fontFamily with font
- Application gets hang after 10 mins due to drawing on canvas continuously
- Can't draw image greater than 2048x2048 HOT 1
- Virtual/In-Memory Canvas HOT 1
- Can't draw Image on release HOT 1
- Doesn't work at all with Expo HOT 5
- Does not work with Expo 50 HOT 1
- `clearRect()` crashes in Expo 49 HOT 1
- does not work in react-native-windows
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-canvas.