原生js实现一个画板自由绘画效果代码

代码语言:html

所属分类:其他

代码描述:原生js实现一个画板自由绘画效果代码,可选择画笔、颜色、可撤销、重做、下载成图片。

代码标签: 原生 js 画板 自由 绘画 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, maximum-scale=.5" />
    <style>
        * {
          margin: 0;
        }
        body {
          display: flex;
          align-items: center;
          justify-content: center;
          height: 100vh;
        }
        #canvas-container {
          width: 100%;
          height: 100vh;
        }
        
        .dt-control-container {
          position: absolute;
          bottom: 0;
          left: 50%;
          transform: translateX(-50%);
          border-top-right-radius: 8px;
          border-top-left-radius: 8px;
          border: 1px solid #404040;
          border-bottom: 1px solid transparent;
          background: #222222;
          display: flex;
          align-items: center;
          padding: 12px;
        }
        .dt-dc-container {
          border-left: 1px solid #404040;
          border-right: 1px solid #404040;
          display: flex;
          align-items: center;
          justify-content: center;
          padding: 0 0.4rem;
        }
        .dt-default-colors {
          position: relative;
          border-radius: 50%;
          width: 28px;
          height: 28px;
          border: solid 2px transparent;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
        }
        
        .dt-default-colors div {
          width: 24px;
          height: 24px;
          border-radius: 50%;
          box-sizing: border-box;
          box-shadow: none;
        }
        .dt-cp-container {
          width: 28px;
          height: 28px;
          border: solid 2px transparent;
          border-radius: 50%;
          display: flex;
          align-items: center;
          justify-content: center;
          padding: 0 0.4rem;
        }
        .dt-color-picker {
          width: 24px;
          height: 24px;
          border-radius: 50%;
          background: radial-gradient(50% 50% at 50% 50%, #ffffff 0%, transparent 100%),
            conic-gradient(
              from 0deg at 50% 50%,
              #ff0000 0deg,
              #ffa800 47.73deg,
              #ffff00 79.56deg,
              #00ff00 121.33deg,
              #00ffff 180.99deg,
              #0000ff 238.67deg,
              #ff00ff 294.36deg,
              #ff0000 360deg
            ),
            #c4c4c4;
          transform: rotate(105deg);
        }
        
        .hide {
          display: none;
        }
        
        .dt-tools-container {
          position: relative;
          width: 50px;
          display: flex;
          align-items: center;
          justify-content: center;
        }
        .dt-tools-container img {
          position: absolute;
          max-height: 70px;
          bottom: -25px;
          transition: all 0.2s;
          cursor: pointer;
        }
        
        .dt-tools-container img:hover {
          bottom: -10px;
          opacity: 0.8;
          transform: scale(1.1);
        }
        #size {
          width: 35px;
          height: 30px;
          margin: 0 0.8rem;
          border: none;
          border-radius: 4px;
          background: #3d3d3d;
          color: #fff;
          font-size: 18px;
          font-weight: 500;
          -moz-appearance: textfield;
        }
        #size::-webkit-outer-spin-button,
        #size::-webkit-inner-spin-button {
          -webkit-appearance: none;
        }
        
        .dt-undo-redo-container {
          display: flex;
          align-items: center;
          justify-content: center;
          border-left: 1px solid #404040;
          padding: 0 0.8rem;
        }
        
        .dt-undo-redo-container img {
          width: 18px;
          cursor: pointer;
        }
        .dt-download-container {
          border-left: 1px solid #404040;
          padding: 0 0.8rem;
        }
        .dt-download-container img {
          width: 18px;
          cursor: pointer;
        }
        #redo {
          margin-left: 0.8rem;
        }
    </style>
</head>

<body>

    <div id="canvas-container">
        <div class="dt-control-container">
            <div class="dt-tools-container">
                <img src="//repo.bfw.wiki/bfwrepo/images/draw/pencil.svg" title="Brush" id="brush" alt="" />
            </div>
            <div class="dt-tools-container">
                <img src="//repo.bfw.wiki/bfwrepo/images/draw/eraser.png" title="Eraser" id="eraser" alt="" />
            </div>
            <div class="dt-tools-container" style="margin-right: 0.8rem">
                <img src="//repo.bfw.wiki/bfwrepo/images/draw/clear-all.png" title="clear" id="clear" alt="" />
            </div>
            <input type="number" min="1" max="100" id="size" value="3" />
            <div class="dt-dc-container">
                <div class="d.........完整代码请登录后点击上方下载按钮下载查看

网友评论0