javascript - Circular canvas with bound that fills based on click and drag points? -


i'm incredibly new javascript, sorry if idea isn't using proper vocab y'all can me great!

does know how make circular canvas allows users fill portion of circle based on click , drag points within circle?

i think make circular canvas them color in, i'd make don't have scribble on circle color in.

a demo: http://jsfiddle.net/m1erickson/lvgwv/

you can use context.arc command draw pie wedge.

listen mouse events , in mousedown set beginning angle of wedge:

    function handlemousedown(e){       e.preventdefault();       mousex=parseint(e.clientx-offsetx);       mousey=parseint(e.clienty-offsety);        // put mousedown stuff here       var dx=mousex-cx;       var dy=mousey-cy;       begin=math.atan2(dy,dx);       isdown=true;     } 

in mousemove can set ending angle of wedge:

    function handlemousemove(e){       if(!isdown){return;}       e.preventdefault();       mousex=parseint(e.clientx-offsetx);       mousey=parseint(e.clienty-offsety);       var dx=mousex-cx;       var dy=mousey-cy;       end=math.atan2(dy,dx);       draw();     } 

example code:

<!doctype html> <html> <head> <link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css --> <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>  <style>     body{ background-color: ivory; }     #canvas{border:1px solid red;} </style>  <script> $(function(){      var canvas=document.getelementbyid("canvas");     var ctx=canvas.getcontext("2d");     ctx.strokestyle="lightgray";     ctx.linewidth=3;     ctx.fillstyle="skyblue";      var $canvas=$("#canvas");     var canvasoffset=$canvas.offset();     var offsetx=canvasoffset.left;     var offsety=canvasoffset.top;     var scrollx=$canvas.scrollleft();     var scrolly=$canvas.scrolltop();      var isdown=false;     var startx;     var starty;      var cx=150;     var cy=150;     var begin,end;      function draw(){         ctx.clearrect(0,0,canvas.width,canvas.height);         ctx.beginpath();         ctx.moveto(cx,cy);         ctx.arc(cx,cy,75,begin,end);         ctx.closepath();         ctx.fill();         ctx.stroke();     }      function handlemousedown(e){       e.preventdefault();       mousex=parseint(e.clientx-offsetx);       mousey=parseint(e.clienty-offsety);        // put mousedown stuff here       var dx=mousex-cx;       var dy=mousey-cy;       begin=math.atan2(dy,dx);       isdown=true;     }      function handlemouseup(e){       e.preventdefault();       isdown=false;     }      function handlemouseout(e){       e.preventdefault();       isdown=false;     }      function handlemousemove(e){       if(!isdown){return;}       e.preventdefault();       mousex=parseint(e.clientx-offsetx);       mousey=parseint(e.clienty-offsety);       var dx=mousex-cx;       var dy=mousey-cy;       end=math.atan2(dy,dx);       draw();     }      $("#canvas").mousedown(function(e){handlemousedown(e);});     $("#canvas").mousemove(function(e){handlemousemove(e);});     $("#canvas").mouseup(function(e){handlemouseup(e);});     $("#canvas").mouseout(function(e){handlemouseout(e);});    }); // end $(function(){}); </script>  </head>  <body>     <h4>drag on canvas create pie-wedge.</h4>     <canvas id="canvas" width=300 height=300></canvas> </body> </html> 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -