Thursday, October 20, 2011

[HTML5] Using canvas element and javascript to modify image

Demo:







HTML5 Canvas is an HTML element that its contents can be rendered with Javascript. This time we will go through how to use HTML5 canvas to add text to the top layer of image. The first thing we have to prepare is a picture. Eventually I found this picture on web and it is quite suitable for this showcase. We can put a message at the paper.


OK, here is the code.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>

    function genImage() {
        var canvas = document.getElementById("myCanvas");
        var context = canvas.getContext("2d");
       
        var bgsrc = document.createElement("img");
        bgsrc.src = "[image path]";
       
        bgsrc.onload = function(){
       
            context.drawImage(this, 0,0);
   
            context.fillStyle = '#000000';
            context.font = "bold 28px 'Lucida Grande',Helvetica,Arial,Verdana,sans-serif";
            context.fillText($("#message").val(), 160, 130);
           
            var imgsrc = canvas.toDataURL('image/jpeg');
            $("#img").attr("src",imgsrc);
        }

    }

    $(document).ready(function(){
        genImage();
    });
</script>
<canvas id='myCanvas' width='400' height='266' style="display:none;"></canvas>
<img id="img" src="https://lh4.googleusercontent.com/-6Y6TdPz2CdE/Tp-V1KgAJHI/AAAAAAAAEGA/eOvQOtgwFuY/s400/11%252520-%2525201.jpg">
<p>
<input type="text" value="Hello Hello!" maxlength="20" id="message"> <input type="button" value="Generate" onClick="genImage()">
</p>


In this example, JQuery has been chosen to simplify javascript calling. It can save lots of development time. Then, we should define the canvas area first.

<canvas id='myCanvas' width='400' height='266' style="display:none;"></canvas>
This is the code that define the canvas named myCanvas with dimension 400x266. It is the same with the picture size. The canvas is just a place for processing the image data. We will render the final image to a HTML image tag at the last step. We should give an ID to a image tag for the final image holder.

<img id="img" src="https://lh4.googleusercontent.com/-6Y6TdPz2CdE/Tp-V1KgAJHI/AAAAAAAAEGA/eOvQOtgwFuY/s400/11%252520-%2525201.jpg">
For the source image bgsrc, you can supply a relative path or base64 data of an image. But please note that URL is not a choice because it will cause security problem and you will got an javascript error.

OK, after the source image has been completely loaded, the first thing we have to draw the image again. Then, draw the text message and tune the position over the image.

            context.drawImage(this, 0,0);
   
            // write clock
            context.fillStyle = '#000000';
            context.font = "bold 28px 'Lucida Grande',Helvetica,Arial,Verdana,sans-serif";
            context.fillText($("#message").val(), 160, 130);
At the last, export the data at canvas to base64 image data, and assign it to the image tag #img. Here you go.

One more point you have to pay attention, word-wrap function is not support with in canvas area now. You have to develop your own word-wrap function for workaround.

Recently, iPhone 4S is the hottest topic around the world. I developed an application, which using canvas, to generate the conversation between Siri and me.

http://siri.we-love-programming.com

Check it out.

Book you may feel interested:

Tuesday, October 18, 2011

Siri can tell - tool created by HTML5 canvas function

iPhone4S is the hottest topic recently. The most innovative function for iPhone4S should be the app - Siri. According to Apple website, Siri lets your voice to send messages, schedule meetings, place phone calls, and more. I think lots of you guys must want to try it. Yes. I also want to try it but I can't buy iPhone4S in my region right now.



Inspired by my friend Chiuto, I have developed a tool (called Siri can tell) that can generate a screen shot of Siri with my tailor-made conversation. The technology at the back end is canvas function by HTML5. By using canvas, you can generate a image at client side on fly. It's pretty cool huh. So, please try the tool at

http://siri.we-love-programming.com

Also, feel free to give me your comment. Thanks.

If you would like to start building HTML5 canvas application too, read this:

Saturday, October 15, 2011

Generating QR code by Google Chart API


Google provide lots of API for developers to develop their own tools. Today I would like to go through a funny feature from Google Chart Tools - QR code generating.


Developer always have to come across with tons of data and make it to a presentable report for end-users. Google Chart Tools can save your a day. However, we just focus on QR code generating function today. If you interest in it, check it out at http://code.google.com/apis/chart/.


Generating QR code by Google API is just a piece of cake. You just have to append your target message to a URL. Here is an example.


I want to make a QR code with a message 'We love programming.', here we go
https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=We%20love%20programming


Actually it will be a image if you click on it. The parameter chs is widthxheight. Parameter chl is the message or URL you want to include. Please do URL encode for your message.


I have written a snippet of code to generate QR code by current URL automatically.

<div id="qrcode"></div>
<script>
var img = document.createElement('img');
img.src = 'https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=' + encodeURIComponent(document.location.href);
document.getElementById('qrcode').appendChild(img);
</script>



Here is just the core function. You can also design your own interface to make a nice QR code generator. For more details, check out the document of Google, http://code.google.com/apis/chart/infographics/docs/overview.html

You can build QR code easily with Google Chart API now, but how QR code can help on your business? Try to read this:

Tuesday, October 4, 2011

First Google+ API application - Google+ Geolocation Timeline


Google+ API has been introduced by Google at mid Sept 2011. Just use my spare time to come across with this and create an application. Hopefully, it is the first application that made with Google+ API. (Please let me know if I am wrong)

So far, Google+ API is quite handy. However, sample code may not enough for developer for reference. Besides, just only  few of methods provided now. For example, user's circle cannot be retrieved by API. Looking forward for Google to enhance this.

Since Google+ API is very new at this moment, I can just find one book only (Developing with Google+) for teaching the technique on how to use this API. But anyway, let's try my first Google+ API application.

http://www.foodogram.me/gplusmap/

This application is mapping your Google+ check-in activities in Google Map. I will upload all source code to github later on. Welcome for any comments.

Book you may feel interested:

Monday, September 26, 2011

Subscribe Google Calendar into iPhone

First of all, you have to prepare your calendar at Google Calendar first. You may create a new calendar inside your account, or export existing calendar. In this article, we create a new calendar to demonstrate the process.
I have created a  calendar named "my plan". Assume that I have input all special day to it. Then click "Calendar settings".
Click the green button "ICAL" at "Calendar Address". You can get the URL with extesion ".ics"
Write down this URL address, then what we need now is set it into your iPhone now.

Ok, tap "setting" icon at your iPhone

Choose "Mail,Contacts,Calendars".



Tap "Add Account".


Tap "Other".


Tap "Add Subscribed Calendar"


Add the URL you wrote down at the top and input it at the "server". Then here you go.
Related Posts Plugin for WordPress, Blogger...