MooTools - ChessPiece Class

by pythondave

HTML

<div id="div1"></div>

JavaScript

var ChessPiece = new Class({
    initialize: function(options){
        if (typeof(options) == 'string') { options = {type:options}; }
        this.type = options.type; //'r': black rook, 'R': white rook, ...
        this.colour = (this.type == this.type.toUpperCase()) ? 'w' : 'b';
        this.imgRoot = 'http://www.chessguru.de/palview/linpcs35/';
        this.imgSrc = this.imgRoot + this.colour + this.type.toLowerCase() + '35.gif';
        this.element = new Element('img', { src: this.imgSrc });
    },
    toElement: function() {
        return this.element;
    }
});

new ChessPiece('r').toElement().inject($('div1'));
new ChessPiece('R').toElement().inject($('div1'));
new ChessPiece('n').toElement().inject($('div1'));