Saturday, February 26, 2011

Sencha Touch iPad Signature JavaScript

We'll be extending an Ext.Panel object from Sencha Touch to create an iPad signature pad. When you click submit it will tie in to the server-side PHP script in this post. It works pretty well, though there are many ways that it could be enhanced.

We are using the Sencha Touch MVC style approach outlined here:

Go ahead and try it out here (iPad/Safari/Chrome/Webkit only):

Save a Base64 Encoded Canvas image to a png file using PHP

This and the next post will demonstrate how to draw on a canvas and then save the file on a server using PHP. Here is the very simple server side code:

The next post will be the slightly more complicated front-end code. This was particularly designed for a Sencha Touch iPad app that will allow people to sign with their finger. It has some flaws, but the basics are pretty reusable and good!

Your result may look something like this:

Tuesday, February 8, 2011

Timing the EcmaScript 5 function

While looking at this awesome list of browser support for EcmaScript 5 features. I decided to time the difference between using, a static method to access the current timestamp, and new Date().getTime(), the previous way to get a current timestamp in JavasScript.

The results are this: As expected, the static method wins!

The testing code can be found here: (I have no idea how long this will stay up):

Or here:

Here are the results running in Chrome 9:

Testing 10000000 times
Testing new Date().getTime() 10000000 times

Here are the results running in FireFox 3.6:

Testing 10000000 times
Testing new Date().getTime() 10000000 times

Here are the reuslts running in FireFox 4:

Testing 10000000 times
Testing new Date().getTime() 10000000 times

Here are the results running in Safari 5:

Testing '' 10,000,000 times
Testing 'new Date().getTime()' 10,000,000 times

Saturday, February 5, 2011

Drawing in the lines

So I'm working on this really sweet HTML5 monster game, where you get to draw your own 8-bit style monsters and fight them against one another. So far I've only got a tiny bit of the monster creation part up and running. Don't worry I'll throw the whole thing up on on github when I'm done.

Anyway, one of the problems I ran into with the canvas was how to keep people's drawings within the predefined lines. The canvas is 500px wide and it's a 20x20 square of 25px blocks.

To create the lines it's very simple.

 ctx = document.getElementById('monster').getContext('2d');

 ctx.fillStyle = "black";
 for (i = 25; i < 500; i = i + 25) {
    ctx.fillRect (i, 0, 1, 500);
    ctx.fillRect (0, i, 500, 1);

       That will draw some simple lines every 25px. Now how about keeping between the lines. It's simple. We just need to round our x, y to the nearest 25px, like so, that way it will always start inside one of our blocks. Then we'll actually be drawing 24px blocks to fit inside the lines completely.

I thought it was pretty neat!

        var draw = false;

 window.onmousedown(function() {
  draw = true;

 window.onmouseup(function() {
  draw = false;

 document.getElementById('monster').onmousemove(function(e) {
  var x = e.clientX - offsetX,
   y = e.clientY - offsetY;
   x = Math.round(x / 25) * 25 + 1;
   y = Math.round(y / 25) * 25 + 1;
  if (draw) ctx.fillRect(x, y, 24, 24); // keep things in the lines

p.s. follow the progress over at

Tuesday, February 1, 2011

iScroll 3.0 Select Box Hacks

iScroll is pretty neat. It was created by cubiq as an attempt to create a workaround for position: fixed on iOS. Sencha Touch has a much more complete solution, but as a simple solution iScroll works fairly well. 

This could be a great time to rant about how mobile safari is the new IE6, and how having to use JavaScript to fix issues that have already been fixed on the web for a long time is ridiculous. (Seriously, just let position: fixed work as long as the viewport and zoom-levels are set properly.) 

Check out the code here:

Anyway, look no further than iScroll.....unless you want to use a select box in the scroller.

I followed this thread about the problem here:

And finally found a solution here:

Here is my select box:

And it all seems pretty good with one additional problem. It seems that the screen gets nudged down just a little bit when you select things, so I had to add this one bit of JavaScript to bring it back to the top after the we choose our item from the select box.

document.getElementById("showing").addEventListener("change", scrollToTop);

function scrollToTop() {

The result looks something like this (everything under the sort by stuff scrolls):