简体   繁体   English

更改 canvas 的背景颜色的 alpha

[英]change the alpha for the background color of the canvas

Hi guy i am trying to clone colol wheel picker from adobe color wheel , i am having a problem with changing the alpha for the color wheel, the code i used to generate the color wheel is referenced from here .Below is the code i use to change the alpha for the background of the canvas(default alpha is 255), but it's not working, i still can't figure out why嗨,伙计,我正在尝试从adobe 色轮克隆 colol 轮选择器,我在更改色轮的 alpha 时遇到问题,我用来生成色轮的代码是从这里引用的。下面是我用来更改画布背景的 alpha(默认 alpha 为 255),但它不起作用,我仍然不知道为什么

useEffect(() => {
    const canvas = canvasRef.current;
    const ctx = canvas.getContext("2d");
    let ImageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
    for (var i = 0; i < canvas.height; i++)
      for (var j = 0; j < canvas.width; j++)
        if ((i + 1) % 4 === 0) {
          ImageData.data[i] = 150;
        }
    ctx.putImageData(ImageData, 0, 0); //put image data back
  }, []);

you can find full code and demo in here , thank you你可以在这里找到完整的代码和演示,谢谢

I'm guessing that you want to change the alpha value of each color component contained in the ImageData array, are you?我猜您想更改ImageData数组中包含的每个颜色分量的 alpha 值,是吗?

In this case your for-loop is simply wrong.在这种情况下,您的 for 循环是完全错误的。 For illustrative purposes try something like this:出于说明目的,请尝试以下操作:

 let canvas = { width: 100, height: 20 }; let values = []; for (var i = 0; i < canvas.height; i++) for (var j = 0; j < canvas.width; j++) if ((i + 1) % 4 === 0) { values.push(i); } document.getElementById("result").innerText = values.toString();
 #result { width: 20em; display: block; word-break: break-all; }
 <span id="result"></span>

If you run the code above you'll notice that the only unique values we get are 3 , 7 , 11 , 15 and 19 .如果您运行上面的代码,您会注意到我们获得的唯一唯一值是37111519

If you want to change the alpha of the whole ImageData array, you simply need to loop over it and change any 4th value.如果你想改变整个 ImageData 数组的 alpha,你只需要循环它并改变任何第 4 个值。 For example:例如:

let ImageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
for (let i = 0; i < ImageData.length; i += 4) {
    ImageData.data[i] = 150;
}

If changing the alpha is what you need, you should be using the canvas globalAlpha:如果您需要更改 alpha,您应该使用 canvas globalAlpha:
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalAlpha https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalAlpha

Here is an example drawing the same shape with different alpha values这是一个使用不同 alpha 值绘制相同形状的示例

 canvas = document.getElementById("cv0"); ctx = canvas.getContext('2d') ctx.fillStyle = "red" function draw(x, y, alpha) { ctx.globalAlpha = alpha ctx.beginPath() ctx.moveTo(x, y) ctx.arc(x+20, y-10, 30, 0, 6) ctx.fill() ctx.globalAlpha = 1 } draw(10, 40, 0.4) draw(80, 40, 0.7) draw(150, 40, 1)
 <canvas id='cv0' width=200 height=90></canvas>


When you provide a sample snippet please remove all other functions, variables and anything else that is not relevant to the problem at hand, read this for more info:当您提供示例片段时,请删除所有其他函数、变量以及与手头问题无关的任何其他内容,阅读此内容以获取更多信息:
https://stackoverflow.com/help/minimal-reproducible-example https://stackoverflow.com/help/minimal-reproducible-example
that will save a lot of time to everyone reading your code for the first time, and you increase your chances of getting a fast response.这将为第一次阅读您的代码的每个人节省大量时间,并增加获得快速响应的机会。


Here is my changes to your code snippet:这是我对您的代码段所做的更改:
https://codesandbox.io/s/gracious-fog-q303zf?file=/src/Canvas.js https://codesandbox.io/s/gracious-fog-q303zf?file=/src/Canvas.js

I'm using the globalAlpha to draw two circles (like yours just smaller) with different alpha我正在使用 globalAlpha 绘制两个具有不同 alpha 的圆圈(就像你的一样小)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM