应用canvas1步步完成照片打码作用的方式

日期:2021-02-22 类型:科技新闻 

关键词:h5小游戏,h5如何制作,免费h5在线制作,h5页面在线制作,测一测h5

原文详细地址
https://github.com/MY729/front-common-funtion/blob/master/picture-code-demo/README.md

预览详细地址
https://my729.github.io/front-common-funtion/picture-code-demo/picture-code.html

提前准备工作中

demo 根据 vue + elelment-ui

最先建立1个html文档, 并引进 vue 和 elelment-ui(留意也有款式文档)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF⑻">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <!-- elelment-ui款式 -->
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
  
</body>
<!-- 引进vue -->
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<!-- 引进element-ui -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
</html>

接下来便可以写大家的打码作用啦

完成思路

  • 建立canvas画布,并即将打码的照片绘图上去
  • 监视电脑鼠标在照片上的点一下,挪动、松开恶性事件,在canvas画布上绘图要打码的地区
  • 解决绘图的打码地区
  • 储存打码后的照片

即将打码的照片绘图到canvas画布上

// 原始化 绘图照片
toCode (currentImg) {
  this.$nextTick(() => {
    // 获得即将绘图的canvas的父元素连接点
    let parentId = document.getElementById('parentId')
    // 原始化照片
    let drawImg = new Image()
    drawImg.setAttribute('crossOrigin', 'anonymous')
    drawImg.crossOrigin = 'Anonymous'
    drawImg.src = currentImg
    // 建立canvas元素并加上到父连接点中
    let addCanvas = document.createElement('canvas')
    parentId.appendChild(addCanvas)
    let canvas = parentId.lastElementChild
    canvas.id = 'imgCanvas'
    if (canvas.getContext) {
      let ctx = canvas.getContext('2d')
      // 绘图照片
      drawImg.onload = function () {
        canvas.width = 720
        canvas.height = 500
        ctx.drawImage(drawImg, 0, 0, 720, 500)
      }
    }
  })
}

点一下打码按钮,绘图打码地区

思路:

  • 电脑鼠标点一下,获得点一下时的座标,每次点一下前将会会存在打过码的地区,先消除画布,再次绘图照片
  • 电脑鼠标挪动,刚开始绘图打码的矩形框,根据挪动的座标和上面点一下的点座标明确绘图的矩形框座标和宽高
  • 将绘图的打码矩形框,切分成1个个宽高15像素的小正方形,并给每一个小正方形生产制造任意色调
  • 电脑鼠标松开,终止绘图矩形框
// 打码
dialogCode (img) {
  let parentId = document.getElementById('parentId')
  let canvas = document.getElementById('imgCanvas')
  if (canvas.getContext) {
    let ctx = canvas.getContext('2d')
    let drawImage = new Image()
    drawImage.crossOrigin = 'Anonymous'
    drawImage.src = img
    drawImage.onload = () => {
      ctx.drawImage(drawImage, 0, 0, 720, 500)
    }
    // 电脑鼠标点一下
    parentId.onmousedown = e => {
      ctx.clearRect(0, 0, canvas.width, canvas.height)
      ctx.drawImage(drawImage, 0, 0, 720, 500)
      this.flag = true
      this.clickX = e.offsetX // 电脑鼠标点一下时的X
      this.clickY = e.offsetY // 电脑鼠标点一下时的Y
    }
    // 电脑鼠标松开
    parentId.onmouseup = () => {
      this.flag = false
    }
    // 电脑鼠标按下
    parentId.onmousemove = e => {
      if (this.flag) {
        ctx.clearRect(0, 0, canvas.width, canvas.height)
        ctx.drawImage(drawImage, 0, 0, 720, 500)
        ctx.beginPath()
        let pixels = [] // 2维数字能量数组,每一个子数字能量数组有5个值(绘图矩形框左上角的X座标、y座标,矩形框的宽、高,转化成的4位任意数用于色调值)
        for (let x = 0; x < (e.offsetX - this.clickX) / 15; x++) {
          for (let y = 0; y < (e.offsetY - this.clickY) / 15; y++) {
            pixels.push([(x * 15 + this.clickX), (y * 15 + this.clickY), 15, 15, Math.floor(Math.random() * 9999)])
          }
          for (let y = 0; y > (e.offsetY - this.clickY) / 15; y--) {
            pixels.push([(x * 15 + this.clickX), (y * 15 + this.clickY), 15, 15, Math.floor(Math.random() * 9999)])
          }
        }
        for (let x = 0; x > (e.offsetX - this.clickX) / 15; x--) {
          for (let y = 0; y > (e.offsetY - this.clickY) / 15; y--) {
            pixels.push([(x * 15 + this.clickX), (y * 15 + this.clickY), 15, 15, Math.floor(Math.random() * 9999)])
          }
          for (let y = 0; y < (e.offsetY - this.clickY) / 15; y++) {
            pixels.push([(x * 15 + this.clickX), (y * 15 + this.clickY), 15, 15, Math.floor(Math.random() * 9999)])
          }
        }
        // 遍历数字能量数组绘图小正方形块
        for (let i = 0; i < pixels.length; i++) {
          ctx.fillStyle = '#bf' + pixels[i][4]
          ctx.fillRect(pixels[i][0], pixels[i][1], pixels[i][2], pixels[i][3])
        }
        ctx.fill()
        ctx.closePath()
      }
    }
  }
}

储存

// 储存
dialogUpload () {
  let canvas = document.getElementById('imgCanvas')
  let tempImg = canvas.toDataURL('image/png')
  let imgURL = document.getElementById('imgURL')
  imgURL.crossOrigin = 'Anonymous'
  imgURL.src = tempImg
}

