The source code is actually a direct port of my AS3 String Avoider version, so I will only comment the lines with actually show something different than the original example.
First, let’s see what we are going to do:
As usual, move the string with the mouse and avoid the maze.
Here is the source code:
And now let’s see the interesting lines of this script:
Line 35: creation of the bitmap data which will be used to draw the maze
Line 36: finally we draw the maze. Actually, we just paste it from the image loaded at line 24
Line 37: updating the bitmap data. This is very important! Since the bitmap content changed by drawing the maze, it needs to be updated in order to have pixel collision working properly.
Line 60: the core of the game: checking collisions by looking at the pixel under the string to see if it’s transparent.
There is a lot to say about it: first, you must use integer coordinates (that’s why I rounded string values).
Second, it does not work on my iPhone, both using Safari and Chrome. This is really a pity because on my iPhone 5 string movement looks really smooth and could be used for a game, but I wasn’t able to find a way to have pixel color check to work.
Hope someone will found a fix to make it work on iOS browsers.
Meanwhile, download the source code with all libraries included.