<?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>The Sea of Ideas &#187; canvas</title>
	<atom:link href="http://paulbakaus.com/tag/canvas/feed/" rel="self" type="application/rss+xml" />
	<link>http://paulbakaus.com</link>
	<description>Capturing the thoughts of Paul Bakaus</description>
	<lastBuildDate>Thu, 02 Feb 2012 15:34:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Why Canvas is not an obvious choice for web games</title>
		<link>http://paulbakaus.com/2010/07/19/why-canvas-is-not-an-obvious-choice-for-web-games/</link>
		<comments>http://paulbakaus.com/2010/07/19/why-canvas-is-not-an-obvious-choice-for-web-games/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 12:47:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Aves Engine]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[rendering]]></category>

		<guid isPermaLink="false">http://paulbakaus.com/?p=340</guid>
		<description><![CDATA[I initially wrote this post for our corporate Dextrose blog at http://blog.dextrose.com but due to its technical nature, I republished it over here in order to get a bit more attention. Enjoy! There has been quite some controversy about the decision to use plain old HTML to render a scene with the Aves Engine versus [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>I initially wrote this post for our corporate Dextrose blog at http://blog.dextrose.com but due to its technical nature, I republished it over here in order to get a bit more attention. Enjoy!  </strong></p>
<p>There has been quite some controversy about the decision to use plain old HTML to render a scene with the Aves Engine versus the utilization of the new popular Canvas tag since my Google tech talk is up (http://www.youtube.com/watch?v=_RRnyChxijA). I wanted to elaborate a bit about the reasoning behind all this.</p>
<h3>Canvas doesn&#8217;t play well with images</h3>
<p>Unfortunately, we happen to use lots and lots of different images in a typical isometric scene with the Aves Engine. With Canvas, there&#8217;s a friction in the API when working with images: When trying to include an image into your canvas render, you need to first construct it via DOM methods (new Image()), set the source, wait until it is loaded and then render it to the canvas. A step that is painful, even if you&#8217;re later on caching the images, and most importantly, a step that is horribly slowing down the Canvas interface with whatever reasoning behind it.</p>
<p>In fact, even with precached DOM representations of images, our basic tests have shown that Canvas based rendering of an isometric scene is almost always 2-3x slower than just dumping out a big HTML string with some class names linked to external stylesheets. External stylesheets just seem to be a much better cache when connected to HTML output than DOM-Canvas.</p>
<h3>Canvas-based redrawing is painful</h3>
<p>There&#8217;s a lot of motion going on within games based on the Aves Engine. They&#8217;re mostly full screen (at least extended to the window size) and feature character sprite animations and transitions. Say a character moves by 20 pixels to the left. It&#8217;s easy to clear the whole canvas and rerender it, but also extremely slow. It&#8217;s however extremely difficult to implement an own partial redrawing system. Here&#8217;s roughly what the engine would need to do:</p>
<ol>
<li>Get the outer boundaries of the whole part that was changed, i.e. the old character position and the new one combined in a rectangle</li>
<li>Redraw this part with all elements not in motion during this redraw – includes characters, objects etc. Engine needs to keep track of all positions of all elements on the viewport all time.</li>
<li>Now that you cleared the part, you can place the character on top again with the new changed position</li>
</ol>
<p>This might roughly give you an idea of the steps involved here. I&#8217;m not saying it is impossible, as this is in fact how most efficient rendering engines do redraws. It&#8217;s just very very time consuming and difficult, and we haven&#8217;t had the chance to explore it yet. On the other hand, the browser rendering engines already do this work for us at the moment, which is really convenient for the time being.</p>
<h3>Action surfaces</h3>
<p>If you have seen one of our demonstrations or videos, you have also seen one of the most unique features of the Aves Engine, our action surfaces. It allows you to place generic HTML content onto any kind of surfaces, transformed to isometric projection. This is working due to the possibilities of applying 2D matrices to elements via CSS Transforms (WebKit, Gecko, Opera) or the matrix filter (Trident [IE]).</p>
<p>Action surfaces obviously also need to be considered in the layering of objects. If a character is walking in front of a wall, the action surface needs to stay behind the character. With HTML based rendering, this is pretty easy, as we&#8217;re just using z-index to control the layering, and the action surface lives in the actual container element.</p>
<p>Now with Canvas, this gets pretty tricky. There&#8217;s no way to render HTML content (or a snapshot of it) to Canvas, first of all. Doing screenshots of portions of the screen and rendering them to canvas was possible some time ago in Firefox, and I think still is for XUL, but is disabled in web context for security reasons (you could snap contents of file inputs etc.). Now this leaves you with only one apparent choice:</p>
<ol>
<li>Collect how many action surfaces need to be displayed in the current render</li>
<li>Count, how many different layers you would need</li>
<li>Split the Canvas render into multiple &lt;canvas&gt; elements for each layer and slide in the DOM based action surfaces inbetween the partial renders</li>
</ol>
<p>Thank god canvas supports see-through transparency, but this is still a pain nonetheless. I&#8217;ve never seen a system like this implemented and working. We&#8217;re happy to be the first, but again, haven&#8217;t tackled it for the alpha version of the engine.</p>
<p>Again, we&#8217;re still experimenting all the time with possible render improvements, and this post was mainly meant to give you some insights in how we do R&amp;D to base our assumptions on actual tests. For us, it&#8217;s not so much about the technology – it&#8217;s about delivering a truly sophisticated gaming experience. We&#8217;ll do our best to squeeze the most out of the open web stack, and we look forward to jumping on Canvas when the time comes!</p>
]]></content:encoded>
			<wfw:commentRss>http://paulbakaus.com/2010/07/19/why-canvas-is-not-an-obvious-choice-for-web-games/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>CSS Transforms for Firefox</title>
		<link>http://paulbakaus.com/2008/08/19/css-transforms-for-firefox/</link>
		<comments>http://paulbakaus.com/2008/08/19/css-transforms-for-firefox/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 12:19:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[css transforms]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[svg]]></category>

		<guid isPermaLink="false">http://paulbakaus.com/?p=14</guid>
		<description><![CDATA[After my first teaser image, quite a few wondered why I&#8217;d bring out a version featuring CSS Transforms for Internet Explorer out instead. The reason why I now completely discontinued the CSS Transforms work for Firefox is that CSS Transforms are being shipped most likely along with the 3.1 release. However, I think the two [...]]]></description>
			<content:encoded><![CDATA[<p>After my first teaser image, quite a few wondered why I&#8217;d bring out a version featuring CSS Transforms for Internet Explorer out instead. The reason why I now completely discontinued the CSS Transforms work for Firefox is that CSS Transforms are being shipped most likely along with the 3.1 release.</p>
<p>However, I think the two approaches were still interesting, so I&#8217;m discussing them with you:</p>
<h2>The Canvas approach</h2>
<p>This was my initial attempt to bring CSS Transforms to Firefox. Since there was no hack like using the Matrix Filter in IE, I had to do it the hard way. I knew that rendering web content to into a canvas was already <a href="http://developer.mozilla.org/en/docs/Drawing_Graphics_with_Canvas#Rendering_Web_Content_Into_A_Canvas">supported by Firefox long ago</a> but was then taken out due to <a href="http://mxr.mozilla.org/mozilla/source/content/canvas/src/nsCanvasRenderingContext2D.cpp#2345">security issues</a>. So I had to take the long road and actually build my own limited rendering engine in Canvas (as some pointed out, of course not featuring things like inputs, buttons, everything natively styled by the OS).</p>
<p>Here&#8217;s how the logical implementation looked like:</p>
<ol>
<li>Find all instances of -webkit-transform</li>
<li>For every found element:</li>
<li>Create a new &lt;canvas&gt; element at the exact same position as the original, with the same constrains</li>
<li>Rotate/modify/translate the whole canvas by the values found in the transform functions</li>
<li>Literally draw borders, background and text (using FF3&#8242;s new text API for canvas) for the original item into the canvas and for all sub items</li>
<li>Recompute the new constrains of the element and reset the constrains of the canvas</li>
</ol>
<p>Although insanely difficult, it was working better than I initially thought, and I was able to create a DIV element with all kinds of sub nodes and rotate it with full speed in a nice animation.</p>
<h2>The SVG approach</h2>
<p>That&#8217;s when I found out about the foreignObject in SVG &#8211; since I never really played with SVG, it was completely new to me. The foreignObject basically is able to display namespaced XML, including HTML in an SVG canvas.</p>
<p>The fun thing here is that I immediately sensed that it was much more promising, because the CSS Transforms API is almost exactly copied from the SVG API, and if I could get the initial work done, I thought to myself, mapping it would be fairly easy.</p>
<p>Then I found about the limitations about SVG. For example, <a href="http://developer.mozilla.org/en/docs/SVG_In_HTML_Introduction">that it&#8217;s not possible to include inline SVG into an HTML page without serving the Content-Type as XHTML</a>. Okay, this was a show blocker for my plugin, because I couldn&#8217;t possibly force people to use XHTML whenever they wanted to use CSS Transforms..</p>
<p>However, <strong>I did actually find a way to display inline SVG in HTML</strong> after many hours of research, and this is the implementation I came up with:</p>
<ol>
<li>Find all instances of -webkit-transform</li>
<li>For each element,</li>
<li>Serialize the whole node (outerHML) into a string (without positioning data in the style attribute)</li>
<li>Wrap it with a prepared SVG XML Header</li>
<li>Also insert the transform value as &lt;g transform=&#8217;..&#8217;&gt;</li>
<li>Encode the whole string to base64</li>
<li>Create a new embed element with the base64 String as data source, and render it to the page (with the position data from the original node on the actual &lt;embed&gt;)</li>
</ol>
<p>If I would have continued with the development of the plugin, than definitely using the SVG approach. It&#8217;s pretty slow to do transformations on a foreignObject in Firefox 3, but you don&#8217;t loose the OS styles, and it&#8217;s so much easier since you don&#8217;t have to port over the CSS Transform API to something different.</p>
<p>See you soon with more exciting projects!</p>
]]></content:encoded>
			<wfw:commentRss>http://paulbakaus.com/2008/08/19/css-transforms-for-firefox/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

