html - How to make a counter using a canvas in javascript -


i'm trying make counter every time click mouse shows number of clicks in canvas. isn't updating canvas.

<!doctype html> <html> <body>  <canvas id="mycanvas" width="300" height="150" style="border:1px solid #d3d3d3;"> browser not support html5 canvas tag.</canvas>   <button onclick="function1()">click here</button>  <script> var animate = setinterval(window.requestanimationframe,1)  var clicks = 0;         function function1()         {         clicks++;         document.getelementbyid('mycanvas').innerhtml = clicks;         };  var c=document.getelementbyid("mycanvas"); var ctx=c.getcontext("2d"); ctx.font="16px verdana"; ctx.filltext("score: " + clicks,190,20); </script> </body> </html> 

so changed code add redraw() function redraw on canvas new number.

and increment() function call 1 after doing clicks++.

var clicks = 0; function increment(){     clicks++;     redraw(); }; var c=document.getelementbyid("mycanvas"); var ctx=c.getcontext("2d"); console.log(ctx); function redraw(){     ctx.clearrect(0, 0, c.width, c.height);     ctx.font="16px verdana";     ctx.filltext("score: " + clicks,190,20); } redraw(); 

you can't use innerhtml canvas because surface drawing on not affected it's inner dom.

here working jsfiddle demo.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -