我正在尝试编写一个小型绘图室应用程序,特别是避免用户需要安装Java,Flash或Shockwave。我在颜色选择器中遇到了一些渐变问题。
具体来说,问题似乎是我找不到正确清除并重新启动渐变的方法。
我正在做一个三行(当前为3 Canvas 原型(prototype))的RGB渐变,类似于SAI Paint工具和其他绘图程序,并且在更新/修改渐变时,它不会更新我期望的方式,结果是牵引杆”显示与输出相比不正确的颜色。
我正在使用addColorStop()更新渐变,但是我得到的几乎就像是在 push 偏移量,而不是替换偏移量一样。
function sendUpdate(p, p2, p3) //sends update to colour bars.
{
// var id;
var p;
cr.beginPath();
rbg.addColorStop(0, "#00" + hxb + hxc ); //00 00 00 black
rbg.addColorStop(1, "#FF" + hxb + hxc ); //FF 00 00 bright red
cr.rect(0, 0, r.width, r.height);
cr.fillStyle = rbg;
cr.fill();
cr.closePath();
//indicator
cr.beginPath();
cr.rect(p - 2, 1, 3, 6);
cr.lineWidth = 1;
cr.strokeStyle = "#E0E0E0";
cr.stroke();
cr.closePath();
cg.beginPath();
cg.rect(0, 0, g.width, g.height);
gbg.addColorStop(0, "#" + hxa + "00" + hxc ); //FF 00 00 bright red.
gbg.addColorStop(1, "#" + hxa + "FF" + hxc ); //FF FF 00 yellow
cg.fillStyle = gbg;
cg.fill();
cg.closePath();
cg.beginPath();
cg.rect(p2 - 2, 1, 3, 6);
cg.lineWidth = 1;
cg.strokeStyle = "#E0E0E0";
cg.stroke();
cg.closePath();
cb.beginPath();
cb.rect(0, 0, b.width, b.height);
bbg.addColorStop(0, "#" + hxa + hxb + "00" ); //FF 00 00 bright red
bbg.addColorStop(1, "#" + hxa + hxb + "FF" ); //FF 00 FF pink/purple
cb.fillStyle = bbg;
cb.fill();
cb.closePath();
cb.beginPath();
cb.rect(p3 - 2, 1, 3, 6);
cb.lineWidth = 1;
cb.strokeStyle = "#E0E0E0";
cb.stroke();
cb.closePath();
document.getElementById("colourIndicator").style.backgroundColor=clr;
}
function link(id, x, p) //takes id(which colourbar) 0-255 value and position 0-170 value and UPDATES COLOUR! Use this to initialize or call!
{
var x;
var p;
if (x <= 255)
{
switch(id)
{
case 0:
hxa = toHex(x);
if (hxa.length == 1) { hxa = "0" + hxa; }
clr = "#" + hxa + hxb + hxc;
document.getElementById("debugc").innerHTML="case0 output: " + hxa + hxb + hxc;
pos1 = p;
sendUpdate(p, pos2, pos3);
break;
case 1:
hxb = toHex(x);
if (hxb.length == 1) { hxb = "0" + hxb; }
clr = "#" + hxa + hxb + hxc;
document.getElementById("debugc").innerHTML="case1 output: " + hxa + hxb + hxc;
pos2 = p;
sendUpdate(pos1, p, pos3);
break;
case 2:
hxc = toHex(x);
if (hxc.length == 1) { hxc = "0" + hxc; }
clr = "#" + hxa + hxb + hxc;
document.getElementById("debugc").innerHTML="case2 output: " + hxa + hxb + hxc;
pos3 = p;
sendUpdate(pos1, pos2, p);
break;
}
}
else
{
x = 255;
p = 170;
link(id, x, p);
}
}
我对javascript很陌生,但我无法自行解决。我已经阅读了有关 Canvas 中渐变的W3部分,以及功能说明。此处找到的一些拾色器和色轮解决方案可能会在以后对我有所帮助,但是它们似乎都没有提到我实际上要反复更新渐变的问题。
如果用户选择了颜色FF0000,则addColorStop后面的注释是我想要的输出,只留下渐变以显示用户可以实现的混合颜色。
哦,在此版本中,创建梯度在函数外部被调用。
最佳答案
我认为您需要“重新启动”您的渐变,就像您在已创建的渐变上添加色阶一样,它们不会覆盖已存在的渐变。
尝试在函数开始时使rbg对象无效,然后再次创建渐变对象