欢迎来到代码驿站!

JavaScript代码

当前位置:首页 > 网页前端 > JavaScript代码

JS实现canvas简单小画板功能

时间:2020-12-30 12:50:31|栏目:JavaScript代码|点击:

本文实例为大家分享了JS实现canvas简单小画板的具体代码,供大家参考,具体内容如下

Html部分:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="./index.css" rel="external nofollow" >
  <title>Document</title>
</head>
<body>
  <div class="container">
    <canvas id="cavs" width="700" height="330"></canvas>
    <ul>
      <li> <input type="color" id="color"> </li>
      <li> <input type="button" id="cleanBoard" value="清屏"></li>
      <li> <input type="button" id="eraser" value="橡皮"></li>
      <li> <input type="button" id="rescind" value=撤销></li>
      <li> <input type="button" id="save" value=保存></li>
      <li><input type="range" id="lineRuler" min="1" max="20"> </li>
    </ul>
  </div>
  <script src="./index.js"></script>
</body>
</html>

CSS部分:

*{
  margin: 0;
  padding: 0;
  list-style: none;
}
.container{
  margin: 30px;
}
#cavs{
  border: 1px solid red;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.6);
  border-radius: 10px;
}
.container ul{
  margin-top: 20px;
  width: 700px;
  text-align: center;
}
.container ul li{
  display: inline-block;
  margin-left: 35px;
}
.container ul li input{
  padding: 6px 15px;
  border-radius: 10px;
  border: none;
  outline: none;
  cursor: pointer;
  transition: box-shadow 0.3s cubic-bezier(0.6, -0.28, 0.735, 0.045);
}
.container ul li input:hover{
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.6);
}

JS部分:

var drawingBoard = {
  cavs: document.getElementById('cavs'),
  ctx: document.getElementById('cavs').getContext('2d'),
  ul_node: document.getElementsByTagName('ul')[0],
  colorBoard: document.getElementById('color'),
  lineRuler: document.getElementById('lineRuler'),
  imgArr: [],//存放图片
  init: function () {
    this.ctx.lineCap = 'round';
    this.ctx.lineJoin = 'round';
    this.drawing();//开始画画
    this.btnsFnAll();
  },
  drawing: function () {
    var self = this;
    var left = this.cavs.offsetLeft;
    this.cavs.onmousedown = function (e) {
      var e_x = e.pageX;//鼠标在画布上的x点
      var e_y = e.pageY;
      self.ctx.beginPath();//开始绘制
      self.ctx.moveTo(e_x - left, e_y - left);//落笔点,开始点
      var imgData = self.ctx.getImageData(0, 0, self.cavs.offsetWidth, self.cavs.offsetHeight);
      self.imgArr.push(imgData)
      document.onmousemove = function (e) {
        self.ctx.lineTo(e.pageX - left, e.pageY - left);//落笔点,开始点
        self.ctx.stroke();
      }
      document.onmouseup = function () {
        document.onmousemove = null;
        self.ctx.closePath();//闭合当前的路径 结束绘制
      }
      this.onpointerleave = function () {
        document.onmousemove = null;
      }
    }
  },
  btnsFnAll: function () {
    var self = this;
    this.ul_node.addEventListener('click', function (e) {
      console.log(e.target.id);
      switch (e.target.id) {
        case 'cleanBoard'://清屏
          self.ctx.clearRect(0, 0, self.cavs.offsetWidth, self.cavs.offsetHeight)
          break;
        case 'eraser'://橡皮
          self.ctx.strokeStyle = "#ffffff";
          break;
        case 'save'://保存
          let url = self.cavs.toDataURL('image/jpg');
          let a = document.createElement('a');
          document.body.appendChild(a);
          a.href = url;
          a.download = '草稿纸';
          a.target = '_blank';
          a.click()
          break;
        case 'rescind'://撤销
          console.log(self.imgArr)
          if (self.imgArr.length > 0) {
            self.ctx.putImageData(self.imgArr.pop(), 0, 0)
          }
          break;
      }
    });
    this.colorBoard.onchange = function () {
      self.ctx.strokeStyle = this.value;
    };
    this.lineRuler.onchange = function () {
      self.ctx.lineWidth = this.value
    }
  }
}
drawingBoard.init();

上一篇:Ajax::prototype 源码解读

栏    目:JavaScript代码

下一篇:微信小程序生成分享海报方法(附带二维码生成)

本文标题:JS实现canvas简单小画板功能

本文地址:http://www.codeinn.net/misctech/38188.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有