Ext.util.SpriteManager
Ext Components, JavaScript 3 Comments »One of the ways of improving web page performance (especially in RIA - Rich Internet Applications) where there are lots of small images that need to be displayed, e.g. on a toolbar, is to create a compound image and then use css to select a section of the image to display. In the javascript world this is known as using sprites. In the world of C#/vb/Delphi this is well known and there have always been ImageList components that store images in this form.
So instead of creating single images : ![]()
we create a composite image e.g.
(have added a second row as well)
To make the process of assigning the correct css style to select on of the images to display, I have created a SpriteManager addition to Ext JS as follows:
Ext.namespace('Ext', 'Ext.util');
Ext.util.SpriteManager = function(config){
Ext.apply(this,config,{
spriteSource:true,
spriteWidth:1,
spriteHeight:1,
spriteColumns:1,
spriteRows:1
});
};
Ext.util.SpriteManager.prototype = {
/**
* Gets the CSS Style for the appropriate image
* @param {Object...} spriteLocation Expects an object with an X and Y location of the sprite to display
* @return {String} returns the CSS style string enclosed in braces {}
*/
getSpriteCSS : function(spriteLocation){
var locationX = spriteLocation.x-1 || 0;
var locationY = spriteLocation.y-1 || 0;
var spriteCss = ['{'];
var spriteW = this.spriteWidth / this.spriteColumns;
var spriteH = this.spriteHeight / this.spriteRows;
var spriteXPos = locationX * spriteW;
var spriteYPos = locationY * spriteH;
if(!typeof this.spriteSource =="boolean")
{
spriteCss.push('background:url(' + this.spriteSource +');');
}
spriteCss.push('height:' + spriteH + 'px;');
spriteCss.push('width:' + spriteW + 'px;');
spriteCss.push('background-position:-' + spriteXPos + 'px -' + spriteYPos + 'px;');
spriteCss.push('}');
return spriteCss.join("");
},
/**
* Applies the CSS Style for the appropriate sprite image to an element
* @param {element} Ext.Element to apply the sprite style to.
* @param {Object...} spriteLocation Expects an object with an X and Y location of the sprite to display
*/
applySpriteCss : function(el, spriteLocation)
{
var css = this.getSpriteCSS(spriteLocation);
el.applyStyles(css);
}
};
Here is an example of the usage:
<script type="text/javascript">
var spriteManager = new Ext.util.SpriteManager({
spriteSource:"sprites.png",
spriteWidth:64,
spriteHeight:34,
spriteColumns:4,
spriteRows:2
});
</script>
...
<img id="test" src="images/default/s.gif" />
<a href="javascript:spriteManager.applySpriteCss(Ext.get('test'),{x:1,y:1});">Image 1,1</a>
<a href="javascript:spriteManager.applySpriteCss(Ext.get('test'),{x:2,y:1});">Image 2,1</a>
<a href="javascript:spriteManager.applySpriteCss(Ext.get('test'),{x:1,y:2});">Image 1,2</a>
<a href="javascript:spriteManager.applySpriteCss(Ext.get('test'),{x:2,y:2});">Image 2,2</a>
** Updated 2/5/2007 - spriteSource does not now need to be supplied if the element already has a css style specifying the background image.
Recent Comments