Quick hack to deal with touch and mouse events

If you build web pages that are interactive and work on desktops and mobile devices then chances are that you need to work with touch and click events. For mobile devices you’d want to use events like touchstart, touchmove, and touchend and for desktops you’d use mouse events (mousedown, mousemove, and mouseup).

Most of the time the code you execute as a response to these events is the same between these two platforms. So one way to avoid duplicating your code is to take advantage of the JavaScript’s dynamic nature to inject additional data into the event object and chain the click and touch event listeners.

Here is a snippet of code that illustrates my point:

function init() {
    var el = document.getElementById('myDiv');
    el.addEventListener('mousemove', onMouseMove);
    el.addEventListener('touchmove', onTouchMove);
}

function onMouseMove(e) {
    e.touches = [{clientX: e.clientX, clientY: e.clientY}];
    onTouchMove(e);
}

function onTouchMove(e) {
    //do something with e.touches[0].clientX or e.touches[0].clientY

}

The code should be pretty simple to follow:

I put together a simple page so you can see the code and test it on you computer/devices: demo here.

What method do you use to deal with this?

Debugging web pages remotely using a PlayBook tablet

In  a previous post I explained how you can use weinre to remotely ”debug” web pages that run on your mobile devices. If you are familiar with weinre then you also know its biggest limitations: no JavaScript debug support and CSS styles introspection.

Now there is no need to be sad if you happen to have a PlayBook device. If so, then you are back in business. What do I mean by that? I mean full access to Web Inspector/Developer Tools goodies while browsing web pages on the PlayBook tablet. Yes, including JavaScript debugging and CSS introspection.

A question or a doubt might pop in at this point … how this could this be possibly useful for developing web applications that run on mobile devices when Android and iOS devices have the greatest market share? Relax my friend, no need to lose your faith :D Because iOS, Android, and PlayBook default browsers are all based on WebKit. Although there are some differences between them I think they are close enough that you can get lots of value out of using the PlayBook device as the “debug” mobile platform. It also performs fine, at least today. I mean it is not too far off from the latest mobile phones or tablets.

So let me guide you through the steps you need to complete if you want to remotely debug web pages using a PlayBook.

Read more

Canvas Quirks

While using Canvas 2D context for drawing stuff I discovered that the drawing line API can surprise you a bit especially when drawing horizontal or vertical lines. Here is a screenshot with a Canvas element and 5 lines drawn using lineTo() calls:

In case you haven’t noticed, let me tell you what’s wrong with this: the lines are suposed to be 1 pixel width and black. Clearly what you see on the screen is not 1 pixel and the lines are somehow grayish. It looks more like 2 pixels. The code for drawing this looks like this:


<input onclick="draw()" type="button" value="draw" />

<script type="text/javascript">// <![CDATA[
function draw() {
    var context, i, y;

    context = document.getElementById('canvas').getContext('2d');
    y = 20;
    context.lineWidth = 1;
    context.strokeStyle = '#000000';
    for (i = 0; i < 5; i++) {
       context.moveTo(0, y);
       context.lineTo(450, y);
       y += 10;
    }
    context.stroke();
}
// ]]></script>

Let’s change the line width to 2 (line 10 in the above code snippet) and check the result:


Interesting, isn’t it? So the lines width is basically the same, but the color now is really black. Now, let’s try something else: change the line width back to 1 and adjust the y property of the moveTo/lineTo functions with o.5 (line 13/14):

context.moveTo(0, y + 0.5);
context.lineTo(450, y + 0.5);

And surprise, surprise the lines are now exactly 1 pixel and black:

So what’s happening? After some research I think that this is what is happening:

Using fillRect() function instead of lineTo()

If you don’t like adding those 0.5 to any coordinate when using the lineT0() API then you can actually use the drawing rectangle API. As you probably already guessed, the trick is to draw a rectangle of one pixel for one dimension and the length you need for the other one. So here is the script for drawing 5 horizontal lines:

function draw() {
    var context, i, y;

    context = document.getElementById('canvas').getContext('2d');
    y = 20;

    for (i = 0; i < 5; i++) {
       context.fillRect(0, 10 + y, 450, 1);
       y += 10;
    }
}

And here is the result:

If you are wondering about performance differences between lineTo() and fillRect() then you shouldn’t. fillRect() is probably even faster than lineTo().

You can see here a page that illustrates the differences between lineTo() and fillRect() when using integer coordinates.

Debugging Web Pages and PhoneGap apps on mobile devices

If you are a web developer chances are that Google Chrome Developer Tools, Safari Web Inspector, or a Firefox equivalent are your best friends when it comes to debugging web pages (check CSS styles, verify generated HTML, or debug JavaScript). Isn’t that great? I mean not so long ago we used to debug JavaScript using alert() calls.

Once you get used to these tools it is hard not to have them. And this is exactly what happens when you move to mobile web development. As you probably know there is no web inspector running on your iOS device browser.

How do you debug your web apps when running on mobile devices? The best answer to this question is using weinre (if you know a better way, please let me know).

Read more

CoffeeScript or JavaScript?

Recently I played with CoffeeScript a bit. Friends were talking about CoffeeScript and also I read couple of interesting blog posts. Based on my limited experience with it here is my understanding of what is cool and not so cool.

Cool:

  1. It is much more compact than JavaScript. This translates to up to 1/3 less lines of code than the equivalent JavaScript
  2. Offers the concept of classes – something that is familiar for people coming from the Java world
  3. Insulates you from JavaScript pitfalls like global scope
  4. Some times you can get a performance boost – hard to tell when and by how much
  5. Some compile-time error checking, which helps you to write code with fewer bugs right off the bat

Not so cool:

  1. Hard to debug. You have to debug the generated JavaScript code and from there you have to trace back to the CoffeeScript code
  2. Writing less code can actually be something bad. Anyone remember Perl? And how hard it can be to read Perl code?
  3. Adds another layer of abstraction on top of the browser stack. Adding to this jQuery or other frameworks and you can easily be in a situation where you can put together reasonably complex apps but you have no idea of what is happening under the hood nor how to fix a bug that originates from the frameworks you used
  4. While I understand from why is done the way it was, I still think it would have been nice to have optional data typing

Maybe not so surprisingly , I’ve seen people coming from OOP languages (Java or C#) that totally love CoffeeScript and this is their preferred way to write JavaScript apps.

As I said, I just played with it. So if I misunderstood something or completely missed a point please let me know.

Switch to our mobile site