React Native 中的洪水填充图像(油漆桶工具,为图像着色)

问题描述 投票:0回答:1

是否有库或其他可以操作图像的本地反应库?我正在寻找可以用于在本机反应中对图像进行洪水填充的东西。

我想制作一个像那些那样的着色应用程序,并且我现在想要一个油漆桶工具和画笔。

着色应用程序1

对于可以在图像上使用画笔和油漆桶工具(洪水填充)的库/模块有什么想法吗?文档和教程会很棒。

react-native paint npm-install flood-fill
1个回答
0
投票

您可以尝试结合使用 q-floodfillreact-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;

© www.soinside.com 2019 - 2024. All rights reserved.