我目前正在为我正在制作的一个网站编写一个颜色选择器,线性渐变似乎不会变成全白。我正在使用画布来获取RGB颜色数据。请参阅this screenshot
如你所见,我得到的最大值是254。我使用以下函数将渐变绘制到画布上。
draw_color_square(hue){
var gradB = this.ctx.createLinearGradient(0, 0, 0, 255);
gradB.addColorStop(0, "white");
gradB.addColorStop(1, "black");
var gradC = this.ctx.createLinearGradient(0,0,255,0);
gradC.addColorStop(0, "hsla(" + hue + ",100%,50%,0)");
gradC.addColorStop(1, "hsla(" + hue + ",100%,50%,1)");
this.ctx.fillStyle = gradB;
this.ctx.fillRect(0, 0, 255, 255);
this.ctx.fillStyle = gradC;
this.ctx.globalCompositeOperation = "multiply";
this.ctx.fillRect(0, 0, 255, 255);
this.ctx.globalCompositeOperation = "source-over";
}
我已检查滑块是否移动到位置0,0
发布于 2018-08-19 07:45:51
你几乎所有的东西都移动了1个像素。画布的宽度必须为256像素,您将其设置为255像素。填充which只填充255个像素的宽度,而渐变从像素0(据我所知还不存在)开始,直到255像素。我已经更新了一切,让它能像预期的那样工作。
最重要的部分是这一部分:
draw_color_square(hue){
var gradB = this.ctx.createLinearGradient(1, 1, 1, 256);
gradB.addColorStop(0, "white");
gradB.addColorStop(1, "black");
var gradC = this.ctx.createLinearGradient(1,1,256,1);
gradC.addColorStop(0, "hsla(" + hue + ",100%,50%,0)");
gradC.addColorStop(1, "hsla(" + hue + ",100%,50%,1)");
this.ctx.fillStyle = gradB;
this.ctx.fillRect(0, 0, 256, 256);
this.ctx.fillStyle = gradC;
this.ctx.globalCompositeOperation = "multiply";
this.ctx.fillRect(0, 0, 256, 256);
this.ctx.globalCompositeOperation = "source-over";
}
https://stackoverflow.com/questions/51912760
复制相似问题