<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>The Karma Project: Code Less, Teach More</title>
	<atom:link href="http://karmaeducation.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://karmaeducation.org</link>
	<description>Code Less, Teach More</description>
	<lastBuildDate>Thu, 18 Feb 2010 08:58:16 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='karmaeducation.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/1b1c80cf141d778b01e0decb8ff1ac12?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>The Karma Project: Code Less, Teach More</title>
		<link>http://karmaeducation.org</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://karmaeducation.org/osd.xml" title="The Karma Project: Code Less, Teach More" />
	<atom:link rel='hub' href='http://karmaeducation.org/?pushpress=hub'/>
		<item>
		<title>Introducing the Karma Lesson Template</title>
		<link>http://karmaeducation.org/2010/02/18/introducing-the-karma-lesson-template/</link>
		<comments>http://karmaeducation.org/2010/02/18/introducing-the-karma-lesson-template/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 08:19:47 +0000</pubDate>
		<dc:creator>bryanwb</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[karma]]></category>
		<category><![CDATA[education]]></category>
		<category><![CDATA[olpc]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery UI]]></category>
		<category><![CDATA[chromium]]></category>

		<guid isPermaLink="false">http://karmaeducation.org/?p=459</guid>
		<description><![CDATA[I have created a simple template for individual karma lessons. In this post, I will explain how to use the template and what still needs to be implemented. The template still has some rough spots that I would very much appreciate feedback on.

Before reading on, you should <a href="http://git.sugarlabs.org/projects/karma-lesson/repos/mainline/archive/7f70350b9d64f94b49ed2a2470aa67d4a128fc4a.tar.gz">download the lesson template</a> directly or use git to <a href="http://git.sugarlabs.org/projects/karma-lesson/repos/mainline">clone the repository</a>.

[sourcecode]
$ git clone git://git.sugarlabs.org/karma-lesson/mainline.git
[/sourcecode]

<strong>What the Template Provides</strong>
<ul>
<li>Layout that matches the XO's screen resolution</li>
<li>Navigation bar - with links to help text, lesson plan, and teacher's note</li>  
<li>Viewer for  lesson plan and teacher's note</li>
<li>Scoreboard, timer, Start, Stop, Restart buttons all in the footer</li>
<li>Helpers to make your lesson event-driven rather than using a single control loop</li> 
<li>Basic i18n for strings, but this is still in flux</li>
</ul>
<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=459&subd=karmaproject&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>I have created a simple template for individual karma lessons. In this post, I will explain how to use the template and what still needs to be implemented. The template still has some rough spots that I would very much appreciate feedback on.</p>
<p>Before reading on, you should <a href="http://git.sugarlabs.org/projects/karma-lesson/repos/mainline/archive/7f70350b9d64f94b49ed2a2470aa67d4a128fc4a.tar.gz">download the lesson template</a> directly or use git to <a href="http://git.sugarlabs.org/projects/karma-lesson/repos/mainline">clone the repository</a>.</p>
<pre class="brush: plain;">
$ git clone git://git.sugarlabs.org/karma-lesson/mainline.git
</pre>
<p><strong>What the Template Provides</strong></p>
<ul>
<li>Layout that matches the XO&#8217;s screen resolution</li>
<li>Navigation bar &#8211; with links to help text, lesson plan, and teacher&#8217;s note</li>
<li>Viewer for  lesson plan and teacher&#8217;s note</li>
<li>Scoreboard, timer, Start, Stop, Restart buttons all in the footer</li>
<li>Helpers to make your lesson event-driven rather than using a single control loop</li>
<li>Basic i18n for strings, but this is still in flux</li>
</ul>
<p><strong>Most Important Files in the Template</strong></p>
<p>These are the files that will modify to create your lesson</p>
<ul>
<li> index.html  &#8211;  the markup for your lesson</li>
<li>js/lesson.js &#8211; the code for your lesson</li>
<li>css/lesson.css &#8211;  the layout for your lesson </li>
</ul>
<p><strong>Meet <code>index.html</code></strong></p>
<div id="attachment_463" class="wp-caption aligncenter" style="width: 310px"><a href="http://karmaproject.files.wordpress.com/2010/02/karma-lesson.png"><img src="http://karmaproject.files.wordpress.com/2010/02/karma-lesson.png?w=300&#038;h=175" alt="" title="karma-lesson" width="300" height="175" class="size-medium wp-image-463" /></a><p class="wp-caption-text">index.html</p></div>
<p>The html markup in <code>index.html</code> is quite simple</p>
<pre class="brush: plain;">
&lt;body&gt;
        &lt;div id=&quot;kHeader&quot;&gt;
        &lt;/div&gt;

	&lt;!-- Put the help text inside #kHelp --&gt;
	&lt;div id=&quot;kHelp&quot; title=&quot;Help Title&quot;&gt; Help text here&lt;/div&gt;
	&lt;div id=&quot;feedback&quot;&gt;&lt;/div&gt;
	&lt;!-- #kMain is where the magic happens, the main frame where your lesson
              its stuff --&gt;
	&lt;div id=&quot;kMain&quot;&gt;
	  &lt;strong id=&quot;itWorks&quot;&gt;It Works!&lt;/strong&gt;
	&lt;/div&gt;
	&lt;div id=&quot;kFooter&quot;&gt;
	&lt;/div&gt;
&lt;/body&gt;
</pre>
<p>That is very little html for actual page that is rendered. That is because #kHeader, #feedback, and kFooter are just <em>scaffolding</em> that we use to hang jQuery UI widgets on.</p>
<p>Here is a wireframe of <code>index.html</code> that will help you understand the dimensions and layout<br />
<a href="http://karmaproject.files.wordpress.com/2010/02/lesson-wireframe.png"><img src="http://karmaproject.files.wordpress.com/2010/02/lesson-wireframe.png?w=300&#038;h=223" alt="" title="lesson-wireframe" width="300" height="223" class="aligncenter size-medium wp-image-467" /></a></p>
<p>As you can see from the wireframe, the lesson has a default width of 1200px and height of 900px. This gives you 1200px X 760px in #kMain to use for your lesson.</p>
<p>This is to match the dimensions of the XO. We could default to a smaller resolution that matches most desktops, like 1024 x 760 or 800&#215;600 and scale up the lesson on the XO. We don&#8217;t do this for two reasons. First, scaling up is cpu-intensive and slow as molasses on the XO  and second, scaled up raster images (.png, .jpg) &#8216;pixelate&#8217; and look like crap. If we only used SVG images throughout the template, this would be a non-issue, as SVG scales up and down without performance cost.</p>
<p>I have created a function in the Karma.scaleWindow that will scale down the entire lesson to 950px X 760px. This leaves you only 950px X 460px.  Please don&#8217;t develop a Karma lesson at this resolution if you intend it to run on the XO. I created this function to scale down your lesson to run on non-XO&#8217;s with the resolution 1024 x 760. With a bit of work you can make sure your lesson works at both 1200px X 900px. By far the easiest way to do this is to only use SVG. </p>
<pre class="brush: plain;">
Karma.scaleWindow();   /*scales window to 1024 x 760 if current browser window is narrower than 1150px */
</pre>
<p>If anyone has a better idea how to handle different screen resolutions while still accommodating the XO&#8217;s performance issues, please let me know.</p>
<p><strong>Additional files that may be of special interest</strong></p>
<ul>
<li>start.html</li>
<li>kDoc.html</li>
<li>lessonPlan.html</li>
<li>teachersNote.html</li>
</ul>
<p>The start.html page is an introductory page to the lesson that provides big obvious links to the Lesson Plan and Teacher&#8217;s Note. It also indicates very clearly which grade and academic subject the lesson is for. The Lesson Plan describes how to lead the children through the lesson in a classroom. The Teacher&#8217;s Note explains what the purpose of the lesson is and where it fits into the curriculum. index.html also has quick links to the Lesson Plan and Teacher&#8217;s Note but they are harder to find. The start page may seem unnecessary to some, but the teachers in Nepal tell us that they like it.</p>
<p><a href="http://karmaproject.files.wordpress.com/2010/02/start-page.png"><img src="http://karmaproject.files.wordpress.com/2010/02/start-page.png?w=300&#038;h=175" alt="" title="start-page" width="300" height="175" class="aligncenter size-medium wp-image-470" /></a></p>
<p>I have created a simple viewer kDoc.html for viewing the lesson plan and teacher&#8217;s note.<br />
I cannot understate the importance of the lesson plan and teacher&#8217;s note. Figuring out how to use educational software effectively with 30+ energetic kids is not a trivial task. </p>
<p><a href="http://karmaproject.files.wordpress.com/2010/02/lesson-plan.png"><img src="http://karmaproject.files.wordpress.com/2010/02/lesson-plan.png?w=300&#038;h=175" alt="" title="lesson-plan" width="300" height="175" class="aligncenter size-medium wp-image-472" /></a></p>
<h1>Widgets</h1>
<p>Remember the minimalist markup for kHeader, kFooter, feedback. All three are <a href='http://jqueryui.com'>jQuery UI widgets</a> that have to be attached to HTML &lt;div&gt; elements.</p>
<p>A jQuery UI widget typically consists of a JavaScript file and a CSS file. Here are the widgets and the files that make them up:</p>
<ul>
<li>kHeader &#8212; js/ui.kHeader.js  and css/ui.kHeader.css</li>
<li>feedback &#8212; js/ui.feedback.js and css/ui.feedback.css</li>
<li>kFooter &#8212; js/ui.kFooter.js and css/ui.kFooter.css</li>
</ul>
<p>See the <a href="http://karma.sugarlabs.org/docs/index.html">API documentation online</a> or at docs/index.html in the template</p>
<p>Attaching a widget to a page</p>
<pre class="brush: plain;">
var $kFooter = $('#kFooter').kFooter();  //where #kFooter is a &lt;div&gt; element
</pre>
<p>Using different initialization parameters</p>
<pre class="brush: plain;">
var $kFooter = $('#kFooter').kFooter({'winningScore': 6, timer: true, scoreboard: false,
						pauseButton: true, startButton: true});
</pre>
<p>Notice that II assigned the result of the kFooter method to the variable <code>$kFooter</code>. This variable is cached reference that I can use to call new methods on the kFooter later and it is much faster than using a fresh jQuery selector each time like <code>$('#kFooter').kFooter(/* some method */);</code>. The naming convention for such variables is to prefix them with the &#8216;$&#8217; character to signify the variable refers to a jQuery object.</p>
<p>All three widgets have their own methods that you can use to interact with them. The kFooter widget, for example, has methods getScore(), inc(), dec(), setScore(), startTimer(), stopTimer(), and more to manage its built-in scoreboard and timer</p>
<p>Calling methods on jQuery UI widgets is bit different. You pass the method name as an argument in order to call it. Here are examples of calling methods on the kFooter widget:</p>
<pre class="brush: plain;">
$('#kFooter').kFooter('inc');   //increments the score
//or if u have a reference
$kFooter.kFooter('inc');

// To pass arguments to the method, simply append them as additional arguments
$kFooter.kFooter('inc', 2);  //increment score by 2 rather than the default of 1
</pre>
<h1>Event-Driven User Interface</h1>
<p>When you create your first lesson, it is very tempting to use a loop like</p>
<pre class="brush: plain;">
while(true){

  if(/* user does X */){
       exit the loop
  }

}
</pre>
<p>This code, while well-intentioned will cause your browser to display the horrible &#8220;Unresponsive script&#8221; message. The code also makes the user interface sluggish and generally unresponsive.</p>
<p><img alt="" src="http://img488.imageshack.us/img488/7730/scriptwarningunresponsisb1.jpg" class="aligncenter" width="562" height="125" /></p>
<p>You can avoid this terrible message by making your code event-driven. This is a big word for using events and flags to control the behavior of your lesson.</p>
<p>Here is some code that look event-driven but actually is just a modified for loop and should be avoided. It can also cause the dreaded &#8220;Unresponsive script&#8221; message.</p>
<pre class="brush: plain;">
var timerId;

var game = function(){
      /* do stuff */
      timerId = setTimeout(game, 10);
}

if(/* user wants to quit */){
     clearInterval(timerId);
}
</pre>
<p>Here is a good example of event-driven code, modified code take from ui.kFooter.js</p>
<pre class="brush: plain;">
var timerRunning = true;
var runTimer = function(){
	if (timerRunning) === false){
	    return;  /* setting timerRunning to false stops the timer */
       }
        /* code for manipulating the time displayed */

	timerId = setTimeout(runTimer, 1000);
};

//start the timer
runTimer();

//to stop the timer, set the flag timerRunning to false
timerRunning = true;
</pre>
<p>Things to Remember:<br />
* Use setTimeout rather than setInterval<br />
* Set flags to stop a background animation or timer rather than clearInterval()</p>
<p>Triggering custom Events and listening for them</p>
<p>If you initialize the kFooter widget with a winning score, it will emit a custom event &#8220;kFooterWinGame&#8221; event when that score is reached.</p>
<pre class="brush: plain;">
var $kFooter = $('#kFooter').kFooter({'winningScore': 6});

//show congratulations message when kFooterWinGame event is emitted
kFooter.bind('kFooterWinGame',
		function(){
		    $('#congratulationsMessage').show();
		});
</pre>
<p>The API documentation details the events that each widget emits.</p>
<h1>Unresolved Issues</h1>
<p>Here are some unresolved issues that I will continue working on and would most appreciated help with.</p>
<ul>
<li>Scaling &#8211; scaling and raster graphics (.png, .jpg files) just don&#8217;t play well together</li>
<li>Icons &#8211; jQuery UI icons are all png&#8217;s, which means they scale poorly. They are also too small and hard to override</li>
<li>Sound plays poorly on chromium on linux &#8212; pls up vote <a href='http://code.google.com/p/chromium/issues/detail?id=22698'>this issue</a> in the chromium bug tracker to encourage the chromium guys to fix it.</li>
<li>internationalization &#8212; still in flux, doesn&#8217;t integrate with <a href='http://translate.sourceforge.net/wiki/pootle/index'>pootle</a></li>
<li>Firefox 3.6 performance is pretty sluggish on the XO. We really need to figure out how to get chromium running on the XO. I believe it will be at least 2x faster.</li>
</ul>
<p>Internationalization is still very much in flux &#8211; I have created a <a href='http://github.com/bryanwb/jquery-i18n'>basic jQuery plugin</a> for handling it but it leaves much to be desired. I just recently discovered google chrome&#8217;s i18n mechanism for chrome extensions and I believe that it is far superior. I hope to port it to jQuery, which should take some time. I really think that chrome, jQuery, and Firefox&#8217;s jetpack should agree on a common API for i18n. Integration with pootle will be a pain but there more incentive for others to integrate with it if all three platforms used the same tooling.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/karmaproject.wordpress.com/459/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/karmaproject.wordpress.com/459/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/karmaproject.wordpress.com/459/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/karmaproject.wordpress.com/459/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/karmaproject.wordpress.com/459/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/karmaproject.wordpress.com/459/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/karmaproject.wordpress.com/459/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/karmaproject.wordpress.com/459/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/karmaproject.wordpress.com/459/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/karmaproject.wordpress.com/459/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=459&subd=karmaproject&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://karmaeducation.org/2010/02/18/introducing-the-karma-lesson-template/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/21904e6423d8faa7c6d7d2c94f11271b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bryanwb</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2010/02/karma-lesson.png?w=300" medium="image">
			<media:title type="html">karma-lesson</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2010/02/lesson-wireframe.png?w=300" medium="image">
			<media:title type="html">lesson-wireframe</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2010/02/start-page.png?w=300" medium="image">
			<media:title type="html">start-page</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2010/02/lesson-plan.png?w=300" medium="image">
			<media:title type="html">lesson-plan</media:title>
		</media:content>

		<media:content url="http://img488.imageshack.us/img488/7730/scriptwarningunresponsisb1.jpg" medium="image" />
	</item>
		<item>
		<title>A Strategy for localizing jQuery UI plugins</title>
		<link>http://karmaeducation.org/2010/01/28/a-strategy-for-localizing-jquery-ui-plugins/</link>
		<comments>http://karmaeducation.org/2010/01/28/a-strategy-for-localizing-jquery-ui-plugins/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 04:29:30 +0000</pubDate>
		<dc:creator>bryanwb</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQueryUI]]></category>
		<category><![CDATA[l10n]]></category>

		<guid isPermaLink="false">http://karmaeducation.org/?p=457</guid>
		<description><![CDATA[This is  a repost of <a href="https://forum.jquery.com/newtopic#Topic/14737000000672346">this post</a> to the jQuery UI forum
I have to support several locales for the jQuery UI plugins I am creating and am trying to think of a consistent way to support localization of numeral characters and any strings that may be embedded in the plugin. I think the mechanism I have in mind may be useful to the larger jQuery UI set of plugins.
<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=457&subd=karmaproject&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>This is  a repost of <a href="https://forum.jquery.com/newtopic#Topic/14737000000672346">this post</a> to the jQuery UI forum<br />
I have to support several locales for the jQuery UI plugins I am creating and am trying to think of a consistent way to support localization of numeral characters and any strings that may be embedded in the plugin. I think the mechanism I have in mind may be useful to the larger jQuery UI set of plugins.</p>
<p>To clarify, this proposal is mean to support localization of numeral characters and strings embedded in a plugin, not dates or currency formats ($ 1.000,00  vs. 1,000.00) but it could conceivable support those as well. If you aren&#8217;t already aware, a number of languages like Arabic and Hindi use different numeral characters for 1-9. For example, ४ is 4 in Hindi.</p>
<p>I propose the following methods for localizing strings and numbers</p>
<p><strong>New Functions</strong></p>
<p>$._({String}, [locale])<br />
find translation of string for current locale. In case you have multiple locales loaded, you can pass a different locale from the default one. Say the default the locale is English but you need to interject Nepali. An example of this would be an English Lesson for Nepali children.</p>
<p>$._c({context}, {String}, [locale])<br />
find translation of string for current locale in a given context. In case you have multiple locales loaded, you can pass a different locale from the default one. Say the default the locale is English but you need to interject Nepali. An example of this would be an English Lesson for Nepali children.</p>
<p>$._n({Number}, [locale])<br />
convert the number to specified locale. </p>
<p>While &#8216;_&#8217; is used to prefix private variables and methods in js, it is the standard shorthand for the GNU gettext() method in a number of languages and frameworks.</p>
<p><strong>Loading Locales</strong></p>
<p>I think locales should be loaded individually for each plugin rather than a big page wide plugin. Also, the locale information should be embedded in the main plugin code. It bulks up the code and different versions of the code for each locale á la datepicker, makes the code harder to maintain.</p>
<p>All the locale info should be in a .json file in the ui.plugin_name.l10n.locale_name.json file</p>
<p>for an example datepicker plugin</p>
<p>ui.datepicker.js<br />
ui.datepicker.css<br />
ui.datepicker.l10n.ne.json   //&#8217;ne&#8217; for nepal, for country of residence <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>datepicker.js would then load the json file as $.ui.datepicker.l10n.ne</p>
<p>I recommend using the l10n namespace because otherwise we would conflict with any two-letter property name.</p>
<p>.json localization json files for a site could be consolidated into a single .json file for deployment.</p>
<p><strong>The Problem of Contexts</strong></p>
<p>In English, we often use the same word to mean different things in different contexts. A good example is &#8220;right&#8221; which can mean a direction and affirmation. Spanish, Nepali, and many other languages do not use the same word for the direction and affirmation.</p>
<p>GNU gettext, the standard bearer for open-source localization, lets you specify contexts for this kind of situation. </p>
<p>_c(context, string, [locale])  could be used</p>
<p>For example, _c(&#8216;map&#8217;, &#8216;right&#8217;)  for the context of a map and _(&#8216;test&#8217;, &#8216;right&#8217;) to fetch the translation in the context of a test on the same page.</p>
<p>I am not exactly sure how to handle contexts, but it is important to consider</p>
<p><strong>Summary</strong></p>
<p>I hope to have a basic prototype of this up and running by the end of the week.</p>
<p>I haven&#8217;t explored how to use this mechanism to handle currency and date formats, I think it could easily do so. A harder problem is handling the localization of css, something I haven&#8217;t put hard thought into.</p>
<p>Ideas have been liberally borrowed/stolen from</p>
<p>http://www.gnu.org/software/gettext/</p>
<p>http://jsgettext.berlios.de/doc/html/Gettext.html</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/karmaproject.wordpress.com/457/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/karmaproject.wordpress.com/457/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/karmaproject.wordpress.com/457/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/karmaproject.wordpress.com/457/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/karmaproject.wordpress.com/457/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/karmaproject.wordpress.com/457/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/karmaproject.wordpress.com/457/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/karmaproject.wordpress.com/457/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/karmaproject.wordpress.com/457/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/karmaproject.wordpress.com/457/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=457&subd=karmaproject&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://karmaeducation.org/2010/01/28/a-strategy-for-localizing-jquery-ui-plugins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/21904e6423d8faa7c6d7d2c94f11271b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bryanwb</media:title>
		</media:content>
	</item>
		<item>
		<title>Get Started with Jake, a Rake clone in JavaScript</title>
		<link>http://karmaeducation.org/2010/01/15/get-started-with-jake-a-rake-clone-in-javascript/</link>
		<comments>http://karmaeducation.org/2010/01/15/get-started-with-jake-a-rake-clone-in-javascript/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 08:33:16 +0000</pubDate>
		<dc:creator>bryanwb</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[commonJS]]></category>
		<category><![CDATA[jake]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[karma]]></category>
		<category><![CDATA[narwhal]]></category>

		<guid isPermaLink="false">http://karmaeducation.org/?p=415</guid>
		<description><![CDATA[In my last post, I gave an <a href="http://karmaeducation.org/2010/01/15/getting-started-with-narwhal-a-standard-library-for-javascript/">introduction to narwhal</a>, a standard library for JavaScript. This time I will explore Jake, a <a href="http://rubyrake.org">Rake</a> clone in JavaScript. I have been using it for the repetitive tasks in the <a href="git.sugarlabs.org/projects/karma/repos/mainline">Karma project</a> like generating documentation, cleaning up temporary files, and checking out the latest version and packaging it for deployment. You may want to take a look at my <a href="http://git.sugarlabs.org/projects/karma/repos/mainline/blobs/master/Jakefile">Jakefile</a> before you work through this tutorial.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=415&subd=karmaproject&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>In my last post, I gave an <a href="http://karmaeducation.org/2010/01/15/getting-started-with-narwhal-a-standard-library-for-javascript/">introduction to narwhal</a>, a standard library for JavaScript. This time I will explore Jake, a <a href="http://rubyrake.org">Rake</a> clone in JavaScript. I have been using it for the repetitive tasks in the <a href="git.sugarlabs.org/projects/karma/repos/mainline">Karma project</a> like generating documentation, cleaning up temporary files, and checking out the latest version and packaging it for deployment. You may want to take a look at my <a href="http://git.sugarlabs.org/projects/karma/repos/mainline/blobs/master/Jakefile">Jakefile</a> before you work through this tutorial.</p>
<p><strong>What Jake doesn&#8217;t have</strong></p>
<ul>
<li>XML files</li>
<li>Special syntax</li>
</ul>
<p>Jake only has pure JavaScript. This is a good thing. (mental) Context switching is expensive, particularly for utility scripts you might only look at once every few months.</p>
<p><strong>Why use *-ake?</strong></p>
<p>Managing a software project, especially web development projects, I find myself needing to do a number of repetitive and mind-numbing tasks that often, but not always, need to be done in a certain order. When packaging a new release of Karma, I usually want to check out the latest code from several repositories, put all the files in a temporary directory, remove unnecessary files, minify the source, and copy common JavaScript and CSS files to a common directory. Sometimes, I just want to check out the latest code. Sometimes I just want to minify the source and not move around the JavaScript and CSS files.</p>
<p>Here are the Tasks</p>
<ol>
<li>Check out files from version control</li>
<li>Copy files to a temporary build/ directory</li>
<li>Copy Common JS and CSS to a common directory, change references to new paths</li>
<li>Remove files not needed for deployment to the server like docs, tests, utilities</li>
<li>Minify JS and CSS files</li>
<li>Optionally, compress project files into a tarball or zip file</li>
</ol>
<p>Here is how those same tasks map to Jake tasks</p>
<ol>
<li>Check out files from version control <span style="color:#0000ff;"> ==&gt; jake checkout</span></li>
<li>Copy files to a temporary build/ directory  <span style="color:#0000ff;">==&gt; jake build-dir</span></li>
<li>Copy Common JS and CSS to a common directory, change references to new paths  <span style="color:#0000ff;">==&gt;jake move-common-files</span></li>
<li>Remove files not needed for deployment to the server like docs, tests, utilities <span style="color:#0000ff;">==&gt; jake repack</span></li>
<li>Minify JS and CSS files <span style="color:#0000ff;"> ==&gt; jake minify</span></li>
<li>Optionally, compress project files into a tarball or zip file  <span style="color:#0000ff;">==&gt; jake package</span></li>
</ol>
<p>Now some of these tasks depend on previous tasks so I shouldn&#8217;t be able to run them unless the previous tasks have been completed. But if they have been completed, I don&#8217;t want to rerun them. This is what buildmasters call &lt;em&gt;build dependencies&lt;/em&gt;.</p>
<p>There is a task I haven&#8217;t yet specified, &lt;code&gt; jake build &lt;/code&gt; which depends on tasks 1-6. I will show you an example build task later in the tutorial. Now let&#8217;s go back to creating basic tasks.</p>
<p><strong>First Steps with Jake</strong></p>
<p>Next, let&#8217;s set up jake. Narwhal has a package manager called tusk, which is comparable to ruby&#8217;s gem and Python&#8217;s easy_install.</p>
<pre class="brush: plain;">
$ tusk install jake
</pre>
<pre class="brush: plain;">
you@computer$ jake
No Jakefile found (looking for: jakefile, Jakefile, jakefile.js, Jakefile.js, jakefile.j, Jakefile.j)
</pre>
<p>Jake, like its siblings Rake and Make, requires a Jakefile that contains <em>jake tasks</em>.</p>
<p>Here is a very simple Jakefile</p>
<pre class="brush: plain;">
//import the jake module
//This is equivalent to python's &quot;import&quot; statement
var  JAKE = require(&quot;jake&quot;);
// JAKE.task('taskname', [ dependencies ], function(){});
JAKE.task('hello', function(){
      print('hello world');
});
</pre>
<p>Like Rakefiles, Jakefiles contain pure JavaScript. You should see a number of commands that are specific to narwhal. It is beyond the scope of this tutorial to cover those. I recommend you take a look at the narwhal documentation <a href="http://github.com/280north/narwhal/tree/master/docs/">here</a>.</p>
<p>To execute this simple hello task, run the following in the same directory as the Jakefile</p>
<pre class="brush: plain;">
$ jake hello
hello
</pre>
<p>Now let&#8217;s do something more interesting. I frequently need to regenerate the documentation for karma.js using a long shell command that I can never remember all the switches for. I use the excellent <a href="http://code.google.com/p/jsdoc-toolkit/">jsdoc-toolkit</a>.</p>
<pre class="brush: plain;">
JAKE.task('docs', function(){
        var path = './tools/jsdoc-toolkit';
         if(FILE.exists(path)){
             var cmd = 'java -jar ' + path + '/jsrun.jar ' +
                 path + '/app/run.js ' + './js/karma.js -d=docs/ ' +
                 '-t=tools/jsdoc-toolkit/templates/jsdoc/';
             OS.system(cmd);
         } else {
             print(&quot;The folder ./tools/jsdoc-toolkit isn't present &quot; +
                   &quot;you need it to generate documentation&quot;);
         }

});
</pre>
<p>May prefer passing an array to OS.system rather than a string.</p>
<pre class="brush: plain;">
var cmd = [
   'java', '-jar', path + '/jsrun.jar',
   path + '/app/run.js', './js/karma.js',
   '-d=docs/', '-t=tools/jsdoc-toolkit/templates/jsdoc/'
];
OS.system(cmd);
</pre>
<p>One task that I run quite quickly is &#8220;jake clean&#8221; which I use to get rid all the temporary files created by text editors. Importing the submodule <em>jake/clean</em> automatically adds the &#8220;clean&#8221; task to your Jakefile. The next step is to add regular expressions that I want cleaned. The syntax for regular expressions here is not what I am used to. If may look odd to you too.</p>
<pre class="brush: plain;">
var CLEAN_LIB = require('jake/clean');  //adds the tasks clean and clobber
var CLEAN = CLEAN_LIB.CLEAN;
//include the temporary files created by text editors
CLEAN.include('**/#*#', '\.#*' , '**/\.tmp*',&quot;**/\.*\.*\.swp&quot;);
CLEAN.exclude('\.git');    //don't touch my .git directory!
</pre>
<p>The regular expressions in the <code>CLEAN.include()</code> statement may look odd and that is because they are not regular expressions at all. Rather they are <a href="http://en.wikipedia.org/wiki/Glob_(programming)">glob expressions</a>.</p>
<p>Now, let&#8217;s run the clean task</p>
<pre class="brush: plain;">
$ jake clean
</pre>
<p>Here I tell the clobber task to delete my entire build-dir/ directory</p>
<pre class="brush: plain;">
var CLOBBER = CLEAN_LIB.CLOBBER;
CLOBBER.include('**/build-dir');
CLOBBER.exclude('\.git');
</pre>
<p>Often during a build process you find yourself checking if a particular file or directory exists and creating it if it doesn&#8217;t. Jake&#8217;s <code>filedir()</code> method is great shorthand for this. JAKE.filedir() which runs the task&#8217;s action if a) the target file (passed as the task name) doesn&#8217;t exist, or b) any of the dependencies&#8217; timestamps are newer than the target&#8217;s timestamp.</p>
<pre class="brush: plain;">
filedir (&quot;build-dir&quot;, [&quot;debug&quot;, &quot;release&quot;], function()
{
    FILE.mkdirs(&quot;build-dir&quot;);
});
</pre>
<p>Now let&#8217;s go back to the 6 build tasks I outlined earlier in this article. Let&#8217;s create the <code>build</code> meta-task. We can specify dependencies for the <code>build</code> task as an array for the second argument to Jake.task. The dependencies will be run before the the <code>build</code> task if they are not up-to-date.</p>
<pre class="brush: plain;">
JAKE.task('build',['checkout', 'build-dir', 'move-common-files', 'repack', 'minify', 'package'],  function()
{
    /* do any finishing touches */
});
</pre>
<p>I have just scratched the surface of what Jake can do. You may want to look through my <a href="http://git.sugarlabs.org/projects/karma/repos/mainline/blobs/master/Jakefile">Jakefile</a> for ideas. Cappuccino&#8217;s <a href="http://github.com/280north/cappuccino/blob/jake/Jakefile">Jakefile</a> is also a great example of what can be done with Jake.</p>
<p><a href="http://github.com/280north/jake">Jake</a> is a great tool and narwhal is a very useful platform. While quite young, <a href="http://www.narwhaljs.org">narwhal</a> is quite a stable and feature-rich platform. The biggest drawback that I have found working with narwhal is that documentation is quite lacking. Hopefully this will be resolved shortly.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/karmaproject.wordpress.com/415/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/karmaproject.wordpress.com/415/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/karmaproject.wordpress.com/415/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/karmaproject.wordpress.com/415/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/karmaproject.wordpress.com/415/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/karmaproject.wordpress.com/415/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/karmaproject.wordpress.com/415/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/karmaproject.wordpress.com/415/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/karmaproject.wordpress.com/415/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/karmaproject.wordpress.com/415/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=415&subd=karmaproject&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://karmaeducation.org/2010/01/15/get-started-with-jake-a-rake-clone-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/21904e6423d8faa7c6d7d2c94f11271b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bryanwb</media:title>
		</media:content>
	</item>
		<item>
		<title>Getting Started with Narwhal, a Standard Library for JavaScript</title>
		<link>http://karmaeducation.org/2010/01/15/getting-started-with-narwhal-a-standard-library-for-javascript/</link>
		<comments>http://karmaeducation.org/2010/01/15/getting-started-with-narwhal-a-standard-library-for-javascript/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 02:38:11 +0000</pubDate>
		<dc:creator>bryanwb</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[commonJS]]></category>
		<category><![CDATA[jake]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[narwhal]]></category>
		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://karmaeducation.org/?p=431</guid>
		<description><![CDATA[I have been writing system administration scripts for a couple of years now, first with just regular Bash and later Python. In terms of bash scripting, I have to admit that while my invocations of my favorite tools, such as sed and find, have become more complicated my use of shell scripting programming statements has remained pretty basic. I don't write shell scripts on a daily basis so in the weeks between writing scripts I manage to re-forget how to write a proper for loop and how to use getopts. <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=431&subd=karmaproject&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>I have been writing system administration scripts for a couple of years now, first with just regular Bash and later Python. In terms of bash scripting, I have to admit that while my invocations of my favorite tools, such as sed and find, have become more complicated my use of shell scripting programming statements has remained pretty basic. I don&#8217;t write shell scripts on a daily basis so in the weeks between writing scripts I manage to re-forget how to write a proper for loop and how to use getopts. </p>
<p>Sadly, this same applies to Python as I am no longer programming in Python on a regular basis. My old setup.py scripts are starting to look unfamiliar. Like a large and growing portion of web developers out there, I spend more than 90% of my time writing JavaScript and the other 10% a mix of PHP, Java, ruby, Python, etc. About 6 months ago I discovered the <a href="http://www.commonjs.org">CommonJS project</a> which seeks to create a standard library for JavaScript and <a href="http://narwhaljs.org">Narwhal</a> implementation of that developing library. There are many great uses of narwhal but my current favorite is the <a href="http://github.com/280north/jake">jake</a> tool, which is clone of ruby&#8217;s popular <a href="http://rubyrake.org/">rake</a> tool. I have been using it for the repetitive tasks in the <a href="git.sugarlabs.org/projects/karma/repos/mainline">Karma project</a> like generating documentation, cleaning up temporary files, and checking out the latest version and packaging it for deployment. I will cover Jake in my next tutorial.</p>
<p>&#8220;But JavaScript is a toy language!&#8221; you declare. &#8220;It isn&#8217;t meant for serious stuff like system administration.&#8221; Let me break it to you softly, by historical accident JavaScript may now be the most popular programming language and boasts some of the fastest run-times for a dynamic language. It isn&#8217;t any less-suited to utility scripting than its more august cousins Ruby and Python.</p>
<p><b>Setting up narwhal</b></p>
<p>Download and extract the http://github.com/280north/narwhal/zipball/master or  http://github.com/280north/narwhal/tarball/master archive, or</p>
<pre class="brush: plain;">
$ git clone git://github.com/280north/narwhal.git
</pre>
<p>You should append the following text to your .bashrc file and then open a new terminal</p>
<pre class="brush: plain;">
export PATH=$PATH:~/narwhal/bin
</pre>
<p>Run &#8220;narwhal&#8221; or &#8220;js&#8221; (they are equivalent).</p>
<p>You need the java5 or java6 JDK to run narwhal which by default runs on top of Rhino, a JavaScript implemented on the Java JVM. Narwhal doesn&#8217;t play well with OpenJDK, so we need to get the JDK from sun. Below are instructions for getting Sun&#8217;s JDK and setting it up on Ubuntu Linux.</p>
<p>First add these lines to your /etc/apt/sources.list<br />
<code><br />
deb http://us.archive.ubuntu.com/ubuntu/ jaunty multiverse<br />
deb http://us.archive.ubuntu.com/ubuntu/ jaunty-updates multiverse<br />
</code><br />
Then run</p>
<pre class="brush: plain;">
$ sudo aptitude update
$ sudo aptitude install sun-java6-jdk     # alternately, sun-java5-jdk
</pre>
<p>You may want to remove the extra lines from your sources.list after you have finished installing the package.</p>
<p>Next you may need to tell Ubuntu explicitly to use Sun’s JDK</p>
<pre class="brush: plain;">
$ sudo update-alternatives —config java

# There are at least 2 alternatives which provide `java’. 

Selection Alternative 

+ * 1 /usr/lib/jvm/java-6-openjdk/jre/bin/java
2 /usr/lib/jvm/java-1.5.0-sun/jre/bin/java 

Press enter to keep the default[*], or type selection number:
# Choose #2
</pre>
<p>To test your install just run narwhal from the command line</p>
<pre class="brush: plain;">
you@computer:/$ narwhal
Rhino 1.7 release 3 PRERELEASE 2009 12 12
js&gt; print(&quot;hello&quot;)
hello
js&gt;
</pre>
<p>We can run our little &#8220;Hello World&#8221; as a script</p>
<pre class="brush: plain;">
// hello.js
print('hello world');
</pre>
<p><code><br />
$ narwhal hello.js<br />
hello<br />
</code></p>
<p>Narwhal has a number of command line options which you can list with <code>narwhal --help</code></p>
<p><b>A Note on Narwhal Engines</b><br />
Narwhal is a standard library for JavaScript, that can run on several different JavaScript implementations. So far it runs on top of Rhino, google&#8217;s V8, and Webkit&#8217;s JavaScriptCore. I believe that there are plans to run on top of the awesome <a href="http://nodejs.org">node.js</a> in the near future. You may find a Rhino a bit slow for your tastes but don&#8217;t let that put you off of Narwhal.</p>
<p>If you are using a Mac you can easily install the JavaScriptCore engine which is part of Webkit. It is approximately 10 times faster than rhino. I have not yet gotten it running successfully on linux. Narwhal has a package manager called tusk, which is comparable to ruby&#8217;s gem and Python&#8217;s easy_install.</p>
<pre class="brush: plain;">
$ tusk install narwhal-jsc
$ cd packages/narwhal-jsc
$ make
 # or &quot;make webkit&quot; if you want to use the jsc context from a webkit instance,
# and thus have access to the DOM and other APIs
 </pre>
<p>From here on, you can run narwhal-jsc by simply running &#8220;narwhal-jsc&#8221; or you can make it your default engine for narwhal by adding <code>NARWHAL_ENGINE=jsc</code> in your .bashrc or whatever file you use to control your bash shell settings.</p>
<p>That&#8217;s it for now, stay tuned for a tutorial on Jake, a rake clone. If you aren&#8217;t familiar with make or rake, they are excellent tools for automating repetitive administration tasks.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/karmaproject.wordpress.com/431/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/karmaproject.wordpress.com/431/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/karmaproject.wordpress.com/431/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/karmaproject.wordpress.com/431/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/karmaproject.wordpress.com/431/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/karmaproject.wordpress.com/431/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/karmaproject.wordpress.com/431/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/karmaproject.wordpress.com/431/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/karmaproject.wordpress.com/431/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/karmaproject.wordpress.com/431/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=431&subd=karmaproject&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://karmaeducation.org/2010/01/15/getting-started-with-narwhal-a-standard-library-for-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/21904e6423d8faa7c6d7d2c94f11271b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bryanwb</media:title>
		</media:content>
	</item>
		<item>
		<title>Creating a Scoreboard Widget with jQuery UI</title>
		<link>http://karmaeducation.org/2010/01/12/creating-a-scoreboard-widget-with-jquery-ui/</link>
		<comments>http://karmaeducation.org/2010/01/12/creating-a-scoreboard-widget-with-jquery-ui/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 13:30:04 +0000</pubDate>
		<dc:creator>bryanwb</dc:creator>
				<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://karmaeducation.org/?p=405</guid>
		<description><![CDATA[I really, really hope that OLE Nepal can move from developing its content in Adobe Flash to JavaScript and HTML5. As I explained to someone recently, this depends entirely on how easy we can make it to quickly stick together a lesson from flexible building blocks. The basic building blocks are user interface widgets. Since [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=405&subd=karmaproject&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>I really, really hope that OLE Nepal can move from developing its content in Adobe Flash to JavaScript and HTML5. As I explained to someone recently, this depends entirely on how easy we can make it to quickly stick together a lesson from flexible building blocks. The basic building blocks are user interface widgets. Since Karma only uses openweb technologies, those widgets can only be created using some combination of html5, javascript, and CSS.</p>
<p>I have no desire to create my own UI widget framework when a number of good ones already  exist. But which framework to choose? There is extjs, dojox, cappuccino, sproutcore, jQuery UI, and others. I wanted a flexible toolkit that could be used in pieces and would have a very low learning curve. This disqualified cappuccino and Sproutcore because those frameworks are both very powerful and very different from other popular web development tools and paradigms. </p>
<p>I settled on jQuery UI for a number of reasons:</p>
<ol>
<li>It is very simple to create your own plugin</li>
<li>It works with CSS and html, not against them</li>
<li>There seems to be a lot of momentum behind jQuery UI</li>
<li><a href="http://themeroller.com">Themeroller.com</a> rocks</li>
<li>A number of core developers have backgrounds in graphic design</li>
</ol>
<p>I really like that actual graphic designers develop jQuery UI. That means that design issues and usability are the forefront. </p>
<p>Enough technology strategy, let&#8217;s get to the tutorial.</p>
<p>Om, myself, Roshan, and Vaibhaw sat down some days ago and created a custom theme for OLE Nepal&#8217;s lessons. This included things such as:</p>
<ul>
<li> border thickness and color</li>
<li> default font and font-size </li>
<li> what buttons should look like when they have the focus, hover, are active</li>
<li>The drop shadow thickness and orientation</li>
<li> and a whole lot more</li>
</ul>
<p>I highly recommend you take a look at <a href="http://themeroller.com">themeroller.com</a>.  If you use the CSS classes in the jQuery UI CSS framework in your application, you get a consistent theme with very little work. A great benefit is that you can change your theme later without having to change your application code at all.<br />
<a href="http://karmaproject.files.wordpress.com/2010/01/themeroller.png"><img src="http://karmaproject.files.wordpress.com/2010/01/themeroller.png?w=300&#038;h=168" alt="" title="themeroller" width="300" height="168" class="aligncenter size-thumbnail wp-image-407" /></a></p>
<p>Then I downloaded the theme and renamed the main theme file ui.theme.css to <a href="http://git.sugarlabs.org/projects/karma/repos/mainline/blobs/master/css/karma.css">karma.css</a></p>
<p>To add the theme to your application, add the following inside the &lt;head&gt; element of your html</p>
<pre class="brush: plain;">
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;../../css/ui.core.css&quot; /&gt;
&lt;link type=&quot;text/css&quot; rel=&quot;stylesheet&quot; href=&quot;../../css/karma.css&quot; /&gt;
</pre>
<p>The widget we needed most urgently was a scoreboard widget for displaying the user&#8217;s score in a game a place to stick game control buttons like &#8220;Play Again&#8221;, &#8220;Start&#8221;, and &#8220;Stop&#8221;. Between Vaibhaw, roxan, and myself we keep creating different scoreboards when one common widget will usually suffice. We decided we need a scoreboard that could be laid out vertically or horizontally as needed. We also wanted a simple set of functions for incrementing the score, decrementing, resetting it, etc.</p>
<p>I spent about two days writing the scoreboard widget, which wasn&#8217;t too bad considering that this was my first jQuery plugin and the end result works with our themeroller theme. Some other time, I will go into the details of how I created the widget, perhaps after I create a couple more. </p>
<p>To add the scoreboard to your application, you first have to include a number of files. Notice that you have to add ui.scoreboard.css <strong>and</strong> ui.scoreboard.js</p>
<pre class="brush: plain;">
        &lt;link type=&quot;text/css&quot; rel=&quot;stylesheet&quot; href=&quot;../../css/ui.scoreboard.css&quot; /&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;../../js/jquery-1.3.2.js&quot;&gt;&lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;../../js/ui.core.js&quot;&gt;&lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;../../js/ui.scoreboard.js&quot;&gt;&lt;/script&gt;
</pre>
<p>You have to have an empty &lt;div&gt; element in your html to hold the scoreboard</p>
<pre class="brush: plain;">
    &lt;div id='scoreArea'&gt;&lt;div&gt;
</pre>
<p>After including all these library files, it takes only one line of JavaScript to make the scoreboard appear on your page.</p>
<pre class="brush: plain;">
  $('#scoreArea').scoreboard();
</pre>
<p>The scoreboard defaults to a horizontal layout as you can see below<br />
<a href="http://karmaproject.files.wordpress.com/2010/01/scoreboard-h.png"><img src="http://karmaproject.files.wordpress.com/2010/01/scoreboard-h.png?w=600&#038;h=336" alt="" title="a horizontal scoreboard" width="600" height="336" class="aligncenter size-medium wp-image-408" /></a></p>
<p>Laying out the scoreboard only requires one additional parameter</p>
<pre class="brush: plain;">
  $('#scoreArea').scoreboard({layout:'vertical});
</pre>
<p><a href="http://karmaproject.files.wordpress.com/2010/01/scoreboard-v.png"><img src="http://karmaproject.files.wordpress.com/2010/01/scoreboard-v.png?w=600&#038;h=336" alt="" title="a vertical scoreboard" width="600" height="336" class="aligncenter size-medium wp-image-409" /></a></p>
<p>You can override the defaults by passing in new values to the scoreboard constructor</p>
<pre class="brush: plain;">
$('#scoreArea').scoreboard({
     layout: 'vertical',
     winningScore: 6,
     score: 0
});
</pre>
<p>Any options you specify in the hash will override the default values; any options you don&#8217;t specify will use the default values.  After you have initialized a plugin, you can change any option at any time using the option method:</p>
<pre class="brush: plain;">
$('#my-elem').scoreboard('option', 'winningScore', 8);
</pre>
<p>scoreboard currently has the following public methods</p>
<pre class="brush: plain;">
setScore(x)
setTotal(x)
getScore()
getTotal()
reset()
inc([val])     //increments score, defaults to 1 unless a value val is passed
dec([val])     //increments score, defaults to 1 unless a value val is passed
incTotal([val])
decTotal([val])

$('#scoreArea').scoreboard('getScore');  //invokes the getScore method
$('#scoreArea').scoreboard('setScore', 5);  //invokes setScore method with the argument 5
</pre>
<p>One really option of using jQuery, is that I can effortlessly issue custom events. </p>
<p>Usually a lesson has code like this to determine if the game has been won</p>
<pre class="brush: plain;">
function questionAnswered(answer) {
   if( answer === correctAnswer){
      //reward user

      if ( totalCorrect === winningScore ) {
            /* stop game and reward user */
     }
   } else {
       /* inform user their answer was wrong */

  }
};
</pre>
<p>The scoreboard emits a custom event <em>&#8216;winGame&#8217; </em> when the winning score is reached. Rather than using an if statement to check if the winning score has been reached I can listen for the &#8216;winGame&#8217; event using jQuery&#8217;s bind method. The benefit of doing this is that repeatable logic lives in the widget and the lesson code can listen for events to control the game rather than run a CPU-intensive game loop</p>
<p>Here is the super-easy code in the widget for emitting the &#8216;winGame&#8217; event</p>
<pre class="brush: plain;">
//ui.scoreboard.js
 if(winningScore === currentScore){
			  this.element.trigger('winGame');
		      }
</pre>
<p>and here is the code for listening for the even</p>
<pre class="brush: plain;">
//lesson.js
 $('#scoreArea').bind('winGame',
		function(){
		    /* reward user */
		});
</pre>
<p>After scoreboard, I need to create widgets for help, simple tests, a Nepali typepad. I also need to figure out how to document these with jsdoc and how best to unit test them.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/karmaproject.wordpress.com/405/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/karmaproject.wordpress.com/405/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/karmaproject.wordpress.com/405/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/karmaproject.wordpress.com/405/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/karmaproject.wordpress.com/405/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/karmaproject.wordpress.com/405/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/karmaproject.wordpress.com/405/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/karmaproject.wordpress.com/405/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/karmaproject.wordpress.com/405/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/karmaproject.wordpress.com/405/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=405&subd=karmaproject&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://karmaeducation.org/2010/01/12/creating-a-scoreboard-widget-with-jquery-ui/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/21904e6423d8faa7c6d7d2c94f11271b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bryanwb</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2010/01/themeroller.png?w=300" medium="image">
			<media:title type="html">themeroller</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2010/01/scoreboard-h.png?w=600" medium="image">
			<media:title type="html">a horizontal scoreboard</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2010/01/scoreboard-v.png?w=600" medium="image">
			<media:title type="html">a vertical scoreboard</media:title>
		</media:content>
	</item>
		<item>
		<title>Karma version 0.2 Released</title>
		<link>http://karmaeducation.org/2009/12/20/karma-version-0-2-released/</link>
		<comments>http://karmaeducation.org/2009/12/20/karma-version-0-2-released/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 16:23:41 +0000</pubDate>
		<dc:creator>bryanwb</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[karma]]></category>
		<category><![CDATA[svg]]></category>

		<guid isPermaLink="false">http://karmaeducation.org/?p=375</guid>
		<description><![CDATA[We are proud to release Karma <a href="http://git.sugarlabs.org/projects/karma/repos/mainline/commits/f5f4abe09af0a328c31b9aa1b34bef5985a83c04">version 0.2</a> today. You can test out the demos <a href="http://karma.sugarlabs.org">here</a>. You need Firefox 3.5 or Google Chrome/Chromium to run the demo. You can download the Karma-2.xo bundle <a href="">here</a>. We now have a well-documented <a href="http://karma.sugarlabs.org/docs/index.html">API</a> and a four part <a href="http://karmaeducation.org/2009/12/14/an-introduction-to-karma-js/">tutorial</a>.

The Karma Project aims to create high-quality open-source educational software using openweb technologies, with special emphasis on the Sugar desktop educational environment. karma.js is a javascript library for manipulating HTML 5 and SVG in any context.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=375&subd=karmaproject&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>We are proud to release Karma <a href="http://git.sugarlabs.org/projects/karma/repos/mainline/">version 0.2</a> today. You can test out the demos <a href="http://karma.sugarlabs.org">here</a>. You need Firefox 3.5 or Google Chrome/Chromium to run the demo. You can download the Karma-2.xo bundle <a href="http://activities.sugarlabs.org/en-US/sugar/downloads/file/26522/karma-2.xo">here</a>. We now have a well-documented <a href="http://karma.sugarlabs.org/docs/index.html">API</a> and a four part <a href="http://karmaeducation.org/2009/12/14/an-introduction-to-karma-js/">tutorial</a>.</p>
<p>The Karma Project aims to create high-quality open-source educational software using openweb technologies, with special emphasis on the Sugar desktop educational environment. karma.js is a javascript library for manipulating HTML 5 and SVG in any context.</p>
<p><a href="http://karmaproject.files.wordpress.com/2009/12/happychimp120x125.png"><img src="http://karmaproject.files.wordpress.com/2009/12/happychimp120x125.png?w=120&#038;h=125" alt="" title="happyChimp120x125" width="120" height="125" class="alignright size-full wp-image-394" /></a><br />
New Features:</p>
<ul>
<li>Stable API</li>
<li>Now much more reliable</li>
<li>Pre-loading of SVG images</li>
<li><a href="http://karma.sugarlabs.org">10+ example lessons</a></li>
</ul>
<p>Features that didn&#8217;t make it into Release 0.2:</p>
<ul>
<li>Internationalization mechanism for inline text</li>
<li>new browsing layout (Chakra)</li>
</ul>
<p>I am particularly proud of the Karma version of <a href="http://karma.sugarlabs.org/examples/Conozco_Uruguay/index.html">&#8220;Conozco a Uruguay&#8221;</a>. You can try it out online right away.</p>
<p><a href="http://karma.sugarlabs.org/examples/Conozco_Uruguay/index.html"><img src="http://karmaproject.files.wordpress.com/2009/12/screenconozco162x100.png?w=180&#038;h=110" alt="" title="screenConozco162x100" width="180" height="110" class="aligncenter size-thumbnail wp-image-392" /></a></p>
<p>If you are interested in Karma, the first step is to join our <a href="http://groups.google.com/group/karmajs">Google Group</a> and to look through our <a href="http://karmaeducation.org/2009/12/14/an-introduction-to-karma-js/">four-part tutorial series</a>.</p>
<ol>
<li><a href="http://karmaeducation.org/2009/12/14/an-introduction-to-karma-js/">Introduction to karma.js</a></li>
<li><a href="http://karmaeducation.org/2009/12/17/karma-tutorial-part-ii-comparing-html-5-canvas-and-svg/">Comparing HTML 5 Canvas and SVG</a></li>
<li><a href="http://karmaeducation.org/2009/12/17/tutorial-iii-building-a-geography-lesson/">Digging into Inkscape</a></li>
<li><a href="http://karmaeducation.org/2009/12/17/tutorial-iii-the-adventure-continues-javascript-and-svg/">JavaScript and SVG</a></li>
</ol>
<p><a href="http://www.olenepal.org">OLE Nepal</a> and <a href="http://sugarlabs.org">SugarLabs</a> deserve special thanks for their continued support of the Karma Project.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/karmaproject.wordpress.com/375/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/karmaproject.wordpress.com/375/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/karmaproject.wordpress.com/375/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/karmaproject.wordpress.com/375/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/karmaproject.wordpress.com/375/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/karmaproject.wordpress.com/375/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/karmaproject.wordpress.com/375/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/karmaproject.wordpress.com/375/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/karmaproject.wordpress.com/375/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/karmaproject.wordpress.com/375/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=375&subd=karmaproject&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://karmaeducation.org/2009/12/20/karma-version-0-2-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/21904e6423d8faa7c6d7d2c94f11271b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bryanwb</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/happychimp120x125.png" medium="image">
			<media:title type="html">happyChimp120x125</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/screenconozco162x100.png?w=150" medium="image">
			<media:title type="html">screenConozco162x100</media:title>
		</media:content>
	</item>
		<item>
		<title>Tutorial IV: The Adventure Continues &#8211; JavaScript and SVG</title>
		<link>http://karmaeducation.org/2009/12/17/tutorial-iii-the-adventure-continues-javascript-and-svg/</link>
		<comments>http://karmaeducation.org/2009/12/17/tutorial-iii-the-adventure-continues-javascript-and-svg/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 19:08:04 +0000</pubDate>
		<dc:creator>bryanwb</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[education]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[karma.js]]></category>
		<category><![CDATA[svg]]></category>

		<guid isPermaLink="false">http://karmaeducation.org/?p=361</guid>
		<description><![CDATA[<strong>Section II: Writing the HTML . . . 5!</strong>

Step 1: It's all about the !DOCTYPE

It all starts with the doctype, please make sure document starts with the following doctype declaration

[sourcecode]

[/sourcecode]
<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=361&subd=karmaproject&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>This  article is the fourth in a series of four</p>
<ol>
<li><a href="http://karmaeducation.org/2009/12/14/an-introduction-to-karma-js/">Introduction to karma.js</a></li>
<li><a href="http://karmaeducation.org/2009/12/17/karma-tutorial-part-ii-comparing-html-5-canvas-and-svg/">This article,Comparing HTML 5 Canvas and SVG</a></li>
<li><a href="http://karmaeducation.org/2009/12/17/tutorial-iii-building-a-geography-lesson/">Digging into Inkscape</a></li>
<li><a href="http://karmaeducation.org/2009/12/17/tutorial-iii-the-adventure-continues-javascript-and-svg/">This article, JavaScript and SVG</a></li>
</ol>
<p><strong>Section II: Writing the HTML . . . 5!</strong></p>
<p>Step 1: It&#8217;s all about the !DOCTYPE</p>
<p>It all starts with the doctype, please make sure document starts with the following doctype declaration</p>
<pre class="brush: plain;">
&lt;!DOCTYPE html&gt;
</pre>
<p>DO NOT USE the following</p>
<pre class="brush: plain;">
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot;
     &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;
</pre>
<p>Karma will throw a big fat ugly error message and be otherwise unkind</p>
<p>Step 2: Put the JavaScript in</p>
<pre class="brush: plain;">
    &lt;script type=&quot;text/javascript&quot; src=&quot;../../js/jquery-1.3.2.min.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;../../js/karma.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;../../js/jquery.svg.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;../../js/jquery.svgdom.js&quot;&gt;&lt;/script&gt;
    &lt;!-- Put your code in lesson.js, please --&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;js/lesson.js&quot;&gt;&lt;/script&gt;
</pre>
<p>Step 3: You should &lt;object&gt;</p>
<p>Please use &lt;object&gt; tag to embed your SVG into the HTML</p>
<p>Step 4: Don&#8217;t put style information into the document, put it in lesson.css</p>
<p>Please don&#8217;t do the following</p>
<pre class="brush: plain;">
&lt;div id=&quot;badDiv&quot; style=&quot;display:inline;font-size:bigger;&quot;&gt; Karma Rulez! &lt;/div&gt;
</pre>
<p>The &#8220;Karma Rulez!&#8221; part is excusable but please don&#8217;t use the<br />
<strike>style</strike> attribute. It is not only bad practice but it<br />
will totally screw up the as-yet-unwritten internationalization<br />
library <strong>mundo.js</strong></p>
<p><strong>Section III: The Presentation Layer with CSS</strong></p>
<p>Good News, you can use the same CSS file for both your html and SVG<br />
To use an external css file in your SVG, you need to link it in just above the first &lt;svg&gt; tag.</p>
<pre class="brush: plain;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;
&lt;?xml-stylesheet href=&quot;../../css/lesson.css&quot; type=&quot;text/css&quot;?&gt;
&lt;svg ......
</pre>
<p>We want to make sure that the text on the map and the spaceship are invisible. Let&#8217;s also make the cursor become a pointer when it moves over a capital icon.</p>
<pre class="brush: plain;">
.text { display: none; }
.capital.city { cursor: pointer;}
.spaceship { display: none;}
</pre>
<p>Much of our program logic will be to reveal elements of the map and the spaceship when the user answers correctly.</p>
<p>CSS is a complicated, tricky beast that I can&#8217;t begin to explain here.  Let&#8217;s move onto something easy, like JavaScript!</p>
<p><strong>Section IV: Writing the game logic with JavaScript</strong></p>
<p>Step 1: Meet jQuery, jQuery meet you</p>
<p>jQuery is a fantastic library for interacting with the HTML Document<br />
Object Model or DOM. What is this &amp;^&amp;$$! DOM? The DOM is the<br />
programming interface for HTML in the browser. Working with it<br />
directly is pretty painful but jQuery abstracts it to something that<br />
is actually quite pleasant to work with. We start with jQuery using the following:</p>
<pre class="brush: plain;">
$(document).ready(
    function(){
        //Once the HTML on this page has fully loaded,
	// run my code here

    }
);
</pre>
<p>$(document).ready doesn&#8217;t mean that <em>everything</em> is ready. It only means that all the html and &lt;img&gt; elements have finished downloading. It does not mean than any &lt;audio&gt;, &lt;video&gt;, or &lt;svg&gt; elements have finished downloading. This is important as your program will fail if it tries to manipulate any of these elements before they are <em>ready</em>.</p>
<p>Step 2: Tell Karma about your assets and Karma will put them into collections</p>
<pre class="brush: plain;">
	var k = Karma({
			  svg :[
                              {name:'capitals', domId: 'capitals'},
			      {name:'alien', domId: 'alien'},
			      {name:'spaceship', domId: 'spaceship'},
			      {name: 'help', domId: 'helpScreen'},
			      {name:'playAgain', domId:'playAgain'},
			      {name:'helpIcon', domId:'helpIcon'},
			      {name:'quitIcon', domId:'quitIcon'}
			  ],
			  audio:[
			      {name:'correct', file:'correct.ogg'},
			      {name:'incorrect', file:'incorrect.ogg'}
			  ]
	});
</pre>
<p>The Karma() method initializes Karma.karma object with assets we pass<br />
to it. During this initialization, the svg array we pass in is<br />
converted to the <strong>&#8220;collection&#8221;</strong> object k.svg. You can access any of the values k.svg<br />
using the name you passed in as the key. The &#8216;name&#8217; property is arbitrary. I use this instead of the filename or domID because those can often be lengthy.</p>
<pre class="brush: plain;">
var mySvg = k.svg.helpIcon  //same as &lt;svg id=&quot;helpIcon&quot;&gt;
var myHeight = k.svg.helpIcon.height;
var myWidth = k.svg.helpIcon.width;

var name = 'playAgain';
var mySvg2 = k.svg[name];  //you can reference different assets programmatically

//You can even loop through the entire collection, but remember a collection
//is an object and not an array
$.each(k.svg, function(mySvg){ mySvg.css('display', 'none');
</pre>
<p>Accessing these items using the collections has an optional<br />
performance benefit. Every time you use a css selector such as<br />
document.getElementByid(&#8217;someId&#8217;) or $(&#8216;#someId&#8217;), you run a search<br />
through the entire DOM.</p>
<p>Karma currently supports five different <strong>collection</strong> types, k.audio,<br />
k.canvas, k.svg, and k.video. Please note that the prefix &#8216;k&#8217; is an<br />
arbitratry shortcut to Karma.karma and not by default part of the<br />
global scope.</p>
<p>Karma will also throw errors if any of these assets can&#8217;t be<br />
loaded. You will probably find this very helpfulin debugging your<br />
application. Karma() also attaches helper methods and properties.</p>
<p>You do not have to tell Karma about all the images, svg images, and audio files in your application. You only need to tell Karma about the ones you want to helper methods for and to be fully loaded when you call Karma.ready()</p>
<p>Step 2: Get Ready!</p>
<p>Most likely, you won&#8217;t want to start our program until all of our assets are ready. $(document).ready only makes sure that the HTML and images have loaded. For this reason, Karma provides Karma.ready(), which blocks your program from running until all the assets you told it about in Karma( ) have fully loaded.</p>
<pre class="brush: plain;">
$(document).ready(
    function(){
	var k =	Karma({ /* tell Karma about your assets */});

	k.ready(function (){

        //Once the HTML on this page has fully loaded,
	// run my code here
	}
    }
);
</pre>
<p>You have to wrap your code in function () { } otherwise it will<br />
execute immediately, without waiting for Karma to be ready().</p>
<p>Step 3: Attach Event Handlers to the capitals on the map</p>
<pre class="brush: plain;">

 $.map($('.capital.city', capRoot), function(elem){
		$(elem, capRoot).bind('click', function(event) {
                        if (isActive){
		             checkAnswer(event.target);
                        } else {
                               return;
                        }
	       });
});
// $('.capital.city', capRoot)  is a CSS selector that matches every city icon on the map
// $.map( )  executes the anonymous function on each matching item
// .bind() is a jQuery method that attaches an Event Listener
// isActive is a global variable, when the game is paused, it is false
// When the game is paused rather than removing the event handler, simply set isActive to false
</pre>
<p>Step 4: Make the alien talk</p>
<p>This is easy, use jQuery&#8217;s text() method to change the text</p>
<p>The CSS selector &#8216;foreignObject #alienQuestion&#8217; matches an element with the id alienQuestion preceded by a foreignObject tag. For some reason it doesn&#8217;t work without including foreignObject in the selector. Perhaps this is a quirk having to do with using the foreignObject tag. </p>
<pre class="brush: plain;">
 $('foreignObject #alienQuestion', k.svg.alien.root).text('some Text');
</pre>
<p>Step 5: When user makes correct choice, display text and reveal part of the spaceship</p>
<pre class="brush: plain;">
//complex command that splits out a part name from those not yet displayed
var part = parts.splice(0,1)[0];  

//previously display for these items was set to 'none', 'block' makes them visible again
$('#' + part, spaceshipRoot).css('display', 'block');
$('foreignObject #alienQuestion', k.svg.alien.root).text(&quot;Correct! &quot; + question.capitalName
    +  &quot; is the capital of &quot; + question.deptName);
//display the text of State and capital
$('.text.' + question.dept, capRoot).css('display', &quot;block&quot;);
</pre>
<p>Step 6: Make the spaceship fly away</p>
<pre class="brush: plain;">
	    var flyAway = function(){
		var isLaunching = true;

		var startEngines = function(){
		var shipFire1 = $('#shipFire1', spaceshipRoot);
		var shipFire2 = $('#shipFire2', spaceshipRoot);
		var toggle = true;		    

	   //this animation alternates the display of two images
           //to simulate rocket propulsion
	    var toggleFires = function(){
			if(isLaunching){
			    if(toggle){
				shipFire1.css('display', &quot;none&quot;);
				shipFire2.css('display', &quot;block&quot;);
			    }else{
				shipFire1.css('display', &quot;block&quot;);
				shipFire2.css('display', &quot;none&quot;);
			    }
			    //toggle fires
			    toggle = !toggle;
			    setTimeout(toggleFires, 400);
			}
		    };

		    toggleFires();
		};

		var fly =  function(){
		    $('#spaceship').animate({&quot;bottom&quot;:&quot;550px&quot;},
			    {&quot;duration&quot;:8000,
			    &quot;complete&quot;: function(){
				isLaunching = false;
				showPlayAgain();
			    }});
		};

		var blastOff = function(){
		    startEngines();
		    setTimeout(fly, 2000);
		};

		blastOff();

	    };
</pre>
<p>Step 7: Dialog Boxes</p>
<p>This part is actually quite easy</p>
<p>A &#8216;dialog box&#8217; is nothing more than an absolutely positioned &lt;div&gt;</p>
<p>The following code takes a div element that previously hidden and centers it in front of everything else in the screen using the &#8216;z-index&#8217; property</p>
<pre class="brush: plain;">
$('#helpScreen').css({&quot;position&quot;: &quot;absolute&quot;,
				&quot;width&quot;: &quot;420px&quot;, &quot;height&quot;: &quot;360px&quot;,
				'top': '25px', 'left': '20%',
				'z-index' : 20,  'display':'block', &quot;opacity&quot;: 1});
</pre>
<p>For further exploration, I highly recommend reading through the <a href="http://git.sugarlabs.org/projects/karma/repos/mainline/blobs/master/examples/Conozco_Uruguay/js/lesson.js">original source code</a>.<br />
I haven&#8217;t explained every part of this exercise in detail so please leave me comments with what you don&#8217;t understand or think should be explained in greater detail.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/karmaproject.wordpress.com/361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/karmaproject.wordpress.com/361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/karmaproject.wordpress.com/361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/karmaproject.wordpress.com/361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/karmaproject.wordpress.com/361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/karmaproject.wordpress.com/361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/karmaproject.wordpress.com/361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/karmaproject.wordpress.com/361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/karmaproject.wordpress.com/361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/karmaproject.wordpress.com/361/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=361&subd=karmaproject&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://karmaeducation.org/2009/12/17/tutorial-iii-the-adventure-continues-javascript-and-svg/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/21904e6423d8faa7c6d7d2c94f11271b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bryanwb</media:title>
		</media:content>
	</item>
		<item>
		<title>Tutorial III: Digging into Inkscape</title>
		<link>http://karmaeducation.org/2009/12/17/tutorial-iii-building-a-geography-lesson/</link>
		<comments>http://karmaeducation.org/2009/12/17/tutorial-iii-building-a-geography-lesson/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 17:10:00 +0000</pubDate>
		<dc:creator>bryanwb</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[education]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[svg]]></category>
		<category><![CDATA[Uruguay]]></category>

		<guid isPermaLink="false">http://karmaeducation.org/?p=332</guid>
		<description><![CDATA[In the previous two tutorials I introduced you to the karma.js library and to working with HTML 5 &#60;canvas&#62; and &#60;svg&#62;. In this tutorial, I will walk you through creating a geography lesson from scratch. This lesson will teach how user the location of the different
states of the country Uruguay. 

I confess, I did not create this lesson from scratch. The excellent original "Conozco a Uruguay" (I know Uruguay) was created by Gabriel Eirea and his friends at CeibalJam!, a grassroots organization developing educational software for Uruguay's nationwide OLPC
implementation. The original version of Conozco a Uruguay was written entirely in Python, using the excellent pygame library, a python wrapper around the animation library SDL. Why rewrite it using HTML5 and JavaScript? The reasoning is simple. CeibalJam! spends a lot of its time teaching Uruguayan volunteers how to write educational software using Python. At the same time, there are a large number of web developers in Uruguay who could start developing with Karma with only minimal training. This situtation holds true for Nepal as well. We spend a lot of our time teaching developers how to program in flash when there are lots of local developers who already know HTML and JavaScript.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=332&subd=karmaproject&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>This  article is the third in a series of four</p>
<ol>
<li><a href="http://karmaeducation.org/2009/12/14/an-introduction-to-karma-js/">Introduction to karma.js</a></li>
<li><a href="http://karmaeducation.org/2009/12/17/karma-tutorial-part-ii-comparing-html-5-canvas-and-svg/">Comparing HTML 5 Canvas and SVG</a></li>
<li><a href="http://karmaeducation.org/2009/12/17/tutorial-iii-building-a-geography-lesson/">This article, Digging into Inkscape</a></li>
<li><a href="http://karmaeducation.org/2009/12/17/tutorial-iii-the-adventure-continues-javascript-and-svg/">JavaScript and SVG</a></li>
</ol>
<p>In the previous two tutorials I introduced you to the karma.js library and to working with HTML 5 &lt;canvas&gt; and &lt;svg&gt;. In this tutorial, I will walk you through creating a geography lesson from scratch using Inkscape. Tutorial #4 will cover manipulating our SVG graphics using JavaScript</p>
<p>This lesson will teach how user the location of the different<br />
states of the country Uruguay. </p>
<p>I confess, I did not create this lesson from scratch. The excellent original &#8220;Conozco a Uruguay&#8221; (I know Uruguay) was created by Gabriel Eirea and his friends at CeibalJam!, a grassroots organization developing educational software for Uruguay&#8217;s nationwide OLPC<br />
implementation. The original version of Conozco a Uruguay was written entirely in Python, using the excellent pygame library, a python wrapper around the animation library SDL. Why rewrite it using HTML5 and JavaScript? The reasoning is simple. CeibalJam! spends a lot of its time teaching Uruguayan volunteers how to write educational software using Python. At the same time, there are a large number of web developers in Uruguay who could start developing with Karma with only minimal training. This situtation holds true for Nepal as well. We spend a lot of our time teaching developers how to program in flash when there are lots of local developers who already know HTML and JavaScript.</p>
<p><strong>The Plot</strong></p>
<p>A poor alien has crash-landed on earth. The pieces of his spaceship have scattered across Uruguay. The user must help him locate the correct state where individual pieces are located. Once all the parts are found, the friendly alien can fly home.</p>
<p>Before we go any further please <a href="karma.sugarlabs.org/examples/Conozco-Uruguay">play the game</a> at least once. You can browse all the source code for this example <a href="http://git.sugarlabs.org/projects/karma/repos/mainline/trees/master/examples/Conozco-Uruguay">here</a>.</p>
<p><strong>How we are going to do it</strong></p>
<p>We will have three main graphical elements, a political map of Uruguay, a spaceship, and an alien that asks the user questions. We will use SVG images for all three. The main action through out this application will be alternately showing and hiding text that happens to be superimposed on graphical images. SVG particularly shines in<br />
this use case.</p>
<p><strong>Section 1: Digging into Inkscape</strong></p>
<p>In creating the Karma version of Conozco, I cheated. I didn&#8217;t create all the graphics from hand. In fact I didn&#8217;t draw a single one of them. I converted the original <em>.png</em> images to <em>.svg</em> using inkscape&#8217;s Trace Bitmap feature. I added a few images such as the help and exit icons. Those are GNOME icons that I copied.</p>
<p>You may find this section frustrating if you have never used inkscape before. I highly recommend you run throught the Inkscape Basic tutorial. You can find it through the Help menu in inkscape. Help &gt; Tutorials &gt; Inkscape:Basic</p>
<p>Step 1: Convert the map</p>
<p>File &gt; Import<br />
Choose the image in folder tutorial3/mapStates.png</p>
<p>Path &gt; Trace Bitmap</p>
<p>Select Edge Detection and then click OK<br />
<div id="attachment_334" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/maptracebitmap.png"><img src="http://karmaproject.files.wordpress.com/2009/12/maptracebitmap.png?w=150&#038;h=99" alt="Tracing the Bitmap" title="Tracing the Bitmap" width="150" height="99" class="size-thumbnail wp-image-334" /></a><p class="wp-caption-text">Tracing out the map</p></div></p>
<p>The Trace Bitmap dialog doesn&#8217;t close automatically so just click the X in the upper right corner.</p>
<p>Now click on your image and drag it to the right. You have two maps! The one on top is the SVG version. The one below is the old png. Delete the one below it.<br />
<div id="attachment_335" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/twomaps.png"><img src="http://karmaproject.files.wordpress.com/2009/12/twomaps.png?w=150&#038;h=80" alt="maps superimposed" title="Maps superimposed" width="150" height="80" class="size-thumbnail wp-image-335" /></a><p class="wp-caption-text">The one below is png and the one on top SVG</p></div></p>
<p>You may find that the white rectangle does not line up with your map. You can fix this with File &gt; Document Properties  then click the button &#8220;Fit Page to Selection.&#8221;<br />
<div id="attachment_336" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/fittoselection.png"><img src="http://karmaproject.files.wordpress.com/2009/12/fittoselection.png?w=150&#038;h=80" alt="fit page to selection" title="Fit page to selection" width="150" height="80" class="size-thumbnail wp-image-336" /></a><p class="wp-caption-text">Fit the page to the map</p></div></p>
<p>Step 2: Create the Capitals and the States</p>
<p>The great thing about SVG is that you don&#8217;t have to remember any x,y coordinates. Just put something on the map, set the element ID, and you can always access it later using a CSS selector statement.</p>
<p>Let&#8217;s create the marker for the state capitals</p>
<p>Click the button with the circle icon in the left vertical tool bar, then create a little circle</p>
<p>Next, right click on your circle and choose the option &#8220;Fill and Stroke&#8221;</p>
<p>At the fill tab, set all values to 255, to fill the circle with white<br />
<div id="attachment_337" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/fillandstrokefill.png"><img src="http://karmaproject.files.wordpress.com/2009/12/fillandstrokefill.png?w=150&#038;h=80" alt="" title="fillandstrokeFill" width="150" height="80" class="size-thumbnail wp-image-337" /></a><p class="wp-caption-text">Set the color for the fill</p></div></p>
<p>Then go to the &#8220;Stroke paint&#8221; tab, make the R, G, and B values 0, 0, 0<br />
<div id="attachment_338" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/fillandstrokestroke.png"><img src="http://karmaproject.files.wordpress.com/2009/12/fillandstrokestroke.png?w=150&#038;h=80" alt="" title="fillandstrokeStroke" width="150" height="80" class="size-thumbnail wp-image-338" /></a><p class="wp-caption-text">Set the color for the Stroke/Outline</p></div></p>
<p>Now drag your little capital into place on the map.</p>
<div id="attachment_340" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/dragcapital1.png"><img src="http://karmaproject.files.wordpress.com/2009/12/dragcapital1.png?w=150&#038;h=80" alt="" title="dragCapital" width="150" height="80" class="size-thumbnail wp-image-340" /></a><p class="wp-caption-text">Drag onto map</p></div>
<p>Click the &#8220;Text&#8221; icon on the left vertical toolbar. We are going to create two &lt;text&gt; fields, one for the state and one for the state capital. Make sure the font size isn&#8217;t too big for the area you highlighted<br />
<div id="attachment_343" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/createtextarea.png"><img src="http://karmaproject.files.wordpress.com/2009/12/createtextarea.png?w=150&#038;h=80" alt="" title="createTextArea" width="150" height="80" class="size-thumbnail wp-image-343" /></a><p class="wp-caption-text">Create the text elements</p></div></p>
<p>Step 3: Add element ids and class names to the states and capital cities</p>
<p>Click on the icon you created for the capital, then click on the icon &#8220;Edit XML Tree&#8221; in the upper menu bar.<br />
<div id="attachment_342" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/clickoneditxmltree.png"><img src="http://karmaproject.files.wordpress.com/2009/12/clickoneditxmltree.png?w=150&#038;h=80" alt="" title="clickOnEditXMLTree" width="150" height="80" class="size-thumbnail wp-image-342" /></a><p class="wp-caption-text">Edit the XML Tree manually</p></div></p>
<p>Highlight the &#8220;id&#8221; attribute in the upper right box. Then type in a new id in the bottom blank area. MAKE SURE YOU CLICK SET otherwise your change will not stick.</p>
<p>Set the id to &#8220;cap&#8221; + yourCapitalName<br />
<div id="attachment_344" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/capitalsetid.png"><img src="http://karmaproject.files.wordpress.com/2009/12/capitalsetid.png?w=150&#038;h=90" alt="" title="capitalSetId" width="150" height="90" class="size-thumbnail wp-image-344" /></a><p class="wp-caption-text">Set the element id</p></div></p>
<p>Now we are going to create a new attribute &#8220;class&#8221;</p>
<p>Go to the box where there currently is id and type &#8220;class&#8221; in its place. For the value, type in:</p>
<p><code><br />
capital city yourCapitalName yourStateName<br />
</code><br />
make sure you click SET!</p>
<div id="attachment_345" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/typinginclass.png"><img src="http://karmaproject.files.wordpress.com/2009/12/typinginclass.png?w=150&#038;h=90" alt="" title="typingInClass" width="150" height="90" class="size-thumbnail wp-image-345" /></a><p class="wp-caption-text">Type in class names</p></div><br />
Repeat the above for your capital icons</p>
<p>Set the id&#8217;s for all the capital text areas with &#8220;text&#8221; + CapitalName<br />
and the id&#8217;s for the States with &#8220;textDept&#8221; + YourStateName. Using<br />
these prefixes and camelCase consistently will save your <em>tuckus</em> later.</p>
<p>For each state text area, add the following class names<br />
<code><br />
text dept yourStateName yourCapitalName<br />
</code></p>
<p>For each capital text area, add the following class names<br />
<code><br />
text capital yourCapitalName<br />
</code></p>
<p>You may notice that I use &#8220;dept&#8221; frequently. That is because Uruguay calls their states &#8220;departamentos.&#8221; It was easier for me to continue using their convention rather than using &#8220;state.&#8221;</p>
<p>I can&#8217;t overstate the importance of setting the id and class properties correctly and to do it consistently. </p>
<p>Using these properties I can do operations such as the following</p>
<pre class="brush: plain;">
//hide all text on the map
$('.text', k.svg.capitals.root).css('display', 'none');

//show the text only for yourState and yourCapital
$('.text.yourStateName', k.svg.capitals.root).css('display', 'block');
</pre>
<p>Wow! Them css selectors aRrrre powerful stuff!</p>
<p>Step 4: Put it into the directory assets/svg</p>
<p>If you haven&#8217;t created a project folder yet, now is a good time. Here is a good template</p>
<pre class="brush: plain;">
mykarma/
        assets/
               audio/
               svg/
        css/
        js/
</pre>
<p>Step 5: Convert the alien</p>
<p>File &gt; Import   choose alien.png</p>
<p>Path &gt; Trace Bitmap&#8230;</p>
<p>Go to the area &#8220;Multiple Scans: create groups of paths&#8221; and select the option &#8220;Colors&#8221;</p>
<p>Next Click OK. </p>
<p><div id="attachment_346" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/tracecolors.png"><img src="http://karmaproject.files.wordpress.com/2009/12/tracecolors.png?w=150&#038;h=99" alt="" title="traceColors" width="150" height="99" class="size-thumbnail wp-image-346" /></a><p class="wp-caption-text">Trace the Colors</p></div>
<p>Delete the image underneath as you did before.</p>
<div id="attachment_347" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/deleteimageunderneath.png"><img src="http://karmaproject.files.wordpress.com/2009/12/deleteimageunderneath.png?w=150&#038;h=80" alt="" title="deleteImageUnderneath" width="150" height="80" class="size-thumbnail wp-image-347" /></a><p class="wp-caption-text">Delete the Image Below</p></div>
<p>Step 6: Create the Word Bubble</p>
<p>Click the button &#8220;Create Rectangles and Squares&#8221; and drag out a nice big rectangle</p>
<div id="attachment_348" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/createwordbubble.png"><img src="http://karmaproject.files.wordpress.com/2009/12/createwordbubble.png?w=150&#038;h=80" alt="" title="createWordBubble" width="150" height="80" class="size-thumbnail wp-image-348" /></a><p class="wp-caption-text">Create the Word Bubble</p></div>
<p>Give the rectangle nice rounded corners, set the values in the Rx and Ry boxes to 50</p>
<div id="attachment_349" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/setrxry.png"><img src="http://karmaproject.files.wordpress.com/2009/12/setrxry.png?w=150&#038;h=80" alt="" title="setRxRy" width="150" height="80" class="size-thumbnail wp-image-349" /></a><p class="wp-caption-text">Round the corners</p></div>
<p>If you can&#8217;t see the Rx and Ry boxes, click the &#8220;Create Rectangles and Square&#8221; box again</p>
<p>Click on Text in the left vertical icon bar and type some text in the word bubble area. Make text area as big as the word bubble.</p>
<div id="attachment_350" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/wordbubblecreatetextarea.png"><img src="http://karmaproject.files.wordpress.com/2009/12/wordbubblecreatetextarea.png?w=150&#038;h=80" alt="" title="wordBubbleCreateTextArea" width="150" height="80" class="size-thumbnail wp-image-350" /></a><p class="wp-caption-text">Create the Text Area</p></div>
<p>Go to File &gt; Document Properties and click &#8220;Fit Page to Selection&#8221;<br />
<div id="attachment_351" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/fittoselection1.png"><img src="http://karmaproject.files.wordpress.com/2009/12/fittoselection1.png?w=150&#038;h=80" alt="" title="fitToSelection" width="150" height="80" class="size-thumbnail wp-image-351" /></a><p class="wp-caption-text">Fit the page to your image</p></div></p>
<p>Save your SVG as alien.svg and close Inkscape</p>
<p>Step 7: Text Don&#8217;T Wrap in SVG! A Beautiful Hack</p>
<p>In previous, tutorial I explained that inkscape does not support word wrapping in &lt;text&gt; elements. Let&#8217;s put into practice the hack I discussed last time.</p>
<p>Open up alien.svg in a text editor</p>
<p>Go to the portion of your file with the text in it. If it is<br />
surrounded with &lt;flowPara&gt; tag, it should be fine. Leave it as<br />
it is. If it is in a &lt;text&gt; element, delete the entire &lt;text&gt; element and replace it with the following:</p>
<pre class="brush: plain;">
 &lt;text&gt;
 &lt;foreignObject
     id=&quot;textHack&quot;&gt;
    &lt;xhtml:body&gt;
      &lt;xhtml:div
         id=&quot;alienQuestion&quot;
         style=&quot;font-size:20px&quot;&gt;&lt;/xhtml:div&gt;
    &lt;/xhtml:body&gt;
  &lt;/foreignObject&gt;
  &lt;/text&gt;
</pre>
<p>Step 8: Convert the spaceship</p>
<p>There may be a much easier way to do this. If you know of one, please leave a comment.<br />
Import ship.png</p>
<p>Path &gt; Trace Bitmap</p>
<p>Select Multiple Scan, Grays and click OK</p>
<div id="attachment_352" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/shipmultscansgray.png"><img src="http://karmaproject.files.wordpress.com/2009/12/shipmultscansgray.png?w=150&#038;h=99" alt="" title="shipMultScansGray" width="150" height="99" class="size-thumbnail wp-image-352" /></a><p class="wp-caption-text">Multiple Scans with Grayscale</p></div><br />
Drag out your grayscale version of the rocket</p>
<p>In the left vertical bar, click paint bucket</p>
<p>Select the red color from the lower color bar<br />
<div id="attachment_354" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/pickcolor.png"><img src="http://karmaproject.files.wordpress.com/2009/12/pickcolor.png?w=150&#038;h=80" alt="" title="pickColor" width="150" height="80" class="size-thumbnail wp-image-354" /></a><p class="wp-caption-text">Pick a color</p></div>
<p>dump paint into the left wing in the grayscale image, then drag it away</p>
<div id="attachment_355" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/paintthewing.png"><img src="http://karmaproject.files.wordpress.com/2009/12/paintthewing.png?w=150&#038;h=80" alt="" title="paintTheWing" width="150" height="80" class="size-thumbnail wp-image-355" /></a><p class="wp-caption-text">Paint the Wing</p></div>
<div id="attachment_356" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/dragoutthewing.png"><img src="http://karmaproject.files.wordpress.com/2009/12/dragoutthewing.png?w=150&#038;h=80" alt="" title="dragOutTheWing" width="150" height="80" class="size-thumbnail wp-image-356" /></a><p class="wp-caption-text">Drag out the wing</p></div>
<p>repeat for the rest of the space ship to reassemble your ship</p>
<p>Delete the original image and the grayscale</p>
<p>Highlight each individual piece of the ship then choose &#8220;Edit the XML tree&#8221; and set the id</p>
<div id="attachment_357" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/settingidforshipleftwing.png"><img src="http://karmaproject.files.wordpress.com/2009/12/settingidforshipleftwing.png?w=150&#038;h=87" alt="" title="settingIdForShipLeftWing" width="150" height="87" class="size-thumbnail wp-image-357" /></a><p class="wp-caption-text">Set the Id on the left wing</p></div>
<p>There are other SVG images in the Karma version of Conozco a Uruguay but there is nothing more advanced in them than what we have covered so far. Whatever you do, please put all the your SVG elements into the folder assets/svg/</p>
<p>Next up is manipulating SVG using JavaScript.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/karmaproject.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/karmaproject.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/karmaproject.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/karmaproject.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/karmaproject.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/karmaproject.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/karmaproject.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/karmaproject.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/karmaproject.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/karmaproject.wordpress.com/332/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=332&subd=karmaproject&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://karmaeducation.org/2009/12/17/tutorial-iii-building-a-geography-lesson/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/21904e6423d8faa7c6d7d2c94f11271b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bryanwb</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/maptracebitmap.png?w=150" medium="image">
			<media:title type="html">Tracing the Bitmap</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/twomaps.png?w=150" medium="image">
			<media:title type="html">Maps superimposed</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/fittoselection.png?w=150" medium="image">
			<media:title type="html">Fit page to selection</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/fillandstrokefill.png?w=150" medium="image">
			<media:title type="html">fillandstrokeFill</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/fillandstrokestroke.png?w=150" medium="image">
			<media:title type="html">fillandstrokeStroke</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/dragcapital1.png?w=150" medium="image">
			<media:title type="html">dragCapital</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/createtextarea.png?w=150" medium="image">
			<media:title type="html">createTextArea</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/clickoneditxmltree.png?w=150" medium="image">
			<media:title type="html">clickOnEditXMLTree</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/capitalsetid.png?w=150" medium="image">
			<media:title type="html">capitalSetId</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/typinginclass.png?w=150" medium="image">
			<media:title type="html">typingInClass</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/tracecolors.png?w=150" medium="image">
			<media:title type="html">traceColors</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/deleteimageunderneath.png?w=150" medium="image">
			<media:title type="html">deleteImageUnderneath</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/createwordbubble.png?w=150" medium="image">
			<media:title type="html">createWordBubble</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/setrxry.png?w=150" medium="image">
			<media:title type="html">setRxRy</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/wordbubblecreatetextarea.png?w=150" medium="image">
			<media:title type="html">wordBubbleCreateTextArea</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/fittoselection1.png?w=150" medium="image">
			<media:title type="html">fitToSelection</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/shipmultscansgray.png?w=150" medium="image">
			<media:title type="html">shipMultScansGray</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/pickcolor.png?w=150" medium="image">
			<media:title type="html">pickColor</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/paintthewing.png?w=150" medium="image">
			<media:title type="html">paintTheWing</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/dragoutthewing.png?w=150" medium="image">
			<media:title type="html">dragOutTheWing</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/settingidforshipleftwing.png?w=150" medium="image">
			<media:title type="html">settingIdForShipLeftWing</media:title>
		</media:content>
	</item>
		<item>
		<title>Karma Tutorial Part II: Comparing HTML 5 Canvas and SVG</title>
		<link>http://karmaeducation.org/2009/12/17/karma-tutorial-part-ii-comparing-html-5-canvas-and-svg/</link>
		<comments>http://karmaeducation.org/2009/12/17/karma-tutorial-part-ii-comparing-html-5-canvas-and-svg/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 04:40:54 +0000</pubDate>
		<dc:creator>bryanwb</dc:creator>
				<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://karmaeducation.org/?p=314</guid>
		<description><![CDATA[This  article is the second in a series of four

Introduction to karma.js
This article,Comparing HTML 5 Canvas and SVG
Digging into Inkscape
JavaScript and SVG

In &#8220;Introduction to karma.js&#8221; I gave an overview of how the library works. In this article, I want to give an overview of the relative merits of HTML 5 &#60;canvas&#62; and SVG in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=314&subd=karmaproject&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>This  article is the second in a series of four</p>
<ol>
<li><a href="http://karmaeducation.org/2009/12/14/an-introduction-to-karma-js/">Introduction to karma.js</a></li>
<li><a href="http://karmaeducation.org/2009/12/17/karma-tutorial-part-ii-comparing-html-5-canvas-and-svg/">This article,Comparing HTML 5 Canvas and SVG</a></li>
<li><a href="http://karmaeducation.org/2009/12/17/tutorial-iii-building-a-geography-lesson/">Digging into Inkscape</a></li>
<li><a href="http://karmaeducation.org/2009/12/17/tutorial-iii-the-adventure-continues-javascript-and-svg/">JavaScript and SVG</a></li>
</ol>
<p>In &#8220;Introduction to karma.js&#8221; I gave an overview of how the library works. In this article, I want to give an overview of the relative merits of HTML 5 &lt;canvas&gt; and SVG in the context of creating your application. In the next article, I will walk you through the creation of a geography lesson using karma.js. You will need google chrome or Firefox &gt; 3.5 and the following libraries:</p>
<ul>
<li>karma.js</li>
<li>jquery-1.3.2.js</li>
<li>jquery.svg.js</li>
<li>jquery.svgdom.js</li>
</ul>
<p>You can grab them all <a href="http://karma.sugarlabs.org/karma-demos/js/">here</a></p>
<p>This tutorial will require a basic understanding of css selectors, <a href="http://jquery.com">jQuery</a>, and the SVG editing application <a href="http://www.inkscape.org">inkscape</a>. karma.js works fine without jQuery but this tutorial makes relies on both jQuery and the jQuery plugin jQuery SVG. If you use chromium, make sure you have web-inspector installed. If you are a Firefoxer, you will need <a href="http://getfirebug.com">Firebug</a>.</p>
<p><strong>Your Weapons, &lt;audio&gt;, &lt;video&gt;, &lt;canvas&gt;, and &lt;svg&gt;</strong></p>
<p>The success of the Karma Project is contingent upon the good browser support for HTML 5 and SVG. We have seen amazing advances in web technology over the course of the last 12 months, so Karma&#8217;s prospects look good! Using SVG for web applications is not well-established, in part because SVG is very complicated specification and in part because until recently SVG has gotten very little love from the Browser vendors. </p>
<p><strong>Twilight SVG: The Standard that Came Back from the Dead</strong></p>
<p>SVG&#8217;s stagnation changed in a big way when Google put serious development resources behind it. One of those resources is Brad Neuberg, whom they hired in spring 2009 to evangelize for SVG and other web technologies. There is now a lot of momentum behind SVG. </p>
<p>HTML 5 &lt;canvas&gt; has a nice, simple API and it is fast. Fellow uber-nerds should be excited by the nascent WebGL specification and upcoming GPU acceleration. SVG is quite slow compared to canvas but that shouldn&#8217;t be an argument against it. Canvas is faster because it doesn&#8217;t have to save its own state. For a number of applications like this tutorial, it would take you much longer to write the same program using straight canvas and would be much harder to maintain. Further, the speed benefits may be negligible in the scope of the larger application. to roughly paraphrase Brad Neuberg, when your graphics need to maintain their state, use SVG. When your graphics don&#8217;t need to maintain their own state and are doing performance-sensitive operations, use canvas.</p>
<p>I particularly like the workflows that SVG enables. With SVG, you can first create your images in inkscape, then embed them in your document, then manipulate the image with your code. With canvas, you have to manually write the code to draw the image entirely with javascript code and then frequently redraw large portions of it.</p>
<p>Let&#8217;s walk through drawing a circle and then moving it horizontally across the screen using &lt;canvas&gt; and then SVG</p>
<p><strong>&lt;canvas&gt; Example</strong></p>
<p><a href="http://karma.sugarlabs.org/karma-demos/canvas-example.html">View the Example</a></p>
<p>1. Create the element in your html, make sure you set the width and height in your markup. Your image will become incredibly distorted if you try to set the dimensions using css. I can&#8217;t remember exactly why. </p>
<pre class="brush: plain;">
&lt;canvas id=&quot;myCanvas&quot; width=&quot;800px&quot; height=&quot;600px&quot;&gt;&lt;/canvas&gt;
</pre>
<p>2. Tell Karma about it  </p>
<pre class="brush: plain;">
var k = Karma({
                            canvas : [
                                       { name : &quot;myCircle&quot;, domId: &quot;myCircle&quot; }
                                     ]
                          });
</pre>
<p>3. </p>
<pre class="brush: plain;">
            k.canvas.myCircle.fillStyle('#000000').beginPath()
	    .arc(100,100,50,0,Math.PI*2,true).closePath().fill();
			var MAX_X = 600;
			var myX = 30;		

	    var timerId = setInterval(function() {
               if (myX &lt; MAX_X){
		   k.canvas.myCircle.clear();
		   myX = myX + 20;
		   k.canvas.myCircle.fillStyle('#000000').beginPath()
		   .arc(myX,100,50,0,Math.PI*2,true).closePath().fill();
	       } else {
                   //stop the animation
                   clearInterval(timerId);
               }
               }, 100);
</pre>
<p><a href="http://karma.sugarlabs.org/karma-demos/canvas-example.html">View the Full Example</a></p>
<p>Please note that the above example uses the <a href="http://karma.sugarlabs.org/docs/index.html">Karma API</a> heavily, which is just wraps around the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">HTML 5 API</a> to save you boatloads of typing.</p>
<p><strong>SVG example</strong></p>
<p><a href="http://karma.sugarlabs.org/karma-demos/svg-example.html">View the Example</a></p>
<p>1. Draw the circle using inkscape. I precisely choose whatever color I want with having to actually understand hexadecimal color codes.<br />
<div id="attachment_326" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/drawingcircle.png"><img src="http://karmaproject.files.wordpress.com/2009/12/drawingcircle.png?w=150&#038;h=80" alt="Drawing a circle in SVG" title="drawingCircle" width="150" height="80" class="size-thumbnail wp-image-326" /></a><p class="wp-caption-text">Creating a circle in Inkscape</p></div></p>
<p>2. Give the circle element the id=&#8221;svgCircle&#8221; by right-clicking on the circle and choosing Object properties</p>
<div id="attachment_327" class="wp-caption aligncenter" style="width: 160px"><a href="http://karmaproject.files.wordpress.com/2009/12/settingid.png"><img src="http://karmaproject.files.wordpress.com/2009/12/settingid.png?w=150&#038;h=87" alt="Setting the Element Id in inkscape" title="Setting the Element Id" width="150" height="87" class="size-thumbnail wp-image-327" /></a><p class="wp-caption-text">Setting the Element Id in inkscape</p></div>
<p>3. embed in the html using the object tag</p>
<pre class="brush: plain;">
 &lt;object id=&quot;myCircle&quot; data=&quot;assets/svg/circle.svg&quot; type=&quot;image/svg+xml&quot; width=&quot;800px&quot; height=&quot;600px&quot;&gt; &lt;/object&gt;
</pre>
<p>4. Tell Karma about it </p>
<pre class="brush: plain;">
var k = Karma({
                            svg : [
                                       { name : &quot;myCircle&quot;, domId: &quot;myCircle&quot; }
                                     ]
                          });
</pre>
<p>5. Move it!  Using the translate transformation</p>
<p><code><br />
	$('#svgCircle', k.svg.myCircle.root).animate({svgTransform:'translate(400,0)'}, 5000);<br />
</code></p>
<p><a href="http://karma.sugarlabs.org/karma-demos/svg-example.html">View the Example</a></p>
<p>What I like about SVG is that it feels very &#8220;webby.&#8221; That is, I can manipulate the SVG DOM (Document Object Model) much the same way that I can manipulate the HTML DOM. I can also manipulate the presentation using the same css file I use for my HTML.</p>
<p>I must make an important caveat, SVG&#8217;s created in inkscape often do not behave in the browser as you expect them to. I suspect this is because heretofore inkscape has been primarily used to create images for non-browser contexts. That said, I believe that the inkscape team are very interested in making inkscape an integral part of the web development toolset.</p>
<p>I had the pleasure of meeting Josh Andler and Jon Cruz from the Inkscape team at last summer&#8217;s Google Summer of Code conference. Both were very enthusiastic about better integrating inkscape with the web workflow.</p>
<p><strong>SVG and CSS</strong></p>
<p>One of the great benefits of using SVG is that you can use the same css file as you do for your HTML document. To use an external css file in your SVG, you need to link it in just above the first &lt;svg&gt; tag.</p>
<pre class="brush: plain;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;
&lt;?xml-stylesheet href=&quot;../../css/lesson.css&quot; type=&quot;text/css&quot;?&gt;
&lt;svg ......
</pre>
<p>When you use the same css file for both your HTML and SVG documents, you need to make sure your element Id&#8217;s are unique across all of them. I found it helpful to prefix my svg element Id&#8217;s with &#8220;svg.&#8221; In an application with multiple svg images embedded, I may consider prefixing each SVG element Id with &#8220;svg&#8221; followed by a short identifier for that particular image, for example  &#8220;svgAlienQuestion&#8221; or &#8220;svgShipWing.&#8221;</p>
<p><strong>jQuery and SVG</strong></p>
<p>jQuery is a little Javascript library that has taken the webdev world by storm. The jQuery library itself is quite small and almost entirely focuses on DOM manipulation. In this regard it reminds me of the UNIX philosophy &#8220;Do one thing and do it well.&#8221;  <a href="http://keith-wood.name/svg.html">jQuery SVG</a> is a plugin that allows you to manipulate SVG <em>documents</em> in much the same way you use jQuery to manipulate the HTML DOM.  Pay special attention to the word &#8220;document.&#8221; Each SVG image embedded in your HTML is a separate document with its own DOM. You need to pass jQuery SVG root element for your target document so that jQuery SVG know which DOM to use.</p>
<pre class="brush: plain;">
// In this example you have 3 SVG images embedded in your HTML
// alien, ship, and map. All 3 have been passed to the Karma in the Karma({ svg : [  . . .  block

//To change the text inside the alien SVG
$('#question', k.svg.alien.root).text('Some new Text!');

//To add and event listener to the left wing of the ship
$('#leftWing', k.svg.ship.root).bind('click', function() { ....  

//hide a mountain on the map
$('#mountainEverest', k.svg.map.root).css('display', 'none');
</pre>
<p>jQuery SVG makes SVG manipulation very intuitive to those already familiar with jQuery. One drawback to jQuery SVG is that it is not a well-supported tool. Author Keith Wood has done us all a tremendous favor by writing but as yet there isn&#8217;t a larger support community around it. I myself patched the .css() method just last weekend to work on Firefox &gt; 3.5 and Chromium. I did not thoroughly test it and I highly doubt that the .css() method will work on Internet Explorer. SVG lovers, if you really want to see SVG adoption take off, I can&#8217;t recommend a better place to put your efforts than jQuery SVG.</p>
<p>You can get my patched version of jQuery SVG <a href="http://github.com/bryanwb/jQuery-SVG">here</a> and the original at <a href="http://keith-wood.name/svg.html">Keith Wood&#8217;s site</a>.</p>
<p><strong>The Vampire&#8217;s Dilemma</strong><br />
Since SVG is a Vampire Specification, only recently back from the dead, documentation and examples in the HTML context are relatively scarce. Frequently, the most complete documents are the API specifications from the W3C. It is both frustrating and bewildering to work from the W3C specs as you never quite know if X browser has implemented that spec and to what degree of fidelity.</p>
<p>I have also found a lot more browser bugs and inconsistencies using SVG as compared to &lt;canvas&gt;. One glaring bug/feature, depending on how you look at it, is that chromium will not load an SVG document if its display property is set to &#8216;none&#8217;. Firefox will load an SVG with its display set to &#8216;none&#8217;. It took me a number of hours to figure out this inconsistency as it isn&#8217;t documented anywhere that I could find. No, I haven&#8217;t filed a bug on this yet but I seriously intend to. I swear on my dead pet hamster&#8217;s grave.</p>
<p>There is also a well-known glaring gap in the SVG 1.1 API. SVG&#8217;s &lt;text&gt; element does not support line wrapping for text. You have break your text manually into &lt;textspan&gt; elements in order to keep it from overflowing bounds of your &lt;text&gt; element. Luckily, there is a clever hack to get around this problem.<br />
Thanks to <a href="http://starkravingfinkle.org/blog/2007/07/firefox-3-svg-foreignobject/">Mark Finkle</a> for this fix.</p>
<pre class="brush: plain;">
 &lt;text&gt;
 &lt;foreignObject
     id=&quot;textHack&quot;&gt;
    &lt;xhtml:body&gt;
      &lt;xhtml:div
         id=&quot;alienQuestion&quot;
         style=&quot;font-size:20px&quot;&gt;Some Old Text&lt;/xhtml:div&gt;
    &lt;/xhtml:body&gt;
  &lt;/foreignObject&gt;
  &lt;/text&gt;
</pre>
<p>To change out the text in the above block, I simply use a css selector and jQuery&#8217;s text() method:</p>
<pre class="brush: plain;">
 $('foreignObject #alienQuestion', k.svg.alien.root).text(&quot;Some New Text!&quot;);
</pre>
<p>That may look like a lot of boilerplate text but consider the alternative. Using &lt;canvas&gt; you would have have to redraw the entire canvas area in order to change text embedded in your drawing. That is a considerable waste of computational resources.</p>
<p>That&#8217;s it for this installment, stay tuned for Part III, where I will walk you through the creation of a geography lesson.</p>
<p>For further reading, I highly recommend the following resources:</p>
<ul>
<li><a href="http://docs.jquery.com/Tutorials:How_jQuery_Works">How jQuery works</a></li>
<li><a href="http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery">Getting started with jQuery</a></li>
<li>The tutorials under the Help menu within the Inkscape application. They are far more comprehensive than anything I have found online,</li>
<li>Mark Pilgrim&#8217;s freakin&#8217; awesome <a href="http://diveintohtml5.org/">Dive into HTML 5</a></li>
<li>The <a href="http://keith-wood.name/svgRef.html">reference guide</a> for jQuery SVG</li>
<li>Brad Neuberg&#8217;s awesome <a>video introduction</a> to HTML 5</li>
<li>If you&#8217;ve got the time, the comprehensive <a href="http://www.w3.org/Graphics/SVG/IG/resources/svgprimer.html">SVG Primer</a> by David Dailey</li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/karmaproject.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/karmaproject.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/karmaproject.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/karmaproject.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/karmaproject.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/karmaproject.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/karmaproject.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/karmaproject.wordpress.com/314/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/karmaproject.wordpress.com/314/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/karmaproject.wordpress.com/314/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=314&subd=karmaproject&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://karmaeducation.org/2009/12/17/karma-tutorial-part-ii-comparing-html-5-canvas-and-svg/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/21904e6423d8faa7c6d7d2c94f11271b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bryanwb</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/drawingcircle.png?w=150" medium="image">
			<media:title type="html">drawingCircle</media:title>
		</media:content>

		<media:content url="http://karmaproject.files.wordpress.com/2009/12/settingid.png?w=150" medium="image">
			<media:title type="html">Setting the Element Id</media:title>
		</media:content>
	</item>
		<item>
		<title>An Introduction to karma.js &#8211; Making HTML 5 Easy</title>
		<link>http://karmaeducation.org/2009/12/14/an-introduction-to-karma-js/</link>
		<comments>http://karmaeducation.org/2009/12/14/an-introduction-to-karma-js/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 10:04:51 +0000</pubDate>
		<dc:creator>bryanwb</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[education]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[olpc]]></category>
		<category><![CDATA[svg]]></category>

		<guid isPermaLink="false">http://karmaeducation.org/?p=296</guid>
		<description><![CDATA[While the Karma Project is a broad initiative to develop education software using openweb technologies, at its core is a relatively simple JavaScript library. The karma.js library has three primary functions
<ol>
	<li> Preload assets such as images, svgs, canvases, audio, and video and does some basic error checking on them</li>
	<li>Put those assets into <em>collections</em> where they can easily be referenced</li>
	<li>Attach helper methods to the individual references.</li>
</ol><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=296&subd=karmaproject&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>This is the first in a series tutorials about karma.js</p>
<ol>
<li><a href="http://karmaeducation.org/2009/12/14/an-introduction-to-karma-js/">This article, an Introduction to karma.js</a></li>
<li><a href="http://karmaeducation.org/2009/12/17/karma-tutorial-part-ii-comparing-html-5-canvas-and-svg/">Comparing HTML 5 Canvas and SVG</a></li>
<li><a href="http://karmaeducation.org/2009/12/17/tutorial-iii-building-a-geography-lesson/">Digging into Inkscape</a></li>
<li><a href="http://karmaeducation.org/2009/12/17/tutorial-iii-the-adventure-continues-javascript-and-svg/">JavaScript and SVG</a></li>
</ol>
<p>While the <a href="http://karma.sugarlabs.org">Karma Project</a> is a broad initiative to develop education software using openweb technologies, at its core is a relatively simple JavaScript library. The karma.js library has three primary functions</p>
<ol>
<li> Preload assets such as images, svgs, canvases, audio, and video and do some basic error checking on them</li>
<li>Put those assets into <em>collections</em> where they can easily be referenced</li>
<li>Attach helper methods to the individual references.</li>
</ol>
<p><strong>What Karma Doesn&#8217;t Yet Do</strong></p>
<ol>
<li>It doesn&#8217;t provide animation functions. The current mix of Karma lessons use a hodge-podge of <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/">HTML 5</a> canvas, <a href="http://raphaeljs.com/">Raphael.js</a>, and <a href="http://keith-wood.name/svg.html">jQuery SVG</a> to provide animation. Raphael.js is perhaps the most fully-featured and most robust of these three methods. I personally find that jQuery SVG is the least mature but the easiest to use. We may later promote a particular tool as the default for animation.</li>
<li>Doesn&#8217;t fully support internationalization. You can use the _() GNU gettext function with karma lessons but there is currently no open-source mechanism for internationalizing the strings embedded in html markup. GNU gettext only supports localization of strings in application code. The main goal for version 0.3 of karma.js will be support for localizing inline text.</li>
<li>It doesn&#8217;t support video yet</lI>
</ol>
<p><strong>What Karma isn&#8217;t</strong></p>
<p>Karma&#8217;s name is not a religious reference but to the first two syllables of Rabi <u>Karma</u>charya&#8217;s last name. The logo for Karma means &#8220;om&#8221; or &#8220;everything&#8221; in Sanskrit. It is in not the character for karma. I made the logo &#8220;om&#8221; after Om Prakash Yadav, the amazing graphic designer/artist/manager at OLE Nepal. I</p>
<p><strong>First Things First</strong></p>
<p>Your first step in using karma.js is to make sure you set your document type to HTML 5. karma.js will throw a big fat error if you try to use your good &#8216;ol html 4.01 or XML declaration.</p>
<pre class="brush: xml;">
&lt;!DOCTYPE html&gt;
</pre>
<p>That&#8217;s it! Who would have thought migrating to HTML 5 would be so painless?</p>
<p>Next, include the karma.js file, which you can get <a href="http://git.sugarlabs.org/projects/karma/repos/mainline/blobs/raw/fa7ff58e819211669fa26e5d5bfefa36a1c45646/js/karma.js"> here </a>. Also, you should put in a link to your application code. Let&#8217;s say for the sake of this article you put your application code in <code> js/lesson.js </code> though karma.js does not require you to use that convention.</p>
<pre class="brush: xml;">
 &lt;script type=&quot;text/javascript&quot; src=&quot;js/karma.js&quot;&gt;&lt;/script&gt;
&lt;!-- Your application code in lesson.js --&gt;
 &lt;script type=&quot;text/javascript&quot; src=&quot;js/lesson.js&quot;&gt;&lt;/script&gt;
</pre>
<p>While we are at it, let&#8217;s add in a css file called lesson.css. Again, karma doesn&#8217;t require you to use that convention. In the future, our team here in Nepal will probably put the lesson specific styles in lesson.css and styles for all the lessons we use in Nepal in nepal.css. Iran, Bolivia, Equatorial Guinea, etc. may want to do the same.</p>
<pre class="brush: xml;">
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;css/lesson.css&quot; /&gt;
</pre>
<p><strong>Initializing Karma</strong></p>
<p>karma.js only adds the value &#8220;Karma&#8221; to the global namespace. All of the Karma library lives within the Karma namespace.</p>
<p>The Karma() function initializes the Karma.karma object and its &#8220;collections&#8221; of images, audio, svg, videos, and canvases. During this initialization step karma.js creates the collections and does basic error-checking on them. For example, Karma(  ) will throw errors for invalid properties passed in to it. In essence, you tell the Karma about the assets you want it to know about and Karma will 1) start loading them and 2) throw errors if there are problems accessing those assets. In the future, the Karma() constructor will support loading different locales and a variety of other options.</p>
<pre class="brush: plain;">
//lesson.js

 var k = Karma({
	image: [
	    {name: &quot;ball&quot;,   file: &quot;ball37px.png&quot;},
	    {name: &quot;balloon&quot;, file: &quot;balloon37px.png&quot;},
	    {name: &quot;banana&quot;, file: &quot;banana37px.png&quot;},
	    {name: &quot;chilli&quot;, file: &quot;chilli.png&quot;},
	    {name: &quot;fish&quot;  , file: &quot;fish64px.png&quot;},
	    {name: &quot;flower&quot;, file: &quot;flower37px.png&quot;},
	    {name: &quot;normalChimp&quot;, file: &quot;normalChimp_120x125.png&quot;},
	    {name: &quot;happyChimp&quot;, file: &quot;happyChimp_120x125.png&quot;},
	    {name: &quot;sadChimp&quot;, file: &quot;sadChimp_120x125.png&quot;}],
	audio: [
	    {name: &quot;correct&quot;,  file: &quot;correct.ogg&quot;},
	    {name: &quot;incorrect&quot;, file: &quot;incorrect.ogg&quot;},
	    {name: &quot;trigger&quot;, file: &quot;trigger.ogg&quot;}
	],
         svg :[
                              {name:'capitals', domId: 'capitals'},
			      {name:'alien', domId: 'alien'},
			      {name:'spaceship', domId: 'spaceship'},
			      {name: 'help', domId: 'helpScreen'},
			      {name:'playAgain', domId:'playAgain'},
			      {name:'helpIcon', domId:'helpIcon'},
			      {name:'quitIcon', domId:'quitIcon'}
	],
         canvas: [
			      {name:&quot;topLt&quot;, domId:&quot;topLtCanvas&quot;},
			      {name:&quot;topRt&quot;, domId:&quot;topRtCanvas&quot;},
			      {name:&quot;bottomLt&quot;, domId:&quot;bottomLtCanvas&quot;},
			      {name:&quot;bottomMd&quot;, domId:&quot;bottomMdCanvas&quot;},
			      {name:&quot;bottomRt&quot;, domId:&quot;bottomRtCanvas&quot;},
			      {name:&quot;timer&quot;, domId:&quot;timerCanvas&quot;},
			      {name:&quot;scorebox&quot;, domId:&quot;scoreboxCanvas&quot;},
			      {name:&quot;chimp&quot;, domId:&quot;chimpCanvas&quot;}
	]
    });
</pre>
<p>I include both svg and canvases in this example but in a real application you would likely only use one or the other.</p>
<p>Notice that every asset has the &#8220;name&#8221; property.  You define the name property. It is not tied to any actual characteristic of the asset. Its purpose is to give you a short, easy identifier for the asset. DOM and file ID&#8217;s are frequently long, descriptive, and painful to type repeatedly.</p>
<p>You access the asset from each collection like so:<br />
<code><br />
k.image.ball, k.audio.correct, k.svg.alien<br />
</code></p>
<p>You can also iterate through the collections using jQuery&#8217;s $.each function. You cannot use JavaScript&#8217;s map, forEach, and filter methods because each collection is an object, not an array.</p>
<pre class="brush: plain;">
$.each(k.audio, function(audio) { audio.play();});
</pre>
<p>The Karma() function attaches helper functions specific to each asset type. The helper functions support function chaining. Here are a few examples. For details please see the <a href="http://karma.sugarlabs.org/docs/index.html">API documentation</a>. I have not yet done a good job of documenting the helper methods for k.audio or k.canvas</p>
<pre class="brush: plain;">
k.audio.correct.stop().play();
k.svg.capitals.getElementById('capitalMontevideo').setProperty('display', 'none');
k.canvas.scorebox.clear().drawImage(k.image.monkey.media, x, y).strokeStyle('#000000').fillStroke();
</pre>
<p>As you might have guessed, this can save a lot of typing. I intend to rapidly add helper functions as I work more with karma.js</p>
<p>I have added helper functions for almost the entire <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html">HTML 5 canvas API</a> but not yet documented it.</p>
<p><strong>How does Karma locate your assets? </strong></p>
<p>Well the location of some of these assets will be specified inside your html but others may not be, particularly audio and video. karma.js <strong>requires</strong> you to put your assets in the following directory structure:</p>
<pre>
 assets/
           audio/
           image/
           svg/
           video/
</pre>
<p>If your image <em>chilli.png</em> is in pictures/ instead of assets/image/ , Karma will throw an error. However, this is only true for the assets you tell Karma about in the Karma() constructor. You can locate images that are not passed in to Karma anywhere that you want</p>
<p><strong>Get Ready</strong></p>
<p>Before you proceed with your application, you need to wrap your application code with Karma.ready() much like you would with jQuery&#8217;s $(document).ready(function () { &#8230;});</p>
<pre class="brush: plain;">
// lesson.js
Karma.ready( function () {
      // your code here
});
</pre>
<p>Karma.ready blocks your program from running until all the assets you passed to the Karma() constructor are fully loaded.</p>
<p>If you are using jQuery, simply wrap your the Karma.ready() within your $(document).ready</p>
<pre class="brush: plain;">
// lesson.js
$(document).ready(function () {
    var k = Karma ( { /* your assets */ });
    k.ready( function () {
         // your code here
     });
});
</pre>
<p>So that is karma.js in a nutshell. Very soon I hope to have a extended tutorial that walks you through an example lesson. If you are curious about the internals of karma.js please <a href="git.sugarlabs.org/projects/karma">checkout the code</a>, <a href="http://karma.sugarlabs.org/tests">run the tests</a>, and play with the <a href="http://karma.sugarlabs.org/">demos</a>. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/karmaproject.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/karmaproject.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/karmaproject.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/karmaproject.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/karmaproject.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/karmaproject.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/karmaproject.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/karmaproject.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/karmaproject.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/karmaproject.wordpress.com/296/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=karmaeducation.org&blog=7404495&post=296&subd=karmaproject&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://karmaeducation.org/2009/12/14/an-introduction-to-karma-js/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/21904e6423d8faa7c6d7d2c94f11271b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bryanwb</media:title>
		</media:content>
	</item>
	</channel>
</rss>