我正在尝试编写一个小的绘画应用程序,特别是为了避免用户需要安装Java、Flash或Shockwave。但在颜色选择器中,我遇到了一些渐变问题。
具体来说,问题似乎是我找不到正确清除和重新启动渐变的方法。我正在进行一个三行(目前是3个画布原型)R G B渐变,类似于SAI Paint工具和其他绘图程序,但在更新/修改渐变时,它没有按我预期的方式更新,结果是“绘制栏”显示的颜色与输出不匹配。
我正在使用addColorStop()来更新渐变,但我得到的几乎就像是将偏移量推出去而不是替换它。
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还不是很熟悉,自己无法解决这个问题。我阅读了W3关于canvas渐变的部分和函数说明。这里找到的一些颜色选择器和颜色轮解决方案可能会在以后帮助我,但似乎没有提到我的问题,那就是如何重复更新渐变。
如果用户选择颜色FF0000,则addColorStop后面的注释是我想要的输出,将渐变留给用户来显示可以实现的混合颜色。
哦,这个版本中创建渐变是在函数外调用的。