Jayant's Blog

Tuesday, September 16, 2008

iPhone SVG Events

The way the events work on a browser is pretty simple. If you want to listen to a particular event (say mousedown) on the whole document, you simply add an event listener to the document: document.addEventListener("mousedown", listenerFunction, false);. Setting 'false' for the 'useCapture' argument sets the event to bubble, which is the preferred event flow.

The test that we have is really simple. On your iPhone, the page renders a circle, a rectangle & of course these are rendered within a SVG container which is created within a background div. Tap on the circle, then rect and finally the background div. The test pages have 'touchstart' or 'click' event listeners registered by default. Click the 'Add'/'Remove' buttons to add/remove event listeners from the SVG element. There are 2 listener functions, one for logging document events and one for svg events. The events are logged with 'short function name: event.type : event.target'.

If you didn't know this already, you can enable the debug console in Safari on the iPhone.

Now, on the iPhone, the 'touchstart' event is similar to the 'mousedown' on a browser. Go to http://ecodevil.net/dev/blog/events_touch.html using your iPhone. Click on the circle, then rect and finally the white background. You should see 3 events logged.



Now click on the 'Add' button and as expected you will see 1 event (for the button). Now clear the console and click the circle, rect & background again. This time, you will have 5 events logged.



This is actually the expected behavior.

Now from your desktop Safari, go to http://ecodevil.net/dev/blog/events_click.html. You can enable your developer menu as well, see 'Safari Developer FAQ'. You will notice that you have 3 click events logged.



Now click on the 'Add' button and try the test again. You will once again have 1 + 5 events.



Again try http://ecodevil.net/dev/blog/events_click.html using your iPhone 2.1. When you run through the first set of tests, you will see no events logged.



Now click on the 'Add' button and try the test again. You will now see 4 events logged, but there is no click event on the background div.



So What?
So basically, events don't work exactly the same on the iPhone as the desktop browser. On a desktop browser, if an event listener is registered to listen to a particular event on the document, any event of the same type, on any element on the document bubbles up and will be logged, unless the event is stopped from propagating. But seems like on the iPhone, the touch/gesture events work by default on the document as well as the SVG element. But for the click/mouse* events to work on the document, there needs to be an event listener registered to listen to the SVG element. And removing this event listener will stop all click/mouse* events on the document.

Labels: , ,

Monday, September 15, 2008

Simple SVG Shapes

Here's a simple test page which will draw different SVG shapes on your canvas. Of course if your iPhone OS is not 2.1, go get it and try out http://ecodevil.net/dev/blog/shapes.html. So lets looks at the code for http://ecodevil.net/dev/blog/svg.js.

Firstly, I have 3 JavaScript functions. The createNode is the main one which creates the appropriate SVG node using the node type and applies all properties from the argument hash to the new node. The createImage & createText are just specialized uses of this function.

<javascript>
function createNode(type, hash) {
var node = svg.appendChild(document.createElementNS("http://www.w3.org/2000/svg", type));
for (var key in hash) {
node.setAttribute(key, hash[key]);
}
return node;
}

function createImage(href, hash) {
var node = createNode("image", hash);
node.setAttributeNS("http://www.w3.org/1999/xlink", "href", href);
return node;
}

function createText(text, hash) {
var node = createNode("text", hash);
node.appendChild(document.createTextNode(text));
return node;
}
</javascript>

The next is the test function which is called on document.onload. It creates a rect, circle, ellipse, line, polyline, polygon, image, text & path. I've found the Pike's SVG Tutorial to be a good quick reference for SVG syntax.

<javascript>
function test() {
var div = document.getElementById("surface");
svg = div.appendChild(document.createElementNS("http://www.w3.org/2000/svg", "svg"));
svg.setAttribute("style", "left:0px; top:0px; width:320px; height:326px;");

createNode("rect", { x:10, y:10, width:50, height:30, fill:"rgb(0,255,0)" });
createNode("circle", { cx:95, cy:25, r:15, fill:"rgb(255,0,0)" });
createNode("ellipse", { cx:170, cy:25, rx:35, ry:15, fill:"rgb(0,0,255)" });
createNode("line", { x1:230, y1:10, x2:290, y2:40, stroke:"rgb(255,0,255)", "stroke-width":3 });
createNode("polyline", { points:"10,100 10,75 65,75 65,100 120,100 120,75 175,75 175,100 230,100 230,75 285,75 285,100", stroke:"rgb(0,255,255)", "stroke-width":3, fill:"none" });
createNode("polygon", { points:"10,125 285,165 285,125 10,165 10,125", fill:"rgb(255,255,0)" });

createImage("http://news.bbc.co.uk/shared/img/v4/header_blocks.gif", { x:10, y:190, width:107, height:32 });
createText("Ola World", { x:210, y:215, "font-size":32, "text-anchor":"middle", "font-weight":"bold" });
createNode("path", { d:"M10,265 L10,300 L100,265 C100,265 300,265 200,300 H285", stroke:"rgb(0,0,0)", "stroke-width":5, fill:"none" });
}
</javascript>

Thats it. You can try this page using Safari/Firefox/Opera or Chrome... not IE.

Labels: , ,

Friday, September 12, 2008

iPhone 2.1 now supports SVG

At iPhone 2.0, SVG was to be supported but when trying using the iPhone safari browser, even simple stuff it didn't work. So it was promised that the support would be there at the next major release and well its here. There's not too much noise about it in the blogs... but this is HUGE.

Currently dojox.gfx defaults to canvas on the iPhone but the following bug will fix that, hopefully in time for v1.2.

The iPhone has had touch & gesture events since iPhone 2.0 for handling single & double-touch events respectively. With this release, there should be support for drawing vector geometry using SVG and also events on SVG elements.

More to come...

Labels: , ,

Saturday, September 06, 2008

YouTube video

ESRI puts videos about its software online, including on youtube. Recently Andy stopped by to have a chat with Jeremy & me about the ArcGIS JavaScript API. You can see my office, with my Colts cap, the Dilbert head, my miniature planes and my Star Wars fighter models. I forgot about the chat that morning and forgot to shave and wear a good shirt. Oh well! Link to the video

Labels: