About Us

Bulletpoint StarImulus® is a technology focused design + interactive agency.

In addition to our client services we also have a few products in the works. Our office is always filled with chatter and this blog is an outlet for our creative energy, rants and ideas.

Podium

Stacks!
Imulus built a task management solution that finally works for teams. It's a life saver, learn more at usestacks.com.

Featured Project

Category: tutorials

Jun18

CoffeeScript: Namespaces, Modules and Inheritance

First, some JavaScript background.

Skip to the good stuff

Here at Imulus, we like to namespace our JavaScript by wrapping our classes in static modules. We accomplish this by simply creating an object literal with a variety of members: classes (constructors), constants, variables and static methods.

Here is a pared down example of a simple module:

var Application = {           // the module, a static wrapper

 FXSPEED : 500,               // a constant (sort of)

 user : {                     // a nested static object
   id : 123
 },

 toggle : function(){ ... },  // a static method

 Interface : function(){      // a class

   this.initialize = function(){
     this.active = true;
     this.build();
     return this;
   }

   this.build = function(){
     ...
   }

   this.doSomething = function(){
     return "did something";
   }

   return this.initialize();
 }  

}

The benefits of this technique are obvious; it allows us to have loosely-coupled, modular code that is nicely organized, available without instantiation and protects the global namespace from pollution.

Application.active // true

Application.user.id // 123

var interface = new Application.Interface()
interface.doSomething() // 'did something'

In reality, however, one mammoth object literal in a single file is a lousy way to structure your code. Instead, what we do is organize module members into separate files in a logical fashion, å la Ruby. The module and all of its static members exist in a single file named after the module, and all of the module’s classes exist in a directory named after the module with each class file named after itself without the namespace. By using this structure, files are much easier to find because the naming convention mirrors the the module namespace.

The directory structure ends up looking like this:

|-- scripts
    |-- application.js
    |-- application
        |-- controller.js
        |-- data.js
        |-- interface.js

And the module above would be structured like this:

// scripts/application.js
// creates the wrapper module and contains all static members

var Application = Application || {
  FXSPEED : 500,
  user : {
    id : 123
  },
 toggle : function(){ ... },
}
// scripts/application/interface.js

Application.Interface = function(){
  this.initialize = function(){
    this.active = true;
    this.build();
    return this;
  }

  this.build = function(){
    ...
  }

  this.doSomething = function(){
    return "did something";
  }

  return this.initialize();
}
// scripts/application/controller.js

Application.Controller = function(){
  ...
}

You get the picture. This probably looks familiar to a Rubyist. Here’s the analog:

|-- lib
    |-- application.rb
    |-- application
        |-- interface.rb
# application.rb

module Application
 FXSPEED : 500,             

 def self.user
  { 'id' => 123 }
 end

 def self.toggle
  ...
 end
end
# application/interface.rb

module Application
  class Interface

    attr_accessor :active

    def initialize
      @active = true
      build
    end

    def build
      ...
    end

    def do_something
      'did something'
    end
  end
end

Snakes on a Train

By now it’s probably evident how much we obsess over code structure, organization and namespacing. While experimenting with CoffeeScript, this has been an ongoing concern.

This is the example from the CoffeeScript demo page:

class Animal
  constructor: (@name) ->

  move: (meters) ->
    alert @name + " moved " + meters + "m."

class Snake extends Animal
  move: ->
    alert "Slithering..."
    super 5

class Horse extends Animal
  move: ->
    alert "Galloping..."
    super 45

sam = new Snake "Sammy the Python"
tom = new Horse "Tommy the Palomino"

sam.move()
tom.move()

I love that Coffee has made dealing with prototypes much easier with the ‘class’ and ‘extends’ keywords. This is great, and by itself works like a charm for singular, somewhat global classes. However, for any application with a sizeable codebase this simply isn’t practical without ending up with insane class names.

The ideal solution would be to introduce a ‘module’ keyword similar to Ruby’s.

module Zoo
  class Animal
    constructor: (@name) ->

    move: (meters) ->
      alert @name + " moved " + meters + "m."

bob = new Zoo::Animal "Bob the Bird"
bob.move()

Other people agree. But there are a number of JS limitations that prevent this from being a viable solution in CoffeeScript.

The Really Good Stuff

By its own golden rule, CoffeeScript is: “It’s just JavaScript”. So, we can namespace our CoffeeScript classes in the same manner we organize our modules in vanilla JS.

Extending on the example above (ha-ha, get it?), I’ll demonstrate how to namespace your Coffee classes in a simple, elegant way.

Focusing only on the snakes for now, lets set up our basic classes.

class Animal
  constructor: (@name) ->

  move: (meters) ->
    console.log @name + " moved " + meters + "m."

class Snake extends Animal
  move: ->
    super 5

george = new Snake "George"
george.move() // George moved 5m

Now, we want to create ‘Forest’ and ‘Savanna’ modules that have a habitat and snakes of their own.

animal-diagram

Just like JavaScript, we could wrap all of the module members in an object literal:

Forest =
  habitat: "dense highland dotted with lakes and streams"
  Snake: class extends Snake
    move: ->
      super 10

george = new Forest.Snake "George"
george.move() // George moved 5m.

But we would run into the same problem when the codebase reaches any kind of scale: enormous, single-file object literals. So let’s introduce a new function for creating module members.

module = (name) ->
  global[name] = global[name] or {}

(Yes, I am aware that this is attaching the objects to the global namespace. This is what we want. It allows us to add members to the module later.)

