How to draw in javascript canvas and compare it to a shape? -
so keep short i'm wondering how able track user drawing moment click when let go , compare check accuracy say, perfect circle?
would possible, , if best ways check accuracy compared perfect circle , how?
also thinking of if user has drawn circle start , end points don't meet, maybe draw line connect them, etc?
i did "stay inside text" game while back. try trace number. more stay inside lines while tracing, higher score is.
you're welcome start , modify needed. :-)
var canvas=document.getelementbyid("canvas"); var ctx=canvas.getcontext("2d"); var $canvas=$("#canvas"); var canvasoffset=$canvas.offset(); var offsetx=canvasoffset.left; var offsety=canvasoffset.top; var scrollx=$canvas.scrollleft(); var scrolly=$canvas.scrolltop(); ctx.font="216px arial"; ctx.fillstyle="white"; ctx.filltext("2",100,200); ctx.stroketext("2",100,200); var cw=canvas.width; var ch=canvas.height; var imgdata=ctx.getimagedata(0,0,cw,ch); var data=imgdata.data; var isdown=false; var wasinside=true; var score=0; function draw(mousex,mousey){ var alpha = data[((cw*mousey)+mousex)*4+3]; score+=(alpha>19?1:-1); if(alpha<20 && wasinside){ wasinside=false; ctx.fillstyle="white"; ctx.fillrect(0,0,cw,ch); ctx.fillstyle="white"; ctx.filltext("2",100,200); ctx.stroketext("2",100,200); }else if(alpha>19 && !wasinside){ wasinside=true; ctx.fillstyle="white"; ctx.fillrect(0,0,cw,ch); ctx.fillstyle="green"; ctx.filltext("2",100,200); ctx.stroketext("2",100,200); } } function handlemousedown(e){ e.preventdefault(); mousex=parseint(e.clientx-offsetx); mousey=parseint(e.clienty-offsety); var alpha = data[((cw*mousey)+mousex)*4+3]; wasinside=(alpha<20); score=0; isdown=true; draw(mousex,mousey); } function handlemouseup(e){ e.preventdefault(); isdown=false; $("#score").text("score: "+score); } 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); draw(mousex,mousey); } $("#canvas").mousedown(function(e){handlemousedown(e);}); $("#canvas").mousemove(function(e){handlemousemove(e);}); $("#canvas").mouseup(function(e){handlemouseup(e);}); $("#canvas").mouseout(function(e){handlemouseout(e);}); $(window).scroll(function(){ var bb=canvas.getboundingclientrect(); offsetx=bb.left; offsety=bb.top; });
body{ background-color: white; } #canvas{border:1px solid red;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <h4>drag inside number<br>number stays green while you're inside.</h4> <h3 id="score">score:</h3> <canvas id="canvas" width=300 height=300></canvas>
Comments
Post a Comment