吸管工具,canvas模拟吸管工具,吸取图片中的颜色值 vue.js 颜色值 转换

 canvas-straw.vue :吸取颜色组件 

<template>
  <div class="color-content" :style="{'--size': boxSize + 'px', '--pix-size': '10px'}" v-if="value">
    <div class="close-icon" @click="close">close</div>
      <div class="img-box">
        <img :src="src" ref="img" crossOrigin  @load="initCanvas()" alt="origin-img">
      </div>
    <div class="pix-box" :style="pixPos">
      <div class="center" :style="{'borderColor': `rgb(${color})`}"></div>
      <div class="htmls" v-html="innerVal"></div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'canvas-straw',
  model: {
    event: 'on-change',
    prop: 'value'
  },
  props: {
    boxSize: {
      type: Number,
      default: 100
    },
    value: {
      type: Boolean,
      default: false
    },
    src: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      color: '153, 153, 153',
      innerVal: '',
      mouseInfo: {
        clientY: 0,
        clientX: 0,
        space: 20,
        size: 100
      }
    }
  },
  computed: {
    pixPos () {
      const width = window.innerWidth
      const height = window.innerHeight
      let {clientY, clientX, space, size} = this.mouseInfo
      let left = clientX
      let top = clientY
      if ((clientY + size) > height) {
        top = clientY - size - space
      } else {
        top += space
      }
      if ((clientX + size) > width) {
        left = clientX - size - space
      } else {
        left += space
      }
      return `left: ${left}px; top:${top}px`
    }
  },
  methods: {
    close () {
      this.$emit('on-change', false)
    },
    initCanvas () {
      let oImg = this.$refs.img
      let canvas = this.draw(oImg)
      oImg.addEventListener('click', (e) => {
        const [r, g, b] = this.color.split(',')
        console.log({r, g, b})
        this.$emit('on-change', {r, g, b})
      })
      oImg.addEventListener('mousemove', (e) => {
        this.mouseInfo.clientY = e.clientY
        this.mouseInfo.clientX = e.clientX
        let x = e.offsetX
        let y = e.offsetY
        this.color = this.getPix(x, y, canvas.ctx)
      })
    },
    // 画图
    draw (img) {
      let style = window.getComputedStyle(img)
      let width = parseInt(style.width)
      let height = parseInt(style.height)
      img.style.width = width + 'px'
      img.style.height = height + 'px'
      img.style.maxWidth = width + 'px'
      img.style.maxHeight = height + 'px'
      let canvas = document.createElement('canvas')
      canvas.width = width
      canvas.height = height
      let ctx = canvas.getContext('2d')
      ctx.drawImage(img, 0, 0, width, height) // 这里一定要写上获取到图片的宽高,否则生成的图片和原图片大小不一样,吸取到的颜色不准
      return {
        ctx,
        canvas
      }
    },
    // 获取16进制颜色
    gethex (r, g, b) {
      r = r.toString(16)
      g = g.toString(16)
      b = b.toString(16)
      // 补0
      if (r.length === 1) r = '0' + r
      if (g.length === 1) g = '0' + g
      if (b.length === 1) b = '0' + b
      let hex = r + g + b
      // 简化处理,如 FFEEDD 可以写为 FED
      if (r.slice(0, 1) === r.slice(1, 1) && g.slice(0, 1) === g.slice(1, 1) && b.slice(0, 1) === b.slice(1, 1)) {
        hex = r.slice(0, 1) + g.slice(0, 1) + b.slice(0, 1)
      }
      return hex
    },
    // 获取像素颜色
    getPix (x, y, context) {
      const size = 10
      const num = this.boxSize / 2 / size // boxSize (必须是偶数 盒子大小) / 一半 / 每个像素大小
      x = x - num // 减掉自身像素个数的开始坐标
      y = y - num
      // 读取图片像素信息
      const w = num * 2 + 1 // 图片大小是 像素个数的2倍 (并多一行一列像素 为了视觉上的中心点 所以必须是奇数)
      const h = num * 2 + 1
      const centerPos = Math.ceil(w / 2) // 获取中心点坐标 向上取整
      let imageData = context.getImageData(x, y, w, h) // 截取 当前坐标下 w,h大小画图的数据
      const pos = this.getPos(imageData.data, w) // 计算当前截取画布下的像素信息(读取像素长度控制在千万以内 否则易导致浏览器崩溃)
      // 生成矩阵数据
      let arr = []
      Array(w).fill().map((item, index) => {
        let tx = index + 1
        const inners = Array(h).fill().map((item2, index2) => {
          let ty = index2 + 1
          const color = pos.get(`${tx},${ty}`)
          // 创建 10 * 10 px大小为单位的像素块
          arr.push(`<div data-set="${tx},${ty}" style="left:${index * 10}px; top:${index2 * 10}px; background: rgb(${color})"></div>`)
          return '#' + color
        })
        return inners
      })
      // 更新数据
      this.innerVal = arr.join('')
      // 返回当前中心坐标的 颜色值
      return pos.get(`${centerPos},${centerPos}`)
    },

    // 计算像素信息并返回
    getPos (data, imgWidth) {
      let pos = new Map()
      let length = data.length

      for (let i = 0; i < length; i++) {
        if (i % 4 === 0) { // 每四个元素为一个像素数据 r,g,b,alpha
          let x = i / 4 % imgWidth + 1 // 横坐标
          let y = Math.floor(i / 4 / imgWidth) + 1 // 纵坐标
          let alpha = Math.round(data[i + 3] / 255 * 100) / 100 // alpha 值

          if (data[i + 3] === 255) { // 没有alpha 值
            // let hex = this.gethex(data[i], data[i + 1], data[i + 2])
            let hex = `${data[i]}, ${data[i + 1]}, ${data[i + 2]}`
            pos.set(`${x},${y}`, hex)
          } else if (alpha > 0) { // 有alpha 值
            let rgba = `${data[i]}, ${data[i + 1]}, ${data[i + 2]}, ${alpha}`
            pos.set(`${x},${y}`, rgba)
          }
        }
      }
      return pos
    }
  }
}
</script>
<style lang='less' scoped>
.color-content {
  background-color: rgba(0 , 0, 0, 0.5);
  z-index: 9999;
  position: fixed;
  left: 0px;
  top: 0px;
  width: 100%;
  height: 100%;
  text-align: center;
  padding: 20px;
  .img-box {
    display: flex;
    width: 100%;
    height: 100%;
    justify-content: center;
    align-items: center;
  }
  img {
    cursor: crosshair;
    max-width: 100%;
    max-height: 100%;
  }
}
.close-icon {
  position: fixed;
  right: 10px;
  top: 10px;
  cursor: pointer;
  transition: ease-in-out 0.3s;
  color: #ebebeb;
  &:hover{
    color: #ff0000;
  }
}
</style>
<style lang="less">
  @pix-size: 10px;
 .pix-box {
    z-index: 999;
    position: absolute;
    // left: 20px;
    top: 30px;
    width: calc(var(--size) + @pix-size + 2px);
    height: calc(var(--size) + @pix-size + 2px);
    box-sizing: border-box;
    border: #fff solid 1px;
    border-radius: 50%;
    overflow: hidden;
    box-shadow: 0px 0px 5px #999;
    div.htmls {
      position: absolute;
      left: 0px;
      top: 0px;
      width: 100%;
      height: 100%;
      & > div {
        width: @pix-size;
        height: @pix-size;
        position: absolute;
        border: none;
      }
    }
    div.center {
      position: absolute;
      width: @pix-size;
      height: @pix-size;
      left: calc(var(--size) / 2);
      top: calc(var(--size) / 2);
      box-sizing: border-box;
      border: #999 solid 1px;
      z-index: 10;
      filter: invert(100%);
    }
    background: url('./images/bg-pixel.jpg')
  }

