Activate your free membership today | Log-in

Tuesday, November 10th, 2009

Closure Graphics Fun

Category: Closure

<p>Stephen Thomas has been experimenting with Closure and the graphics libraries:

This does something similiar to what I do on Stark, my visual mud project [github page] that uses the Processing JS library and the HTML 5 <canvas> element, but instead of creating a draw function that is executed x amounts per seconds, you just call a draw function and assign it to a variable for future interactions. Here is the source code for this example (you can also just look at the current page's source):

JAVASCRIPT:
  1.  
  2.     var graphics = goog.graphics.createGraphics(200, 150);
  3.  
  4.     // define the colors for the squares and the dot
  5.     var square_fill = new goog.graphics.SolidFill('yellow');
  6.     var square_stroke = new goog.graphics.Stroke(2, 'green');
  7.     var dot_fill = new goog.graphics.SolidFill('blue');
  8.     var dot_stroke = new goog.graphics.Stroke(1, 'black');
  9.  
  10.     // the dot's initial position
  11.     var dot = {x: 1, y: 1};
  12.  
  13.     // properties   
  14.     var size = 40;
  15.     var margin = 5;
  16.     var width = size - margin;
  17.     var num_rows = 3;
  18.     var num_cols = 4;
  19.  
  20.     // draw the squares
  21.     for (var x = 0; x <num_cols; x++) {
  22.         for (var y = 0; y <num_rows; y++) {
  23.             graphics.drawRect(margin + x * size, margin + y * size, width, width, square_stroke, square_fill);
  24.         }
  25.     }
  26.  
  27.     // draw the dot
  28.     dot['graphic'] = graphics.drawEllipse(margin + dot['x'] * size + width / 2, margin + dot['y'] * size + width / 2, width / 4, width / 4, dot_stroke, dot_fill);
  29.    
  30.     // call if the dot's position changes
  31.     redraw_dot = function() {
  32.         dot['graphic'].setCenter(margin + dot['x'] * size + width / 2, margin + dot['y'] * size + width / 2);
  33.     }
  34.    
  35.     // key event handler
  36.     var key_handler = new goog.events.KeyHandler(document);
  37.     var key_event = function (e) {
  38.         if (e.keyCode == goog.events.KeyCodes.UP && dot['y']> 0) {
  39.             dot['y'] -= 1;
  40.         } else if (e.keyCode == goog.events.KeyCodes.RIGHT && dot['x'] <= num_cols - 2) {
  41.             dot['x'] += 1;
  42.         } else if (e.keyCode == goog.events.KeyCodes.DOWN && dot['y'] <= num_rows - 2) {
  43.             dot['y'] += 1;
  44.         } else if (e.keyCode == goog.events.KeyCodes.LEFT && dot['x']> 0) {
  45.             dot['x'] -= 1;
  46.         }
  47.         redraw_dot();
  48.     }
  49.    
  50.     // put everything together
  51.     goog.events.listen(key_handler, 'key', key_event);
  52.     graphics.render(document.getElementById('shapes'));
  53.  

Related Content:

  • Bringing closure to Java
    The issue of writing closure for Java programs is still debatable. Three authors offer a way to achieve "closure without complexity," but it sparks a...
  • AVL
    Video fun...
  • FUN
    Description ( Functions ) file...
  • FUN
    Unknown Apple II...
  • FUN
    Koolmoves...

Posted by Dion Almaer at 6:45 am
5 Comments

+----
1.4 rating from 66 votes

5 Comments »

Comments feed TrackBack URI

Just to show comparison I repeated the same demo with Raphaël & jQuery: http://tr.im/notclosure

Result:
Google Closure Demo: JavaScript: 795 Kb, 57(!) http requests
Raphaël/jQuery Demo: JavaScript: 238 Kb, 2 http requests

Comment by DmitryBaranovskiy — November 10, 2009

Dimitry:

That hardly seems reasonable. As with Dojo, Closure is meant to be used with the build tools that it comes with. If you make a build, you should be able to get it down to only a couple of requests and only the code that actually executes (thanks to the Closure compiler’s dead code stripping).

Regards

Comment by slightlyoff — November 10, 2009

@slightlyoff, Well, talking without showing is hardly convincible. Make a small demo that proves your point. :)

Comment by DmitryBaranovskiy — November 10, 2009

@DmitryBaranovskiy, bravo.

This closure example seems like its really just to demonstrate the API. Otherwise it’s like using a dump truck to move a grain of sand.

Comment by fernmicro — November 15, 2009

@DmitryBaranovskiy your ignorance is amusing. you want proof? here you go! http://www.nwhite.net/lab/closure/

Google Closure Demo: Javascript 12kb, 1 http request

Suddenly your solution doesn’t look all that attractive. The build process is beautiful and very easy to use.

Comment by nwhite — November 16, 2009

Leave a comment

You must be logged in to post a comment.