Skip to content

iSparta/apng-canvas-with-controller

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

APNG-canvas-with-controller

APNG-canvas-with-controller is library for controling apng,which base on (apng-canvas).

API

  • ani.stop()
  • ani.play()
  • ani.goto()
  • ani.playto()

Usage example

APNG.ifNeeded(function() {
	for (var i = 0; i < document.images.length; i++) {
		var img = document.images[i];
		if (/\.png$/i.test(img.src)) {
			var apng=APNG.animateImage(img);
			apng.done(function(ani){
				// listen playing,you can get current frame
				ani.playing=function(t){
				    console.log(t)
				    if(t==20){
				        ani.stop();
				    }
				}
				$("stop").addEventListener("click",function(){
				    // stop apng
				    ani.stop().done(function(){
				        console.log("stop")
				    });
				})
				$("play").addEventListener("click",function(){
				    // play apng
				    ani.play().done(function(){
				        console.log("play")
				    });
				});
				$("goto").addEventListener("click",function(){
				    var val=$("input_goto").value;
				    // goto the frame
				    ani.goto(parseInt(val)).done(function(){
				        console.log("goto")
				    });
				});
				$("playto").addEventListener("click",function(){
				    var val=$("input_playto").value;
				    // playto the frame
				    ani.playto(parseInt(val)).done(function(){
				        console.log(ani.nowFrame)
				    });
				});
			})
		}
	}
});

APNG-canvas README

APNG-canvas

(README по-русски)

APNG-canvas is a library for displaing Animated PNG files in the browsers with canvas support (Google Chrome, Internet Explorer 9, Apple Safari).

Working demo: http://davidmz.github.com/apng-canvas/ (around 3 Mb of apng files)

Discussion in LJ: http://david-m.livejournal.com/tag/apng-canvas (in russain)

API

The library creates a global object APNG, which has several methods. All methods are asynchronous and most of them receive an optional callback-argument. Methods must be called after DOM tree is loaded.

For deferred calls, these methods return promise objects. If jQuery is available, then its promises are used, in other case a compatible interface which supports methods done, fail, then and always is used. If the method is finished successfully, callback and done handlers are called (with the same parameters), in case of error fail hendlers are called with the error message.

APNG.ifNeeded(callback?)

The callback is called without arguments, and only when browser supports canvas but not APNG. Only in that case it makes sense to use this library. Other methods (except checkNativeFeatures) should be called from the callback.

APNG.animateImage(img)

This method is called without callback. If img.src contains a link to the correct APNG file, then this methods creates canvas, in which APNG animations would be played. Then the method selects optimal strategy for animaton depending on the browser:

  • For WebKit-based broswers (Chrome and Safari): source image is replaced by a transparent gif plus background canvas where the animation is played. That allows to keep the img object, its attributes and event handlers.
  • For other browsers (Internet Explorer 9): Works similar to the replaceImage method (below): source image img is replaced with canvas animation object.

APNG.replaceImage(img)

This method is called without callback. Replaces img element (HTMLImageElement) with canvas animation. Replacement only works when img contains correct PNG file. The replacement keeps the attributes of img. If jQuery is available, than event handlers are kept too.

This method works the same in all browsers.

APNG.createAPNGCanvas(url, callback?)

Loads PNG file from that url and disassembles it, then creates canvas element and starts the animation.

The callback is only called when the loaded data contains the correct APNG file. The argument is newly created canvas animation element. This element is not a part of the DOM tree, it have to be added manually.

APNG.checkNativeFeatures(callback?)

Checks if the browser supports APNG and canvas. Can be called independently from all other methods. The callback argument is the objects with two binary fields: apng and canvas. True in those fields means the browser supports correcponding technology.

Usage example

APNG.ifNeeded(function() {
    for (var i = 0; i < document.images.length; i++) {
        var img = document.images[i];
        if (/\.png$/i.test(img.src)) APNG.animateImage(img);
    }
});

Limitations

Since the images are loaded by XMLHttpRequest, the images domain should be the same as the webpage domain.

If domains are different, then in Chrome/Safari it is possible to use CORS, by making sure the image server returns Access-Control-Allow-Origin: * header.

Unfortunately, it seems that CORS cannot by used in IE, because the corresponding object XDomainRequest will not return the result as binary data (XMLHttpRequest allows that by using responseBody property).

By the same reason (the use of XMLHttpRequest), the library will not work locally, with file:// protocol.


Thanks to Max Stepin for the translation of this README.

About

apng-canvas-with-controller

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published