</style>

使用方法:

在某个页面中 引入  import canvasStraw from './canvas-straw.vue'  并注册组件

../index.vue 

<template>
  <div>
    <i-button @click="show = !show">打开</i-button>
    <canvas-straw src="http:../images/test.jpg" v-model="show"></canvas-straw>
  </div>
</template>
<script>
import canvasStraw from './canvas-straw.vue'
export default {
  components: {
    canvasStraw
  },
  data () {
    return {
      show: false
    }
  }
}
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Vue隐藏PDF.js工具栏,你可以使用以下步骤: 1. 首先,你需要在Vue项目安装并导入PDF.js库。你可以通过npm或yarn安装该库。例如,使用npm安装: ``` npm install pdfjs-dist ``` 然后在你的Vue组件导入库: ```javascript import pdfjsLib from 'pdfjs-dist'; ``` 2. 在Vue组件的`mounted`生命周期钩子函数,加载并渲染PDF文档。你可以使用PDF.js提供的`getDocument()`方法来加载文档,并在加载成功后将其渲染到页面上。同时,你可以通过设置`hideToolbar`选项为`true`来隐藏工具栏。 ```javascript mounted() { const pdfPath = 'path/to/your/pdf/file.pdf'; const container = this.$refs.pdfContainer; pdfjsLib.getDocument(pdfPath).promise.then(pdf => { pdf.getPage(1).then(page => { const canvas = this.$refs.pdfCanvas; const context = canvas.getContext('2d'); const viewport = page.getViewport({ scale: 1 }); canvas.width = viewport.width; canvas.height = viewport.height; page.render({ canvasContext: context, viewport: viewport, }); const viewer = new pdfjsViewer.PDFViewer({ container: container, hideToolbar: true, // 隐藏工具栏 }); viewer.setDocument(pdf); }); }); } ``` 3. 在Vue模板添加一个容器元素以渲染PDF,并将其与相应的引用关联起来。 ```html <template> <div> <div ref="pdfContainer"></div> <canvas ref="pdfCanvas"></canvas> </div> </template> ``` 通过以上步骤,你就可以在Vue加载并渲染PDF文档,并隐藏PDF.js工具栏了。记得根据你的实际需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值