<?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>@dtinth&#039;s Blog &#187; Canvas</title>
	<atom:link href="http://blog.dt.in.th/category/javascript/canvas/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dt.in.th</link>
	<description>Personal Weblog of Thai Pangsakulyanont</description>
	<lastBuildDate>Wed, 30 Nov 2011 07:53:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Size Placeholder Maker</title>
		<link>http://blog.dt.in.th/2010/05/placeholder/</link>
		<comments>http://blog.dt.in.th/2010/05/placeholder/#comments</comments>
		<pubDate>Sun, 02 May 2010 12:44:02 +0000</pubDate>
		<dc:creator>the DtTvB</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.dt.in.th/.html</guid>
		<description><![CDATA[Written in JavaScript. Designed to work on Firefox. Useful for making mockups. Click the above button. function dodrawme(w, h) { var canvas = document.createElement('canvas'); canvas.width = w; canvas.height = h; var ctx = canvas.getContext('2d'); ctx.beginPath (); var r = 5; &#8230; <a href="http://blog.dt.in.th/2010/05/placeholder/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Written in JavaScript. Designed to work on Firefox. Useful for making mockups.</p>

<div>
    <p><input type="button" value="Make me one!" onclick="domakeme()" /></p>
    <p id="plho">Click the above button.</p>
    <script type="text/javascript">
        function dodrawme(w, h) {
            var canvas = document.createElement('canvas');
            canvas.width = w;
            canvas.height = h;
            var ctx = canvas.getContext('2d');
            ctx.beginPath ();
            var r = 5;
            ctx.fillStyle = 'rgba(200,200,200, 0.6)';
            ctx.arc (r, r, r, -Math.PI, -Math.PI / 2, false);
            ctx.arc (w - r, r, r, -Math.PI / 2, 0, false);
            ctx.arc (w - r, h - r, r, 0, Math.PI / 2, false);
            ctx.arc (r, h - r, r, Math.PI / 2, Math.PI, false);
            ctx.fill ();
            ctx.font = 'bold 10pt Verdana';
            ctx.textAlign = 'center';
            ctx.textBaseline = 'middle';
            ctx.fillStyle = 'rgba(255, 255, 255, 0.5)';
            ctx.fillText (w + 'x' + h, w / 2, h / 2);
            ctx.fillStyle = 'rgba(20, 20, 20, 0.6)';
            ctx.fillText (w + 'x' + h, w / 2, h / 2 - 1);
            document.getElementById('plho').innerHTML = '<img src="' + canvas.toDataURL() + '" alt=""\/>';
        }
        function domakeme() {
            var w = prompt('Width?', 200)  || 200;
            var h = prompt('Height?', 200) || 200;
            dodrawme (w, h);
        }
    </script>
</div>

<p>Right click and copy the resulting image then you can paste it on your favorite image editor.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dt.in.th/2010/05/placeholder/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Canvas Renderer &#8211; generate images quickly using JavaScript</title>
		<link>http://blog.dt.in.th/2010/02/canvasrenderer/</link>
		<comments>http://blog.dt.in.th/2010/02/canvasrenderer/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 15:25:18 +0000</pubDate>
		<dc:creator>the DtTvB</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://blog.dt.in.th/.html</guid>
		<description><![CDATA[I&#8217;m writing a webpage, and because my PC is not working right now, I need to do it using my MacBook. And when I use a laptop computer, I don&#8217;t use a mouse, so it is very hard to draw &#8230; <a href="http://blog.dt.in.th/2010/02/canvasrenderer/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m writing a webpage, and because my PC is not working right now, I need to do it using my MacBook.</p>

<p>And when I use a laptop computer, I don&#8217;t use a mouse, so it is very hard to draw graphics in MacBook. Also, GIMP doesn&#8217;t work very well with Mac OS X.</p>

<p>So I come up with something. <strong>Why don&#8217;t I draw images using only code?</strong></p>

<p>I came up with this. It&#8217;s called <strong>Canvas Renderer</strong>, a small python script that takes a JavaScript file, and generate images. It uses WebKit and the Canvas element. On Mac OS X it uses PyObjC and WebKit framework. On Linux it uses PyWebKitGtk.</p>

<p>You can <a title="Canvas Renderer" href="http://gist.github.com/308771"><strong>get the code here.</strong></a></p>

<p>On a Mac, you need to <code>sudo easy_install pyobjc-framework-WebKit</code> first. On Linux, just install PyWebKitGtk. As far as I know it comes with Ubuntu Karmic. To run the code,</p>

<pre><code>python canvasrenderer.py
</code></pre>

<p class="center">The above command takes the JavaScript code from the standard output.</p>

<pre><code>python canvasrenderer.py file.js
</code></pre>

<p class="center">The above command takes the JavaScript code from a the file specified in the arguments. Try running:</p>

<pre><code>python canvasrenderer.py test.js
</code></pre>

<p class="center">and you should get something like this:</p>

<div class="content-image" style="padding-bottom: 1em; text-align: center">
    <span class="corner1" style="width: 250px"><span class="corner2"><span class="corner3"><span class="corner4"><span class="corner5"><img src="http://tnk.dt.in.th/blog-data/2010-03/02-2257.png" style="border: 0; display: block; margin: 0 auto;" width="200" height="100" alt="Result of python canvasrenderer.py test.js"></span></span></span></span></span>
</div>

<h2>The API</h2>

<p>Functions</p>

<ul>
<li>newFile(width, height) &#8211; creates a new blank canvas.</li>
<li>saveFile(filename) &#8211; saves the image.</li>
</ul>

<p>Variables (these variables are reset after calling newFile)</p>

<ul>
<li>canvas &#8211; the canvas element</li>
<li>ctx &#8211; the canvas context</li>
<li>w &#8211; width of the canvas</li>
<li>h &#8211; height of the canvas</li>
</ul>

<p>Right now there are no ways to import image and use it.</p>

<p>Oh. Another thing: you can use bash to stick files together and then run it.</p>

<pre><code>cat libs.js background.js | python canvasrenderer.py
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.dt.in.th/2010/02/canvasrenderer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

