Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,005
    Thanks
    58
    Thanked 10 Times in 10 Posts

    Question sprite.js advice

    Hi, can someone explain what the first line does this sprite.js js file?

    Code:
    (function() {
      function LoaderProxy() {
        return {
          draw: $.noop,
          fill: $.noop,
          frame: $.noop,
          update: $.noop,
          width: null,
          height: null
        };
      }
      
      function Sprite(image, sourceX, sourceY, width, height) {
        sourceX = sourceX || 0;
        sourceY = sourceY || 0;
        width = width || image.width;
        height = height || image.height;
        
        return {
          draw: function(canvas, x, y) {
            canvas.drawImage(
              image,
              sourceX,
              sourceY,
              width,
              height,
              x,
              y,
              width,
              height
            );
          },
          
          fill: function(canvas, x, y, width, height, repeat) {
            repeat = repeat || "repeat";
            var pattern = canvas.createPattern(image, repeat);
            canvas.fillColor(pattern);
            canvas.fillRect(x, y, width, height);
          },
          
          width: width,
          height: height
        };
      };
      
      Sprite.load = function(url, loadedCallback) {
        var img = new Image();
        var proxy = LoaderProxy();
        
        img.onload = function() {
          var tile = Sprite(this);
          
          $.extend(proxy, tile);
          
          if(loadedCallback) {
            loadedCallback(proxy);
          }
        };
        
        img.src = url;
        
        return proxy;
      };
     
      var spriteImagePath = "images/";
    
      window.Sprite = function(name, callback) {
        return Sprite.load(spriteImagePath + name + ".png", callback);
      };
      window.Sprite.EMPTY = LoaderProxy();
      window.Sprite.load = Sprite.load;
    }());
    Last edited by VIPStephan; 11-21-2012 at 03:42 PM. Reason: corrected code BB tags


  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    It's in fact the first and last line combined that create an immediately executing anonymous function.

    This construct is often used to create a scope outside of the global scope.

  • #3
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,005
    Thanks
    58
    Thanked 10 Times in 10 Posts
    Quote Originally Posted by devnull69 View Post
    It's in fact the first and last line combined that create an immediately executing anonymous function.

    This construct is often used to create a scope outside of the global scope.
    Hi, what is the point of that?


  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    Sometimes you want to avoid "flooding" the global scope so that different scripts do not interfere with each other (for example by using the same names for separate variables that should be global to each script but that should not be used/overwritten by another one).

    With the inner scope you create your own "namespace" for your variables and objects.

  • Users who have thanked devnull69 for this post:

    nikos101 (11-21-2012)

  • #5
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,005
    Thanks
    58
    Thanked 10 Times in 10 Posts
    cool, wonder why does he write

    $.noop instead of
    function (){}


  • #6
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,005
    Thanks
    58
    Thanked 10 Times in 10 Posts
    Any ideas how to convert this code into typescript?


  • #7
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,005
    Thanks
    58
    Thanked 10 Times in 10 Posts
    Also this code is some of the most confusing I have ever seen, what is going on:

    Sprite.load = function(url, loadedCallback) {
    var img = new Image();
    var proxy = LoaderProxy();

    img.onload = function() {
    var tile = Sprite(this);

    $.extend(proxy, tile);

    if(loadedCallback) {
    loadedCallback(proxy);
    }
    };

    img.src = url;

    return proxy;
    };

    var spriteImagePath = "images/";


    window.Sprite = function(name, callback) {
    return Sprite.load(spriteImagePath + name + ".png", callback);
    };



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •