Bubblemark Test for HTML5 Canvas

bubble

This is a Bubblemark test clone which I’m developing to test canvas capabilities.
You can see a live example here (requires a canvas compatible browser - excanvas won’t support this).

JavaScript libraries used:
jQuery
Doodle.js
It also uses part of one of the Doodle.js example code for basic bounce physics.

  1. // Initialize when everything on the page is loaded
  2. $(document).ready(function() {
  3.  var canvas = document.getElementById("mycanvas");
  4.  //check for support here:
  5.  if (canvas.getContext){
  6.   init();
  7.  }
  8.  else {
  9.   //javascript fallback
  10.   alert( "Sorry, your browser does not support the canvas element." );
  11.  }
  12. });
  13.  
  14. // Start the application
  15. function init() {
  16.  (function(oo) {
  17.   oo.canvas('#mycanvas');  
  18.  
  19.   //settings
  20.   var numballs;
  21.   var radius = 26;
  22.   var balls = [];
  23.  
  24.   function resetBalls() {
  25.    //reset balls array
  26.    balls.length = 0;
  27.  
  28.    //get new number of balls from user input
  29.    numballs = $( '#numballs' ).val();
  30.  
  31.    for(var i = 0; i < numballs; i++) {
  32.     /**
  33.      * - Create each new ball as an image shape keeping
  34.      * the circle properties for collision detection.
  35.      * - Set coordinates and radius.
  36.      * - Randomize starting velocity
  37.      * - IMPORTANT: For image shapes always define visibility
  38.      */
  39.     var ball = oo.image({src:'./resources/ball.png',
  40.           x: i * 100,
  41.           y: i * 50,
  42.           radius:radius,
  43.           vx: Math.random() * 10 - 5,
  44.           vy: Math.random() * 10 - 5,
  45.           mass:radius,
  46.           coord:'local',
  47.           visible:true});
  48.     //push each ball into the balls array
  49.     balls.push(ball);
  50.    }
  51.   }
  52.  
  53.   resetBalls();
  54.  
  55.   //setting the invisible box boundaries (in this case the canvas)
  56.   Bouncy.boundries = {
  57.    left: 0 - radius, // HACK: boundaries correction for image shapes
  58.    right: oo.canvas().width - radius,
  59.    top: 0 - radius,
  60.    bottom: oo.canvas().height - radius,
  61.   };
  62.  
  63.   // Change the number of balls on button click event
  64.   $( '#numBtn' ).click( function() {
  65.    resetBalls();
  66.   });
  67.  
  68.   //Animate is an endless loop
  69.   oo.animate(function() {
  70.    // check for wall collision
  71.    for(var i = 0; i < numballs; i++) {
  72.     var ball = balls[i];
  73.     ball.modify({x: ball.x + ball.vx,
  74.         y: ball.y + ball.vy});
  75.     Bouncy.check_walls(ball);
  76.    }
  77.    //for each ball…
  78.    for(i = 0; i < numballs; i++) {
  79.     var ball_a = balls[i];
  80.  
  81.     //check for collisions with the current ball
  82.     for(var j = i + 1; j < numballs; j++) {
  83.      var ball_b = balls[j];
  84.      Bouncy.check_collision(ball_a, ball_b);
  85.     }
  86.  
  87.     //redraw the balls
  88.     balls[i].draw();
  89.    }
  90.  
  91.   //set "frames" and make sure to clear canvas
  92.   }, '40fps', true);
  93.  
  94.  })($doodle);
  95. }

Juggling routines

While I was searching for juggling videos on youtube, I stepped into this amazing 75-year-old show of W.C. Fields. It’s not a pure juggling routine, but most would agree that juggling and comedy are surely a great combination. Old tricks? - still pretty exciting and a source of inspiration. It’s a great exhibition of how one should perform on stage and always maintain the audience’s attention. Even though routines and audiences change, the general ideas behind successful performing still stay more or less the same. Quick, natural changes between tricks, small pauses, program variety.
Enjoy :)

PHP Flood Fill

Flood Filler is a small program in PHP demonstrating the Flood Fill algorithm in a simple way.

My implementation of the same algorithm for a game map generator in C# and ASP.NET

My implementation of the same algorithm for a game map generator in C# and ASP.NET

