Archive for the ‘Front-end development’ Category

Classical inheritance in JavaScript

August 8th, 2010 by JorisO | No Comments | Filed in Front-end development, JavaScript

JavaScript currently lacks classes in the classical sense, but it can be made to imitate classical class-based inheritance hierarchies with a few simple helper functions.

There’s a lot of different opinions on how JavaScript should be made to imitate classical inheritance patterns. The below is just one method of many.

JavaScript Class

The JavaScript object used to represent a class is basically just a constructor function:

function Animal(name){
  this.name = name;
}

//add a method
Animal.prototype.getName = function(){
  return this.name
}
//create an instance
var animal  = new Animal('Tom');

JavaScript subClass

To now allow a new class to subClass our new JavaScript class we need something that performs similar task to the ‘extends’ functionality in languages as Java and PHP.

We can define a JavaScript ‘extends’ function as follows:

function extends(childClass, parentClass){
  var O = function(){}; //create new object to contain parentClass prototype without instantiating parentClass.
  O.prototype = parentClass.prototype
  childClass.prototype = new O(); //setup prototype chain
  childClass.prototype.constructor = childClass; //reset constructor to childClass

  childClass.parentClass = parentClass.prototype; //provide parentClass attribute for childClass constructor functions
  if(parentClass.prototype.constructor == Object.prototype.constructor){
    parentClass.prototype.constructor = parentClass;
  }
}

Now to create a subclass of Animal :

function Sloth(name,bananas){
   Sloth.parentClass.constructor.call(this,name);
   this.bananas = bananas;
}

extend(Sloth,Animal);
Sloth.prototype.getBananas = function(){
  return this.bananas;
}

Now if we’d want to override yet incorporate a parents method in the childClass we could do :

  Sloth.getName = function (){
    var name = Sloth.parentClass.getName.call(this);
    return name + ' was provided by overridden method';
  }

Tags: ,

JavaScript Object Oriented Programming

June 20th, 2010 by JorisO | 1 Comment | Filed in Front-end development, JavaScript

Practical usage of classical object oriented programming methodology in any scripting or programming language can usually be explained by setting apart the language-specific implementation of the principles of polymorphism, inheritance and encapsulation.

JavaScript is somewhat of a different animal when it comes to object oriented programming. It is one of the few widely spread (or even the most widely spread) fully prototype-based programming languages. Prototype based languages depend on premises different from what most programmers familiar with object oriented programming techniques have come to rely on.

(more…)

Tags: ,

@font-face

June 13th, 2010 by JorisO | No Comments | Filed in CSS3, Front-end development

A big handicap graphic design for the web had to put up with until recently was being limited to a small number of ‘web-safe‘ fonts. These web-safe fonts were the lucky few considered so widespread that the chances of having a visitor that didn’t have them installed were negligible.

The fact that something as primary as the ability to download online fonts was lacking in HTML / CSS for so long made it necessary for third party technology-based hacks (as for instance sIFR) to fill up the gap.

(more…)

Tags: , ,

CSS gradients

June 6th, 2010 by JorisO | No Comments | Filed in CSS3, Faster websites, Front-end development

CSS gradients promise a great leap forward for the graphical capabilities of CSS. A CSS gradient is in fact a browser-generated image, consisting of smooth fades between several colors. Letting CSS create these will decrease download times and allows for many new interesting DHTML possibilities.

Both linear and radial gradients are supported by the new CSS specifications. At the time of writing CSS gradients are supported by Mozilla and Webkit browsers but Internet explorer offers something similar in it’s usual non-compliant manner.
(more…)

Tags: , , , ,

CSS multi column text

May 23rd, 2010 by JorisO | 1 Comment | Filed in CSS3, Front-end development

CSS 3 provides a new collection of column properties that allow you to easily distribute a HTML container’s textual content over multiple vertical columns. At the time of writing this property is supported by Mozilla and Webkit browsers, and -moz / -webkit vendor prefixes are still needed.

(more…)

Tags: ,

CSS Text shadow

May 23rd, 2010 by JorisO | 1 Comment | Filed in CSS3, Front-end development

CSS 3 allows you to add drop shadows to text. The text-shadow property’s syntax doesn’t require vendor specific prefixes. That means this method for adding shadows behind text is finalized.

(more…)

Tags: , ,

Flash alternatives already

April 11th, 2010 by JorisO | No Comments | Filed in Front-end development

Flash (“Adobe Flash” formerly Macromedia Flash, and even longer ago as FutureSplash), has in recent years finally gotten some serious competition. Oddly this hasn’t really happened often yet in Flash’s fifteen years of existence.

(more…)

Tags: , ,

CSS3 rounded corners

April 3rd, 2010 by JorisO | 1 Comment | Filed in CSS3, Front-end development

CSS 3 finally delivers specifications for the rounding of corners. CSS 3 allows you to specify a radius for the corners of an element. This offers the promise of no longer having to use awkward rounded corner / border hacks based on images or CSS / JavaScript for such a simple graphic effect.

At the time of writing Internet Explorer’s latest version (8) still doesn’t support this feature in any way. Also CSS vendor prefixes are still necessary for Mozilla (FireFox) and WebKit (Safari/Chrome) based browsers. Surely the vendor-specific prefixes and syntactical quirks will also disappear once the application of this CSS3 feature becomes more commonplace.

(more…)

Tags: , ,

HTML5 canvas transformations

March 28th, 2010 by JorisO | No Comments | Filed in Canvas, Front-end development

Once the contents of a canvas element have been drawn using the 2d context drawing methods or image loading methods you can start using transformation methods to manipulate the drawing state.

To keep track of drawing states you use the canvas 2d context’s save() and restore() methods.

The save() method pushes a copy of the current drawing state onto the drawing state stack. This stack is simply a storage pile of canvas drawing states that can be returned to by successively calling the restore() method.
(more…)

Tags: , , ,

Using images in HTML5 Canvas

March 22nd, 2010 by JorisO | 1 Comment | Filed in Canvas, Front-end development

The html5 canvas element 2d context allows you to display and manipulate image files through it’s drawImage method. The drawImage method is overloaded to provide three sets of arguments for dealing with images.
(more…)

Tags: , ,