You might want to keep the minimum length on the line drawn. To do this, take the pencil section of this example code and change it to something like this:
tools.pencil = function () { var tool = this; // variables for last x, y and min_length of line var lx; var ly; var min_length = 3; this.started = false; // This is called when you start holding down the mouse button. // This starts the pencil drawing. this.mousedown = function (ev) { context.beginPath(); context.moveTo(ev._x, ev._y); tool.started = true; // update last x,y coordinates lx = ev._x; ly = ev._y; }; // This function is called every time you move the mouse. Obviously, it only // draws if the tool.started state is set to true (when you are holding down // the mouse button). this.mousemove = function (ev) { if (tool.started && (Math.sqrt(Math.pow(lx - ev._x, 2) + Math.pow(ly - ev._y, 2)) > min_length)) { context.lineTo(ev._x, ev._y); context.stroke(); lx = ev._x; ly = ev._y; } }; // This is called when you release the mouse button. this.mouseup = function (ev) { if (tool.started) { tool.mousemove(ev); tool.started = false; img_update(); } }; };
Kodra source share