Based on the picture you posted, looks like you want a solution like markE posted in his answer. Although, in the text you mention type="color"
. If you want to use this input you can check this jsfiddle working. In this second case, just remember a lot of browser do not support it yet. Click here if you want to see a list of browser that support it.
Below I will try to detail what changes I did to your jsfiddle.
Firstly, you need to set a callback to the color input
. This mean when the value of the input changes, it calls the method change
. The function change
is getting the value
of the input and set in a global variable called color
.
var color = "rgb(255,0,0)";
function change(e){
color = this.value;
}
document.getElementById("color").onchange = change;
The other change was inside your draw
function. Before the draw it is getting the value in the variable color
. This way, the next time you change the color it will update the color used in the stroke.
ctx.strokeStyle = color;
With those changes, if in the future you decide to use another tool to get the color (for example, you can check the browser to see if it support the input="color"
and use a different color picker in this case), you just need to set the new color in the variable color
.