Skip to content
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

Adding 'false' stops progress event from firing!? #267

Closed
blaasvaer opened this issue Dec 11, 2019 · 20 comments
Closed

Adding 'false' stops progress event from firing!? #267

blaasvaer opened this issue Dec 11, 2019 · 20 comments

Comments

@blaasvaer
Copy link

blaasvaer commented Dec 11, 2019

Properly handling fade in and fade out of images is impossible if I do not add 'false' to:

new createjs.LoadQueue( false );

But when I do, all progress events stop firing.

Now the 'docs' are basically an API doc. Not at all usefull when trying to understand what's going on from a users perspective. WHERE does it explain what happens to events whether I add true or false to the instance? I for sure can't find it ...

@lannymcnie
Copy link
Member

lannymcnie commented Dec 11, 2019

You are toggling tag-based loading (so images load with an tag instead of loading as data (with XHR, or "XMLHttpRequests". As a result, the browser provides no progress events (among other things).

From the main description in the PreloadJS docs:

PreloadJS will try and load content using XHR, since it provides better support for progress and completion events

https://createjs.com/docs/preloadjs/modules/PreloadJS.html

This is just a browser limitation.
Hope this helps!

@blaasvaer
Copy link
Author

blaasvaer commented Dec 11, 2019

Thanks, my issues seems to to be to properly add a loaded image as a background for a DOM element (div). I guess it's because the image is not 'appended' to the DOM after load completion ... how would one go about handling this situation?

@blaasvaer
Copy link
Author

This seems to not be working properly:

imageContainer.css('background-image', 'url(' + imagePath + slideObj.image + ')');

The image is set alright, but with a random delay as if it's being fetched again ... I'm having trouble figuring out how to properly 'append it to the cache' without having to actually append it to the DOM in some hidden way or another hack.

@lannymcnie
Copy link
Member

Yeah, if you set it to a CSS path, it will in fact load again, since CSS doesn't let you set it to a loaded source. When you load with XHR, it doesn't go to the cache.

You might be able to use the loaded image path.

function handleLoad(e) {
  var img=e.result;
  var path=img.src; // with XHR this should be a blob url
}

If it is loaded with tags (XHR=false), it will be in the cache, so it should load immediately, even if there is a small delay.

@blaasvaer
Copy link
Author

Thanks, that's probably what I'm looking for. I'll give it a go and get back.

@blaasvaer
Copy link
Author

So, will the above code actually store the loaded image into the cache, so that the next time I call the url, it will look in the cache? I wouldn't have to pass the actual path variable around, right?

@blaasvaer
Copy link
Author

What I'm asking is: How can I have BOTH a cached image AND progress events?

@lannymcnie
Copy link
Member

When loading with XHR, it uses blob storage, which is similar to the image cache. You need to get that url, or the loaded image from PreloadJS and pass those around.

You can't request the url image again, since it will load it over again.

@blaasvaer
Copy link
Author

blaasvaer commented Dec 11, 2019

So, what I'm looking for is something along the lines of:

<img src="blob:http://localhost/3aeb1382-410c-a041-a5ff-bf2d168b01b6">?

@blaasvaer
Copy link
Author

But how do I set that as a background-image for a div? Will it work just as a 'normal' image url?

@blaasvaer
Copy link
Author

I think this adds a whole unexpected complexity to the use of PreloadJS, as I'd see this sort of thing as being the expected behavior ... maybe that's just me.

@blaasvaer
Copy link
Author

We want to load images. We want to monitor progress. And then we want to do something with these images. That being the whole point of loading them in the first place.

@blaasvaer
Copy link
Author

Where in the docs can I see this described in clear and understandable language? I'd like to learn my way around the docs, as they didn't provide me much help in this case.

@blaasvaer
Copy link
Author

I basically have an array of objects like this:

...
{
	"image": "lamp.jpg",
	"opacity": 1,
	"colorTheme": {
		"color": "#aee7ff",
		"backgroundColor": "black",
		"blendMode": "normal"
	},
	"texts": {
		"header": "Header here",
		"body": "Copy here"
	}
},
...

that I pass around to other parts of the 'app' ... now, would I have to add the 'blob' to each of these objects after load complete, to make the accessible from other parts of the 'app'?

@blaasvaer
Copy link
Author

blaasvaer commented Dec 11, 2019

How would I otherwise set it as a background-image on a div using:

imageContainer.css('background-image', 'url(' + imagePath + <blob> + ')');?

@lannymcnie
Copy link
Member

The library was never really meant for loading CSS images, since CSS images are applied as string paths. The approach I mentioned was a workaround. I have been trying to make a blob url work as a background, but have so far not had any luck :(

I will keep looking into this.

@blaasvaer
Copy link
Author

OK, thanks, I never thought of css images as being special to 'normal' images – everything is basically an reference to an url somewhere in the world. That may be what's kicking me in the balls right now.

And suddenly having to fight with converting image blobs to actually usable images in the browser, was not something I would have expected from this library …

I must say, that from my initial encounter with PreloadJS to the current 'nightmare' I find myself in is somewhat of a surprise.

But, please let me know if you come up with a solution, that can also give us a heads up in the docs ...

@blaasvaer
Copy link
Author

OK, this was a tricky one. After hours of pulling hair, wild guessing, trying to understand the docs etc etc. I think I finally (though not completely confident yet, as I don't know what will hit me in a few minutes, a few hours or maybe even a few days?) got the blob thing to work.

When a file is completed loading, look up the result from the queue (started elsewhere) using the id from the loaded image:

function handleFileLoad ( e ) {
var result = queue.getResult(e.item.id, true); // This will load the binary image data from XHR

// 'Convert' the data into something useful for the browser
blob_url = URL.createObjectURL( result );

...

// And later you can use it like this
imageContainer.css('background-image', 'url(' + blob_url + ')');

Hope this saves someone else the bleedin' nightmare I just went through.

@lannymcnie
Copy link
Member

Glad you found a solution. I was also trying that, but getting errors with my Blob URL.
https://jsfiddle.net/wocd3rj1/

@blaasvaer
Copy link
Author

I just added the missing parenthesis to your code, and now it works:

var queue = new createjs.LoadQueue();
queue.loadManifest({id:"image", src:"https://playpen.createjs.com/CORS/awesome.jpg", type:"image", crossDomain:true});
queue.on("complete", function(event) {
  var img = queue.getResult("image");
  var blob = img.src;
 
  var div = document.getElementById("canvas");
  div.style.backgroundImage = "url('"+ blob +"')";
}) <-- YOU WERE MISSING THIS LAST PARENTHESIS

So, now we have a working snippet right here, eh?

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

2 participants