<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Monday By Noon &#187; scripting</title>
	<atom:link href="http://mondaybynoon.com/tag/scripting/feed/" rel="self" type="application/rss+xml" />
	<link>http://mondaybynoon.com</link>
	<description>A resource for Web designers and developers to read about and discuss their craft.</description>
	<lastBuildDate>Wed, 08 Feb 2012 13:49:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>&#8226; Working with canvas &#8211; Some Background and Basics</title>
		<link>http://mondaybynoon.com/feeder/?FeederAction=clicked&#038;feed=Posts+%28RSS2%29&#038;seed=http%3A%2F%2Fmondaybynoon.com%2F20090323%2Fworking-with-canvas-some-background-and-basics%2F&#038;seed_title=%26%238226%3B+Working+with+canvas+%26%238211%3B+Some+Background+and+Basics</link>
		<comments>http://mondaybynoon.com/20090323/working-with-canvas-some-background-and-basics/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 13:10:23 +0000</pubDate>
		<dc:creator>Jonathan Christopher</dc:creator>
				<category><![CDATA[Accessibility]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://mondaybynoon.com/?p=422</guid>
		<description><![CDATA[Canvas is an HTML element that provides a surface on which to draw with JavaScript<br /><p><a href='http://rss.buysellads.com/click.php?z=1269068&k=2ee344414ac81fbb0f9de6ab08e9831e&a=422&c=1165797468' target='_blank' rel='nofollow'>
				<img src='http://rss.buysellads.com/img.php?z=1269068&k=2ee344414ac81fbb0f9de6ab08e9831e&a=422&c=1165797468' border='0' alt='' /></a></p><p><a href='http://buysellads.com/buy/sitedetails/pubkey/2ee344414ac81fbb0f9de6ab08e9831e/zone/1269068' target='_blank'>Advertise here with BSA</a></p>]]></description>
			<content:encoded><![CDATA[<p>If I had to pick, I think I&#8217;d choose <code>canvas</code> as the single item I&#8217;m most excited about when it comes to &#8220;bleeding edge&#8221; advancements in front end. I&#8217;d still classify <code>canvas</code> as bleeding edge simply because it&#8217;s still building momentum and catching on in the general Web design population. Although it&#8217;s been around for some time, I still run into many designers who either haven&#8217;t heard of the element, or have heard of it, but don&#8217;t quite get it.</p>
<h2>Some background on <code>canvas</code></h2>
<p>Something many people don&#8217;t know is that <code>canvas</code> was originally introduced by <a href="http://www.apple.com">Apple</a> to be used within <a href="http://www.webkit.org">WebKit</a>. The original intent was to use the element in such environments as applications in <a href="http://en.wikipedia.org/wiki/Dashboard_(software)">Dashboard</a> or new tools in <a href="http://www.apple.com/safari/">Safari</a>. After its implementation in WebKit, <code>canvas</code> was adopted by <a href="https://wiki.mozilla.org/Gecko">Gecko</a> (the rendering engine most notably powering <a href="http://www.mozilla.com/firefox/">Firefox</a>), and <a href="http://www.opera.com/">Opera</a>. It was at that point <code>canvas</code> was <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html">standardized by the WHATWG</a>. While <code>canvas</code> isn&#8217;t supported directly by Internet Explorer, the same functionality can be duplicated using a combination of <a href="http://en.wikipedia.org/wiki/Vector_Markup_Language">VML</a> and JavaScript. The <code>canvas</code> element is officially defined by the <a href="http://www.whatwg.org">WHATWG</a> as:</p>
<blockquote cite="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html">
<p>The <code>canvas</code> element represents a resolution-dependent bitmap <code>canvas</code>, which can be used for rendering graphs, game graphics, or other visual images on the fly.</p>
</blockquote>
<h3>Working with <code>canvas</code></h3>
<p><code>canvas</code> is defined by a height and width, much like many other elements in HTML. A set of drawing functions are offered to JavaScript to allow for the creation of various shapes and other dynamically generated graphics.</p>
<p>To include the <code>canvas</code> element in your document, it&#8217;s a matter of simply including the tag with a few attributes:</p>
<pre class="sh_html"><code>&lt;canvas id=&quot;my_canvas&quot; width=&quot;300&quot; height=&quot;200&quot;&gt;&lt;/canvas&gt;</code></pre>
<p><code>width</code> and <code>height</code> are the only two (optional) attributes of <code>canvas</code>, which can be defined as the snippet above, or using <acronym title="Cascading Style Sheets">CSS</acronym>/<acronym title="Document Object Model">DOM</acronym> properties. If no width and height attributes are defined, the <code>canvas</code> will default to 300px wide and 150px tall. You can also give your <code>canvas</code> an id and/or class, and you&#8217;re also able to define other style properties (e.g. margin).</p>
<p>The <code>canvas</code> element closely resembles an img element we&#8217;ve worked with since we made our first website. A big difference, however, is that <code>canvas</code> is not self-closing. This turns out to be a benefit, as we&#8217;re able to easily include alternate content for those browsers that don&#8217;t actively support canvas:</p>
<pre class="sh_html"><code>&lt;canvas id=&quot;my_canvas&quot; width=&quot;300&quot; height=&quot;200&quot;&gt;
	&lt;img src=&quot;images/car.png&quot; alt=&quot;Blue Honda Civic Si&quot; /&gt;
&lt;/canvas&gt;</code></pre>
<p>While Apple&#8217;s original <code>canvas</code> implementation did <em>not</em> require a closing tag, Mozilla&#8217;s implementation does, so it will be in your best interest to use <code>canvas</code> as outlined above.</p>
<p>Including the element in your document can be compared to opening your sketchbook to a new page; you&#8217;re simply provided a surface on which to draw. The ability to draw is enabled through a scripting language such as JavaScript, and you&#8217;re given a toolset of functions to work with. This toolset is controlled by something called a rendering context, used to perform the manipulations you&#8217;re looking for. We&#8217;ll take a quick look at the 2D rendering context.</p>
<h3>Drawing in 2D with canvas</h3>
<p>To work with your newly implemented <code>canvas</code> element, you&#8217;re going to need to access the element using DOM methods. The first method we&#8217;ll look into is <code>getContext</code>. This method allows us to obtain the rendering method we&#8217;re looking to use, as well as its drawing functions we need. We&#8217;re able to pass one parameter to <code>getContext</code>: the context type.</p>
<pre class="sh_javascript"><code>var canvas = document.getElementById('my_canvas');
var context = canvas.getContext('2d');</code></pre>
<p>Once we&#8217;ve identified our DOM node (line 1), we&#8217;re able to access our context using <code>getContext</code> (line 2). It&#8217;s important to remember graceful degradation at all times, and working with <code>canvas</code> is no exception. At this point, we can determine if we&#8217;ve got <code>canvas</code> support by checking to see if <code>getContext</code> is supported:</p>
<pre class="sh_javascript"><code>var canvas = document.getElementById('my_canvas');
if(canvas.getContext)
{
	var context = canvas.getContext('2d');
}
else
{
	// fallback (canvas is not supported)
}</code></pre>
<p>Until this point, we haven&#8217;t been doing much in the way of exciting. In fact, we have yet to draw anything. If you&#8217;ve taken any computer science classes in the past, it&#8217;s very likely that drawing with <code>canvas</code> will heavily remind you of <span class="title">COMPSCI 201: Intermediate Programming with Java</span>. The two dimensional rendering context provided by <code>canvas</code> makes available many similar, if not identical functions, to those provided in Java. For example, drawing a rectangle is accomplished by first defining a <code>fillStyle</code>, and then executing a <code>fillRect</code> to draw the shape on our <code>canvas</code> using the <code>fillStyle</code> previously defined:</p>
<pre class="sh_javascript"><code>function draw_rect()
{
	var canvas = document.getElementById('my_canvas');
	if(canvas.getContext)
	{
		var context = canvas.getContext('2d');
		context.fillStyle = "rgb(100,100,100)";
		context.fillRect(0, 0, 100, 100);
	}
	else
	{
		// fallback (canvas is not supported)
	}
}</code></pre>
<p><a href="/examples/canvas/part1/">View example</a></p>
<p>In addition to defining a <code>fillStyle</code> with <a href="http://en.wikipedia.org/wiki/RGB_color_model">RGB</a> values, you can also incorporate <a href="http://en.wikipedia.org/wiki/RGBA_color_space">RGBA</a> values as well, which opens the door to even more exciting possibilities.</p>
<h3>Some of the many uses for canvas</h3>
<p>The <code>canvas</code> element gives Web designers a completely new avenue to explore. Some of the most widespread use seems to fall upon the creation of graphs, building animations, and in-browser gaming. It&#8217;s been said that <code>canvas</code> is going to not only enrich the Web browsing experience by providing a very lightweight, plugin-free environment, but also do its part to tackle the default decision to resort to Flash when trying to implement any sort of rich interaction with a document.</p>
<p>I&#8217;m really excited to see where <code>canvas</code> takes us over the next five years, as adoption is catching on quickly, and ingenious examples of what <code>canvas</code> is capable of are cropping up on a weekly basis. If you haven&#8217;t had the chance quite yet, check out <a href="http://ejohn.org/blog/processingjs/">Processing.js</a> (which is just mind blowing), <a href="http://ejohn.org/blog/ocr-and-neural-nets-in-javascript/">OCR and Neural Nets in JavaScript</a>, <a href="http://www.chromeexperiments.com/">Chrome Experiments</a>, just to name a few examples of impressive implementations based on canvas. What are some of your favorite implementations?</p>
<p>Should there be enough interest, I&#8217;d like to walk through some of the more advanced functionality provided by <code>canvas</code> rendering contexts in subsequent parts of this &#8216;Working with canvas&#8217; series. Hopefully it&#8217;s a subject a number of readers are interested in, but I&#8217;m most curious to see if that&#8217;s the case. Has <code>canvas</code> reached a point where you feel it will be a necessary part of your knowledge base?</p>
<br /><p><a href='http://rss.buysellads.com/click.php?z=1269068&k=2ee344414ac81fbb0f9de6ab08e9831e&a=422&c=1574937258' target='_blank' rel='nofollow'>
				<img src='http://rss.buysellads.com/img.php?z=1269068&k=2ee344414ac81fbb0f9de6ab08e9831e&a=422&c=1574937258' border='0' alt='' /></a></p><p><a href='http://buysellads.com/buy/sitedetails/pubkey/2ee344414ac81fbb0f9de6ab08e9831e/zone/1269068' target='_blank'>Advertise here with BSA</a></p>]]></content:encoded>
			<wfw:commentRss>http://mondaybynoon.com/20090323/working-with-canvas-some-background-and-basics/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Database Caching 1/48 queries in 0.041 seconds using apc
Object Caching 474/531 objects using apc

Served from: www.mondaybynoon.com @ 2012-02-11 09:17:30 -->
