{"id":2121,"date":"2010-12-18T23:09:01","date_gmt":"2010-12-19T04:09:01","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=2121"},"modified":"2010-12-18T23:09:01","modified_gmt":"2010-12-19T04:09:01","slug":"playing-with-basics-of-html5-canvas","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/playing-with-basics-of-html5-canvas\/","title":{"rendered":"Playing with basics of HTML5 Canvas"},"content":{"rendered":"<p>Here&#8217;s a snippet of something I did tonight to play a bit with the Canvas and 2d Graphics context objects in javascript.<\/p>\n<p>[html]<br \/>\n&lt;html&gt;<br \/>\n  &lt;head&gt;<br \/>\n    &lt;title&gt;Playing with Canvas&lt;\/title&gt;<br \/>\n  &lt;\/head&gt;<\/p>\n<p>  &lt;body style=&quot;padding:0 0; margin: 0 0; background:black&quot;&gt;<\/p>\n<p>    &lt;canvas id=&quot;myCanvas&quot; width=&quot;1024&quot; height=&quot;1024&quot; &gt;&lt;\/canvas&gt;<\/p>\n<p>    &lt;script type=&quot;text\/javascript&quot;&gt;<\/p>\n<p>    \/\/convert to CSS friendly Hex String<br \/>\n    function d2h (n) {<br \/>\n      var result = n.toString(16);<br \/>\n      if (n &lt; 16) {<br \/>\n\tresult = &#8216;0&#8217;+result;<br \/>\n      }<br \/>\n      return result;<br \/>\n    }<\/p>\n<p>    var preparingBackground = true;<br \/>\n    var canvas = document.getElementById(&quot;myCanvas&quot;);<br \/>\n    var ctx = canvas.getContext(&quot;2d&quot;);<\/p>\n<p>    var canvasWidth = parseInt(canvas.getAttribute(&quot;width&quot;));<br \/>\n    var canvasHeight = parseInt(canvas.getAttribute(&quot;height&quot;));<\/p>\n<p>    var r=255; var red;<br \/>\n    var g=255; var green;<br \/>\n    var b=255; var blue;<\/p>\n<p>    var rectHeight = canvasHeight\/256;<br \/>\n    var rectWidth = canvasWidth\/256;<\/p>\n<p>    var rectX = 0;<br \/>\n    var rectY = 0;<\/p>\n<p>    var Rect = function(xval,yval,speedXval,speedYval,r,g,b) {<br \/>\n      this.x = xval;<br \/>\n      this.y = yval;<\/p>\n<p>      this.speedX = speedXval;<br \/>\n      this.speedY = speedYval;<\/p>\n<p>      this.r = r;<br \/>\n      this.g = g;<br \/>\n      this.b = b;<\/p>\n<p>      \/\/so colors dont jump from 255 to 0.<br \/>\n      this.rDelta = 1;<br \/>\n      this.gDelta = 1;<br \/>\n      this.bDelta = 1;<\/p>\n<p>      this.width = 1;<br \/>\n      this.height = 1;<\/p>\n<p>      \/\/so rectangle colors wont change so fast.<br \/>\n      this.colorChangeDefaultValue=1;<br \/>\n      this.colorChangeAcumulator=1;<\/p>\n<p>      this.move = function () {<br \/>\n\tthis.x = this.x+this.speedX;<br \/>\n        this.y = this.y+this.speedY;<\/p>\n<p>\t\/\/if you reach a borders change direction.<br \/>\n        if ((this.x+this.width &gt;= canvasWidth &amp;&amp; this.speedX &gt; 0) ||<br \/>\n\t    (this.x &lt;= 0 &amp;&amp; this.speedX &lt; 0)) {<br \/>\n\t  this.speedX = this.speedX*-1;<br \/>\n\t}<\/p>\n<p>        if ((this.y+this.height &gt;= canvasHeight &amp;&amp; this.speedY &gt; 0) ||<br \/>\n\t    (this.y &lt;= 0 &amp;&amp; this.speedY &lt; 0)) {<br \/>\n\t  this.speedY = this.speedY*-1;<br \/>\n\t}<br \/>\n      }<\/p>\n<p>      \/\/draw and change colors<br \/>\n      this.draw = function () {<br \/>\n\t\/\/play with color gradients when you have a chance&#8230;<br \/>\n\tif (this.colorChangeAcumulator&#8211;==0) {<br \/>\n\t  if ((this.r &gt;= 255 &amp;&amp; this.rDelta &gt; 0) ||<br \/>\n              (this.r &lt;= 0 &amp;&amp; this.rDelta &lt; 0)) {<br \/>\n\t    this.rDelta=this.rDelta*-1;<br \/>\n\t  }<\/p>\n<p>\t  if ((this.g &gt;= 255 &amp;&amp; this.gDelta &gt; 0) ||<br \/>\n              (this.g &lt;= 0 &amp;&amp; this.gDelta &lt; 0)) {<br \/>\n\t    this.gDelta=this.gDelta*-1;<br \/>\n\t  }<\/p>\n<p>\t  if ((this.b &gt;= 255 &amp;&amp; this.bDelta &gt; 0) ||<br \/>\n              (this.b &lt;= 0 &amp;&amp; this.bDelta &lt; 0)) {<br \/>\n\t    this.bDelta=this.bDelta*-1;<br \/>\n\t  }<\/p>\n<p>\t  this.r=(this.r+this.rDelta) % 256;<br \/>\n\t  this.g=(this.g+this.gDelta) % 256;<br \/>\n\t  this.b=(this.b+this.bDelta) % 256; <\/p>\n<p>\t  this.colorChangeAcumulator=this.colorChangeDefaultValue;<br \/>\n\t  this.width++;<br \/>\n\t  this.height++;<br \/>\n\t}<\/p>\n<p>\t\/\/paint the reactangle<br \/>\n\tctx.fillStyle=&quot;#&quot;+d2h(this.r)+d2h(this.g)+d2h(this.b);<br \/>\n\tctx.fillRect(this.x,this.y,this.width,this.height);<br \/>\n      }<br \/>\n    }<\/p>\n<p>    \/\/animation interval ids<br \/>\n    var prepareBackgroundIntervalId = 0;<br \/>\n    var rectsIntervalId = 0;<\/p>\n<p>    var speedX = 7;<br \/>\n    var speedY = 7;<\/p>\n<p>    var theRects = [new Rect(0,0,speedX,speedY,10,44,100),<br \/>\n\t\t    new Rect(canvasWidth,canvasHeight,-speedX,-speedY,125,0,0),<br \/>\n\t\t    new Rect(canvasWidth,0,-speedX,speedY,0,210,80),<br \/>\n\t\t    new Rect(0,canvasHeight,speedX,-speedY,40,40,0),<br \/>\n\t\t    new Rect(canvasWidth\/2,0,0,speedY,-4,40,40),<br \/>\n\t\t    new Rect(canvasWidth\/2,canvasHeight,4,-speedY,0,100,250),<br \/>\n\t\t    new Rect(0,canvasHeight\/2,speedX,0,210,100,100),<br \/>\n\t\t    new Rect(canvasWidth,512,-speedX,0,100,10,0)];<\/p>\n<p>    function prepareBackground() {<\/p>\n<p>      if (preparingBackground) {<br \/>\n\tr=r-1;<br \/>\n\tg=g-1;<br \/>\n\tb=b-1;<\/p>\n<p>\tred = d2h(r);<br \/>\n\tgreen = d2h(g);<br \/>\n\tblue = d2h(b);<\/p>\n<p>\tctx.fillStyle = &quot;#&quot;+red+green+blue;<br \/>\n\tctx.fillRect(rectX,rectY,canvas.getAttribute(&quot;width&quot;),rectHeight);<\/p>\n<p>\trectY = rectY + rectHeight;<\/p>\n<p>\tpreparingBackground = !(r==0 &amp;&amp; g==0 &amp;&amp; b==0);<br \/>\n      } else {<br \/>\n\tclearInterval(prepareBackgroundIntervalId);<br \/>\n\trectsIntervalId = setInterval(&quot;moveRects()&quot;,33);<br \/>\n      }<br \/>\n    }<\/p>\n<p>    function moveRects() {<br \/>\n      for (i in theRects) {<br \/>\n\tvar rect = theRects[i];<br \/>\n\trect.draw();<br \/>\n\trect.move();<br \/>\n      }<br \/>\n    }<\/p>\n<p>    prepareBackgroundIntervalId = setInterval(&quot;prepareBackground()&quot;,1);<\/p>\n<p>    &lt;\/script&gt;<br \/>\n  &lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<br \/>\n[\/html]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a snippet of something I did tonight to play a bit with the Canvas and 2d Graphics context objects in javascript. [html] &lt;html&gt; &lt;head&gt; &lt;title&gt;Playing with Canvas&lt;\/title&gt; &lt;\/head&gt; &lt;body style=&quot;padding:0 0; margin: 0 0; background:black&quot;&gt; &lt;canvas id=&quot;myCanvas&quot; width=&quot;1024&quot; height=&quot;1024&quot; &gt;&lt;\/canvas&gt; &lt;script type=&quot;text\/javascript&quot;&gt; \/\/convert to CSS friendly Hex String function d2h (n) { var result [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[30],"tags":[],"class_list":["post-2121","post","type-post","status-publish","format-standard","hentry","category-geeklife"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-yd","jetpack-related-posts":[{"id":2632,"url":"https:\/\/www.gubatron.com\/blog\/javascript-get-n-random-elements-from-a-list\/","url_meta":{"origin":2121,"position":0},"title":"javascript: Get N random elements from a List","author":"gubatron","date":"January 5, 2012","format":false,"excerpt":"[javascript] \/** * Walks linearly through the list to find an element. * returns true if it's found. *\/ function elementIn(collection, element) { for (var i=0; i < collection.length; i++) { if (collection[i]==element) { return true; } } return false; } \/** * Returns a new list of n random\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2671,"url":"https:\/\/www.gubatron.com\/blog\/how-to-shuffle-a-listarray-in-javascript\/","url_meta":{"origin":2121,"position":1},"title":"How to shuffle a List (or Array) in Javascript","author":"gubatron","date":"February 24, 2012","format":false,"excerpt":"[javascript] \/** * Returns number starting from offset up to n-1 *\/ function getRandomWithOffset(n,offset) { return Math.floor(Math.random()*n+offset); } \/** * Returns random integer from 0 to n-1 *\/ function getRandom(n) { return getRandomWithOffset(n,0); } \/** Fisher\u2013Yates shuffle algorithm O(n) *\/ function shuffleList(list) { for (var i=list.length-1; i>0; i--) { var\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2749,"url":"https:\/\/www.gubatron.com\/blog\/deploying-html5-apps-on-cloudfront-with-efficient-invalidation-requests\/","url_meta":{"origin":2121,"position":2},"title":"Deploying HTML5 apps on CloudFront with efficient invalidation requests","author":"gubatron","date":"June 8, 2012","format":false,"excerpt":"So you decided to build your next web app\/site using nothing but HTML5 and Javascript. No server side processing for anything related to UI. This means you will be coding a lot of JavaScript. Wouldn't it be nice to put all that static HTML and JS on your CloudFront CDN\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3,"url":"https:\/\/www.gubatron.com\/blog\/javascript-quicksort-implementation-with-dynamic-comparator\/","url_meta":{"origin":2121,"position":3},"title":"Javascript Quicksort implementation with dynamic comparator.","author":"gubatron","date":"February 29, 2012","format":false,"excerpt":"[javascript] Array.prototype.swap=function(a, b) { var tmp=this[a]; this[a]=this[b]; this[b]=tmp; } function quickSort(array,comparator) { qsort(array,0,array.length,comparator); } \/** * NOTE: the comparator is a dynamic function you will define like so comparator(a,b) { if (a > b) return 1; else if (a < b) return -1; else { return 0; } } *\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":402,"url":"https:\/\/www.gubatron.com\/blog\/an-useful-example-on-how-to-extend-javascript\/","url_meta":{"origin":2121,"position":4},"title":"An useful example on how to extend Javascript","author":"gubatron","date":"November 29, 2006","format":false,"excerpt":"Today I found out I could extend the functionality of __EXISTING__ html classes with javascript. I was manipulating DOM objects, better known as HTMLElement objects, and I needed a way to print the HTML that represents the tag of the object, not what's contained by the tags... a friend told\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":436,"url":"https:\/\/www.gubatron.com\/blog\/how-to-remove-elements-from-a-listbox-in-javascript-ie-and-firefox\/","url_meta":{"origin":2121,"position":5},"title":"How to remove elements from a listbox in Javascript (IE and Firefox)","author":"gubatron","date":"January 11, 2007","format":false,"excerpt":"\/\/By Gubatron, just a silly function to clear the contents of a listbox object var ua = navigator.userAgent.toLowerCase(); var Browser = new Object() Browser.isIE = window.ActiveXObject ? true : false; Browser.isFirefox = (ua.indexOf(\"firefox\")!=-1); function clearListbox(listboxObject) { if (Browser.isFirefox) { \/\/in firefox \"Option\" objects remove themselves. var options = listboxObject.options while\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=2121"}],"version-history":[{"count":0,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2121\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=2121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=2121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=2121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}