javascript - drawing line on canvas that's "round" -


i have code allows me draw line on canvas

var canvas, ctx, flag = false, prevx = 0, currx = 0, prevy = 0, curry = 0, dot_flag = false;  var x = "black", y = 10;  function initialize() {     canvas = document.getelementbyid('can');      const bounds = canvas.getboundingclientrect();     canvas.width = bounds.width;     canvas.height = bounds.height;      ctx = canvas.getcontext("2d");      w = canvas.width;     h = canvas.height;      canvas.addeventlistener("mousemove", function (e) {         findxy('move', e)     }, false);     canvas.addeventlistener("mousedown", function (e) {         findxy('down', e)     }, false);     canvas.addeventlistener("mouseup", function (e) {         findxy('up', e)     }, false);     canvas.addeventlistener("mouseout", function (e) {         findxy('out', e)     }, false); }  function draw(e) {     ctx.beginpath();     ctx.moveto(prevx, prevy);     ctx.lineto(currx, curry);     ctx.strokestyle = x;     ctx.linewidth = y;     ctx.stroke();     ctx.closepath(); }  function getmousepos(canvas, evt) {     var rect = canvas.getboundingclientrect();     return {       x: evt.clientx - rect.left,       y: evt.clienty - rect.top     }; }  function findxy(res, e) {     var pos = getmousepos(canvas, e);     if (res == 'down') {         prevx = currx;         prevy = curry;          currx = pos.x;         curry = pos.y;          flag = true;         dot_flag = true;         if (dot_flag) {             ctx.beginpath();             ctx.fillstyle = x;             ctx.fillrect(currx, curry, 2, 2);             ctx.closepath();             dot_flag = false;         }     }     if (res == 'up' || res == "out") {         flag = false;     }     if (res == 'move') {         if (flag) {             prevx = currx;             prevy = curry;             currx = pos.x;             curry = pos.y;             draw();         }     } } 

with high no y 10 line get's drawn thick horizontal line, know can draw circular line, site autodraw can make nice sketches there.

here's screen record of type of lines makes https://www.youtube.com/watch?v=e0sdfldab6u&feature=youtu.be

draw arc before drawing line. make drawn line "round".

var canvas, ctx, flag = false,     prevx = 0,     currx = 0,     prevy = 0,     curry = 0,     dot_flag = false;    var x = "black",     y = 5;  initialize()    function initialize() {     canvas = document.getelementbyid('can');       const bounds = canvas.getboundingclientrect();     canvas.width = bounds.width;     canvas.height = bounds.height;       ctx = canvas.getcontext("2d");       w = canvas.width;     h = canvas.height;       canvas.addeventlistener("mousemove", function(e) {        findxy('move', e)     }, false);     canvas.addeventlistener("mousedown", function(e) {        findxy('down', e)     }, false);     canvas.addeventlistener("mouseup", function(e) {        findxy('up', e)     }, false);     canvas.addeventlistener("mouseout", function(e) {        findxy('out', e)     }, false);  }    function draw(e) {     //arc     ctx.beginpath();     ctx.arc(currx, curry, y, 0, math.pi * 2);     ctx.fillstyle = x;     ctx.fill();     //line     ctx.beginpath();     ctx.moveto(prevx, prevy);     ctx.lineto(currx, curry);     ctx.linewidth = y * 2;     ctx.strokestyle = x;     ctx.stroke();  }    function getmousepos(canvas, evt) {     var rect = canvas.getboundingclientrect();     return {        x: evt.clientx - rect.left,        y: evt.clienty - rect.top     };  }    function findxy(res, e) {     var pos = getmousepos(canvas, e);     if (res == 'down') {        prevx = currx;        prevy = curry;          currx = pos.x;        curry = pos.y;          flag = true;        dot_flag = true;        if (dot_flag) {           ctx.beginpath();           ctx.fillstyle = x;           ctx.fillrect(currx, curry, 2, 2);           ctx.closepath();           dot_flag = false;        }     }     if (res == 'up' || res == "out") {        flag = false;     }     if (res == 'move') {        if (flag) {           prevx = currx;           prevy = curry;           currx = pos.x;           curry = pos.y;           draw();        }     }  }
canvas { border: 1px solid }
<canvas id="can" width="200" height="200"></canvas>


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -