Unexpected behaviour in console.log()

In web development, I am constantly printing things to the console in order to help with debugging and pin point problems.

However today I have found a “gotcha” with console.log() that tripped me off for several hours and I couldn’t find anything on the net about it so hopefully this helps someone.

The problem is that when printing arrays to the console, it doesn’t necessarily show the live value of the array at the time of calling.
You could print an object or array to the console, and then change one of its value after printing it, and console.log() will print the changed value not the value before printing.

Here’s an example

var my_array = [111];
console.log(my_array);
my_array[1] = 222;

This prints:

[111, 222]

When I was expecting it to print:

[111]

This means that console.log() doesn’t always show the “live” representation of the real values at the time of the call.

However if I access a property on the array it will show the live values.

Example:

var my_array = [111];
console.log(my_array.length, my_array[1]);
my_array[1] = 222;

This prints as expected:

1, undefined

This is hugely annoying if you aren’t aware of this, (Or in my case I lost an entire afternoon) :)

Comments { 0 }

Export Facebook Photo Albums for Google Plus

Now that Google Plus (+)? has been taking off and slowly gaining traction around the globe a lot of people are thinking how am I going to get my Facebook albums and photos onto Google Plus??

There’s a quick and easy solution if you have a Mac.

Go here and click “View in Mac App Store”: http://itunes.apple.com/us/app/photofetch-facebook-photo/id412389506?mt=12

It’s $1.99 but well worth it for saving you all that time downloading each individual photo manually!
It’s quick and simple and does the job.

Load up the program, sign into Facebook and then click on the albums you want to download and click “Save to a Folder…”.
From there you will be able to upload all the photos to Google Plus very simply and easily.

Here are some reviews from the Mac App Store:

  • DaLoveMachine – “I downloaded this to update my google+ account. Worked like a charm and saved me alot of time.”
  • Daniel Goldman – “I just got invited to Google+. One of the things I really wanted to take with me to Google+ was all my photos. There were a bunch of them! Well I guess I could have gone to each of my albums and “saved as”… but then I would be doing that for a full day or two. This app did it for me in under five minutes. It pulled the album and saved them all to a tidy folder of my choosing. I could have pushed them to IPhoto if I wanted, but I just want to move them over to Google+. I paid two bucks. Even if I never, ever use this again (I probably wont), it was still totally worth the measly two bucks for me. Heck, if I comensated myself for the full workday of individually downloading each image I would have spent waaaay more than two bucks, eh?… Thanks dev for a simple solution to what could have been a very time-consuming set of tasks. I hope you have great success with all the people wanting to go over to Google+ WITH their photos from FaceBook!”
Comments { 0 }

Using Google Maps to show a streetview of a house based on an address

Everyone loves the streetview service that Google maps provides. Naturally the Google API provides the ability to embed streetview within a web application if you feed it the right data and co-ordinates.

What it doesn’t do naturally and easily is automatically point the streetview camera an address.
This is something that I thought would be simple but was actually more difficult than it sounds.

Although it is fairly simple to create a streetview panorama at a given GPS point, what is more complicated is to ensure that the streetview camera is pointing in the correct direction (point of view, or yaw).

Here is the script I wrote (based on some other things i’ve found in various places) to calculate the direction / yaw / point of view of the panorama so that it always points in the correct direction when you give it some co-ordinates.
This assumes you have 2 divs:

<div id="map_canvas"></div>
<div id="pano"></div>

And that you are using the Google Maps API V3

You can then simply pass a text address to:

load_map_and_street_view_from_address('25 some rd, someville, Some Country'); 

Script below:

function load_map_and_street_view_from_address(address) {

          var geocoder = new google.maps.Geocoder();
	  geocoder.geocode( { 'address': address}, function(results, status) {
	    if (status == google.maps.GeocoderStatus.OK) {
	        var gps = results[0].geometry.location;
		create_map_and_streetview(gps.lat(), gps.lng(), 'map_canvas', 'pano');
	    }
	});
  }

    var map;
    var myPano;
    var panorama;
    var houseMarker;
    var addLatLng;
    var panoOptions;
    function create_map_and_streetview(lat, lng, map_id, street_view_id) {
	var googlePos = new google.maps.LatLng(lat,lng);

	panorama = new google.maps.StreetViewPanorama(document.getElementById("pano"));
	addLatLng = new google.maps.LatLng(lat,lng);
	var service = new google.maps.StreetViewService();
	service.getPanoramaByLocation(addLatLng, 50, showPanoData);	

    var myOptions = {
	zoom: 14,
	center: addLatLng,
	mapTypeId: google.maps.MapTypeId.ROADMAP,
	backgroundColor: 'transparent',
	streetViewControl: false,
	keyboardShortcuts: false,

    }
      var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
       var marker = new google.maps.Marker({
            map: map,
            position: addLatLng
        });
    }

    function showPanoData(panoData, status) {
      if (status != google.maps.StreetViewStatus.OK) {
	$('#pano').html('No StreetView Picture Available').attr('style', 'text-align:center;font-weight:bold').show();
        return;
      }
    $('#pano').show();
      var angle = computeAngle(addLatLng, panoData.location.latLng);

      var panoOptions = {
	    position: addLatLng,
	    addressControl: false,
	    linksControl: false,
	    panControl: false,
	    zoomControlOptions: {
		style: google.maps.ZoomControlStyle.SMALL
	    },
	    pov: {
		heading: angle,
		pitch: 10,
		zoom: 1
	    },
	    enableCloseButton: false,
	    visible:true
	};

	panorama.setOptions(panoOptions);

    }

    function computeAngle(endLatLng, startLatLng) {
      var DEGREE_PER_RADIAN = 57.2957795;
      var RADIAN_PER_DEGREE = 0.017453;

      var dlat = endLatLng.lat() - startLatLng.lat();
      var dlng = endLatLng.lng() - startLatLng.lng();
      // We multiply dlng with cos(endLat), since the two points are very closeby,
      // so we assume their cos values are approximately equal.
      var yaw = Math.atan2(dlng * Math.cos(endLatLng.lat() * RADIAN_PER_DEGREE), dlat)
             * DEGREE_PER_RADIAN;
      return wrapAngle(yaw);
   }

   function wrapAngle(angle) {
	if (angle >= 360) {
	    angle -= 360;
	} else if (angle < 0) {
	    angle += 360;
	}
	return angle;
    };

Note: some of this syntax uses Jquery, but it should be fairly easily ported to other frameworks or straight JS.

Comments { 3 }

Cross-browser CSS Grab cursors for dragging

For a project I was looking at the best way to implement a grabbing hand cursor for dragging an element that would work well in all browsers.
After many google searches I have collated my findings below and hopefully it helps someone.

Firstly you’ll need some javascript to detect the current browser.
I use Browser Detect which works really well.

If it’s Firefox you can use the “-moz-grabbing” and “-moz-grab” cursors built in.

cursor: -moz-grab

For the other browsers you’ll need to load them from a URL.
You can download the cursors from here:
http://www.google.com/intl/en_ALL/mapfiles/closedhand.cur
http://www.google.com/intl/en_ALL/mapfiles/openhand.cur

the format in css should be:

cursor: url(/closedhand.cur) 4 4, move;

Where the “4 4″ are the co-ordinates, and the “move” is the fall-back cursor if the url fails.

In IE it will fail if you have the x/y co-ordinates, so it needs to be like this:

cursor: url(/closedhand.cur), move;

In Opera it will fail completely if you have any of the above :)
So we just need to ovveride for opera.

cursor: move;

either way the code below should be self explanatory:

var dragCursor;
var curBrowser = BrowserDetect.browser;
// IE doesn't support co-ordinates
var cursCoords = (curBrowser == "Explorer") ? "" : " 4 4"; 

if (dragging) {
	dragCursor = (curBrowser == "Firefox") ? "-moz-grabbing" : "url(/closedhand.cur)" + cursCoords + ", move";
	// Opera doesn't support url cursors and doesn't fall back well...
	if (curBrowser == "Opera") dragCursor = "move";
} else {
	dragCursor = (curBrowser == "Firefox") ? "-moz-grab" : "url(/openhand.cur)" + cursCoords + ", move";
}

document.getElementById('objectToDrag').style.cursor = dragCursor;
Comments { 2 }

Set Go to line number keyboard shortcut in visual studio

This took me longer than it should have to figure out so here you go.

To set a short cut to go to a line number in a document. I am used to pressing Ctrl + G.
If this doesn’t work your keyboard shortcuts might be set to a different setting.

Go to Tools -> Options -> Environment -> Keyboard

Type “edit.goto  in the “Show commands containing box”

Then select “press shortcut keys” and do the shortcut you would like assign eg. Ctrl + G.
Then press “Assign”

You are done.

Comments { 0 }

Backing up your Data


Why do I need a backup?

All hard drives can fail. If a hard drive fails, recovering data is unlikely and expensive. So having a back up of the important data in your computer is essential, particularly those which are irreplaceable such as family photos or work documents.


How do I backup my data?

Most common devices used for a backup are:

  • External Hard Drives are great for general use. You can use them over and over again, they usually have quite a lot of space, and they are good for backing up large amounts of data. Remember that external hard drives can fail too so make sure you keep the copy on your computer.
  • CD’s and DVD’s are also good mediums for backing up data, and are reasonably cheap if you just want to backup a few things like your photo collection, music and documents.
  • USB Flash Drives are great ways of storing small amounts of data, like work documents, etc.
  • A Backup is essentially just a copy of the data you backup, so the simplest way of backing up is just copying the data and pasting it in the device you want to backup to. There is also software that will do this for you automatically.

    Backing up onto a server

    A server is a computer which sole purpose is to hold and give out data. Backing your data onto a server is becoming more popular in homes. Having multiple computers, each one having its own backup can be costly and messy. This is where a server is handy. With a server, all the computers are connected via a network and the backup runs through the network. Servers are also handy for a centralised place for sharing data on a network.

    If you would like to purchase a server please talk a Spanner Box specialist.


    Things to Remember

    To help prevent hard drive failure, it is good to get a UPS (uninterrupted power supply). This is a device that ensures devices plugged into it get clean constant power without spikes or drops and also has a backup battery, in event of a power cut.

    It is best to not bump your computer too much as this can cause hard drive heads to scratch the discs and corrupt data.

    It is also not recommended to turn off your computer at the wall or by the switch. If your hard drive is working and you suddenly cut power to it you can damage it or corrupt data.

    Keeping your hard drive at a good operating temperature helps prevent hard drive failure. Usually a hard drive by itself won’t over heat, but in certain situations hard drives may be hotter than normal. If you are worried about your hard drive over heating it is a good idea to have extra cooling fans installed in your computer.

Comments { 0 }

PC Tips

 These are just some tips to make your PC run fast when it starts going slow

How does the processor make the computer slow?

Your processor is like the brains of your computer it does the calculations.

It”s common for processors to use a large amount of processing power for short periods of time when loading software. That is why the more programs you have programmed to load on start-up, the longer it takes to start up .

Scenario

You have six programs that load at start up, and all require 6 seconds loading time. That is roughly 30 seconds longer than if you had only one program loading at start-up. Generally as a computer gets older the owner will install more programs . Many of these load on start-up because software makers often program their software to load during start-up to try and make people use their software more.


How to turn off start up programs

The easiest way to do this is by using msconfig.exe, a utility that allows you to control a number of settings on your computer.

Turn on “msconfig.exe” in Windows XP

1. Press the “start” button in the bottom left hand corner
2. Select the program “RUN” and turn on.
3. Type in “msconfig” then press enter

To turn on msconfig.exe in Windows Vista

1. Press the “Windows” button in the bottom left hand corner
2. Type in ‘msconfig’ in the search field then press enter

A window should pop up with ‘system configuration’. Select the “Startup” tab

Uncheck the boxes of the programs you don’t want to load during start-up, click the apply button in the bottom right hand corner then ok. The next time the computer starts up it should be a lot faster!

If you don’t know what a program does, don’t turn it off! You can do google searches on programs if you want to know what they do.


How does memory (RAM) usage make the computer slow?

Memory is like short term storage for the computer. Everything that is running is stored in the memory to be used by the processor.

All of your programs use up memory. The more programs you have running at one time, the more memory it will use. However, using up memory won”t actually slow your computer down until you run out of memory.

Scenario

An old computer has 256MB of RAM. This computer is running Windows XP which uses about 200MB of RAM itself. This leaves only 56MB of free RAM. If you open MSN Messenger, Microsoft Word, and Internet Explorer these programs combined will use 157MB. This creates 357MB of RAM, being used by a computer which only has 256MB of Ram.

In this situation the computer will use the Hard drive (long term memory), as replacement for RAM. Since the hard drive runs a lot slower than RAM the computer will start to run slowly.

However, if you have 2048MB (2GB) of RAM and you only use 357MB then your computer will run fine until you use more than 2048MB of ram.

Comments { 0 }

Ugly Test Available on App Store!

Ugly Test is now Available on App Store. Go get it now.
You can get it here

Here is the description:

Have fun pranking your friends with this fun little app.

Place your finger on the scanner to scan your fingerprint to detect your ugliness.

Use the hidden buttons to rig the scanner and always get a perfect ‘non-ugly’ result.

Your friends will never understand why the results come out so abusive every time for them!
Watch them try to figure out how it works and eventually give up and accept their ugliness as a scientific fact!

* Realistic graphical interface
* Real Vibration and scanner-like sound effects
* 2 Secret buttons to rig the results with.
* Long list of Result possibilities.
* Animated cheat instruction screen

Very simple to use and very fun to play with.

Ugly Test iphone App allows you to cheat with hidden buttons

 

Ugly Test iphone App has great realistic scientific animation.

Comments { 0 }

The Ugly Test iPhone App – only 0.99c

We have decided that even though we have put a lot of long hours into The Ugly Test, that we  would release it as a 0.99c app in the app store.

You’ll definitely want to check it out, and now being poor is no longer an excuse.


Comments { 0 }

New iPhone App – The Ugly Test

Jayco Design has just finished its first iPhone application: The Ugly Test.

The Ugly Test Screen Shot

It should be released over the next week or so.

Comments { 0 }