{"id":1358,"date":"2010-09-03T15:56:59","date_gmt":"2010-09-03T21:56:59","guid":{"rendered":"http:\/\/www.evolutionarydesigns.net\/blog\/2010\/09\/03\/use-the-canvas-element-in-html-to-create-graphics\/"},"modified":"2010-09-12T13:21:38","modified_gmt":"2010-09-12T19:21:38","slug":"use-the-canvas-element-in-html-to-create-graphics","status":"publish","type":"post","link":"http:\/\/www.evolutionarydesigns.net\/blog\/2010\/09\/03\/use-the-canvas-element-in-html-to-create-graphics\/","title":{"rendered":"Use The Canvas Element in HTML To create Graphics"},"content":{"rendered":"<p><a href=\"https:\/\/i0.wp.com\/www.evolutionarydesigns.net\/blog\/wp-content\/uploads\/2010\/09\/1283549784_Firefoxpandaroux.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" style=\"display: inline; margin: 0px 10px 0px 0px; border-width: 0px;\" title=\"Firefox Rules!\" src=\"https:\/\/i0.wp.com\/www.evolutionarydesigns.net\/blog\/wp-content\/uploads\/2010\/09\/1283549784_Firefoxpandaroux_thumb.png?resize=200%2C200\" border=\"0\" alt=\"Firefox Rules!\" width=\"200\" height=\"200\" align=\"left\" \/><\/a> I often see articles, tutorials and info sites focusing <a href=\"http:\/\/en.wikipedia.org\/wiki\/HTML5\" target=\"_blank\">HTML5<\/a> and from those articles I see a lot of benefits to HTML5. At the same time, I get clients and friends asking me about what\u2019s <a class=\"zem_slink\" title=\"HTML\" rel=\"wikipedia\" href=\"http:\/\/en.wikipedia.org\/wiki\/HTML\">HTML<\/a> and do they do they need it on their sites. To be honest,\u00a0 you do not really or at least not yet. Its still under development and not all browsers support HTML5. Once HTML5 is fully out of testing more browsers will support HTML5. When that happens, it may be time to upgrade your site. It will be the new standard that has features such as video play back, drag-and-drop, and a variety other integrated multimedia features. What this means is, that users that are on systems such as <a class=\"zem_slink\" title=\"iPad\" rel=\"homepage\" href=\"http:\/\/www.apple.com\/ipad\/\">iPads<\/a> and other non flash using hardware, will be able to view embedded videos or mp3s embedded in websites with out a plugin. Of course this is just my understanding of what HTML5 is and what is possible. I am still researching this topic and so far I love it. As I learn more, I will share my findings with you.<\/p>\n<h1>HTML5 Can Create Images<\/h1>\n<p>Over the last few weeks, I have stumbled across several short and sweet <a class=\"zem_slink\" title=\"Snippet (programming)\" rel=\"wikipedia\" href=\"http:\/\/en.wikipedia.org\/wiki\/Snippet_%28programming%29\">code snippets<\/a> you can use on your site or just to play around with it. All the code can be copied into your own projects. The code was put together from various sites and modification by me.<\/p>\n<p><strong><span style=\"color: #ff0000;\">NOTE:<\/span> Not all browsers support HTML5\/CSS3 including Windows Explore. But there are work a rounds for them. <\/strong><\/p>\n<p>Over the next few posts, I will share some examples of my favorite code snippets and I did to them or how its used on a project. Also, keep in mind some of the more graphic intensive code can slow your load time.<\/p>\n<h2>Canvas Element<\/h2>\n<p><a href=\"https:\/\/i0.wp.com\/www.evolutionarydesigns.net\/blog\/wp-content\/uploads\/2010\/09\/canvas3.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" style=\"display: inline; border: 0px;\" title=\"image created by HTML canvas \/JavaScript \" src=\"https:\/\/i0.wp.com\/www.evolutionarydesigns.net\/blog\/wp-content\/uploads\/2010\/09\/canvas_thumb3.png?resize=619%2C204\" border=\"0\" alt=\"image created by HTML canvas \/JavaScript \" width=\"619\" height=\"204\" \/><\/a><\/p>\n<p>Check out the live <a href=\"http:\/\/www.evolutionarydesigns.net\/demo\/canvasdemo2.html\" target=\"_blank\">Demo<\/a>.<\/p>\n<p>One of the new elements of HTML5 is the <a class=\"zem_slink\" title=\"Canvas element\" rel=\"wikipedia\" href=\"http:\/\/en.wikipedia.org\/wiki\/Canvas_element\">canvas element<\/a> which allows for dynamic scriptable rendering of 2D shapes and <a class=\"zem_slink\" title=\"Bitmap\" rel=\"wikipedia\" href=\"http:\/\/en.wikipedia.org\/wiki\/Bitmap\">bitmap<\/a> images. Canvas is a low level procedural model that will update a bitmap. The canvas element has a drawable\u00a0 region that is defined in the HTML code using the height and width attributes. To access the drawable area, <a href=\"http:\/\/javascript.internet.com\/\" target=\"_blank\">JavaScript <\/a>is used.<\/p>\n<p>The above image is an animated graphic created by using the canvas element and JavaScript. Below is the code I used to create the image. To modify it, you can change the #hex color codes and the dimensions.<\/p>\n<h3>Code Only<\/h3>\n<p>[js]<br \/>\n &lt;canvas id=&quot;demoCanvas&quot; width=&quot;1200&quot; height=&quot;400&quot;&gt;&lt;\/canvas&gt;<br \/>\n&lt;script type=&quot;text\/javascript&quot;&gt;<br \/>\nvar canvas, graphics;<br \/>\nfunction draw_init() {<br \/>\n canvas = document.getElementById(&#8216;demoCanvas&#8217;);<br \/>\n if (canvas.getContext) {<br \/>\n graphics = canvas.getContext(&#8216;2d&#8217;);<br \/>\n }<br \/>\n draw();<br \/>\n}<br \/>\nfunction draw() {<br \/>\n var theme = [&quot;#0042B2&quot;,&quot;#007DB2&quot;,&quot;#0006B2&quot;,&quot;#FFFFFF&quot;,&quot;#000000&quot;];<br \/>\n var x = Math.random() * 1200;<br \/>\n var y = Math.random() * 400;<br \/>\n var size = (Math.random() * 100) + 20;<br \/>\n var num_circles = (Math.random() * 10) + 2;<br \/>\n for (var i = size; i &gt; 0; i-= (size\/num_circles)) {<br \/>\n graphics.fillStyle = theme[ (Math.random() * 5 &gt;&gt; 0)];<br \/>\n graphics.beginPath();<br \/>\n graphics.arc(x, y, i * .5, 0, Math.PI*2, true);<br \/>\n graphics.closePath();<br \/>\n graphics.fill();<br \/>\n }<br \/>\n t = setTimeout(&#8216;draw()&#8217;,1000);<br \/>\n}<br \/>\ndraw_init();<br \/>\n&lt;\/script&gt;<br \/>\n[\/js]<\/p>\n<h3>Demo Site Code<\/h3>\n<p>[html]<br \/>\n&lt;html&gt;<br \/>\n &lt;head&gt;<br \/>\n&lt;title&gt;HTML5\/CSS3 Canvas Demo and Tutorial&lt;\/title&gt;<br \/>\n&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<br \/>\n&lt;H1 align=&quot;center&quot;&gt;HTML5\/CSS3 Canvas Demo and Tutorial&lt;\/H2&gt;<\/p>\n<p>  &lt;canvas id=&quot;demoCanvas&quot; width=&quot;1200&quot; height=&quot;400&quot;&gt;&lt;\/canvas&gt;<br \/>\n&lt;script type=&quot;text\/javascript&quot;&gt;<br \/>\nvar canvas, graphics;<br \/>\nfunction draw_init() {<br \/>\n\tcanvas = document.getElementById(&#8216;demoCanvas&#8217;);<br \/>\n\tif (canvas.getContext) {<br \/>\n\t\tgraphics = canvas.getContext(&#8216;2d&#8217;);<br \/>\n\t}<br \/>\n\tdraw();<br \/>\n}<br \/>\nfunction draw() {<br \/>\n\tvar theme = [&quot;#0042B2&quot;,&quot;#007DB2&quot;,&quot;#0006B2&quot;,&quot;#FFFFFF&quot;,&quot;#000000&quot;];<br \/>\n\tvar x = Math.random() * 1200;<br \/>\n\tvar y = Math.random() * 400;<br \/>\n\tvar size = (Math.random() * 100) + 20;<br \/>\n\tvar num_circles = (Math.random() * 10) + 2;<br \/>\n\tfor (var i = size; i &gt; 0; i-= (size\/num_circles)) {<br \/>\n\t\tgraphics.fillStyle = theme[ (Math.random() * 5 &gt;&gt; 0)];<br \/>\n\t\tgraphics.beginPath();<br \/>\n\t\tgraphics.arc(x, y, i * .5, 0, Math.PI*2, true);<br \/>\n\t\tgraphics.closePath();<br \/>\n\t\tgraphics.fill();<br \/>\n\t}<br \/>\n\tt = setTimeout(&#8216;draw()&#8217;,1000);<br \/>\n}<br \/>\ndraw_init();<br \/>\n&lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<br \/>\n[\/html]<\/p>\n<div class=\"zemanta-pixie\" style=\"margin-top: 10px; height: 15px;\"><img data-recalc-dims=\"1\" decoding=\"async\" class=\"zemanta-pixie-img\" style=\"border: medium none; float: right;\" src=\"https:\/\/i0.wp.com\/img.zemanta.com\/pixy.gif\" alt=\"\" \/><span class=\"zem-script pretty-attribution\"><script src=\"http:\/\/static.zemanta.com\/readside\/loader.js\" type=\"text\/javascript\"><\/script><\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I often see articles, tutorials and info sites focusing HTML5 and from those articles I see a lot of benefits to HTML5. At the same time, I get clients and friends asking me about what\u2019s HTML and do they do they need it on their sites. To be honest,\u00a0 you do not really or at [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","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":""},"categories":[6],"tags":[581,121,181,358,359,133,589,1484],"class_list":["post-1358","post","type-post","status-publish","format-standard","hentry","category-web-design","tag-canvas-element","tag-css3","tag-firefox","tag-html","tag-html5","tag-javascript","tag-markup-language","tag-tutorials","entry","has-post-thumbnail"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pJPdG-lU","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.evolutionarydesigns.net\/blog\/wp-json\/wp\/v2\/posts\/1358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.evolutionarydesigns.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.evolutionarydesigns.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.evolutionarydesigns.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.evolutionarydesigns.net\/blog\/wp-json\/wp\/v2\/comments?post=1358"}],"version-history":[{"count":0,"href":"http:\/\/www.evolutionarydesigns.net\/blog\/wp-json\/wp\/v2\/posts\/1358\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.evolutionarydesigns.net\/blog\/wp-json\/wp\/v2\/media?parent=1358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.evolutionarydesigns.net\/blog\/wp-json\/wp\/v2\/categories?post=1358"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.evolutionarydesigns.net\/blog\/wp-json\/wp\/v2\/tags?post=1358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}