{"id":2437,"date":"2011-10-22T17:03:37","date_gmt":"2011-10-22T22:03:37","guid":{"rendered":"http:\/\/www.lonhosford.com\/lonblog\/?p=2437"},"modified":"2015-07-31T18:25:49","modified_gmt":"2015-07-31T23:25:49","slug":"html5-canvas-bobbing-circle-animation-demonstrating-sine-wave-movement","status":"publish","type":"post","link":"https:\/\/www.lonhosford.com\/lonblog\/2011\/10\/22\/html5-canvas-bobbing-circle-animation-demonstrating-sine-wave-movement\/","title":{"rendered":"HTML5 Canvas Bobbing Circle Animation Demonstrating Sine Wave Movement"},"content":{"rendered":"<p>This is an example of using the HTML5 canvas and using JavaScript Math.sin to produce a bobbing animation of a circle. <img loading=\"lazy\" decoding=\"async\" alt=\"\" src=\"https:\/\/lh5.googleusercontent.com\/-Q1AOCEfKFCw\/TqMg8U-onLI\/AAAAAAAAGJk\/BXAnMHMSH1g\/s800\/sine_wave_bobbing_published.jpg\" class=\"alignleft\" width=\"288\" height=\"288\" \/> <\/p>\n<p><strong><a  href=\"\/content\/html5\/canvas\/sine_wave_bobbing.html\" target = \"_blank\">Demo<\/a><\/strong><\/p>\n<p><strong><a onclick=\"javascript: pageTracker._trackPageview('\/downloads\/html5\/canvas\/sine_wave_bobbing.html.zip'); \" href=\"https:\/\/www.lonhosford.com\/content\/html5\/canvas\/sine_wave_bobbing.html.zip\">Download Source<\/a><\/strong><\/p>\n<p>This example is in one HTML document that contains all the JavaScript. <figure style=\"width: 100px\" class=\"wp-caption alignright\"><a href=\"http:\/\/www.amazon.com\/gp\/product\/1430236655\/ref=as_li_ss_il?ie=UTF8&#038;tag=hosfordusa&#038;linkCode=as2&#038;camp=217145&#038;creative=399373&#038;creativeASIN=1430236655\"><img decoding=\"async\" border=\"0\" src=\"http:\/\/ws.assoc-amazon.com\/widgets\/q?_encoding=UTF8&#038;Format=_SL110_&#038;ASIN=1430236655&#038;MarketPlace=US&#038;ID=AsinImage&#038;WS=1&#038;tag=hosfordusa&#038;ServiceVersion=20070822\" ><\/a><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.assoc-amazon.com\/e\/ir?t=hosfordusa&#038;l=as2&#038;o=1&#038;a=1430236655&#038;camp=217145&#038;creative=399373\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" style=\"border:none !important; margin:0px !important;\" \/><a href=\"http:\/\/www.amazon.com\/gp\/product\/1430236655\/ref=as_li_ss_tl?ie=UTF8&#038;tag=hosfordusa&#038;linkCode=as2&#038;camp=217145&#038;creative=399373&#038;creativeASIN=1430236655\">Foundation HTML5 Animation with JavaScript<\/a><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.assoc-amazon.com\/e\/ir?t=hosfordusa&#038;l=as2&#038;o=1&#038;a=1430236655&#038;camp=217145&#038;creative=399373\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" style=\"border:none !important; margin:0px !important;\" \/><br \/>\n<figcaption class=\"wp-caption-text\">Learn More<\/figcaption><\/figure><\/p>\n<p>JQuery is included only to detect when the document is ready and start the timer for the animation. The timer calls an update function which updates the data values for the items on the canvas. In this case it is just the animated object. The timer also calls the draw function that calls the animated object&#8217;s draw function. <\/p>\n<p>I am using the Google CDN for JQuery.<\/p>\n<p>This example adds the canvas node to the body dynamically in JavaScript and is just a technique. You could include the canvas tag statically.<\/p>\n<p>You can view the full html file at the end of the post. I will excerpt parts to explain what is going on inside the code.<\/p>\n<p>[ad name=&#8221;Google Adsense&#8221;]<\/p>\n<p>Once JQuery is ready the animation timer is started. The timer calls the update and draw methods. <\/p>\n<pre class=\"brush: jscript; first-line: 70; title: ; notranslate\" title=\"\">\r\n\t\/\/ JQuery ready\r\n\t$(function() \r\n\t{\r\n\t\t\/\/ Start here.\r\n\t\t\/\/ Timer for animation.\r\n\t\tsetInterval(function() \r\n\t\t{\r\n\t\t\tupdate();\r\n\t\t\tdraw();\r\n\t\t}, 1000\/FPS);\r\n\t});\r\n<\/pre>\n<p>This is the draw method. It simply clears the canvas, draws the canvas background and border, and then calls the draw method of the animation object called player. <\/p>\n<pre class=\"brush: jscript; first-line: 61; title: ; notranslate\" title=\"\">\r\n\t\/\/ Draw the views\r\n\tfunction draw() \r\n\t{\r\n\t\tcanvas.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);\r\n\t\tcanvas.strokeStyle = &quot;red&quot;;\r\n\t\tcanvas.strokeRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);\r\n\t\tcanvas.fillStyle = &quot;#ccc&quot;;\r\n\t\tcanvas.fillRect(.5, .5, CANVAS_WIDTH-1, CANVAS_HEIGHT-1);\r\n\t\tplayer.draw();\r\n\t}\r\n<\/pre>\n<p>The model animation data is maintained in the update function. There is the angle and the player x and y positions to update.<\/p>\n<p>The player x value is the horizontal center of the canvas. That does not change in this example.<\/p>\n<p>Line 57 computes the player y value. The values of 1 to -1 are computed from the angle using the Math.sin function. The angle variable increments infinitely. The Math.sin function uses the angle values as real possible angles in geometry you are accustomed to using.<\/p>\n<p>The yRange value of 200 multiplied by Math.sin(angle) results in values between -200 and 200. <\/p>\n<p>Adding the result to canvasCenterY, the vertical center of the canvas, creates the y value plus or minus the vertical center of the canvas.<\/p>\n<pre class=\"brush: jscript; first-line: 49; title: ; notranslate\" title=\"\">\r\n\t\/\/ Update the model data.\r\n\tfunction update() \r\n\t{\r\n\t\t\/\/ Coordinate for x is center canvas width.\r\n\t\tplayer.x = canvasCenterX;\r\n\t\t\/\/ Coordinate y is the sine of the angle \r\n\t\t\/\/ for a positive or negative percentage of the range \r\n\t\t\/\/ using the canvas vertical for the center of the range.\r\n\t\tplayer.y = canvasCenterY + Math.sin(angle) * yRange;\r\n\t\t\/\/ Angle changed by speed.\r\n\t\tangle += speed;\r\n\t}\r\n<\/pre>\n<p>[ad name=&#8221;Google Adsense&#8221;]<br \/>\nThis is the object that is being animated. It contains some basic properties for a circle and contains a method for drawing.<\/p>\n<pre class=\"brush: jscript; first-line: 30; title: ; notranslate\" title=\"\">\r\n\tvar player = {\r\n\t\tcolor: &quot;#00A&quot;,\r\n\t\tx: 0,  \t\/\/ Starting x\r\n\t\ty: 0,\t\/\/ Starting y\r\n\t\twidth: 32,\r\n\t\theight: 32,\r\n\t\tradius: 20,\r\n\t\tdraw: function() \/\/ Draw the player.\r\n\t\t{\r\n\t\t\tcanvas.fillStyle = this.color;\t\t\r\n\t\t\tcanvas.beginPath();  \r\n\t\t\tcanvas.arc(this.x,this.y,this.radius,0,Math.PI*2,true); \r\n\t\t\tcanvas.fill();\r\n\t\t\tcanvas.lineWidth = 5;\r\n    \t\tcanvas.strokeStyle = &quot;black&quot;;\r\n    \t\tcanvas.stroke();\r\n\t  \t}\r\n\t};\t\r\n<\/pre>\n<p>The entire HTML document for your convenience to view and copy.<\/p>\n<pre class=\"brush: xml; collapse: true; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n&lt;!DOCTYPE HTML&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta charset=&quot;UTF-8&quot;&gt;\r\n&lt;script language=&quot;javascript&quot; src=&quot;https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.6.4\/jquery.min.js&quot; type=&quot;text\/javascript&quot;&gt;&lt;\/script&gt;\r\n&lt;title&gt;HTML5 Canvas Based Animation Sine Wave Movement Using Bobbing Circle&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;script type='text\/javascript'&gt;\r\n\tvar CANVAS_WIDTH = 480;\t\t\/\/ Canvas width\r\n\tvar CANVAS_HEIGHT = 480;\t\/\/ Canvas height\r\n\tvar FPS = 30;\t\t\t\t\/\/ Frames per second\r\n\t\/\/ Canvas center points adjusted for stroke.\r\n\tvar canvasCenterX = (CANVAS_WIDTH - 1) \/ 2;\r\n\tvar canvasCenterY = (CANVAS_HEIGHT - 1) \/ 2;\r\n\t\/\/ Speed of movement\r\n\tvar speed = .05;\r\n\t\/\/ Angle for computing cosine and sin.\r\n\tvar angle = 0;\r\n\t\/\/ Range of the vertical movement.\r\n\tvar yRange = 200;\r\n\t\/\/ Create a canvas element variable.\r\n\tvar canvasElement = $(&quot;&lt;canvas width='&quot; + CANVAS_WIDTH + \r\n\t  &quot;' height='&quot; + CANVAS_HEIGHT + &quot;'&gt;&lt;\/canvas&quot;);\r\n\t\/\/ Reference to the canvas 2d context.\r\n\tvar canvas = canvasElement.get(0).getContext(&quot;2d&quot;);\r\n\t\/\/ Dynamically append a canvas element to the body tag.\r\n\tcanvasElement.appendTo('body');\t\r\n\t\/\/ Object defining the player we are moving.\r\n\tvar player = {\r\n\t\tcolor: &quot;#00A&quot;,\r\n\t\tx: 0,  \t\/\/ Starting x\r\n\t\ty: 0,\t\/\/ Starting y\r\n\t\twidth: 32,\r\n\t\theight: 32,\r\n\t\tradius: 20,\r\n\t\tdraw: function() \/\/ Draw the player.\r\n\t\t{\r\n\t\t\tcanvas.fillStyle = this.color;\t\t\r\n\t\t\tcanvas.beginPath();  \r\n\t\t\tcanvas.arc(this.x,this.y,this.radius,0,Math.PI*2,true); \r\n\t\t\tcanvas.fill();\r\n\t\t\tcanvas.lineWidth = 5;\r\n    \t\tcanvas.strokeStyle = &quot;black&quot;;\r\n    \t\tcanvas.stroke();\r\n\t  \t},\r\n\t};\t\r\n\t\/\/ Update the model data.\r\n\tfunction update() \r\n\t{\r\n\t\t\/\/ Coordinate for x is center canvas width.\r\n\t\tplayer.x = canvasCenterX;\r\n\t\t\/\/ Coordinate y is the sine of the angle \r\n\t\t\/\/ for a positive or negative percentage of the range \r\n\t\t\/\/ using the canvas vertical for the center of the range.\r\n\t\tplayer.y = canvasCenterY + Math.sin(angle) * yRange;\r\n\t\t\/\/ Angle changed by speed.\r\n\t\tangle += speed;\r\n\t}\r\n\t\/\/ Draw the views\r\n\tfunction draw() \r\n\t{\r\n\t\tcanvas.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);\r\n\t\tcanvas.strokeStyle = &quot;red&quot;;\r\n\t\tcanvas.strokeRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);\r\n\t\tcanvas.fillStyle = &quot;#ccc&quot;;\r\n\t\tcanvas.fillRect(.5, .5, CANVAS_WIDTH-1, CANVAS_HEIGHT-1);\r\n\t\tplayer.draw();\r\n\t}\r\n\t\/\/ JQuery ready\r\n\t$(function() \r\n\t{\r\n\t\t\/\/ Start here.\r\n\t\t\/\/ Timer for animation.\r\n\t\tsetInterval(function() \r\n\t\t{\r\n\t\t\tupdate();\r\n\t\t\tdraw();\r\n\t\t}, 1000\/FPS);\r\n\t});\r\n&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<div id=\"fb-root\"><\/div>\n<p><script src=\"https:\/\/connect.facebook.net\/en_US\/all.js#appId=105467682877384&amp;xfbml=1\"><\/script><fb:like href=\"https:\/\/www.lonhosford.com\/lonblog\/2011\/10\/22\/html5-canvas-bobbing-circle-animation-demonstrating-sine-wave-movement\/\" send=\"true\" width=\"450\" show_faces=\"true\" font=\"\"><\/fb:like><\/p>\n<div id=\"fb-root\"><\/div>\n<p><script src=\"https:\/\/connect.facebook.net\/en_US\/all.js#xfbml=1\"><\/script><fb:comments href=\"https:\/\/www.lonhosford.com\/lonblog\/2011\/10\/22\/html5-canvas-bobbing-circle-animation-demonstrating-sine-wave-movement\/\" num_posts=\"3\" width=\"500\"><\/fb:comments><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is an example of using the HTML5 canvas and using JavaScript Math.sin to produce a bobbing animation of a circle. Demo Download Source This example is in one HTML document that contains all the JavaScript. JQuery is included only to detect when the document is ready and start the timer for the animation. The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[74,75,76],"class_list":["post-2437","post","type-post","status-publish","format-standard","hentry","category-general","tag-html","tag-html5-2","tag-html5-canvas"],"_links":{"self":[{"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/posts\/2437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/comments?post=2437"}],"version-history":[{"count":54,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/posts\/2437\/revisions"}],"predecessor-version":[{"id":3669,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/posts\/2437\/revisions\/3669"}],"wp:attachment":[{"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/media?parent=2437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/categories?post=2437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/tags?post=2437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}