Blog
Canvas + Accelerometer on Android
Tue, Apr 27, 2010
by
Joe B

Over the past month, I was working from home, and when I wasn’t busy wiping spit-up from my kid’s face, I was working on various stuff, including a new Android PhoneGap demo to replace the one that I took off the Android market. The problem with these demos is that they do not stand the test of time. However, this one should. After playing with C5Bench a couple months ago, I decided to do a quick hack of the Canvas. After many bug fixes to PhoneGap Android later, I finally managed to put together this simple example:

Now, of course, this is an early version, and the code has been thrown together very quickly, and there are bugs, which is why I haven’t posted it in the market, but you can download and try out the application here, or scan in the QR code below.

This code is based on a few examples that I found on the web of good Canvas Tutorials, namely the Breakout Clone tutorial by Bill Mill. It’s a pretty solid tutorial and shows how to effects like bounce fairly effectively. Of course, I didn’t add bounce, because I wanted more of a sliding bubble effect. I also decided to NOT use the jQuery framework to keep the javascript code as small as possible. (In fact, I didn’t even use XUI, since I didn’t see the need).

That being said, I’m definitely going to have to work on my own JS library at some point that maintains complex objects, since redrawing functions can quickly become large and cumbersome, even when using a JS library, which is better suited to the DOM and not pixel-manipulation.

So, basically you move around two objects which are directly related to each other on a screen with the accelerometer, and the method that we use is below:


var watchAccel = function()
{
var canvas = document.getElementById("canvas");
canvas.width = document.body.clientWidth;
canvas.height = document.body.clientHeight - 100;
ctx = canvas.getContext("2d");
var succ = function(a){
updateValues(a);
updateDraw(a);
};
var fail = function(){};
var opt = {};
opt.frequency = 100;
timer = navigator.accelerometer.watchAcceleration(succ, fail, opt);
}

Now, what’s interesting is how the frequency is handled. Currently, we use the setInterval to do this, exactly like the iPhone. However, this actually kinda sucks, and we may in the future move more of this into the Java land, similar to how we manage the Geolocation code that we use. However, this is good enough to create the sliding bubble effect of the application. I hope to have the final version of the app up and working before Google IO, however I’ll probably post it up to GitHub this week, feel free to try and and let me know if there’s serious issues.

› Visit the original post

Introducing iPhone Cleavage
Wed, Apr 21, 2010
by
Jesse

So if you want to make an iPhone app using your HTML/CSS/JavaScript skills, what do you use? PhoneGap! obviously.

If you want to add some iPhone native controls, screens, whatever… PhoneGap Plug-ins seem like a fairly easy path, even though they are still largely undocumented, and unknown to most, at least they are somewhat planned.

But what if you have a native iPhone app already, and you want to quickly throw in some phonegap. Before today, there was no easy way to do this because phonegap for iPhone was architected to be the one and only Application delegate. This is the newest use case I have chosen to address.

This approach of breaking PhoneGap functionality into it’s own control space makes some exciting new things possible in the near future. Including:

  • Multiple page / view apps where the views are persistent ( instead of having to reload or navigate between pages )
  • Sandboxed child apps
  • The ability to divide work among multiple developers, without having to constantly keep track of other’s changes.
  • Better division of responsibility within code modules ( divide and conquer methodologies ).
  • Drop-in functionality, for code libraries, add them to your app more easily, and build up your control library over time.

Introducing Cleavage for iPhone.

First off, you may be wondering about the name. I wanted to stick with the ‘gap’ theme so I started brainstorming on other types of gaps.

At first I considered Diastema as it has a nice ring to it. After thinking on that for awhile I decided it would be best not to alienate phonegap developers as we are predominantly Madonna, Letterman, and Mad Magazine fans. ( What, me worry?)

Other Gaps I considered were :

  • Rift – too sci-fi
  • Gape – um, too close to Gap
  • Fissure – too literal, and weird …
  • cavity, chasm, crack, … – just not it

So cleavage it is! .. and given it’s current state it is very much as Jerry Seinfeld says.

“Looking at cleavage is like looking at the sun. You can’t stare at it long, it’s too risky. You get a sense of it then you look away.”

Please keep in mind that this is very much a work in progress, but expect some updates in the near future.

I have posted the PhoneGap control sample app to: http://github.com/purplecabbage/cleavage

Documentation and such to follow later in the week, I hope, here is a quick getting started description.

In order to use this in your own ( pre-existing ) iPhone OS project, you will need to:

  1. add all PhoneGap required frameworks to your project ( the sample project has them all )
  2. add a reference to PhoneGapLib ( you will need the latest checked in version )
  3. place your html/js/css in a www folder in the project and be sure it is packaged in the root of your app when built
  4. include phonegap.js from PhoneGapLib, as it is not auto-copied over for you like it is in a typical phonegap iPhone app.
  5. set linker flags for -all_load and -ObjC

› Visit the original post

PhoneGap and the Apple developer license agreement.
Wed, Apr 14, 2010
by
Jesse

Yesterday I made a quick update to a previous blog post. I will go into a little more depth here as some folks mis-understood that this was a new topic because it was part of the older post.

Recent changes to the Apple iPhone SDK developer license agreement have been a hot topic over the last week. There has been much speculation over how far reaching the 3.3.1 clause of the agreement is, not to mention the business strategy behind it. I will not join that conversation, but I will speak of what I have learned with regards to the impact on PhoneGap (iPhone/iPad).

Through email discussions with Apple, I specifically asked what, if any, impact did this have on present/future applications submitted to the App store that were built using PhoneGap. In no uncertain terms, my contacts at Apple have assured me that “PhoneGap is not in violation of the 3.3.1 clause of the license agreement.”

How this affects other tool-chains like Appcelerator, Flash CS5, Corona, MonoTouch, … I have absolutely no idea. All I can say is that PhoneGap is okay.

I will post updated information if I receive it, I have done a few other things to test the waters. Yesterday I submitted my iPhone PhoneGap Tutorial application to the App Store, I will post info here when/if it is approved ( although it may be rejecting for offering insufficient functionality, as it is a tutorial ).

The tutorial source code is available here: http://bit.ly/Jestitute

› Visit the original post

iPhoneGap Plugs
Thu, Apr 1, 2010
by
Jesse

Okay, in your phonegap application in XCode, ( Without EVER having to touch the PhoneGapLib )

1) right click the plugins folder and select Add/NewFile/ObjectiveCClass
2) Call your new class : TestPlug
3) Paste the following code into the .m + .h files:

// In the .h file …..
#import
#import “PhoneGapCommand.h”
@interface TestPlug : PhoneGapCommand {
}
- (void)doit:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end

// in the .m file

#import “TestPlug.h”

@implementation TestPlug

- (void)addThemUp:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSUInteger argc = [arguments count];
int total = 0;
for(int n = 0; n < argc; n++)
{
total += [[ arguments objectAtIndex:n] intValue];
}
NSString* retStr = [ NSString stringWithFormat:@"alert("%d");",total];
[ webView stringByEvaluatingJavaScriptFromString:retStr ];
}
@end

4) In your JS code, somewhere after deviceready, call your command like this.

PhoneGap.exec(“TestPlug.addThemUp”,1,2,3,4);

Build and run!

Note the structure of the PhoneGap.exec call
1) a command name : TestPlug
2) a method name : addThemUp
3) a variable length list of arguments

Next time, I’ll get into passing objects via the ‘options’ object.

If you have a compelling plug-in you want to share, you can fork my repo for plugins and send me a pull request :

http://github.com/purplecabbage/PhoneGap-Plugins

If your plugin makes sense on multiple devices, and it is implemented with standards in mind, it may end up in PhoneGapLib.

› Visit the original post

JavaScript Fun and PhoneGap Training in Washington DC in April
Thu, Apr 1, 2010
by
Andre Charland

As the calendar flips over to April, we’re stoked about some of the things Nitobi is up to this month. For starters, we’ll be at JSConf in Washington DC from April 17-18, 2010. If you haven’t been to JSConf you’re missing out on one of the best JavaScript programming events of the year. It’s sold out this year–likely because the theme is pirates–but be sure to put it on your calendar for 2011.

While we’re at JSConf, we’ll be connecting with the good people of Washington DC for a PhoneGap training session. We’ve been having a great time spreading the PhoneGap love across North America and we hope you’ll join us for this full-day workshop on April 16, 2010. You’ll learn how to build mobile applications in HTML and JavaScript while still taking advantage of core features of the Android, iPhone, Blackberry, Symbian and Palm.

The Latham Hotel, 3000 M Street NW
Washington DC
April 16, 2010
9:30 am – 4:30 pm

Register today for the Washington DC PhoneGap Training Workshop.

See you in the Capital!

› Visit the original post