Here’s the code:

  1. /*
  2.  * @title:  Flood Fill Class
  3.  * See below class for example input.
  4.  */
  5. class FloodFiller {
  6.  
  7.     private $x, $y, $fill, $searchNext, $map;
  8.  
  9.     public function Scan( $map, $point ) {
  10.         // We create the list of traversable squares(fill)
  11.         // and a to-search queue(searchNext[])
  12.         // where we insert our starting point.
  13.         $this->map          = $map;
  14.         $this->fill         = array();
  15.         $this->searchNext   = array();
  16.         $this->searchNext[] = array('x' => $point[ 'x' ], 'y' => $point[ 'y' ]);
  17.  
  18.         // As long as there are items in the queue
  19.         // keep filling!
  20.         while ( !empty( $this->searchNext ) ) {
  21.  
  22.             // Get the next square item and erase it from the list  
  23.             $next = array_pop( $this->searchNext );
  24.             $this->x = $next[ 'x' ];
  25.             $this->y = $next[ 'y' ];
  26.  
  27.             // Check square. If it's traversable we add
  28.             // the square to our fill list and we turn the
  29.             // square untraversable to prevent future checking.
  30.             if ( $this->map[ $this->x ][ $this->y ] == 1 ) {
  31.                 $this->map[ $this->x ][ $this->y ] = 0;
  32.                 $this->fill[] = array( 'x' => $this->x, 'y' => $this->y );
  33.                 $this->CheckDirections();
  34.  
  35.             }
  36.         }
  37.         return $this->fill;
  38.     }
  39.     private function CheckSquare( $checkX, $checkY ) {
  40.         // if we can fill this square we add it to our queue
  41.         if ( $this->map[ $checkX ][ $checkY ] == 1 ) {
  42.             $this->searchNext[] = array( 'x' => $checkX, 'y' => $checkY );    
  43.         }
  44.     }
  45.     private function CheckDirections() {
  46.         // Perform a check of all adjacent squares
  47.         $this->CheckSquare( $this->x, $this->y - 1 );
  48.         $this->CheckSquare( $this->x, $this->y + 1 );
  49.         $this->CheckSquare( $this->x - 1, $this->y );
  50.         $this->CheckSquare( $this->x + 1, $this->y );
  51.     }
  52. }
  53.  
  54. // Example Input:
  55. // 6×6 map
  56. // 1: valley
  57. // 0: mountain/sea
  58. // Starting Point: 3.3
  59. $map = array(
  60.                //0  1  2  3  4  5
  61.     array( /*0*/ 1, 0, 1, 0, 1, 0 ),
  62.     array( /*1*/ 0, 1, 1, 0, 1, 0 ),
  63.     array( /*2*/ 1, 1, 1, 1, 0, 0 ),
  64.     array( /*3*/ 1, 1, 0, 1, 1, 1 ),
  65.     array( /*4*/ 0, 0, 1, 1, 1, 0 ),
  66.     array( /*5*/ 1, 0, 1, 0, 0, 0 )
  67.     );
  68.  
  69. // Starting Point: 3.3
  70. $point = array ( 'x' => 3, 'y' => 3 );
  71.  
  72. // new class instance
  73. $floodFiller = New FloodFiller();
  74.  
  75. // Print the final list of filled coordinates
  76. print_r( $floodFiller->Scan( $map, $point ) );

FOSDEM 2009

Fosdem 2009
Last year I didn’t manage to visit FOSDEM, though I’ve been told it was amazing. So, this year during the weekend of 7th and 8th February I get to have a second chance :)

You can see some photos from the last year’s event here (from the ones that did go, featuring the Kamibu team :P).

Accommodation in Brussels is pretty cheap if you manage to make a reservation early. You can even find cheap rooms that also provide a connection to the internet. But I don’t think anyone would wish to stay in their room for long, there are many conferences to attend to, many interesting things to hear and learn, events to participate in, and of course a beautiful city to visit.

I really can’t wait!

Dynamically Generated Greasemonkey Scripts

I recently made a Greasemonkey Script which I intended to use in some project of mine. It took me quite a while to write and debug ( damn css developers who use “!important” anywhere they do not know how to make things work correctly  :P ).

My script contained a couple of configuration variables, that needed to be reconfigured depending on the machine the script would be used on. Considering the specific network’s users as potential idiots, I decided that it would be better if each machine’s administrator could just log-in, submit a simple form and receive the script ready for use. It would also be much more simple in case there was need for an update (no re-configuration needed separately on each machine).

I though that instead of creating a different file each time a user requested the script it would be better to try sending the output of the PHP script directly, by adding the correct headers. No luck.

What I leant after asking the kind user’s of #greasemonkey :

Greasemonkey doesn’t read a file’s headers or contents in order to identify a script. So even if you change the output headers to a different content-type or content-disposition, greasemonkey won’t open the file for you.

All it reads is the file’s extension “.user.js”

Conclusion:

add

  1. header( 'Content-Type: text/javascript' );
  2. header( 'Content-Disposition: inline; filename="filename.user.js"' );
  3. //change "filename.user.js" to the desired filename
  4. echo $file_contents; //your output here</strong>

to your php script

and make sure that the link to the script is something similar to

gm_script.php?.user.js

alternatively you can use mod_rewrite to rewrite .user.js to .php (a much cleaner solution if you have the time to do that).

the monkey

SVN repository on root directory - “out of date”

As nice as it sounds, you cannot have a repository on the root folder. There seems to be a problem with Apache and its virtual folders. If you insist to do so, you will most likely receive error messages  when commiting:

svn: The version resource does not correspond to the resource within the transaction. Either the requested version resource is out of date (needs to be updated), or the requested version resource is newer than the transaction root (restart the commit).

Do not fall into the trap of deleting and re-adding the files, as it will only temporarily solve the problem.

In fact this problem occurs only on files found at the root directory, the second time you commit a modification on the same file.

If that’s the case then the solution is to move your repository to another directory, for ex. to http://youraddress.com/svn/rep

I hope this helps someone.

Happy Coding :)

Happy 2008

Such an elegant dog...

Nom d’un chien! Dehors! Vite! (what’s that, mud on your nose again? )

It beats the kind doggies in “I’m legend” for sure.

Wikiquote:  Among all the other things, studying also brings paranoia.

Decadence

Bury me...

Bury me...

Irony is just a kind of iron melting technique.

Non Compatible

I just couldn't resist...

I just couldn't resist...

Why I supported New Zealand

Hakaaaaaaa!!!

Hakaaaaaaa!!!

Today the All-Blacks have been defeated by the french rugby national team, 18-20.

But they will always win in style.