<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0' gd:etag='W/&quot;D0MGQn88cCp7ImA9WhJRFk8.&quot;'><id>tag:blogger.com,1999:blog-4714972168307757691</id><updated>2012-07-18T21:00:23.178+05:30</updated><title>Mitesh Mehta's Blog</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://miteshblog.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4714972168307757691/posts/default?redirect=false&amp;v=2'/><link rel='alternate' type='text/html' href='http://miteshblog.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Mitesh B. Mehta</name><uri>http://www.blogger.com/profile/09961356419680542180</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry gd:etag='W/&quot;CEQERns4cSp7ImA9WhJRFk0.&quot;'><id>tag:blogger.com,1999:blog-4714972168307757691.post-7874338629443317279</id><published>2012-07-18T14:16:00.003+05:30</published><updated>2012-07-18T14:35:07.539+05:30</updated><app:edited xmlns:app='http://www.w3.org/2007/app'>2012-07-18T14:35:07.539+05:30</app:edited><title>HTML5 - Wrap Text in Canvas</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div style="font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif; text-align: left;"&gt;
It is very important feature that should be included with draft of new features of html5 canvas but unfortunately its not included. I have developed function that makes it possible to wrap text inside the canvas.&lt;/div&gt;
&lt;div style="font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif; text-align: left;"&gt;
&lt;br /&gt;
Here is the code that will wrap the text on end of the canvas width. It is written in javascript. So to use just add it to your javascript.&lt;/div&gt;
&lt;div style="font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif; text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif; text-align: left;"&gt;
&lt;b&gt;Parameter Details:&lt;/b&gt;&lt;/div&gt;
&lt;div style="font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif; text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;ul style="font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif; text-align: left;"&gt;
&lt;li&gt;context = canvas context variable name.&lt;/li&gt;
&lt;li&gt;text = Whole text string that to be added in the canvas.&lt;/li&gt;
&lt;li&gt;x = starting x point of the text in the canvas.&lt;/li&gt;
&lt;li&gt;y = starting y point of the text in the canvas.&lt;/li&gt;
&lt;li&gt;maxWidth = canvas's width.&lt;/li&gt;
&lt;li&gt;lineHeight = Distance between two lines.&lt;/li&gt;
&lt;/ul&gt;
&lt;div style="font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif; text-align: left;"&gt;
&lt;/div&gt;
&lt;div style="font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif; text-align: left;"&gt;
function wrapText(context, text, x, y, maxWidth, lineHeight) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; var words = text.split(" ");&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; var line = "";&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for(var n = 0; n &amp;lt; words.length; n++) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; var testLine = line + words[n] + " ";&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; var metrics = context.measureText(testLine);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; var testWidth = metrics.width;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; if(testWidth &amp;gt; maxWidth) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; context.fillText(line, x, y);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; line = words[n] + " ";&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; y += lineHeight;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; else {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; line = testLine;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; context.fillText(line, x, y);&lt;br /&gt;
}&lt;/div&gt;
&lt;div style="font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif; text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif; text-align: left;"&gt;
For Ex.&amp;nbsp;&lt;/div&gt;
&lt;div style="font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif; text-align: left;"&gt;
&amp;nbsp;&amp;nbsp; var text = "HTML5 Wrap Text function in Javascript written here is helped me a lot.";&lt;br /&gt;
&amp;nbsp;&amp;nbsp; var x = 20;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; var y = 20;&lt;/div&gt;
&lt;div style="font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif; text-align: left;"&gt;
&amp;nbsp;&amp;nbsp; wrapText(context, text, x, y, (canvas.width - 8), 25) &lt;/div&gt;
&lt;div style="font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif; text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;span style="color: red;"&gt;*&lt;/span&gt; line Height 25 because mine font size is 20.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;Note : Single canvas for single paragraph&lt;/span&gt; &lt;span style="font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;is to be used&lt;/span&gt;.&lt;br /&gt;
&lt;div style="font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif; text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif; text-align: left;"&gt;
Hope this will help you a lot to all HTML5 Developer. Give your comments to improve this article.&lt;/div&gt;
&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4714972168307757691-7874338629443317279?l=miteshblog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://miteshblog.blogspot.com/feeds/7874338629443317279/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://miteshblog.blogspot.com/2012/07/html5-wrap-text-in-canvas.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4714972168307757691/posts/default/7874338629443317279?v=2'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4714972168307757691/posts/default/7874338629443317279?v=2'/><link rel='alternate' type='text/html' href='http://miteshblog.blogspot.com/2012/07/html5-wrap-text-in-canvas.html' title='HTML5 - Wrap Text in Canvas'/><author><name>Mitesh B. Mehta</name><uri>http://www.blogger.com/profile/09961356419680542180</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>