{"id":180,"date":"2011-08-23T09:55:19","date_gmt":"2011-08-23T14:55:19","guid":{"rendered":"http:\/\/www.rymocs.com\/blog3\/?p=180"},"modified":"2011-08-23T09:55:19","modified_gmt":"2011-08-23T14:55:19","slug":"html5-drawing-images-and-adding-text-to-the-canvas-element","status":"publish","type":"post","link":"http:\/\/www.rymocs.com\/blog3\/2011\/08\/23\/html5-drawing-images-and-adding-text-to-the-canvas-element\/","title":{"rendered":"HTML5: Drawing images and adding text to the canvas element"},"content":{"rendered":"<p>In the previous post, \u201c<a href=\"http:\/\/www.techrepublic.com\/blog\/webmaster\/html5-working-with-the-canvas-element\/565?tag=content;blog-list-river\" target=\"_blank\">HTML5: Working with the canvas element<\/a>,\u201d I reviewed several ways to manipulate this element, including creating rectangles, circles, triangles, and diamond shapes by drawing paths, and with various shades using rgba. In this segment we will review drawing images and playing around with adding text to the canvas.<\/p>\n<p>The <em>canvas drawImage<\/em><strong> <\/strong>function using JavaScript allows you to create an image object within the defined canvas area. The <em>drawImage<\/em> function includes three parameters and is expressed in the form <em>drawImage(object image, float x, float y)<\/em>. The image file formats supported within the canvas element and <em>drawImage<\/em> function are .jpg, .gif, and .png. For the draw image function to work, we have to set the variable and define the image object. For this example, we set it to \u201cimg\u201d as is expressed in <em>var img = new Image();<\/em> and then the image source is provided, followed by the image onload, and the final <em>drawImage <\/em>function with the parameters defined as in the example below:<\/p>\n<pre>\/\/ Draw Image function<\/pre>\n<pre>var img = new Image();<\/pre>\n<pre>img.src = \"images\/Figure_A.png\";<\/pre>\n<pre>img.onload = function() {<\/pre>\n<pre>context.drawImage(img, 145, 145);<\/pre>\n<pre>};<\/pre>\n<p>The resulting canvas image is displayed below as shown in Firefox 3.6:<\/p>\n<h4>Figure A<\/h4>\n<p><a href=\"http:\/\/i.techrepublic.com.com\/blogs\/08182011figure_a.gif\" target=\"_blank\"><img loading=\"lazy\" title=\"08182011figure_a\" src=\"http:\/\/i.techrepublic.com.com\/blogs\/08182011figure_a.gif\" alt=\"\" width=\"183\" height=\"186\" \/><\/a><\/p>\n<p>(<a href=\"http:\/\/www.techrepublic.com\/blog\/webmaster\/html5-drawing-images-and-adding-text-to-the-canvas-element\/586?tag=content;blog-list-river\" target=\"_blank\">Read more&#8230;<\/a>)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous post, \u201cHTML5: Working with the canvas element,\u201d I reviewed several ways to manipulate this element, including creating rectangles, circles, triangles, and diamond shapes by drawing paths, and&hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10,5],"tags":[],"_links":{"self":[{"href":"http:\/\/www.rymocs.com\/blog3\/wp-json\/wp\/v2\/posts\/180"}],"collection":[{"href":"http:\/\/www.rymocs.com\/blog3\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.rymocs.com\/blog3\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.rymocs.com\/blog3\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.rymocs.com\/blog3\/wp-json\/wp\/v2\/comments?post=180"}],"version-history":[{"count":2,"href":"http:\/\/www.rymocs.com\/blog3\/wp-json\/wp\/v2\/posts\/180\/revisions"}],"predecessor-version":[{"id":182,"href":"http:\/\/www.rymocs.com\/blog3\/wp-json\/wp\/v2\/posts\/180\/revisions\/182"}],"wp:attachment":[{"href":"http:\/\/www.rymocs.com\/blog3\/wp-json\/wp\/v2\/media?parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.rymocs.com\/blog3\/wp-json\/wp\/v2\/categories?post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.rymocs.com\/blog3\/wp-json\/wp\/v2\/tags?post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}