源代码

拷贝到html文档可预览

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF⑻">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>应用canvas1步步完成照片打码作用</title>
  <!-- elelment-ui款式 -->
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  <style type="text/css">
    .rc-code__buttons {
      margin: 20px;
    }
  </style>
</head>
<body>
  <div id="app">
    <div class="rc-code__buttons">
      <h1>vue新项目中应用canvas1步步完成照片打码作用</h1>
      <el-button type="primary" @click="dialogCode(data.img_url)">打码</el-button>
      <el-button type="success" @click="dialogUpload()">储存</el-button>
    </div>
    <el-row>
      <el-col :span="12"><h3>点一下打码按钮,在照片上绘图打码地区; 点一下储存,转化成打码后的照片</h3></el-col>
      <el-col :span="12"><h3>储存后的照片</h3></el-col>
      <el-col :span="12"><div id="parentId"></div></el-col>
      <el-col :span="12"><img id="imgURL"/></el-col>
    </el-row>
  </div>
</body>
<!-- 引进vue -->
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<!-- 引进element-ui -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
new Vue({
  el: '#app',
  data () {
    return {
      data: {
        img_url: 'https://avatars0.githubusercontent.com/u/26196557?s=460&v=4'
      },
      flag: false, // 是不是绘图矩形框
      clickX: '', // 刚开始绘图矩形框时,电脑鼠标点一下时的x座标
      clickY: '' // 刚开始绘图矩形框时,电脑鼠标点一下时的y座标
    }
  },
  mounted() {
    this.toCode(this.data.img_url)
  },
  methods: {
    // 原始化 绘图照片
    toCode (currentImg) {
      this.$nextTick(() => {
        let parentId = document.getElementById('parentId')
        let drawImg = new Image()
        drawImg.setAttribute('crossOrigin', 'anonymous')
        drawImg.crossOrigin = 'Anonymous'
        drawImg.src = currentImg
        let addCanvas = document.createElement('canvas')
        parentId.appendChild(addCanvas)
        let canvas = parentId.lastElementChild
        canvas.id = 'imgCanvas'
        if (canvas.getContext) {
          let ctx = canvas.getContext('2d')
          drawImg.onload = function () {
            canvas.width = 720
            canvas.height = 500
            ctx.drawImage(drawImg, 0, 0, 720, 500)
          }
        }
      })
    },
    // 打码
    dialogCode (img) {
      let parentId = document.getElementById('parentId')
      let canvas = document.getElementById('imgCanvas')
      if (canvas.getContext) {
        let ctx = canvas.getContext('2d')
        let drawImage = new Image()
        drawImage.crossOrigin = 'Anonymous'
        drawImage.src = img
        drawImage.onload = () => {
          ctx.drawImage(drawImage, 0, 0, 720, 500)
        }
        parentId.onmousedown = e => {
          ctx.clearRect(0, 0, canvas.width, canvas.height)
          ctx.drawImage(drawImage, 0, 0, 720, 500)
          this.flag = true
          this.clickX = e.offsetX // 电脑鼠标点一下时的X
          this.clickY = e.offsetY // 电脑鼠标点一下时的Y
        }
        parentId.onmouseup = () => {
          this.flag = false
        }
        parentId.onmousemove = e => {
          if (this.flag) {
            ctx.clearRect(0, 0, canvas.width, canvas.height)
            ctx.drawImage(drawImage, 0, 0, 720, 500)
            ctx.beginPath()
            let pixels = [] // 2维数字能量数组,每一个子数字能量数组有5个值(绘图矩形框左上角的X座标、y座标,矩形框的宽、高,转化成的4位任意数用于色调值)
            for (let x = 0; x < (e.offsetX - this.clickX) / 15; x++) {
              for (let y = 0; y < (e.offsetY - this.clickY) / 15; y++) {
                pixels.push([(x * 15 + this.clickX), (y * 15 + this.clickY), 15, 15, Math.floor(Math.random() * 9999)])
              }
              for (let y = 0; y > (e.offsetY - this.clickY) / 15; y--) {
                pixels.push([(x * 15 + this.clickX), (y * 15 + this.clickY), 15, 15, Math.floor(Math.random() * 9999)])
              }
            }
            for (let x = 0; x > (e.offsetX - this.clickX) / 15; x--) {
              for (let y = 0; y > (e.offsetY - this.clickY) / 15; y--) {
                pixels.push([(x * 15 + this.clickX), (y * 15 + this.clickY), 15, 15, Math.floor(Math.random() * 9999)])
              }
              for (let y = 0; y < (e.offsetY - this.clickY) / 15; y++) {
                pixels.push([(x * 15 + this.clickX), (y * 15 + this.clickY), 15, 15, Math.floor(Math.random() * 9999)])
              }
            }
            for (let i = 0; i < pixels.length; i++) {
              ctx.fillStyle = '#bf' + pixels[i][4]
              ctx.fillRect(pixels[i][0], pixels[i][1], pixels[i][2], pixels[i][3])
            }
            ctx.fill()
            ctx.closePath()
          }
        }
      }
    },
    // 储存
    dialogUpload () {
      let canvas = document.getElementById('imgCanvas')
      let tempImg = canvas.toDataURL('image/png')
      let imgURL = document.getElementById('imgURL')
      imgURL.crossOrigin = 'Anonymous'
      imgURL.src = tempImg
    }
  }
})
</script>
</html>

以上便是本文的所有內容,期待对大伙儿的学习培训有一定的协助,也期待大伙儿多多适用脚本制作之家。