Reputation: 116
Is there a library or something for react native that can manipulate images? I am searching for something which I can use for a flood fill on image in react native.
I want to make a coloring application like those ones and I want to have a paint bucket tool and a brush for now.
Any idea for a library/module that I can use for a brush and a paint bucket tool(flood fill) on an image? Documentation and tutorials would be amazing.
Upvotes: 1
Views: 1717
Reputation: 21
You can try use a combine of q-floodfill and react-native-canvas
import React, { useRef, useState } from 'react';
import { View, StyleSheet, TouchableWithoutFeedback } from 'react-native';
import { Canvas, Image as CanvasImage } from 'react-native-canvas';
import floodFill from 'q-floodfill';
const ShapeColoring = ({ imageUrl }) => {
const canvasRef = useRef(null);
const [canvas, setCanvas] = useState(null);
const [ctx, setCtx] = useState(null);
const handleCanvas = async (canvas) => {
if (canvas) {
const context = canvas.getContext('2d');
setCanvas(canvas);
setCtx(context);
const image = new CanvasImage(canvas);
image.src = imageUrl;
image.addEventListener('load', () => {
canvas.width = image.width;
canvas.height = image.height;
context.drawImage(image, 0, 0);
});
}
};
const handleTouch = (evt) => {
const { locationX, locationY } = evt.nativeEvent;
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const color = { r: 255, g: 0, b: 0, a: 255 }; // Color to fill with
// Perform the flood fill
floodFill(imageData, locationX, locationY, color);
// Update the canvas with the new image data
ctx.putImageData(imageData, 0, 0);
};
return (
<View style={styles.container}>
<Canvas ref={handleCanvas} style={styles.canvas} />
<TouchableWithoutFeedback onPress={handleTouch}>
<View style={styles.touchableArea} />
</TouchableWithoutFeedback>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
canvas: {
width: '100%',
height: '100%',
position: 'absolute',
},
touchableArea: {
width: '100%',
height: '100%',
position: 'absolute',
},
});
export default ShapeColoring;
Upvotes: 0