美文网首页
canvas 获去画布上某一个点的颜色值

canvas 获去画布上某一个点的颜色值

作者: IssunRadiance | 来源:发表于2022-01-17 18:18 被阅读0次
var canvas = this.$refs.bargraphCanvas;
var context = canvas.getContext('2d')
this.getPixelColor(context)
getPixelColor(context) {
      // x轴坐标 , y轴坐标, 1,1取色的范围像素值
      var imageData = context.getImageData(x, y, 1, 1); 
      var pixel = imageData.data;
      var r = pixel[0];
      var g = pixel[1];
      var b = pixel[2];
      var a = pixel[3] / 255;
      a = Math.round(a * 100) / 100;
      var rHex = r.toString(16);
      r < 16 && (rHex = "0" + rHex);
      var gHex = g.toString(16);
      g < 16 && (gHex = "0" + gHex);
      var bHex = b.toString(16);
      b < 16 && (bHex = "0" + bHex);
      var rgbaColor = "rgba(" + r + "," + g + "," + b + "," + a + ")";
      var rgbColor = "rgb(" + r + "," + g + "," + b + ")";
      var hexColor = "#" + rHex + gHex + bHex;
      return {
        rgba: rgbaColor,
        rgb: rgbColor,
        hex: hexColor,
        r: r,
        g: g,
        b: b,
        a: a
      };
    },
借鉴网络上的方法,具体哪里看到的忘记了

相关文章

网友评论

      本文标题:canvas 获去画布上某一个点的颜色值

      本文链接:https://www.haomeiwen.com/subject/agochrtx.html