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

<channel>
	<title>Tools of Change for Publishing &#187; Sanders Kleinfeld</title>
	<atom:link href="http://toc.oreilly.com/sandersk/feed" rel="self" type="application/rss+xml" />
	<link>http://toc.oreilly.com</link>
	<description>Insight, Events, Resources - O&#039;Reilly Media</description>
	<lastBuildDate>Thu, 02 May 2013 20:17:51 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>O&#8217;Reilly&#8217;s journey to EPUB 3</title>
		<link>http://toc.oreilly.com/2013/02/oreillys-journey-to-epub-3.html</link>
		<comments>http://toc.oreilly.com/2013/02/oreillys-journey-to-epub-3.html#comments</comments>
		<pubDate>Thu, 07 Feb 2013 18:11:17 +0000</pubDate>
		<dc:creator>Sanders Kleinfeld</dc:creator>
				<category><![CDATA[Publishing]]></category>
		<category><![CDATA[accessibility]]></category>
		<category><![CDATA[Azardi]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[docbook]]></category>
		<category><![CDATA[EPUB]]></category>
		<category><![CDATA[epub3]]></category>
		<category><![CDATA[Epubcheck 3.0]]></category>
		<category><![CDATA[ereader]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[iBooks 3.0]]></category>
		<category><![CDATA[idpf]]></category>
		<category><![CDATA[Nook]]></category>
		<category><![CDATA[Readium]]></category>
		<category><![CDATA[semantics]]></category>
		<category><![CDATA[sony reader]]></category>
		<category><![CDATA[XSL]]></category>

		<guid isPermaLink="false">http://toc.oreilly.com/?p=61342</guid>
		<description><![CDATA[We at O&#8217;Reilly are very pleased to announce that we have officially upgraded to EPUB 3, and ebook bundles purchased from oreilly.com will now include EPUB 3 files, in addition to Mobi and PDF files. All O&#8217;Reilly ebooks released in &#8230; ]]></description>
				<content:encoded><![CDATA[<p>We at O&#8217;Reilly are very pleased to announce that we have officially upgraded to EPUB 3, and ebook bundles purchased from <a href="http://shop.oreilly.com/category/ebooks.do">oreilly.com</a> will now include EPUB 3 files, in addition to Mobi and PDF files. All O&#8217;Reilly ebooks released in 2013 are now available in EPUB 3 format, and in the coming weeks, we will be updating and rereleasing our backlist ebooks in EPUB 3 as well.</p>
<p>But while we&#8217;re excited to share this news, this article is not merely a press release. The decision of when and how to upgrade to EPUB 3 has been challenging for many in the publishing community, and it has been a long journey for O&#8217;Reilly as well. I&#8217;d like to talk more about why we chose to take this step now, what additional value we believe EPUB 3 provides to our customers, and the challenges and tradeoffs we&#8217;ve tackled in making our EPUBs backward compatible with EPUB 2 platforms.</p>
<p><span id="more-61342"></span></p>
<h2>EPUB 3: Why Now?</h2>
<p><a href="https://en.oreilly.com/toc2013/public/regwith/comm15?intcmp=il-toc-tc13-flipboard-ibookstore-missing-piece"><img class="alignleft" style="border: none; padding-right: 10px;" alt="" src="http://cdn.oreilly.com/radar/images/promos/toc-2013-148x178.gif" width="148" height="178" /></a>It&#8217;s been more than a year since the <a href="http://idpf.org/epub/30">EPUB 3 specification</a> was first approved by the <a href="http://idpf.org/">IDPF</a> (it was released in October of 2011). Which begs the question, &#8220;Why the one-year delay to adopt EPUB 3?&#8221; or less politely, &#8220;What took you so long?&#8221; The answer is that upgrading to EPUB 3 is not a trivial undertaking, nor is it one that can be reasonably taken unilaterally. To successfully produce and deliver EPUB 3 as part of a ebook program, there are two key prerequisites: having the necessary workflows and tools in place to create EPUB files compliant with the EPUB 3.0 specification, and having ereader platforms available that formally support the 3.0 format. As of early 2012, neither of these preconditions was met, but in the past year, there has been much progress on both fronts. Here are some key milestones:</p>
<ul>
<li>December 2011: <a href="http://azardi.infogridpacific.com/">Azardi</a> launches one of the first desktop readers for EPUB 3</li>
<li>February 2012: Launch of <a href="http://readium.org/">Readium</a> project, an open source EPUB 3 reader for Google Chrome browser</li>
<li>October 2012: Apple releases <a href="http://www.apple.com/apps/ibooks/">iBooks 3.0</a>, with formal support for EPUB 3 and accompanying documentation</li>
<li>December 2012: After beta updates throughout the year, <a href="https://code.google.com/p/epubcheck/downloads/detail?name=epubcheck-3.0.zip">Epubcheck 3.0</a> (validator for EPUB 3 content) is officially released</li>
</ul>
<p>Over the past year and a half, O&#8217;Reilly has sponsored the <a href="http://docbook.sourceforge.net/">DocBook project&#8217;s</a> development of open source <a href="http://docbook.sourceforge.net/release/xsl/current/epub3/">XSL stylesheets for transforming DocBook XML content to EPUB 3</a>, which we&#8217;ve used to update our own toolchain to produce EPUB 3 output. With the release of iBooks 3.0 in late 2012, a critical mass of O&#8217;Reilly&#8217;s readers had devices that supported EPUB 3 content. We felt it was time to upgrade our content to EPUB 3 to provide people using 3.0-compliant platforms the best quality reading experience.</p>
<p>Additionally, we believed it was important to further throw our support behind the latest version of the EPUB standard to encourage vendors to upgrade their ereading platforms to support HTML5 and EPUB 3. Since 2011, there has been a chicken-or-egg attitude that&#8217;s pervaded much of the hand-wringing around EPUB 3, where publishers felt justified in holding back from producing EPUB 3 content until there was widespread ereader support, and ereader vendors felt no sense of urgency in adding EPUB 3 support to their products because there was no significant influx of EPUB 3 content from publishers. We&#8217;d like to think that by releasing our content in EPUB 3, we&#8217;re doing our part to help break this impasse and push the industry forward.</p>
<h2>A Look Inside Our New and Improved EPUBs</h2>
<p>Under the hood, EPUB 3 ebooks use HTML5 documents to represent the book content, whereas EPUB 2 documents use XHTML 1.1 documents, but this change in HTML version doesn&#8217;t inherently translate into a visually perceptible difference in how content is presented onscreen; that&#8217;s what CSS is for. The value in the upgrade to HTML5 lies both in the rich semantics it affords for marking up content and the multimedia/interactivity features it enables in ebook content.</p>
<h3>Rich semantics in HTML5 and EPUB 3</h3>
<p>The HTML5 specification adds a handful of new elements that are especially useful for marking up ebook content in a more semantic fashion. Notable new structural elements include <a href="https://developer.mozilla.org/en-US/docs/HTML/Element/section"><code>&lt;section&gt;</code></a>, <a href="https://developer.mozilla.org/en-US/docs/HTML/Element/aside"><code>&lt;aside&gt;</code></a>, and <a href="https://developer.mozilla.org/en-US/docs/HTML/Element/figure"><code>&lt;figure&gt;</code></a>, which can be used to tag sections, sidebars/footnotes/endnotes, and photos/illustrations, respectively. This helps decrease the need to rely heavily on <a href="https://developer.mozilla.org/en-US/docs/HTML/Element/div"><code>&lt;div&gt;</code></a>s with custom classes to block off content for styling. In addition to the new HTML5 structure elements, EPUB 3 introduces the <a href="http://idpf.org/accessibility/guidelines/content/semantics/epub-type.php"><code>epub:type</code></a> attribute, which permits further inflection of HTML5 elements in line with book semantics:</p>
<pre>&lt;aside epub:type="sidebar"&gt;
&lt;p&gt;Fascinating digression on the history of the Web…&lt;/p&gt;
&lt;/aside&gt;</pre>
<p><code>epub:type</code> accepts a set of values defined in the <a href="http://idpf.org/epub/vocab/structure/">EPUB 3 Structural Semantics Vocabulary</a> (and can also accept custom values from other vocabularies through custom prefixes).</p>
<p>Using semantic HTML5 and epub:type inflections is more than just a best-practices exercise or a way to flex your geek cred (&#8220;I use <a href="https://developer.mozilla.org/en-US/docs/HTML/Element/figcaption"><code>&lt;figcaption&gt;</code></a> and <a href="https://developer.mozilla.org/en-US/docs/HTML/Element/hgroup"><code>&lt;hgroup&gt;</code></a> in my EPUBs; how about you?&#8221;). Rich tagging is a key component of making EPUB content as accessible as possible to people with visual disabilities or other reading impairments. Screen readers that encounter an <code>&lt;aside&gt;</code> tag can accurately convey to readers that the text within is tangential to the main book flow. But they will not be able to do the same if that same content is tagged as, say, <code>&lt;div class="rectangular_border"&gt;</code>.</p>
<p>Ereading platforms are already taking advantage of EPUB 3 rich semantics to add features to their software for readers. As an example, Apple&#8217;s iBooks reader now keys special functionality to footnote markers inflected with <code>epub:type="noteref"</code> and corresponding footnote text with <code>epub:type="footnote"</code>, and transforms them into pop-up footnotes for the reader. O&#8217;Reilly has implemented these semantics in its EPUB 3 files; see the screenshot below for an example from <a href="http://shop.oreilly.com/product/0636920024897.do">EPUB 3 Best Practices</a>:</p>
<div id="attachment_61384" class="wp-caption alignnone" style="width: 650px"><a href="http://s.radar.oreilly.com/wp-files/4/2013/02/footnote.png"><img class="size-large wp-image-61384" alt="Pop-up footnote in iBooks Reader for iPad" src="http://s.radar.oreilly.com/wp-files/4/2013/02/footnote-768x1024.png" width="640" height="853" /></a><p class="wp-caption-text">Pop-up footnote in iBooks Reader for iPad</p></div>
<h3>Multimedia/Interactivity in HTML5 and EPUB 3</h3>
<p>Because EPUB 3 supports the full HTML5 element set, it&#8217;s now possible to create fully valid EPUB content that includes <a href="https://developer.mozilla.org/en-US/docs/HTML/Element/audio"><code>&lt;audio&gt;</code></a>, <a href="https://developer.mozilla.org/en-US/docs/HTML/Element/video"><code>&lt;video&gt;</code></a>, and <a href="https://developer.mozilla.org/en-US/docs/HTML/Element/canvas"><code>&lt;canvas&gt;</code></a> content, which opens the door to true interactive, multimedia experiences within ebook content. We&#8217;ve already begun to experiment with these possibilities in EPUB 3. Here&#8217;s a screenshot from <a href="http://shop.oreilly.com/product/0636920022473.do">HTML5 for Publishers</a>, featuring a coloring book implemented using SVG and JavaScript:</p>
<div id="attachment_61386" class="wp-caption alignnone" style="width: 650px"><a href="http://s.radar.oreilly.com/wp-files/4/2013/02/svg_coloring_book.png"><img class="size-large wp-image-61386" alt="coloring book image of a cat" src="http://s.radar.oreilly.com/wp-files/4/2013/02/svg_coloring_book-768x1024.png" width="640" height="853" /></a><p class="wp-caption-text">Interactive SVG+JavaScript coloring book from HTML5 for Publishers</p></div>
<h2>Backward Compatibility with EPUB 2 ereaders</h2>
<p>While the iBooks reader fully supports EPUB 3 content, the same cannot be said most of the major EPUB ereaders on the market, and this poses a challenge for publishers. How do you deliver an EPUB 3 experience for iBooks, but also make your content available on EPUB 2 devices like the NOOK or Sony Reader? One option is to produce two versions of your EPUB content: an EPUB 3 version for devices that can support that spec, and an EPUB 2 version for all other platforms. Another choice is to make backward-compatible EPUB 3 files, which are compliant with the 3.0 spec but also meet EPUB 2 requirements and contain the necessary legacy EPUB 2 metadata to be processed and rendered properly on EPUB 2 platforms.</p>
<p>For the ebook bundles we sell on oreilly.com, we have taken the latter approach and are releasing one, backward-compatible EPUB 3 file in our ebook bundles for download,<a id="ftn_marker" href="#ftn"><sup>*</sup></a> rather than offering both an EPUB 2 and an EPUB 3. We believe there&#8217;s real value in providing a &#8220;universal&#8221; file that can be used on all EPUB ereaders, both in terms of keeping things simple for customers (who shouldn&#8217;t have to concern themselves with whether their ereader is EPUB 3-compliant or not) and eliminating the hassle inherent in juggling two sets of EPUB files should folks want to load their ebooks to both their iPad and NOOK.</p>
<p>Developing &#8220;universal&#8221; EPUB 3 files is a more challenging undertaking for publishers, however. If you love working with EPUB metadata, you&#8217;re in luck: producing backward-compatible EPUB 3 basically means double the metadata and double the fun. Because the EPUB 3 standard modifies the spec requirements for some key metadata points, you&#8217;ll need to provide this metadata twice: once in EPUB 2 format and once in EPUB 3 format. For example, the EPUB 3 spec requires the metadata indicating the ebook cover image to be specified in the OPF file by putting <a href="http://idpf.org/epub/30/spec/epub30-publications.html#sec-item-property-values"><code>properties="cover-image"</code></a> on the corresponding manifest <code>&lt;item&gt;</code> like this:</p>
<pre>&lt;item id="cover-image" properties="cover-image" href="orm_front_cover.jpg" media-type="image/jpeg"/&gt;</pre>
<p>But many EPUB 2 ereaders are expecting the cover image to be specified as follows:</p>
<pre>&lt;meta name="cover" content="cover-image"/&gt;</pre>
<p>So to ensure that cover thumbnails appear properly on all virtual bookshelves across reading systems, you&#8217;ll need to include this metadata both ways.</p>
<p>If you were excited about upgrading to EPUB 3 but were heartbroken by the idea of abandoning the <a href="http://idpf.org/epub/20/spec/OPF_2.0.1_draft.htm#Section2.4.1">NCX Table of Contents</a>, you&#8217;re also in luck, because you&#8217;ll need to continue including an NCX file in order to ensure the table of contents renders on EPUB 2 platforms. Since the EPUB 3 specification mandates a <a href="http://idpf.org/epub/30/spec/epub30-contentdocs.html#sec-xhtml-nav">Navigation Document</a> including a table of contents tagged in XHTML5 (using <a href="https://developer.mozilla.org/en-US/docs/HTML/Element/nav"><code>&lt;nav&gt;</code></a> and <a href="https://developer.mozilla.org/en-US/docs/HTML/Element/ol"><code>&lt;ol&gt;</code></a>/<a href="https://developer.mozilla.org/en-US/docs/HTML/Element/li"><code>&lt;li&gt;</code></a> markup), that means you need two separate TOCs in your backward-compatible EPUB 3s.</p>
<p>While embedding this extra legacy EPUB 2 data will ensure that your metadata problems are solved, it won&#8217;t address a thornier problem, which is that most EPUB 2 ereaders don&#8217;t parse HTML5-specific markup. In other words, if you put a <code>&lt;section&gt;</code> tag in your EPUB file, ereaders like Adobe Digital Editions will just ignore it. This isn&#8217;t a catastrophic problem for HTML5 semantic tags like <code>&lt;section&gt;</code> and <code>&lt;aside&gt;</code>, as they don&#8217;t carry implicit formatting expectations in the same way that an <code>&lt;ol&gt;</code> does. However, it will introduce problems if you&#8217;re implementing cross-references to these HTML5 elements. For example, if you have a <code>&lt;section&gt;</code> as follows:</p>
<pre>&lt;section id="important_instructions"&gt;
&lt;h2&gt;Very Important Instructions&lt;/h2&gt;
&lt;-- And so on... --&gt;</pre>
<p>which you then intend to cross-reference later in your ebook like this:</p>
<pre>&lt;a href="#important_instructions"&gt;Click here to see Very Important Instructions&lt;/a&gt;</pre>
<p>then you&#8217;re in trouble on many EPUB 2 readers, because when they fail to parse <code>&lt;section&gt;</code>, they also fail to parse the <code>id</code> attribute on the tag, which means hyperlinks to it will not work.</p>
<p>To work around this limitation, you may find yourself having to modify the placement of your id attributes (e.g., putting them on the <code>&lt;h2&gt;</code> instead of the <code>&lt;section&gt;</code>) or compromise your semantic tagging (e.g., using <code>&lt;div&gt;</code> instead of <code>&lt;section&gt;</code>), both of which we have done in different circumstances, depending on the results of testing on different EPUB 3 and EPUB 2 readers.</p>
<p>Dealing with multimedia elements like <code>&lt;video&gt;</code> is another area that presents both challenges and tradeoffs. If you&#8217;re adding a <code>&lt;video&gt;</code> tag to your markup, the desired behavior is for a corresponding video to be embedded that the reader can watch. But for ereaders that don&#8217;t support <code>&lt;video&gt;</code>, you&#8217;ll need to provide adequate fallbacks to mitigate the degradation of the experience as much as possible. This may mean providing a poster image in place of the video, or a hyperlink to the same video content on the Web. Here&#8217;s an example of a <code>&lt;video&gt;</code> tag that falls back to a hyperlink:</p>
<pre>&lt;video id="asteroids_video_mp4" width="480" height="270" controls="controls"&gt;
&lt;source src="examples/html5_asteroids.mp4" type="video/mp4"/&gt;
&lt;p&gt;&lt;em style="color: red"&gt;Sorry, the &amp;lt;video&amp;gt; element is not 
supported in your ereader, so you will not be able to watch this video here.&lt;/em&gt; 
If you have Web access, &lt;a href="http://examples.oreilly.com/0636920022473/video/video.html"&gt;
click here&lt;/a&gt; to try running it in your browser.&lt;/p&gt;
&lt;/video&gt;</pre>
<p>And here&#8217;s a side-by-side comparison of how this video element renders on the iBooks reader (which supports embedded <code>&lt;video&gt;</code>), and Adobe Digital Editions 1.8, which does not.</p>
<div id="attachment_61388" class="wp-caption alignnone" style="width: 650px"><a href="http://s.radar.oreilly.com/wp-files/4/2013/02/embedded_video_epub.png"><img class="size-large wp-image-61388" alt="Embedded video of Asteroids game being displayed in iBooks reader on the left, but fallback URL displayed in Adobe Digital Editions on right" src="http://s.radar.oreilly.com/wp-files/4/2013/02/embedded_video_epub-1024x633.png" width="640" height="395" /></a><p class="wp-caption-text">Embedded displayed in the iBooks reader for iPad (left), and video fallback displayed on Adobe Digital Editions (right)</p></div>
<h2>Conclusion</h2>
<p>In the past year, we&#8217;ve seen much progress in the ebook landscape toward greater adoption of EPUB 3. Yet even so, in 2013 we&#8217;re still in the incipient stages of EPUB 3 support, which makes early adoption of the standard a challenge. We hope that the upcoming year will see more widespread adoption of the EPUB 3 standard by publishers and ereader vendors.</p>
<p><a id="ftn" href="#ftn_marker"><sup>*</sup></a> For now, O&#8217;Reilly is continuing to deliver EPUB 2 files to other digital retailers, as many ebook stores will not accept EPUB 3 files for sale, even if these files are backward compatible with EPUB 2 readers.</p>
<p><strong>UPDATE</strong>: On a related note, be sure to read <a href="http://toc.oreilly.com/2013/02/and-along-with-epub-3-new-css.html">this follow-up article</a> from my colleague Nellie McKesson on how we&#8217;re working to simplify and eliminate competing visual distractions for readers.</p>
]]></content:encoded>
			<wfw:commentRss>http://toc.oreilly.com/2013/02/oreillys-journey-to-epub-3.html/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>A Kindle developer&#8217;s 2013 wishlist</title>
		<link>http://toc.oreilly.com/2012/12/a-kindle-developers-2013-wishlist.html</link>
		<comments>http://toc.oreilly.com/2012/12/a-kindle-developers-2013-wishlist.html#comments</comments>
		<pubDate>Thu, 06 Dec 2012 18:30:12 +0000</pubDate>
		<dc:creator>Sanders Kleinfeld</dc:creator>
				<category><![CDATA[Publishing]]></category>
		<category><![CDATA[@amzn-kf8]]></category>
		<category><![CDATA[@amzn-mobi]]></category>
		<category><![CDATA[Adam Hyde]]></category>
		<category><![CDATA[amazon]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[KF8]]></category>
		<category><![CDATA[Kindle]]></category>
		<category><![CDATA[Kindle Paperwhite]]></category>
		<category><![CDATA[Kindle Previewer]]></category>
		<category><![CDATA[MathJax]]></category>
		<category><![CDATA[MathML]]></category>
		<category><![CDATA[media]]></category>
		<category><![CDATA[mobiunpack]]></category>
		<category><![CDATA[monospace font]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://toc.oreilly.com/?p=59776</guid>
		<description><![CDATA[2012 was a good year for Kindle developers. With the unveiling of the first-generation Fire tablet in late 2011 and the release of the KF8 Mobi format in early 2012, designing beautiful ebooks for the Kindle platform became a reality. &#8230; ]]></description>
				<content:encoded><![CDATA[<p>2012 was a good year for Kindle developers. With the unveiling of the first-generation Fire tablet in late 2011 and the release of the <a href="http://www.amazon.com/gp/feature.html?ie=UTF8&amp;docId=1000729511">KF8 Mobi format</a> in early 2012, designing <em>beautiful</em> ebooks for the Kindle platform became a reality. KF8 introduced a fixed-layout specification for Kindle Fire, which opened the door to graphically rich titles—children&#8217;s books, graphic novels—in Mobi format. KF8 also greatly increased CSS2 compliance for standard reflowable ebooks, implemented a handful of CSS3 features (text shadow, rounded borders), and added support for embedded fonts. The subsequent rollout of KF8 to Kindle eInk readers running firmware 3.4 (including the new Kindle Paperwhite) and KF8&#8242;s support for <a href="http://www.w3.org/TR/css3-mediaqueries/">@media queries</a> to enable fallback styling for non-KF8 devices helped to increase rendering parity within the diverse Kindle ecosystem.</p>
<p><span id="more-59776"></span></p>
<p>But while 2012 marks a huge leap forward toward the incorporation of modern Web standards into the Kindle platform, there is still much room for improvement in terms of multimedia/interactivity, content rendering, and ease of ebook development. Here is my humble<a href="#footnote" id="footnotemarker"><strong><sup><span style="font-size: larger">***</span></sup></strong></a> wish list of improvements for the Kindle platform for 2013:</p>
<h2>1. Add support for embedded audio/video to Kindle Fire</h2>
<p>When KF8 was introduced in early 2012, support for audio/video was not included in the format—even though MP3 audio and MP4 video were already supported in Kindle for iOS. Nearly 12 months later, the <a href="http://kindlegen.s3.amazonaws.com/AmazonKindlePublishingGuidelines.pdf"><em>Kindle Publisher Guidelines</em></a> still read, “Currently, only Kindle for IOS [sic] supports audio and/or video content. Kindle e Ink devices and Kindle Fire do not support Kindle Editions with Audio/Video.”</p>
<p>Given that support for streaming multimedia content via Amazon Instant Video is such a highly touted feature of Kindle Fire, it&#8217;s rather surprising that Amazon has not been more assiduously pursuing support for embedded multimedia for Kindle Fire ebooks. As a result of this discrepancy—Kindle Fire supports KF8 but not audio/video, and Kindle for iOS supports audio/video but not KF8—there is <strong>no single Kindle platform</strong> that supports all the ebook features that Amazon offers. Those Kindle readers who opt to buy a Fire over an iPad are penalized by not being able to view embedded video in ebooks, and those who opt to instead read their ebooks on Kindle for iOS are penalized with a lower-quality reading experience, as embedded fonts and many key CSS features will not be supported. This should be rectified ASAP. Here&#8217;s hoping that by this time next year, embedded audio/video is supported on every Kindle tablet device, and that KF8 is supported on Kindle for iOS.</p>
<h2>2. Add KF8 support for MathML</h2>
<p>High-quality typesetting of mathematical equations is a challenge in most digital formats, and Kindle is no exception. Because Kindle&#8217;s KF8 format does not support <a href="http://www.w3.org/TR/html5/mathml.html#mathml">MathML</a> (a XML vocabulary for markup of math content that is part of the HTML5 specification and supported to varying degrees in different desktop and mobile Web browsers), the only viable typesetting option for including complex equations, matrices, etc., in ebooks is to embed the math content as images. However this approach is far from ideal, because when implemented as images, mathematical equations are not searchable or resizable by readers. Optimizing image size can also be challenging. See the Kindle Paperwhite screenshot below featuring two equation images scaled to the same size, which results in the longer equation (bottom) being more heavily &#8220;shrunk&#8221; than the shorter equation (top).</p>
<div id="attachment_59790" class="wp-caption alignnone" style="width: 236px"><a href="http://s.radar.oreilly.com/wp-files/4/2012/12/equation_images_kindle_paperwhite.png"><img class="size-medium wp-image-59790" src="http://s.radar.oreilly.com/wp-files/4/2012/12/equation_images_kindle_paperwhite-226x300.png" alt="Two probability equation images rendered on Kindle Paperwhite, the bottom equation &quot;shrunk&quot; more than the top equation" width="226" height="300" /></a><p class="wp-caption-text">Two probability equation images rendered on Kindle Paperwhite, the bottom equation &#8220;shrunk&#8221; more than the top equation</p></div>
<p>Adding MathML support to KF8 would remove the burden for equation sizing and resolution from publishers and developers, and place it on the ereader&#8217;s rendering engine, where it belongs. As Adam Hyde notes in his <a href="http://toc.oreilly.com/2012/11/math-typesetting.html">TOC post on math typesetting</a>, the current state of MathML support in browsers and on the Web is rather woeful, forcing reliance on third-party libraries like <a href="http://www.mathjax.org/">MathJAX</a> to correct and normalize rendering. But this presents a huge opportunity for ereader vendors like Amazon. Adding robust MathML support may provide a competitive advantage in the likely-to-grow ebook marketplace for math and science textbooks. iBooks already provides limited MathML support via its WebKit engine; your move Amazon!</p>
<h2>3. Add a Monospace Default Font to Kindle Paperwhite</h2>
<p>The Kindle Paperwhite ereader contains <a href="http://wiki.mobileread.com/wiki/List_of_fonts_included_with_each_device#Kindle_Paperwhite">six system fonts</a>: Baskerville, Caecilia, Caecilia Condensed, Futura, Helvetica, and Palatino. None of these is a <a href="http://en.wikipedia.org/wiki/Monospaced_font">monospace font</a>. Monospace fonts are critically important for technical-book publishers, because elements such as code listings and <a href="http://en.wikipedia.org/wiki/ASCII_art">ASCII art</a> must be formatted such that every character is of equal width, in order for them to render properly.</p>
<p>Because Paperwhite supports the KF8 format, Mobi developers can embed their own monospace font if their content requires it (O&#8217;Reilly embeds the <a href="http://font.ubuntu.com/">Ubuntu Mono</a> font family in its Mobis). However, by default, Kindle Paperwhite has Publisher fonts turned off, so readers must navigate to the Kindle font menu themselves and enable the Publisher Font option—which they may not know they need to do. Compare the rendering of the code block below on Kindle Paperwhite with Publisher Fonts turned on (left) and Publisher Fonts turned off (right):</p>
<div id="attachment_59921" class="wp-caption alignnone" style="width: 650px"><a href="http://s.radar.oreilly.com/wp-files/4/2012/12/code_listing_kindle_paperwhite_publisher_fonts.png"><img class="size-large wp-image-59921" src="http://s.radar.oreilly.com/wp-files/4/2012/12/code_listing_kindle_paperwhite_publisher_fonts-1024x679.png" alt="The same code listing displayed on Kindle Paperwhite, with Publisher Fonts turned off (left) and Publisher Fonts turned on (right)" width="640" height="424" /></a><p class="wp-caption-text">The same code listing displayed on Kindle Paperwhite, with Publisher Fonts turned on (left) and Publisher Fonts turned off (right)</p></div>
<p>Kindle customers shouldn&#8217;t have to be educated to enable Publisher Fonts to ensure monospace content is displayed properly, and publishers shouldn&#8217;t be required to embed additional fonts (which bloat Mobi file size) to enable monospace functionality. Ereader vendors should provide a rich set of system fonts, optimized for their rendering engine, that meet the needs of publishers of fiction, technical reference books, or anything in between. Both legacy eInk Kindle devices and the Kindle Fire have monospace system fonts; please extend this support to Paperwhite as well.</p>
<h2>4. Add more granularity to @media query support</h2>
<p>One of the best CSS features added to KF8 was support for two specific @media queries: @amzn-kf8 and @amzn-mobi. These two queries allow Kindle developers to segregate their CSS so that styling that takes advantage of KF8 features (in an @amzn-kf8 block) is ignored on legacy Mobi7 Kindle ereaders, and fallback styling (in an @amzn-mobi block) will be used instead on the legacy devices.</p>
<p>This @media query support was a boon when Kindle Fire was the only device in the Kindle family that was KF8-enabled, because you could use @amzn-kf8 to specify not only KF8-specific features, but also four-color-specific styling for Fire tablets that would be ignored on the black-and-white devices. However, now that eInk readers like Kindle Paperwhite also support KF8, they also use the CSS specified under @amzn-kf8, which means it is not possible to target CSS for the tablet readers and provide graceful degradation for eInk. See the screenshots below, which show the same <a href="http://en.wikipedia.org/wiki/Syntax_highlighting">syntax-highlighted</a> code example side-by-side on Kindle Fire (left) and Kindle Paperwhite (right).</p>
<div id="attachment_59915" class="wp-caption alignnone" style="width: 650px"><a href="http://s.radar.oreilly.com/wp-files/4/2012/12/color_fire_paperwhite.png"><img class="size-large wp-image-59915" src="http://s.radar.oreilly.com/wp-files/4/2012/12/color_fire_paperwhite-1024x688.png" alt="Rendering of syntax-highlighted code listing on the four-color Kindle Fire tablet (left) and the eInk Kindle Paperwhite (right)" width="640" height="430" /></a><p class="wp-caption-text">Rendering of syntax-highlighted code listing on the four-color Kindle Fire tablet (left) and the eInk Kindle Paperwhite (right)</p></div>
<p>I&#8217;d love to see support added for @media queries such as @amzn-kindlefire, @amzn-kindlepaperwhite, and so on, so that Kindle developers are better able to tailor Mobi content to the unique capabilities of each ereader. (This would be an equally welcome feature for other ereader platforms that encompass both eInk and tablet devices—e.g., NOOK, Kobo.)</p>
<h2>5. Add a “View Source” option to Kindle Previewer</h2>
<p><a href="http://www.amazon.com/gp/feature.html?ie=UTF8&amp;docId=1000765261">Kindle Previewer</a> is an excellent emulator that allows you to quickly review how a Mobi file will display on all major Kindle platforms (see screenshot below).</p>
<div id="attachment_59918" class="wp-caption alignnone" style="width: 213px"><a href="http://s.radar.oreilly.com/wp-files/4/2012/12/kindle_previewer.png"><img class="size-medium wp-image-59918" src="http://s.radar.oreilly.com/wp-files/4/2012/12/kindle_previewer-203x300.png" alt="Kindle Previewer for Mac in Paperwhite Mode" width="203" height="300" /></a><p class="wp-caption-text">Kindle Previewer for Mac in Paperwhite Mode</p></div>
<p>But one thing you can&#8217;t do in Previewer is inspect the underlying HTML/CSS source code to debug any rendering oddities that may be present. There are tools available, such as <a href="http://wiki.mobileread.com/wiki/Mobi_unpack">mobiunpack</a>, which you can use to crack open a Mobi file and see the source, but they are neither especially convenient nor user-friendly. Having “View Source” functionality built into Kindle Previewer would be a huge timesaver.</p>
<p>What features are on your 2013 Kindle developer wishlist? Hit the comments and share your requests.</p>
<hr />
<h2><a href="#footnotemarker" id="footnote"><sup><span style="font-size: larger">***</span></sup></a> I&#8217;d love to have put &#8220;EPUB 3 support on Kindle&#8221; at the top of my wishlist, but sadly I don&#8217;t see that being a realistic request.</h2>
<div style="float: left;border-top: thin gray solid;border-bottom: thin gray solid;padding: 20px;margin: 20px 2px;clear: both"><a href="https://en.oreilly.com/toc2013/public/regwith/comm15?intcmp=il-toc-tc13-flipboard-ibookstore-missing-piece"><img style="float: left;border: none;padding-right: 10px" src="http://cdn.oreilly.com/radar/images/promos/toc-2013-148x178.gif" alt="" /></a><a href="https://en.oreilly.com/toc2013/public/regwith/comm15?intcmp=il-toc-tc13-flipboard-ibookstore-missing-piece"><strong>TOC NY 2013</strong></a>— The publishing industry will gather at the Tools of Change for Publishing Conference in New York City, February 12-14, to explore the forces and solutions that are transforming publishing.<a href="https://en.oreilly.com/toc2013/public/regwith/comm15?intcmp=il-toc-tc13-flipboard-ibookstore-missing-piece"><strong>Save 15% on registration with the code COMM15</strong></a></div>
]]></content:encoded>
			<wfw:commentRss>http://toc.oreilly.com/2012/12/a-kindle-developers-2013-wishlist.html/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Ask the Ebook Experts: Text alignment in Fixed-Layout EPUB for iBooks</title>
		<link>http://toc.oreilly.com/2012/09/ask-the-ebook-experts-text-alignment-in-fixed-layout-epub-for-ibooks.html</link>
		<comments>http://toc.oreilly.com/2012/09/ask-the-ebook-experts-text-alignment-in-fixed-layout-epub-for-ibooks.html#comments</comments>
		<pubDate>Thu, 06 Sep 2012 14:07:07 +0000</pubDate>
		<dc:creator>Sanders Kleinfeld</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[EPUB]]></category>
		<category><![CDATA[Fixed-Layout]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[iBooks]]></category>
		<category><![CDATA[InDesign CS6]]></category>
		<category><![CDATA[Webkit engine]]></category>

		<guid isPermaLink="false">http://toc.oreilly.com/?p=56626</guid>
		<description><![CDATA[Q: In a traditional printed book, if a paragraph has not finished when the end of the page is reached, the entire paragraph will be justified. However the [CSS] command ‘text align last’ does not seem to be honoured in &#8230; ]]></description>
				<content:encoded><![CDATA[<p><span style="float: left; font-size: 300%; line-height: 0.85em; margin-right: 0.03em; margin-bottom: -0.25em;">Q:</span> In a traditional printed book, if a paragraph has not finished when the end of the page is reached, the entire paragraph will be justified. However the [CSS] command ‘text align last’ does not seem to be honoured in the last paragraph of the page in fixed layout for the iPad…What seems to happen is that in [InDesign CS6] it ‘looks’ justified but it doesn&#8217;t make it through to the epub version and there is a small gap at the end of the line. If you add text it goes on to a new line. I tried adding whitespace but that didn&#8217;t seem to be accepted…Is the problem with ibooks? Is there any workaround?</p>
<p>—<a href="http://www.andrewrafferty.co.uk/"><em>Andrew Rafferty</em></a></p>
<div id="attachment_56718" class="wp-caption alignnone" style="width: 310px"><a href="http://s.radar.oreilly.com/wp-files/4/2012/09/bad_last_line_large.png"><img class="size-medium wp-image-56718" title="Fixed-layout paragraph with incorrectly justified last line" src="http://s.radar.oreilly.com/wp-files/4/2012/09/bad_last_line_large-300x225.png" alt="Fixed-layout paragraph with incorrectly justified last line" width="300" height="225" /></a><p class="wp-caption-text">Fixed-layout paragraph in iBooks with incorrectly justified last line<br />Image: Andrew Rafferty; <a href="http://www.andrewrafferty.co.uk/products/the-stones-remain1/"><em>The Stones Remain</em></a></p></div>
<p><span style="float: left; font-size: 300%; line-height: 0.85em; margin-right: 0.03em; margin-bottom: -0.25em;">A:</span> When you load a standard EPUB file into iBooks, the application automatically paginates the HTML content based on screen size and settings set by the user (font and font size). Content flows from page to page, and if a paragraph spans a page break, text alignment will be consistent on both pages.</p>
<p>Fixed-layout EPUBs differ from standard EPUBs in that it is the ebook designer who sets the pagination of the book, not the iBooks application. Each XHTML document in a fixed-layout EPUB file corresponds to a distinct page in the book, and no content is flowed from one page to the next.</p>
<p>If you want to mimic a text flow from page to page in a fixed-layout EPUB, you&#8217;ll need to split the text between two separate HTML documents. This poses a challenge if you want your text to be justified, because the <a href="http://www.w3schools.com/cssref/pr_text_text-align.asp"><tt>text-align: justify</tt></a> CSS property does not stretch the final line of a paragraph to the full text-column width.</p>
<p>The good news is that CSS3 offers a solution to this very problem: the <a href="http://www.w3.org/TR/css3-text/#text-align-last"><tt>text-align-last</tt> property</a>, which allows you to indicate how the final line of a text block is aligned. <tt>text-align-last: justify</tt> specifies that the final line should be fully justified, and span the full text column width.</p>
<p>The bad news about this good news is that <tt>text-align-last</tt> is not yet fully honored across all major Web browsers. It is supported in Mozilla-based browsers (Firefox), but is not supported in the <a href="http://en.wikipedia.org/wiki/WebKit">Webkit engine</a>, which powers Safari, Chrome, and—sadly—the iBooks ereader. Neither <tt>text-align-last</tt> nor the WebKit-specific <tt>-webkit-text-align-last</tt>, nor the <a href="http://idpf.org/epub/30/spec/epub30-contentdocs.html#sec-css-text">EPUB3-specific</a> <tt>-epub-text-align-last</tt> will produce the desired effect in the iBooks reader.</p>
<p>But some more good news for the intrepid and patient is there&#8217;s a hack-y HTML/CSS workaround that can achieve the effect of <tt>text-align-last: justify</tt> in iBooks (your mileage may vary on other ereader platforms).</p>
<h2>Tweak word spacing using CSS</h2>
<p>The old-school (dating all the way back to CSS1) <a href="http://www.w3schools.com/cssref/pr_text_word-spacing.asp"><tt>word-spacing</tt></a> property allows you to designate a specific amount of space to place in between words. The following example uses <tt>word-spacing: 7px</tt> to specify that the last seven words on the page should have seven pixels of whitespace between them:</p>
<pre>&lt;p&gt;Everywhere there are mysteries. And the most ancient man-made wonders
of all are the stone monuments erected by our Neolithic and Early 
bronze Age ancestors between 4000 and 1500BC - or, if it is less difficult 
to visualize in this way, between 140 and 240 generations ago. Little 
England (and smaller Scotland and Wales) are rich in these megalithic 
structures. Archaeologists tell us that more than a thousand chambered 
tombs and some 700 stone circles have resisted the 
smoothing iron of wind and rain, the teeth of the plough, the 
&lt;span style="word-spacing: 7px"&gt;grasping hands of wave 
upon wave of&lt;/span&gt;&lt;/p&gt;</pre>
<p>And here&#8217;s a screenshot illustrating how this text renders in iBooks.</p>
<div id="attachment_56720" class="wp-caption alignnone" style="width: 310px"><a href="http://s.radar.oreilly.com/wp-files/4/2012/09/fixed_last_line_large.png"><img class="size-medium wp-image-56720" title="Last line correctly justified with word-spacing property" src="http://s.radar.oreilly.com/wp-files/4/2012/09/fixed_last_line_large-300x225.png" alt="Last line correctly justified with word-spacing property" width="300" height="225" /></a><p class="wp-caption-text">Last line correctly justified in iBooks with word-spacing property<br />Image: Andrew Rafferty; <a href="http://www.andrewrafferty.co.uk/products/the-stones-remain1/"><em>The Stones Remain</em></a></p></div>
<p>The main benefit of this approach is that it gives you fine-grained control over the whitespace in a paragraph. The downside is that it can require a fair amount of trial and error to determine the proper <tt>word-spacing</tt> values to achieve the desired justification effect. If you do decide to use this method, and have a paid iTunes Connect ebooks account, I highly recommend using Apple&#8217;s Book Proofer tool, as it eliminates much of the hassle involved in syncing EPUB files between your computer and your iPad/iPhone/iPod.</p>
<div style="border: 1px dashed red; font-style: italic; padding: 10px;">O&#8217;Reilly&#8217;s Ebook Experts want to help you solve your tough digital-publishing problems. Send questions to <a href="mailto:ebook_experts@oreilly.com">ebook_experts@oreilly.com</a>, and we will publish submissions and responses on the TOC blog in future editions of &#8220;Ask the Ebook Experts&#8221;</div>
]]></content:encoded>
			<wfw:commentRss>http://toc.oreilly.com/2012/09/ask-the-ebook-experts-text-alignment-in-fixed-layout-epub-for-ibooks.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>HTML5 for publishers: Drawing on the screen</title>
		<link>http://toc.oreilly.com/2011/11/html5-for-publishers-canvas.html</link>
		<comments>http://toc.oreilly.com/2011/11/html5-for-publishers-canvas.html#comments</comments>
		<pubDate>Tue, 15 Nov 2011 14:00:00 +0000</pubDate>
		<dc:creator>Sanders Kleinfeld</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[@editpick]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[publishers]]></category>

		<guid isPermaLink="false">http://blogs.oreilly.com/radar/2011/11/html5-for-publishers-canvas.html</guid>
		<description><![CDATA[This excerpt from &#34;HTML5 for Publishers&#34; shows how a simple finger painting canvas can be added to an HTML5-based children&apos;s book ]]></description>
				<content:encoded><![CDATA[<p><em>Most publishers have at least a vague sense that HTML5 is an important content technology, but what does that content actually look like? What can it do? The following excerpt from the free ebook &#8220;<a href="http://shop.oreilly.com/product/0636920022473.do">HTML5 for Publishers</a>&#8221; shows how a simple finger painting canvas can be added to an HTML5-based book.</em></p>
<hr />
<p>Doing animations on the <a href="http://en.wikipedia.org/wiki/Canvas_element">HTML5 Canvas</a> is cool, but what&#8217;s even cooler is letting the user draw on the Canvas herself. With the advent of<br />
    touchscreen phones, tablets, and ereaders, this becomes even more<br />
    compelling, as the user can draw directly on the screen with her finger,<br />
    rather than using a mouse or trackpad. In this section, we&#8217;ll look at how<br />
    to implement a simple &#8220;finger painting&#8221; app in the Canvas, which would be<br />
    a nice fit for a children&#8217;s ebook&#8212;for example, a story that lets kids draw<br />
    their own illustrations to accompany the text, or a preschool textbook<br />
    that uses the finger painting to teach colors and shapes.</p>
<p>Here&#8217;s the HTML we&#8217;ll use to construct the Finger Painting page; the<br />
    <code>&lt;canvas&gt;</code> tag which will hold the drawing area is<br />
    highlighted in <span class="bold"><strong>bold</strong></span>:</p>
<pre>
&lt;!doctype html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;Finger Painting&lt;/title&gt;
&lt;script src="modernizr-1.6.min.js"&gt;&lt;/script&gt;
&lt;script src="finger_painting.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div&gt;
<span class="bold"><strong>&lt;canvas id="canvas" width="500" height="500"&gt;</strong></span>
 Your browser does not support the HTML 5 Canvas.
<span class="bold"><strong>&lt;/canvas&gt;</strong></span>
&lt;/div&gt;
&lt;div&gt;
&lt;h1&gt;Finger Painting&lt;/h1&gt;
&lt;p&gt;Click/tap a color below to select a color, and then drag/swipe on the
  canvas above to draw a picture.&lt;/p&gt;
&lt;p&gt;Color selected: &lt;span id="color_chosen"&gt;Black&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;input type="button" id="Red" style="background-color: red; width: 25px;
height: 25px;"/&gt;
&lt;input type="button" id="Orange" style="background-color: orange; width: 25px;
height: 25px;"/&gt;
&lt;input type="button" id="Yellow" style="background-color: yellow; width: 25px;
height: 25px;"/&gt;
&lt;input type="button" id="Green" style="background-color: green; width: 25px;
height: 25px;"/&gt;
&lt;input type="button" id="Blue" style="background-color: blue; width: 25px;
height: 25px;"/&gt;
&lt;input type="button" id="Purple" style="background-color: purple; width: 25px;
height: 25px;"/&gt;
&lt;input type="button" id="Brown" style="background-color: brown; width: 25px;
height: 25px;"/&gt;
&lt;input type="button" id="Black" style="background-color: black; width: 25px;
height: 25px;"/&gt;
&lt;input type="button" id="White" style="background-color: white; width: 25px;
height: 25px;"/&gt;
&lt;/p&gt;
&lt;p&gt;&lt;input type="button" id="reset_image" value="Reset Drawing"/&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>Note that the color palette below the Canvas has been implemented<br />
    using <code>&lt;input&gt;</code> buttons, which are styled with CSS<br />
    to be the appropriate color and size. The image below displays the page in Chrome for<br />
    Mac.</p>
<p class="image-box-580">
<a href="http://radar.oreilly.com/2011/11/11/1111-finger-painting1-lg.png"><img src="http://radar.oreilly.com/2011/11/11/1111-finger-painting1.png" border="0" width="580" alt="Finger painting interface in Google Chrome" style="margin-bottom: 15px" /></a><br />Finger painting interface in Google Chrome (<a href="http://radar.oreilly.com/2011/11/11/1111-finger-painting1-lg.png">click to enlarge</a>).</p>
<p>In order for the user to be able to draw on the screen, we&#8217;ll need<br />
    to be able to track his cursor motions and clicks within the Canvas. We<br />
    can do so by adding event listeners to the <code>&lt;canvas&gt;</code> element as follows:</p>
<pre>
theCanvas.addEventListener('mousedown', mouse_pressed_down, false);
theCanvas.addEventListener('mousemove', mouse_moved, false);
theCanvas.addEventListener('mouseup', mouse_released, false);</pre>
<p>Now when a user presses down on the mouse within the<br />
    <code>&lt;canvas&gt;</code>, a <code>mousemove</code> event is<br />
    triggered in the browser, and our event listener calls the<br />
    <code>mouse_pressed_down</code> function. Similarly, when the mouse<br />
    is moved within the dimensions of the Canvas, the<br />
    <code>mouse_moved</code> function is called, and when the mouse<br />
    button is released, the <code>mouse_released</code> function is<br />
    called. Let&#8217;s take a look at these three functions:</p>
<pre>
    function mouse_pressed_down (ev) {
    begin_drawing = true;
    context.fillStyle = colorChosen.innerHTML;
}

function mouse_moved (ev) {
    var x, y;
    <em><span class="lineannotation">// Get the mouse position in the canvas</span></em>
    x = ev.pageX;
    y = ev.pageY;

    if (begin_drawing) {
        context.beginPath();
        context.arc(x, y, 7, (Math.PI/180)*0, (Math.PI/180)*360, false);
        context.fill();
        context.closePath();
    }
}

function mouse_released (ev) {
   begin_drawing = false;
}</pre>
<p>The <code>mouse_pressed_down</code> function serves to &#8220;turn<br />
    on&#8221; a drawing event on the canvas. It sets the variable<br />
    <code>begin_drawing</code> to <code>true</code>, and then sets<br />
    the fill color to be used to the current color selected from the color<br />
    palette.</p>
<p>Then when the <code>mouse_moved</code> function is called<br />
    (which occurs any time the mouse is moved somewhere within the Canvas), we<br />
    get the cursor&#8217;s coordinates using the<br />
    <code>pageX</code>/<code>pageY</code> properties. We check if<br />
    the <code>begin_drawing</code> variable is set to<br />
    <code>true</code>, which means that the user has the mouse button<br />
    pressed down, and if so, we draw a circle of the designated color with a<br />
    radius of 7 pixels at the cursor location.</p>
<p>As long as the mouse button is held down while the mouse is moved<br />
    over the Canvas, the <code>mouse_moved</code> function will be<br />
    called every single time the cursor location changes, which means that<br />
    circles will continue to be drawn as the mouse moves, resulting in an<br />
    effect quite similar to the Paintbrush tool in many image-editing<br />
    applications.</p>
<p>When the mouse button is released, the<br />
    <code>begin_drawing</code> variable is set back to<br />
    <code>false</code>, which &#8220;turns off&#8221; the drawing event. This<br />
    ensures that drawing occurs only when the mouse is held down, and not when<br />
    the mouse is moved over the Canvas without the button being<br />
    pressed.</p>
<p>The above code works great on desktop and laptop browsers, where a<br />
    mouse is used to interface with screen elements, but what about<br />
    touchscreen devices like the iPad? In general, touchscreen browsers do not<br />
    support<br />
    <code>mousedown</code>/<code>mousemove</code>/<code>mouseup</code><br />
    events, as there is no mouse button or mouse cursor that they can track;<br />
    all those features are replaced with finger taps and swipes. However,<br />
    <a class="ulink" href="http://www.webkit.org/">WebKit</a>-based browsers support<br />
    a corresponding set of events for tracking finger motions in the browser:<br />
    <code>touchstart</code>/<code>touchend</code>/<code>touchmove</code>.<br />
    So we can implement the same drawing functionality as above using a<br />
    <code>touchmove</code> event listener:</p>
<pre>theCanvas.addEventListener('touchmove', touch_move_gesture, false);</pre>
<p>And the following <code>touch_move_gesture</code><br />
    function:</p>
<pre>function touch_move_gesture (ev) {
    <em><span class="lineannotation">// For touchscreen browsers/readers that support touchmove</span></em>
    var x, y;
    context.beginPath();
    context.fillStyle = colorChosen.innerHTML;
    if(ev.touches.length == 1){
        var touch = ev.touches[0];
        x = touch.pageX;
        y = touch.pageY;
        context.arc(x, y, 7, (Math.PI/180)*0, (Math.PI/180)*360, false);
        context.fill();
    }
}</pre>
<p>(Note: The <code>touchmove</code> handling for touchscreen devices<br />
      is actually much simpler than the mouse-based version, because we don&#8217;t<br />
      even need to track <code>touchstart</code> and<br />
      <code>touchend</code> events. When dealing with a mouse, we need<br />
      to keep track of whether the mouse button is pressed or not when it&#8217;s<br />
      being moved on the canvas. In the touch version, we know that if the<br />
      <code>touchmove</code> event has been triggered, the user has his<br />
      finger on the screen and is intending to draw.)</p>
<p>And that&#8217;s the meat of the finger painting code. All that&#8217;s left is<br />
    the code to initialize the event listeners, track color palette<br />
    selections, and implement the Reset Drawing button functionality. The example below shows the full JavaScript code for our<br />
    finger painting application. (The finger painting JavaScript code <a class="ulink" href="https://github.com/sandersk/HTML5-for-Publishers/blob/master/finger_painting/finger_painting.js">can be downloaded here</a>.)</p>
<pre>
window.addEventListener('load', eventWindowLoaded, false);
function eventWindowLoaded() {
    canvasApp();
}

function canvasSupport () {
    return Modernizr.canvas;
}


function canvasApp(){
    if (!canvasSupport()) {
        return;
    }else{
        var theCanvas = document.getElementById('canvas');
        var context = theCanvas.getContext('2d');
        var redButton = document.getElementById("Red");
        var orangeButton = document.getElementById("Orange");
        var yellowButton = document.getElementById("Yellow");
        var greenButton = document.getElementById("Green");
        var blueButton = document.getElementById("Blue");
        var purpleButton = document.getElementById("Purple");
        var brownButton = document.getElementById("Brown");
        var blackButton = document.getElementById("Black");
        var whiteButton = document.getElementById("White");
        var colorChosen = document.getElementById("color_chosen");
        var resetButton = document.getElementById("reset_image");
        redButton.addEventListener('click', colorPressed, false);
        orangeButton.addEventListener('click', colorPressed, false);
        yellowButton.addEventListener('click', colorPressed, false);
        greenButton.addEventListener('click', colorPressed, false);
        blueButton.addEventListener('click', colorPressed, false);
        purpleButton.addEventListener('click', colorPressed, false);
        brownButton.addEventListener('click', colorPressed, false);
        blackButton.addEventListener('click', colorPressed, false);
        whiteButton.addEventListener('click', colorPressed, false);
        resetButton.addEventListener('click', resetPressed, false);
        drawScreen();
    }

    function drawScreen() {
        theCanvas.addEventListener('mousedown', mouse_pressed_down, false);
        theCanvas.addEventListener('mousemove', mouse_moved, false);
        theCanvas.addEventListener('mouseup', mouse_released, false);
        theCanvas.addEventListener('touchmove', touch_move_gesture, false);
        context.fillStyle = 'white';
        context.fillRect(0, 0, theCanvas.width, theCanvas.height);
        context.strokeStyle = '#000000';
        context.strokeRect(1,  1, theCanvas.width-2, theCanvas.height-2);
    }

    // For the mouse_moved event handler.
    var begin_drawing = false;

    function mouse_pressed_down (ev) {
        begin_drawing = true;
        context.fillStyle = colorChosen.innerHTML;
    }

    function mouse_moved (ev) {
        var x, y;
        // Get the mouse position in the canvas
        x = ev.pageX;
        y = ev.pageY;

        if (begin_drawing) {
            context.beginPath();
            context.arc(x, y, 7, (Math.PI/180)*0, (Math.PI/180)*360, false);
            context.fill();
            context.closePath();
        }
    }

    function mouse_released (ev) {
        begin_drawing = false;
    }

    function touch_move_gesture (ev) {
        // For touchscreen browsers/readers that support touchmove
        var x, y;
        context.beginPath();
        context.fillStyle = colorChosen.innerHTML;
        if(ev.touches.length == 1){
            var touch = ev.touches[0];
            x = touch.pageX;
            y = touch.pageY;
            context.arc(x, y, 7, (Math.PI/180)*0, (Math.PI/180)*360, false);
            context.fill();
        }
    }

    function colorPressed(e) {
        var color_button_selected = e.target;
        var color_id = color_button_selected.getAttribute('id');
        colorChosen.innerHTML = color_id;
    }

    function resetPressed(e) {
        theCanvas.width = theCanvas.width; // Reset grid
        drawScreen();
    }
}</pre>
<p>You can experiment with the Finger Painting app <a class="ulink" href="http://examples.oreilly.com/0636920022473/finger_painting/finger_painting.html">on examples.oreilly.com</a>. The image below shows a completed drawing in the Finger Painting app in the iBooks reader for iPad.</p>
<div align="center">
<p class="image-box-300">
<img src="http://radar.oreilly.com/2011/11/11/1111-finger-painting2.png" border="0" width="300" alt="Author self-portrait in Finger Painting app in iBook" style="margin-bottom: 15px" /></a><br />Author self-portrait in Finger Painting app in iBook.</p>
</div>
<p>Pretty cool, right? Although maybe not as impressive as what you can do in some <a class="ulink" href="http://www.hongkiat.com/blog/beautiful-ipad-fingerpaint-piece-de-resistance/">other touchscreen finger painting apps</a>.</p>
<div style="float: left;border-top: thin gray solid;border-bottom: thin gray solid;padding: 20px;margin: 20px 2px;clear: both"><a href="http://shop.oreilly.com/product/0636920022473.do?cmp=il-radar-ebooks-html5-finger-painting-example"><img style="float: left;border: none;padding-right: 10px" src="http://radar.oreilly.com/2011/11/11/1111-html5-for-publishers-cover.png" /></a><a href="http://shop.oreilly.com/product/0636920022473.do?cmp=il-radar-ebooks-html5-finger-painting-example"><strong>HTML5 for Publishers</strong></a> &mdash;  This free ebook provides an overview of some of the most exciting features HTML5 provides to ebook content creators &mdash; audio/video, geolocation, and the Canvas &mdash; and shows how to put them in action.</div>
<p><strong>Related:</strong></p>
<ul>
<li> <a href="http://shop.oreilly.com/product/0636920022473.do">HTML5 for publishers</a> (free ebook)</li>
<li> <a href="http://radar.oreilly.com/2011/07/what-is-html5.html">What is HTML5?</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://toc.oreilly.com/2011/11/html5-for-publishers-canvas.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
