javascript - this is a program dealing with canvas and java script -
bellow code program suppose lower blinking ball bottom of screen , stay blinking @ bottom. process suppose take place gradually (incrementing factor of 10) happens suddenly. should do?
<!doctype html> <html> <head> <meta charset="iso-8859-1"> <title>animationdemocanvas.html © kari laitinen</title> <script type="text/javascript"> var ball_center_point_x = 300 ; var ball_center_point_y = 240 ; var current_ball_color = "cyan" ; var ball_must_be_shown = true ; function draw_on_canvas() { var canvas = document.getelementbyid( "animation_demo_canvas" ) ; var context = canvas.getcontext("2d") ; context.fillstyle = "rgb( 255, 255, 210 )" ; context.fillrect( 0, 0, canvas.width, canvas.height ) ; if ( ball_must_be_shown == true ) { context.fillstyle = current_ball_color ; context.beginpath() ; ball_center_point_y +=10; context.arc( ball_center_point_x, ball_center_point_y, 50, 0, 2 * math.pi, true ) context.closepath(ball_center_point_y = 429) ; context.stroke() ; context.fill() ; ball_must_be_shown = false ; } else { ball_must_be_shown = true ; } settimeout( function () { draw_on_canvas(); }, 100 ) ; } </script> <style type="text/css"> #centered { width: 600px; height: 500px; margin: 30px auto; border: 1px solid black; } </style> </head> <body onload="draw_on_canvas()"> <div id=centered> <canvas id=animation_demo_canvas width=600 height=500> </canvas> </div> </body> </html>
here's working fiddle you.
var ball_center_point_x = 300 ; var ball_center_point_y = 240 ; var current_ball_color = "cyan" ; var ball_must_be_shown = true ; draw_on_canvas(); function draw_on_canvas() { var canvas = document.getelementbyid( "animation_demo_canvas" ) ; var context = canvas.getcontext("2d") ; context.fillstyle = "rgb( 255, 255, 210 )" ; context.fillrect( 0, 0, canvas.width, canvas.height ) ; //here part in question: if ( ball_must_be_shown == true ) { context.fillstyle = current_ball_color ; context.beginpath() ; if(ball_center_point_y + 50 < canvas.height) ball_center_point_y +=10; context.arc( ball_center_point_x, ball_center_point_y, 50, 0, 2 * math.pi, true ) context.closepath() ; context.stroke() ; context.fill() ; ball_must_be_shown = false ; } else { ball_must_be_shown = true ; } settimeout( draw_on_canvas, 100 ) ; }
basically problems lies in settimeout(), reference function directly, instead of using string. don't settimeout("draw_on_canvas()", 100)
update: if(ball_center_point_y + 50 < canvas.height) ball_center_point_y +=10
;
this portion stops circle going through bottom. since diameter dictated in context.arc() 50 add circle's y-axis(ball_center_point_y), have compared against height of canvas. if condition evaluates true won't update y-axis.
Comments
Post a Comment