PNGPong
Cross-browser solution for displaying transparent PNG’s in IE, Firefox, and Safari.
Table of Contents
- Issues
- Solution
- Change Log
- Download
- Tested Browsers
- How it Works
- Code Examples
- Method Definitions
- List of Parameters
- Parameter Definitions
Issues
IE versions 5.5 – 6.x has issues handling PNG alpha channels. The solution that is available from Microsoft is to use the AlphaImageLoader filter. Now IE 7 is suppose to support this natively (IE 7 Checklist and IE7 Transparent PNG Implementation), but still uses filters to handle the alpha channel. I feel that the filter solutions are still not very clean since Microsoft’s implementation is something that is not supported in the other major browsers like Firefox and Safari.
Here is a quote from a msdn blog post “IE7 Transparent PNG Implementation” that lists “a couple of limitations worth mentioning”:
- Certain filters may not behave as expected due to how filters are implemented. Filters work by rendering the content of their attached element to their work surface and then modifying that surface before compositing it to the output surface. Because of this, alpha blending of a transparent PNG with non-binary transparency may lead to unexpected results. For example, applying a BasicImage filter with an opacity attribute to an IMG element with a transparent PNG will fill the work surface with the transparent PNG alpha alpha blended with a solid gray background. The work surface will then be alpha blended at the specified opacity level with whatever is under the filter.
- MSTime does not support non-binary transparency for PNGs. This is because MSTime doesn’t understand anything other than binary index-based alpha. Because of this, transparent PNGs in MSTime elements are expected to continue to render as they do in previous versions of IE.
Adobe’s Flash Player version 6,0,65,0 (win) or 6,0,67,0 (mac) and higher have support for movies with a transparent background (Adobe TechNote). Now the only problem with this is that Safari is not supported. Here is a list of supported browser:
- IE 3 or higher (win)
- IE 5.1 AND 5.2 (mac)
- Netscape 7.0
- Mozilla 1.0 or higher
- AOL
- CompuServe
I was working on the redesign of www.vw.com 2007 and found that there were certain pages that we would have to build out in HTML vs all Flash. In order to keep the same consistent look and feel across all pages we needed to be able to display designs that relied heavily on transparencies.
The problem that we face is that we could use Flash to display all of our images, which would leave the Safari users with a bad web experience, or we could use good old tags and add filter support for IE. The problem with the latter is that heavy use of filters and doing image swaps can result in an unreliable experience for IE users.
Solution
Here are the two outstanding issues we face:
- IE lacks native support for alpha channels in PNGs without the use of filters.
- Safari lacks support for Flash movies with transparent background.
With the release of Flash Player 8 we received the ability to use loadMovie() to load a SWF, progressive JPEG, unanimated GIF, or PNG file into a MovieClip in Flash Player while the original SWF file is playing.
What we do is combine the best aspects of the two issues listed above with Flash Player 8′s ability to dynamically load PNGs. This leaves us with a cross-browser solution where we use Flash Player to display transparent PNGs in IE and use native support for alpha channels in PNGs for all other browsers.
We take an empty SWF with a bit of Actionscript in order to handle the dynamic loading of the PNG and add functionality to handle image swaps, button states, and onpress handlers. This solution gives us the freedom to have one image asset for Safari, Firefox, and, IE with a single line of Javascript code.
PNGPong consists of 1 Javascript file and 1 swf file and utilizes Geoff Stearns SWFObject to write the object/embed tags for Flash.
Change Log
Download
PNGPong is released under the MIT License.
Download PNGPong 1.0.0 (Zip file) – contains pngpong.js, pngpong.swf, swfobject.js (v1.4.4), and an example page showing how to use it. Just unzip the contents to the root of your website, and surf to http://yourdomain.com/pngpong/example/
Download PNGPong 1.0.0 Source Files (Zip file) – contains pngpong.fla, pngpong.js, pngpong.swf, and swfobject.js (v1.4.4)
Tested Browsers
- IE 6.x (win)
- IE 7.x (win)
- Firefox 1.5 – 2.x (win)
- Firefox 1.5 – 2.x (mac x86, PPC)
- Safari 2.x (mac x86, PPC)
- Camino 1.x (mac x86, PPC)
How it Works
PNGPong is a simple script to use. You can view an example here. All you have to do is unzip pngpong into the root of your webserver and include /pngpong/swfobject.js and /pngpong/pngpong.js into the of your HTML file.
|
1 2 3 4 5 |
<script type="text/javascript" src="/pngpong/swfobject.js">// <![CDATA[ mce:0 // ]]></script><script type="text/javascript" src="/pngpong/pngpong.js">// <![CDATA[ mce:1 // ]]></script> |
Create an empty
and give it a unique id. Then apply some CSS to specify the width and height of your image. Now all you have to do is call the function to embed your image.
|
1 2 3 |
<script type="text/javascript">// <![CDATA[ mce:2 // ]]></script> |
The image will be embeded into the PNGPong will do a browser check and go along 1 of 2 paths: PNGPong will handle preloading of images and can also swap images using a simple Javascript API. The script will handle the communication in the background to the appropriate assets and is transparent to the developer and user. There are 5 basic Javascript functions with 8 total (when adding custom alt attributes): Extended APIs to add custom alt tags to images: pngpong.preloadImage()Adds an image to an array of objects to facilitate image preloading. pngpong.swapImage()Swaps an image with another image. pngpong.serve()Embeds a static image into a div. pngpong.serveButtonUrl()Embeds an image into a div with rollover, rollout, and onmousedown states. When pressed it will redirect the current web-page to the specified url. pngpong.serveButtonJs()Embeds an image into a div with rollover, rollout, and onmousedown states. When pressed it will evoke a Javascript function. pngpong.alt.serve()Extends pngpong.serve() with the ability to add a custom alt tag. pngpong.alt.serveButtonUrl()Extends pngpong.serveButtonUrl() with the ability to add a custom alt tag. pngpong.alt.serveButtonJs()Extends pngpong.serveButtonJs() with the ability to add a custom alt tag.
id="foo".
tag into the div. The image will have it’s visibility set to hidden and an onload event handler will be attached to the image. Once the onload event is complete the script will set the visibility to visible. Javascript functions will be attached to the image to handle rollover, rollout, and onmousedown events for swapping graphics, changing window location or calling a Javascript function.
Code Examples
Method Definitions
List of Parameters
Parameter Definitions
inIdName
inAltName
inImage
inImageOut
inImageOver
inFunction
fooBar().inParams
param1, param2, param3.inUrl
Date
Time
Category
Tags
Pingback: Bookmarked on May 30th at The Life of Bryan
Pingback: zovopojocn
Pingback: SitePoint Blogs » PNG8 - The Clear Winner
Pingback: Simplex-Media » Blog Archive » PNGPong: Cross browser PNG Transparency solution
Pingback: PNG8 - The Clear Winner : Webmaster Talk Blog
Pingback: Confluence: Portable
Pingback: Graphics File Format « Muhammadfauzie’s Blog
Pingback: Alan Ayoub – Enterprise Client Side Development | Alan Ayoub