您好,我用react构建了一个绘图应用程序,我希望在用户从画布上移除鼠标时收到用户绘制的内容.我希望它总是重复,当用户停止绘图时.你建议我做什么来解决这个问题.谢谢你的帮助

import { useEffect, useRef, useState } from "react";
import Menu from "./Menu";
import "./App.css";
import { useStateContext } from '../../contexts/ContextProvider';



const DrawingSEC = () => {

    const canvasRef = useRef(null);
    const ctxRef = useRef(null);
    const [isDrawing, setIsDrawing] = useState(false);
    const [lineWidth, setLineWidth] = useState(2);
    const [lineColor, setLineColor] = useState("black");
    const {screenSize,
      setScreenSize} = useStateContext();
  
    useEffect(() => {
      const handleResize = () => setScreenSize
        (window.innerWidth);
      window.addEventListener('resize', handleResize);
  
      handleResize();
      return () => window.removeEventListener
        ('resize', handleResize);
    });
    
    // Initialization when the component
    // mounts for the first time
    useEffect(() => {
      const canvas = canvasRef.current;
      const ctx = canvas.getContext("2d");
      ctx.lineCap = "round";
      ctx.lineJoin = "round";
      ctx.strokeStyle = lineColor;
      ctx.lineWidth = lineWidth;
      ctxRef.current = ctx;
    }, [lineColor,  lineWidth]);
    
    // Function for starting the drawing
    const startDrawing = (e) => {
      ctxRef.current.beginPath();
      ctxRef.current.moveTo(
        e.nativeEvent.offsetX, 
        e.nativeEvent.offsetY
      );
      setIsDrawing(true);
    };
    
    // Function for ending the drawing
    const endDrawing = () => {
      ctxRef.current.closePath();
      
      setIsDrawing(false);
    };
    
    const draw = (e) => {
      if (!isDrawing) {
        return;
      }
      ctxRef.current.lineTo(
        e.nativeEvent.offsetX, 
        e.nativeEvent.offsetY
      );
        
      ctxRef.current.stroke();
    };
  return (
    <div className="App">
      <div className="draw-area">
        <Menu
          setLineColor={setLineColor}
          setLineWidth={setLineWidth}
          lineWidth={lineWidth}

        />
        <canvas
          onMouseDown={startDrawing}
          onMouseUp={endDrawing}
          onMouseMove={draw}
          ref={canvasRef}
          width={screenSize-300}
          height={`720px`}
        />
      </div>
    </div>
  )
}

export default DrawingSEC


菜单为DrawSec:

import React from "react";
import "./App.css";

const Menu = ({ setLineColor, setLineWidth,lineWidth }) => {
return (
    <div className="Menu">
    <label>color  </label>
    <input
        type="color"
        onChange={(e) => {
        setLineColor(e.target.value);
        }}
        style={{borderRadius:"50%"}}
    />
    <label>linewidth </label>
    <input
        type="range"
        min="3"
        max="20"
        value={lineWidth}
        onChange={(e) => {
        setLineWidth(e.target.value);
        }}
    />
    
    </div>
);
};

export default Menu;


应用程序.css:

@import url("https://fonts.googleapis.com/css2?family=Lobster&display=swap");

.App {
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  background-image: linear-gradient(120deg, #fdfbfb 0%, #ebedee 100%);
}

h1 {
  font-family: "Lobster", cursive;
  font-size: 50px;
  color: #4644f0;
}

.draw-area {
  height: 720px;
  border: 2px solid #808080;
  position: relative;
  background-color: white;
}
.draw-area canvas {
  /* width: 100%; */
}

.Menu {
  width: 650px;
  height: 50px;
  display: flex;
  justify-content: space-evenly;
  border-radius: 5px;
  align-items: center;
  background-color: #a3a3a32d;
  margin: auto;
  margin-top: 10px;
  /* width: 100%; */
  /* display: flex; */
  /* flex-wrap: wrap; */
}

推荐答案

应将此行添加到endDrawing Func中:

      console.log(canvasRef.current.toDataURL());


这将在控制台中为您提供一个链接,您可以将其保存在另一个变量中.

Javascript相关问答推荐

node TS:JWT令牌签名以验证客户端和后台问题之间的身份验证

为什么我的includes声明需要整个字符串?

我可以使用CSS有效地实现最大宽度=100%和最大高度=100%,而无需父母有明确的/固定的宽度和高度,替代方法吗?

单击子元素时关闭父元素(JS)

Google图表时间轴—更改hAxis文本 colored颜色

JQuery. show()工作,但. hide()不工作

使搜索栏更改语言

在react js中使用react—router—dom中的Link组件,分配的右侧不能被 destruct ''

当运行d3示例代码时,没有显示任何内容

空的结果抓取网站与Fetch和Cheerio

在我的html表单中的用户输入没有被传送到我的google表单中

如何避免页面第一次加载时由于CSS样式通过JavaScript更改而出现闪烁

我创建了一个创建对象的函数,我希望从该函数创建的对象具有唯一的键.我怎么能做到这一点?

如果Arrow函数返回函数,而不是为useEffect返回NULL,则会出现错误

如何创建返回不带`new`关键字的实例的类

Webpack在导入前混淆文件名

为什么客户端没有收到来自服务器的响应消息?

Promise.race()返回已解析的promise ,而不是第一个被拒绝的promise

无限循环,因为变量不断被重新声明为其原始值

如何在Angular中分离桌面和移动页面并延迟加载每个页面?