Now we can use the ‘module’ function like this:

module 'Forest'

Forest.habitat = "dense highland dotted with lakes and streams"

Forest.Snake = class extends Snake
 move: ->
   super 10

george = new Forest.Snake "George"
george.move() // George moved 10m.

Pretty elegant, right? Additionally, by using the ‘module’ function we can now organize our code into multiple files:

module = (name) ->
  global[name] = global[name] or {}

# src/animals.coffee
class Animal
  constructor: (@name) ->

  move: (meters) ->
    console.log @name + " moved " + meters + "m."

class Snake extends Animal
  move: (meters) ->
    super meters

# src/forest/snake.coffee
module 'Forest'

Forest.habitat = "dense highland dotted with lakes and streams"

Forest.Snake = class extends Snake
  move: (meters)->
    @distance = meters || 5
    super @distance

# src/savanna/snake.coffee
module 'Savanna'

Savanna.habitat = "grassy woodland small or widely spaced trees"

Savanna.Snake = class extends Snake
  move: (meters)->
    @distance = meters || 10
    super @distance

george = new Forest.Snake "George"
george.move() // George moved 5m

scott = new Savanna.Snake "Scott"
scott.move() // Scott moved 10m

We can also extend standalone classes with module classes:

class Python extends Savanna.Snake
  constructor: ->
    super
    console.log "I am #{@name} the python."
    console.log "I live in the savanna,
    where it is #{Savanna.habitat}."

class Cobra extends Forest.Snake
  constructor: ->
    super
    console.log "I am #{@name} the cobra."
    console.log "I live in the Forest,
      where it is #{Forest.habitat}."

john = new Python "John"
// I am John the python.
// I live in the savanna,
// where it is grassy woodland small or widely spaced trees.

bruce = new Cobra "Bruce"
// I am Bruce the cobra.
// I live in the forest,
// where it is dense highland dotted with lakes and streams.

May21

3rd Thursday HTML5 Presentation & Links

Thanks to all of those who attended the Imulus 3rd Thursday presentation on HTML5 last night. We had some great discussion and topic review. Today we’ve placed the presentation website live at http://html5.imulus.com.

html5

Here are the additional links that got mentioned during the meetup.

This should be everything that was covered, however, if you find something missing feel free to add a comment with a link. We’ll jump on it ASAP.

Updates: A few more HTML5 resources in the list.

Sep7

Tip: Propagate DNS in Minutes not Hours

For those responsible with switching sites from one server to another there’s a sure-fire way to quick propagation. It’s done by lowering the TTL (time-to-live) settings from a typical default of either an hour or day, down to 5 minute intervals. I’ll show you how.

First gain entry into you DNS server. In this example I’m using GoDaddy’s Total DNS Control (even though the UI is terrible).

3

Click on Advanced Mode, change the dropdown for TTL to 1/2 hour. This is the lowest GoDaddy goes but other interfaces, like Probind allow you to drop the intervals by manually entering an number. When finished click OK and you’re done.

2

I suggest starting the process about 1 week before the site is ready to go live. That gives servers across the web ample time to update their caches in advance of your update. Make the IP change when you are ready to update the server IP and you’ll notice a much quicker transition.

BONUS TIP: Also, we HIGHLY recommend using the free OpenDNS service. This system is a savior for giving us almost instantaneous ability to view DNS changes the moment they happen.

Jun10

Keeping up to day with TextMate tips

TextMate rules!My long time friend Erik Reagan (@ErikReagan) runs a website by the name of TextMate Tips. It’s a useful resource for keeping up to date with interesting TextMate hints, shortcuts, and utilities. Further, the site is open for contribution and is always on the look out for new and interesting TextMate uses.

If your a fan of TextMate you should definitely check it out and maybe give it a bookmark if you’re feeling extra generous.

Apr27

Design 102: Receding Reflection Tutorial

For a more basic reflection tutorial find it here. To start, paste your image into a new Photoshop file. Be sure to leave some room at the bottom for your reflection.

step1

To make the photo appear to be receding back, transform the top layer by selecting Edit > Transform > Perspective.

step2

Select one of the corners and drag up enough to make the photo look like it is receding into the background (don’t overdo it) Once you’re done, hit RETURN.

step3

In order to complete the perspective, you need to scale the photo so it doesn’t look stretched. Select Edit > Transform > Scale.

step4

Select one of the side handles and drag over enough to make the photo look like it has maintained the original proportions (once again, don’t overdo it) Once you’re done, hit RETURN.

step5

Next, duplicate the layer your image is on by right clicking the layer and select: Duplicate Layer.

step6

Now select the bottom layer, and under edit > transform> Flip Vertical.

step7

Use the selection tool and move the image down so that the bottom corner of the top layer is touching the top corner of the bottom layer.

step8

With the bottom layer still selected, transform the bottom layer to match the edges of the top layer. Choose Edit > Transform > Perspective. Select the side handle and drag it up till the edges are touching the top layer.

step9

Still using that same bottom layer, create a new layer mask.

step10

Select the gradient tool, and make sure that your foreground color is black and your background color is white. In the gradient options, select linear foreground to background.

step11

Make sure you still have the mask layer selected (you will see lines bordering the corners). Hold down shift and apply a gradient from the bottom of the canvas to the top of the bottom image (make sure you do this in the middle of the canvas.

step12

Apply an opacity to your bottom layer (and crop the canvas if you’d like), and you’re done!

step13

More reflection tutorials to come. Stay tuned.