javascript - Implement polygon tool on HTML5 canvas -


i trying imitate polygon tool in paint allow user draw same on canvas. below have coded far how not exact same paint tool. know there way fill shape once it's drawn. can please help.

    var startpointx = "", startpointy = "", endpointx, endpointy, isnewshape = false; function tool_polygon() { var tool = this; this.started = false;  this.mousedown = function (ev) {     tool.started = true;     tool.x0 = ev.offsetx;     tool.y0 = ev.offsety;       console.log('mousedown');     if (startpointx == "" && startpointy == "") {         startpointx = tool.x0;         startpointy = tool.y0;     }     console.log('startpointx ' + startpointx + ' startpointy ' + startpointy + ' ev.offsetx ' + ev.offsetx + ' ev.offsety ' + ev.offsety + ' isnewshape ' + isnewshape);     //if ((math.abs(startpointx - ev.offsetx) < 5) && (math.abs(startpointy - ev.offsety) < 5) && (startpointx != ev.offsetx && startpointy != ev.offsety) && !isnewshape) {      //keeping average gap of 5 pixels canvas smaller , can't exact start point     if ((math.abs(startpointx - ev.offsetx) < 5) && (math.abs(startpointy - ev.offsety) < 5) && isnewshape) {         alert('point matched');          isnewshape = false ;         context.clearrect(0, 0, canvas.width, canvas.height);         context.beginpath();         context.moveto(endpointx, endpointy);         //context.moveto(ev.offsetx, ev.offsety);         context.lineto(startpointx, startpointy);         startpointx = "";         startpointy = "";         endpointx = "";         endpointy = "";         context.strokestyle = strokecolor;         context.stroke();         context.closepath();         img_update();         tool.started = false;     }     else {         if (startpointx == "" || startpointy == "" || endpointx == "" || endpointy == "")             return;          context.clearrect(0, 0, canvas.width, canvas.height);         context.beginpath();         context.moveto(endpointx, endpointy);         isnewshape = false;         context.lineto(ev.offsetx, ev.offsety);         endpointx = ev.offsetx;         endpointy = ev.offsety;         context.strokestyle = strokecolor;         context.stroke();         context.closepath();         img_update();     }   };  this.mousemove = function (ev) {     if (!tool.started) {         return;     }     console.log('mousemove');     context.clearrect(0, 0, canvas.width, canvas.height);     context.beginpath();     context.moveto(startpointx, startpointy);     context.lineto(ev.offsetx, ev.offsety);     endpointx = ev.offsetx;     endpointy = ev.offsety;     context.strokestyle = strokecolor;     context.stroke();     context.closepath(); };  this.mouseup = function (ev) {     if (tool.started) {         console.log('mouseup');        isnewshape = true;         tool.started = false;         img_update();     } }; 

}

canvas bit-map image. whatever draw on canvas stored pixels not shapes. once drawn cannot go , fill shape. can create shapes objects properties, such fill colour , steps redraw shape. when wish change properties within object , re-draw canvas.

i have constructed project among other things draws polygons can re-colour.

my project http://canvimation.github.com/ may want.

help files @ https://sites.google.com/site/canvimationhelp/

the source code project @ https://github.com/canvimation/canvimation.github.com

please feel free fork or copy code should wish so.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -