Activate your free membership today | Log-in

Friday, July 11th, 2008

iPhone Web Goodies: Drag and Drop with Touch, Resize and Rotate with Gestures

Category: JavaScript, Showcase, iPhone

The video above shows a simple showcase application that Neil Roberts of SitePen created and wrote about.

He testing out the new APIs such as the touch API where he worked with drag and drop:

JAVASCRIPT:
  1.  
  2. node.ontouchmove = function(e){
  3.   if(e.touches.length == 1){ // Only deal with one finger
  4.     var touch = e.touches[0]; // Get the information for finger #1
  5.     var node = touch.target; // Find the node the drag started from
  6.     node.style.position = "absolute";
  7.     node.style.left = touch.pageX + "px";
  8.     node.style.top = touch.pageY + "px";
  9.   }
  10. }
  11.  

And resizing and rotation with the Gestures API:

JAVASCRIPT:
  1.  
  2. var width = 100, height = 200, rotation = ;
  3.  
  4. node.ongesturechange = function(e){
  5.   var node = e.target;
  6.   // scale and rotation are relative values,
  7.   // so we wait to change our variables until the gesture ends
  8.   node.style.width = (width * e.scale) + "px";
  9.   node.style.height = (height * e.scale) + "px";
  10.   node.style.webkitTransform = "rotate(" + ((rotation + e.rotation) % 360) + "deg)";
  11. }
  12.  
  13. node.ongestureend = function(e){
  14.   // Update the values for the next time a gesture happens
  15.   width *= e.scale;
  16.   height *= e.scale;
  17.   rotation = (rotation + e.rotation) % 360;
  18. }
  19.  

Some readers might have noticed that a gesture is just a prettier way of looking at touch events. It’s completely true, and if you don’t handle things properly, you can end up with some odd behavior. Remember to keep track of what’s currently happening in a page, as you’ll probably want to let one of these two operations “win” when they come in conflict.

Posted by Dion Almaer at 10:16 am
Comment here

OOOOO
Rate the above post

Remember the Web Apps; Don’t forget the first iPhone baby today

Category: JavaScript, iPhone

We see the birth of the second baby when it comes to building and running apps on the iPhone. People have already spent almost $100k in the first few hours of the AppStore not being open, so tomorrow is likely to be a great day for Apple, and developers, as people run around clicking on buy without thinking of the price.

I spent some time running the applications, and thinking about how different they are to Web versions. For example, when I launch Twitterific vs. a web based Twitter it actually was faster for the Web page to load Safari up with everything. Twitterific also had a strange feature for loading images. As I moved up and down the list the images looked like they were being pushed out of cache which made for a weird experience.

What if the iPhone offered the ability to aggressively cache "applications". When you open the application it opens its own instance of Safari instead of just linking over to Safari. What if it had access to local storage APIs in WebKit?

The native applications do have benefits. They have access to the camera, addressbook, ... well wait, those could be JavaScript APIs too.

There is the tool chain. You can have fine grained performance knowledge of your application with the Objective-C tools, but with SquirrelFish Apple is getting better and better there too. Other nice tooling could work well when constraining the Web interfaces to the iPhone form factor.

What about games? You couldn't do super monkey ball, or could you if you had a really solid Flash, or feature complete canvas.

The native apps are great, but I am still betting on the Web as a great platform for mobile applications too.

Posted by Dion Almaer at 10:01 am
1 Comment

+++--
3.5 rating from 4 votes

YPulse: Fades and Pulsations Library

Category: Component, JavaScript, Library, Yahoo!

Kent Johnson has released YPulse a simple open source wrapper for the YUI Animation library that makes creating highlight fades and pulsing button glows a bit easier.

You pulse away with something like:

JAVASCRIPT:
  1.  
  2. var pulser = new YAHOO.squarebits.YPulse(
  3.   ‘my-div’,
  4.   ‘backgroundColor’,
  5.   ‘#FFFFFF’, // starting
  6.   ‘#FFFF00′, // ending
  7.   0.75, // The number of seconds for the start-end transition
  8.   0.10, // The number of seconds to wait after completing the start-end transition
  9.   0.75, // The number of seconds for the end-start transition
  10.   0.75, // The number of seconds to wait after completing the end-start transition
  11.   YAHOO.util.Easing.easeBoth, // The YAHOO easing method to use for the start-end transition
  12.   YAHOO.util.Easing.easeBoth // The YAHOO easing method to use for the end-start transition
  13. );
  14.  

Posted by Dion Almaer at 8:42 am
Comment here

+----
1.5 rating from 13 votes

A safety fence for your property lookups

Category: JavaScript, Tip

Michal Till posted a little JavaScript tip that he uses to create a safety fence for accessing properties:

As we all know, null not only does not have any properties, but their existence also can not be tested. So null.property retuns error instead of undefined.

You can end up with something like this:

JAVASCRIPT:
  1.  
  2. if (
  3. (node) &&
  4. (node.nextSibling) &&
  5. (node.nextSibling.className == ...)
  6. ... ) {
  7.    ...
  8.  

There is a neat trick that can simplify the boolean expression. We might supply an empty object {} or zero (0) as an alternative:

JAVASCRIPT:
  1.  
  2. if ( next = (node || 0).nextSibling) ) {
  3.  

Posted by Dion Almaer at 6:39 am
2 Comments

++---
2.7 rating from 17 votes

Thursday, July 10th, 2008

Ojay 0.2: easy keyboarding, a validation DSL, and two new UI widgets

Category: JavaScript, Yahoo!

James Coglan has updated Ojay, the chaining wrapper for YUI that we posted on a few months back.

The new release features really simple keyboard and form scripting and couple of new UI widgets, a new event system and a stack of other improvements:

Ojay.Forms. By far the biggest new package, Ojay.Forms sorts out a real pain point for me in terms of app development. It does two things: it provides unobtrusive replacements for the YAHOO.widget.Button family of classes, and it provides a DSL for handling form validation and Ajax submission.

Ojay.Keyboard. The new keyboard package is an abstraction over YAHOO.util.KeyListener that lets you say what mean without worrying about character codes, for example:

Ojay.Keyboard.listen(document, 'ALT SHIFT S', function() {
    // handle key press
});

It lets you group sets of keyboard commands together so you can make context-sensitive keyboard controls, and gives you easy access to enable/disable key events and their default browser responses. Not much more to say except that you should check out the documentation.

We have two new UI packages, Ojay.Overlay and Ojay.Paginator. Overlay gives you a bunch of classes for positioning content on top of the document, producing lightbox effects and the like, and Paginator implements the content slider effect that’s got a lot of attention recently, including the ability to lazy-load pages of content via Ajax, and easy integration with Ojay.History. Both packages come with a collection of events to allow your code to react to changes to the components, just like you would for DOM elements.

Speaking of events, this release introduces Ojay.Observable, a JS.Class module that allows any class to support the on() method used for listening to events. This module underlies the custom events published by all the Ojay components. More information and examples are in the documentation.

Posted by Dion Almaer at 10:41 am
1 Comment

+++--
3.6 rating from 11 votes

Code specialization; Namespaces again; Parasitic inheritance

Category: JavaScript

Karl Krukow has a new blog that has some interesting Ajax content. Rather than pick at one, we have a synopsis of a few of his recent posts:

Manual code specialization

A technique is presented (in terms of examples) which can give performance boosts in certain situations. The technique relies on higher-order functions, and sharing variables in closures.

Namespacing again

Namespacing has been generally accepted (in one form or another) as a good thing. In this posting, a pair of functions: 'namespace' and 'using' are presented. The former ensures the existence of a namespace, similar to what is offered in common JavaScript libraries. However, it accepts more general forms of namespace declarations:

JAVASCRIPT:
  1.  
  2. namespace({
  3.  com: {
  4.    trifork: {
  5.     tribook: ['model','view','controller']
  6.     }}});
  7.  

The latter function 'using' complements the namespace function. It brings a namespace into scope.

JAVASCRIPT:
  1.  
  2. using(com.trifork.tribook.model).run(function(m) {
  3.  
  4.  m.Reservation = function(start,end){
  5.   //...
  6.  };
  7. });
  8.  

Parasitic inheritance & instanceof

The object function advocated by Crockford in his work on parasitic inheritance makes a new object which has the input object as it's prototype. However 'instanceof' doesn't work with this function. A technique for combining these two is presented.

Posted by Dion Almaer at 10:37 am
Comment here

+++--
3.8 rating from 12 votes

Monday, July 7th, 2008

LLVM and running C as well as Python in the browser

Category: JavaScript, Python

Dan Morrill doesn't like JavaScript 2. His reasoning is a little like the folks who don't want Java.Next to try to copy features from every other language, but to just be the best static language, and let other languages like Scala, Groovy, JRuby (and the hundreds of others like Fan) go in different directions on the same Java platform.

You could argue the same for the browser platform. Why push JavaScript 2 further than cleaning it up, and instead allow other languages to augment it.

This is where technology such as IronMonkey come in, as well as the work that Scott Peterson is doing, written up here:

Scott Petersen from Adobe gave a talk at Mozilla on a toolchain he’s been creating—soon to be open-sourced—that allows C code to be targeted to the Tamarin virtual machine. Aside from being a really interesting piece of technology, I thought its implications for the web were pretty impressive.

If I followed his presentation right, Petersen’s toolchain works something like this:

  1. A special version of the GNU C Compiler—possibly llvm-gcc—compiles C code into instructions for the Low Level Virtual Machine.
  2. The LLVM instructions are converted into opcodes for a custom Virtual Machine that runs in ActionScript, a variant of ECMAScript and sibling of JavaScript.
  3. The ActionScript is automatically compiled into Tamarin bytecode by Adobe Flash, which may be further compiled into native machine language by Tamarin’s Just-in-Time (JIT) compiler.

The toolchain includes lots of other details, such as a custom POSIX system call API and a C multimedia library that provides access to Flash. And there’s some things that Petersen had to add to Tamarin, such as a native byte array that maps directly to RAM, thereby allowing the VM’s “emulation” of memory to have only a minor overhead over the real thing.

The end result is the ability to run a wide variety of existing C code in Flash at acceptable speeds. Petersen demonstrated a version of Quake running in a Flash app, as well as a C-based Nintendo emulator running Zelda; both were eminently playable, and included sound effects and music.

Posted by Dion Almaer at 8:50 am
7 Comments

++++-
4.3 rating from 16 votes

Thursday, July 3rd, 2008

JavaScript Plugins; The beauty of loosely coupled code

Category: JavaScript

James Coglan wrote a piece on There is no such thing as a JavaScript plugin that uses jQuery as a use case for how simple it is to have a plugin contract.

When you think about plugins in many environments, you have strict contracts through interfaces that you have to implement. With jQuery, you can just add on to the jQuery prototype, normally seen through the alias $.fn.

James shows the simple example of:

JAVASCRIPT:
  1.  
  2. $.fn.makeThemRed = function() {
  3.   this.css({color: 'red'});
  4.   return this;
  5. };
  6.  
  7. $('p').makeThemRed();
  8.  

I read this as the power of the simplicity, but John Resig, again in his three parter saw it a little different, I think by thinking too much about the title :)

I will contend that such a thing as plugins exist and are logically distinct from "random JavaScript code that manipulates other JavaScript code" as long as the following points are met:

  1. There have to be explicit points upon which a plugin can attach. James notes the most common one in jQuery (jQuery.fn) but we have tons more - events, animations, selectors - all over the board for developers to snap in to.
  2. Even more importantly: Those points have to be documented or, at the very least, be under some sort of agreement that they will be treated like a normal piece of the user-facing API. In jQuery we treat all plugin extension points as "user-facing API" and only ever change them in major releases (if at all) and always provide an alternative for authors to use.
  3. Finally, there has to be some sort of repository for navigating these plugins. This is a huge differentiator. Simply referring to "code in the wild" as plugins doesn't really cut it if there's no commitment to hosting them and keeping their documentation and examples alive.

Posted by Dion Almaer at 10:16 am
2 Comments

+++--
3.4 rating from 14 votes

Wednesday, July 2nd, 2008

eval(’foo=a’, obj.fn); How you will be private in Firefox 3.1

Category: Firefox, JavaScript

Posted by Dion Almaer at 2:58 pm
8 Comments

++++-
4.1 rating from 14 votes

Tuesday, July 1st, 2008

MooWheel and MooCirclePack for visualizations

Category: JavaScript, MooTools

MooWheel, the JavaScript connections visualization library, has been updated to version 0.2.

Updates include:

  • New data format
  • Text can now be hovered over, in addition to the dot
  • Images can be added for each item

You can see the popular Twitter example
(thanks to Augsto Becciu, creator of TweetWheel).

Also, MooCirclePack has just been released:

MooCirclePack is another stunning visualization library that brings circle packing to JavaScript. It is great for data that can be represented by size (eg: an alternative to a bar graph), or data that can be represented amorphously.

There is a non-Ajax demo, and an Ajax one.

MooCirclePack

Posted by Dion Almaer at 11:02 am
Comment here

+++--
3.9 rating from 20 votes

Monday, June 30th, 2008

ShiftZoom: Zoomify your oversize images

Category: Canvas, JavaScript, Library

ShiftZoom

ShiftZoom 1.0 is the latest tool from Christian Effenberger that allows you to add zoom and pan functionality to oversized images on your webpages. It uses unobtrusive javascript to keep your code clean. Requires no plugin/extension or any other external resource!

It works in all the major browsers - Mozilla Firefox 1.5+, Opera 9+, IE 6+ and Safari. Works also on older browsers supporting images & createElement & getElementById, else it'll degrade and your visitors won't notice a thing.

It is as simple to use as:

HTML:
  1.  
  2. <div><img onLoad="shiftzoom.add(this);" width="400" height="200" .../></div>
  3.  

Posted by Dion Almaer at 11:15 am
10 Comments

++---
2.9 rating from 30 votes

JavaScript Protocol Fuzzer

Category: JavaScript, Testing

Gareth Heyes has written a JavaScript protocol fuzzer which has the goal of "producing every variation of javascript execution from links."

If you check out the demo you see all of the options available to fuzz:

Number of characters - This inserts between 1 and 10 characters in the chosen position

Character position - The string position of the characters chosen. E.g. if you choose “0″ then the “j” will be replaced or appended.

Replace character - Simply replaces the character rather than add characters to the position.

Url encode - Urlencodes the vector before outputting the link.

HTML hex entity encode - Instead of output the character, it uses the HTML hex entity instead.

HTML dec entity encode - Instead of output the character, it uses the HTML decimal entity instead.

Semi-colons - Adds a semi-colon if HTML entities are used.

Random zero fill - Adds a bunch of random zeros if entities are used.

Start from - Is the starting character to begin the fuzz. E.g “0″ is null

He has also found interesting results in various browsers such as: jav�ascript: working, meaning that this would work:

HTML:
  1.  
  2. <a href="jav&#56325ascript:al&#56325ert(1)">test</a>
  3.  

Posted by Dion Almaer at 11:07 am
11 Comments

++---
2.3 rating from 11 votes

Friday, June 27th, 2008

dragtable: drag-and-drop reorderable columns for an HTML table

Category: Component, JavaScript

Dan Vanderkam has announced a new component dragtable:

Over the past several years, Stuart Langridge’s sorttable Javascript library has found widespread use. It’s easy to see why. Just add class=sortable to a table tag and its column headers automatically support click to sort. Pretty slick.

But sometimes sorting just isn’t enough. What if you want to focus on just one or two of the columns in a table? In a client-side application you could drag the columns you care about next to each other. Why not in a web application?

Enter dragtable. Like sorttable, it teaches HTML tables a new trick through a simple class attribute.

Once you have the JavaScript in place, you can simply add a class="draggable", and you can even work with both via class="draggable sortable"

Take a peak at a test bed, and the open source project.

Posted by Dion Almaer at 9:20 am
8 Comments

+++--
3.7 rating from 26 votes