1

In my basic canvas painting tool I want the user to be able to change the color of the stroke by using a color picker. So I want the ctx.strokeStyle to change to whatever color the user picks on the color picker. I've managed to add the color picker to the canvas by using the input type="color" but I'm wondering how I can make the color of the stroke/brush in the canvas to change according to the color picked in the color picker.

So this is my canvas at the moment:

enter image description here

And I want the user to change the stroke color by using this:

enter image description here

brasofilo
  • 25,496
  • 15
  • 91
  • 179
user3088233
  • 51
  • 1
  • 2
  • 4
  • Why cant you get its value on the event `onchange`? [Here](http://jsfiddle.net/Riesling/PEGS4/) is an example on jsfiddle that I found [here](http://stackoverflow.com/questions/13805611/event-handler-for-a-html5-color-input-element). BTW, currently there are some browsers that does not support `type=color`. For a list check this [link](http://html5test.com/compare/feature/form-color-element.html) – wendelbsilva Dec 10 '13 at 21:10
  • 1
    +1+ for having the tab `How to Ask` open in the browser :) – brasofilo Dec 10 '13 at 21:23
  • @wendelbsilva Thanks, I will try having a crack at that... – user3088233 Dec 10 '13 at 21:48
  • Here is my code http://jsfiddle.net/KQHMA/2/ -- So I want somehow for the stroke color to be changed to whatever color the user picks using the color picker. – user3088233 Dec 10 '13 at 21:48
  • @user3088233 [http://jsfiddle.net/wjAdP/](http://jsfiddle.net/wjAdP/). You just need to get the color on the event `onchange` and set it to the `strokeStyle`. From your code I added the call back `change`. In this function it set the `color value` to the variable `color`. And in the `draw` method, it set the `strokeStyle`. – wendelbsilva Dec 10 '13 at 21:53
  • @wendelbsilva Thank you so much! I really appreciate it man :) I love you! – user3088233 Dec 10 '13 at 22:02
  • @user3088233 np. BTW, if you want a solution similar to the colorpicker shown in your picture, check markE answer. You can use both of them to make something like MSPaintBrush. – wendelbsilva Dec 10 '13 at 22:07

3 Answers3

3

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.

wendelbsilva
  • 774
  • 6
  • 21
2

Here's a simple example of a color picker on a "tools" canvas used to set the current color (fill/stroke) on the drawing canvas:

Javascript paint Canvas, need help re-locating buttons?

For your "color wheel" picker, you would paint your wheel on the tools canvas and then use context.getImageData to grab the pixel color data under the mouse cursor.

var imgData=ctx.getImageData(mouseX,mouseY,1,1);

var data=imgData.data;

return("rgba("+data[0]+","+data[1]+","+data[2]+","+data[3]+")");

After the user picks their color on the tools canvas, you can use context.strokeStyle and context.fillStyle to make those colors active on the drawing canvas.

Community
  • 1
  • 1
markE
  • 102,905
  • 11
  • 164
  • 176
  • Here is my code -- http://jsfiddle.net/KQHMA/2/ Would you mind just editing that so that stroke color changes according to the color picker? I would appreciate it so so much! Thankss – user3088233 Dec 10 '13 at 21:42
  • +1 this solution looks similar to the color picker in the picture shown in the question – wendelbsilva Dec 10 '13 at 22:06
  • @Loktar Tell me, how is it totally different from this [picture](http://i.stack.imgur.com/ewu2p.png) he presented? Now im intrigued. – wendelbsilva Dec 10 '13 at 22:09
  • @wendelbsilva I didn't look at his picture, I just looked at the fiddle. But yes according to his image this would also be a good solution (better for cross browser like markE points out) Anyway I would take back my downvote but can't since the answer needs to be edited, I down voted in haste, a lesson that I shouldnt look at questions when busy with other tasks :P – Loktar Dec 11 '13 at 03:00
0

All you need to do is get the value of the color input and set the strokeStyle to that.

Live Demo

var points=new Array(),
    colorInput = document.getElementById("color");

        function start(e){
          var mouseX = e.pageX - canvas.offsetLeft;
          var mouseY = e.pageY - canvas.offsetTop;
          paint = true;
          ctx.beginPath();
          ctx.moveTo(mouseX,mouseY);
          points[points.length]=[mouseX,mouseY];
        };

        function draw(e){

          if(paint){
             var mouseX = e.pageX - canvas.offsetLeft;
             var mouseY = e.pageY - canvas.offsetTop; 
             ctx.lineTo(mouseX,mouseY);
             ctx.stroke();  

             // set the value to the color input
             ctx.strokeStyle = colorInput.value;

             ctx.lineJoin = ctx.lineCap = 'round';

             points[points.length]=[mouseX,mouseY];
          }

        }
        function stop(e){
          paint=false;
         var s=JSON.stringify(points);
            localStorage['lines']=s;
        }  



        var paint=false;
        var canvas = document.getElementById('myCanvas');
        var ctx=canvas.getContext("2d");
        canvas.addEventListener('mousedown',start);
        canvas.addEventListener('mousemove',draw);
        canvas.addEventListener('mouseup',stop);
Loktar
  • 34,764
  • 7
  • 90
  • 104
  • @markE ah crap Im sorry I didnt pay attention to the username... I wish I wouldn't have downvoted in such haste :( I cant remove it since the answer needs to be edited. Also I swear his original question didn't show the custom color picker like that. Anyway I upvoted another random answer of yours :P – Loktar Dec 11 '13 at 02:54
  • heh another comment, I guess his image always did look like that, it was just a link originally so I skipped over it and just looked at the fiddle. – Loktar Dec 11 '13 at 03:01