Skip to content

How do you include this on a web page? #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
adamcoulombe opened this issue Dec 5, 2017 · 11 comments
Open

How do you include this on a web page? #2

adamcoulombe opened this issue Dec 5, 2017 · 11 comments

Comments

@adamcoulombe
Copy link

I want the ability to play/pause the APNG on a webpage. I am having trouble understanding from the instructions on the readme exactly how you get an APNG on the webpage and use this library to control it.

I saw the demo and see that it does this, but the index.js doesnt look like your documentation.

@davidmz
Copy link
Owner

davidmz commented Dec 6, 2017

You should do this (I'll use async/await syntax for brevity):

const apng = parseAPNG(buffer); // buffer is an ArrayBuffer with the image data
await apng.createImages(); // making images for all frames
const player = await apng.getPlayer(ctx); // ctx is a canvas 2D context
player.play() // play apng on canvas
...
player.pause() // pause playback

@adamcoulombe
Copy link
Author

Thanks for your help. I submitted PR #3 to maybe help others get started, the whole babel/webpack workflow was not something I was very familiar with when I found your library, although I am glad to know it now!

@charlesr1971
Copy link

@davidmz Is it possible to read data from IMG tag rather than accessing the data through a file INPUT tag, using filereader(). I have several APNG that load on a webpage, I want loop over these images and access data like duration etc

@davidmz
Copy link
Owner

davidmz commented Sep 12, 2020

@charlesr1971 yes, it possible but not from IMG itself. You should load the image binary data from the src URL (via fetch API or XHR) and parse it using this library.

Also, you can use a bit more hi-level library, https://github.com/davidmz/apng-canvas that have the APNG.animateImage method for it. Note that there are some limitations.

@charlesr1971
Copy link

David. Thanks for your help. I will have a look at the other library apng-canvas, but I wanted to try and avoid XHR

How about this method? After an image has loaded, I recreate it as a Canvas, using:

ctx.drawImage()

And then use:

ctx.getImageData()

Like:

https://jsfiddle.net/donmccurdy/jugzk15b/

Then I can use:

new FileReader()

To access the array buffer and pass it into:

parseAPNG()

@charlesr1971
Copy link

Of course, with my previous method, I wouldn’t need to build the image out. I would just add the APNG duration data, underneath the source image, once it is available.

@davidmz
Copy link
Owner

davidmz commented Sep 12, 2020

No, it wouldn't work. The only way to obtain APNG data is fetch/XHR.

@charlesr1971
Copy link

@davidmz David. Thanks for your advice.

What part of the method outlined in:

https://jsfiddle.net/donmccurdy/jugzk15b/

Would fail?

Thanks

@davidmz
Copy link
Owner

davidmz commented Sep 12, 2020

Sorry, but I cannot provide detailed consultation via Github issues. The getImageData isn't returning raw PNG data, it event isn't have PNG format at all (https://developer.mozilla.org/ru/docs/Web/API/ImageData). It is just an uncompressed 'screenshot' without any animation info.

@charlesr1971
Copy link

@davidmz Thanks for your help...

@charlesr1971
Copy link

And finally, I managed to resolve this problem by using apng-canvas:

jQuery(document).ready(function(){
			
		  var animatedStickerImages = document.querySelectorAll('.animated-sticker-image');
		  var counter = 1;
		  [].forEach.call(animatedStickerImages, function(img) {
			const duration = document.querySelector('##animated-sticker-image-duration-' + counter);
			APNG.parseURL(img.src).then(function(data) {
			  if('playTime' in data){
				var playTime = parseInt(data['playTime']/1000).toFixed(2) + "s";
				duration.appendChild(document.createTextNode(playTime));
			  }
			});
			counter++;
 });

Simple when you know how...

Anyway, great libraries! Thanks...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants