I have a problem with my JavaScript code if you enter the wrong code ( color value) then it must be black. but when I enter somthing like "blablabla" then it will be that I typed the last for exemple: I write Red then it will be a red circle, but after that when I write blablabla will be red too, I just want that when I write a wrong color word it will be Black I Think that I have to write an rgb code so the funktion will check if I write right or wrong
function getPos(canvas, event){
var a = new Number();
var b = new Number();
var canvas= document.getElementById("can1");
if (event.a != undefined && event.b != undefined)
{
a = event.a;
b = event.b;
}
else
{
a = event.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft;
b = event.clientY + document.body.scrollTop +
document.documentElement.scrollTop;
}
a -= canvas.offsetLeft;
b -= canvas.offsetTop;
var ctx = canvas.getContext("2d");
var color1 = document.getElementById('color');
var size = document.getElementById('size');
ctx.beginPath();
if ( size.value > 0)
{
ctx.arc(a,b,size.value, 275*(Math.PI/180), 635*(Math.PI/180), false);
}
else
{
ctx.arc(a,b,15, 275*(Math.PI/180), 635*(Math.PI/180), false);
}
if (color1.value.length < 0)
{
ctx.fillStyle = "#000000";
}
else if (color1.value.length == 0)
{
ctx.fillStyle = '#000000';
}
else
{
ctx.fillStyle = color1.value;
}
ctx.fill();
ctx.stroke();
ctx.closePath();
window.localStorage['IMG'] = canvas.toDataURL();
}
the problem is here I guess:
if (color1.value.length < 0)
{
ctx.fillStyle = "#000000";
}
else if (color1.value.length == 0)
{
ctx.fillStyle = '#000000';
}
else
{
ctx.fillStyle = color1.value;
}