Blog
Image Caching with the HTML5 Canvas
Thu, Sep 24, 2009
by
Jesse

Lately I have been working on an iPhone app ( using PhoneGap of course )  and needed to implement image caching on the client with javascript.

I am already using an SQLite database in mobile safari, so I decided I could store images in Base64 in the DB.  I was able to load the binary image data using XHR, but could not correctly encode it to base64 in javascript.

Exploring another path, I found that there is a method of the HTML5 Canvas toDataURL();

So I wrote this to download the image, instead of my XHR method :



ImageCacheManager.prototype.fetchImage = function(url)
{
var alias = this;
var img = new Image();
img.onload = function()
{
alias.onImageLoaded(this);
};
img.src = url;
}

Then this to handle the loaded image and cache it :



ImageCacheManager.prototype.onImageLoaded = function(img)
{
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img,0,0);
var dataURL = canvas.toDataURL();
this.cacheImageData(img.src, dataURL);
}



To keep things short I have excluded the DB sections, I always check if I have an image cached already before I fetch one, and always write it to the DB when I do fetch.  If an image is retrieved from the DB it can simply be written to the img.src as is.


I foresee all kinds of uses for this, like a javascript based image editor, or a water-marking script ( you can also draw text on the canvas before you pull out it’s bits … )


› Visit the original post

Mobile App Development Training in YVR, Don’t Miss the Early Bird Discount
Thu, Sep 24, 2009
by
brian.leroux

If you’re a developer interested in building cross-platform mobile apps, then you do not want to miss our upcoming PhoneGap Training on October 15 in Vancouver. Give us a day and we can teach you how to build jaw-dropping, cross-platform mobile apps for the iPhone, Android, Blackberry and Nokia with PhoneGap.

PhoneGap is an open source development tool for building fast, easy mobile apps with JavaScript. If you’re a web developer who wants to build mobile applications in HTML and JavaScript while still taking advantage of the core features in the iPhone, Android, Blackberry and Nokia SDKs, PhoneGap is for you. You can find out more about PhoneGap at www.phonegap.com.

Get a $100 Discount till October 1
We’re offering a $100 discount on PhoneGap training till October 1. Simply enter this code (earlybird) in the ‘Enter Discount Code’ field during the registration process and you’ll get $100 off!

Register now because space is super limited.

Vancouver
October 15, 9:30am – 5:30pm
UBC Robson Square
Cost: $399 (after $100 early bird discount)
Register now: http://bit.ly/34SeS7


› Visit the original post

Phonegap Nokia
Wed, Sep 16, 2009
by
Ryan

I just pushed my work on porting phonegap for Nokia WRT (Web Runtime) on the s60 platform to my github repo, and will get brock to drop it into the main phonegap repo over very soon, but I just wanted to get it out that phonegap for nokia is on its way! Nokia WRT’s exposure of device functionality has made it relatively easy to implement phonegap for geolocation, notification, and contact support in nokia wrt widgets. There are some limitations in the WRT environment; one significant one at the moment is that WRT’s device API and menu object functionality are exposed only to one main html file. Hopefully can figure out a way around this to stay in line with the phongap ideology. Stay tuned for phonegap Nokia.


› Visit the original post

PhoneGap BlackBerry Progress
Fri, Sep 11, 2009
by
Fil

Hey everyone,

It’s been a busy past two weeks in the PhoneGap world, especially concerning the BlackBerry branch. We (Nitobi) have put a lot of work into this branch for the past week or two because we’ve got clients who are asking for BlackBerry support for the slew of new mobile applications we’re developing. It’s exciting because these kind of requirements really push the need for expanding the source code and making it better.

I wanted to share with everyone a couple of problems that we’ve encountered recently while working on these projects, and the solutions I employed to solve these problems. For reference, you can find my GitHub repository containing the latest PhoneGap BlackBerry code here.

First, we had quite a serious on-device rendering issue that didn’t come up whilst testing with the BlackBerry simulator. Basically, scrolling would cause the HTML page and all related assets being rendered to ‘tear’ up as you scroll. Picture to provide a better explanation:

Initial rendering / tearing issue we experienced.

Initial rendering / tearing issue we experienced – just after load on the left, after scrolling down and then up on the right.

Warning: BlackBerry code and JDE references ahead. Proceed with caution.

Since then I have solved the problem by calling invalidate() on the application’s Screen’s Field objects, and then invoking Screen.doPaint(). One issue still stands that we currently can only do this in a Timer Task that runs every x milliseconds, so intermittent tearing is still an issue. I tried to resolve this by executing the same invalidate / doPaint code in a custom VerticalFieldManager class extension, that hooked in the redraw code on scrolling events. However, this approach just didn’t work. If anyone has any ideas on how to implement this more elegantly, don’t hesitate to contact me or post a comment – I would love to solve this properly.

Second issue I fixed just today, actually, was a pain-in-the-ass requirement for BlackBerry PhoneGap applications to use a special URI when referencing content that is stored locally on the device. For example, up to this point if your main entry page had an <img> tag that referenced a .png file in the same directory as the main entry page, you would have to set the src attribute of the <img> tag to “data:///<absolute folder path>/mypng.png”. This wasn’t a show-stopper, but it did break the mantra of “write once, deploy to all platforms” that PhoneGap strives to uphold (because, for example, PhoneGap iPhone supports relative and absolute paths within your HTML/CSS/JS).

My solution to this problem was to use a big hash table to store the absolute paths of various resources being loaded, and using the referrer IDs of these resources as the key. Then when we load in requested resources, we check if the referring ID is stored in the hash and build up the absolute path (which we need on the native BlackBerry side to grab the resource data) appropriately. I’m nervous about this approach because I can easily see this causing memory issues down the road – too big of a hash table, or keys being too large.

One mitigating option is, instead of using the entire referrer ID (which can get quite big – essentially it’s the Base64 encoding of the referring resource), hash that down to, say, an MD5 hash, and then use that as a key. That should save some space. Currently, the hashing is done on all resources being loaded, which is obviously overkill. Images don’t need to be hashed, for example.

Questions, comments, ideas, suggestions, please, post away! PhoneGap is an open-source, community-based project, so “just fork it” !


› Visit the original post