Sign in to follow this  
m4rk2s

how to access my assets in jsdeliver

Recommended Posts

Hi,

i have some assets (on dropbox) that i've found on the internet and i want to have them online like what did @samme on jsdeliver this way do you have any idea on how to do it

        preload: function() {
            game.load.baseURL = 'https://cdn.jsdelivr.net/gh/samme/phaser-examples-assets@v1.0.0/';
            game.load.crossOrigin = 'anonymous';
            game.load.image("bar", "sprites/zelda-life.png")
        }

 

Share this post


Link to post
Share on other sites

Hi,

 Phaser way:

            let imageUrl = "https://cloudgames.com/app/img/logo-horizontal.png";

            this.load.crossOrigin = "";
            this.load.image("LOGO", imageUrl);

did not work for me too - all I got was "SecurityError: The operation is insecure.".

But then I found this tutorial: https://webglfundamentals.org/webgl/lessons/webgl-cors-permission.html

and this worked for me:

            var cache = this.cache;
            var img = new Image();
            img.addEventListener('load', function () {

                console.log("***** LOADED ***** ");
                cache.addImage("LOGO", imageUrl, img);

            });
            img.crossOrigin = "";
            img.src = imageUrl;

Now you can create your sprite like this:

var logo = this.game.add.sprite(0, 0, "LOGO");

 

Share this post


Link to post
Share on other sites

@samme thanks for answer, but are you sure? With Phaser none of "" or "anonymous" work - in both cases texture cannot be used in WebGL. That linked tutorial is using "" and it works (it also works with setting to "anonymous").

In fact, this is image I am trying to load and use: "https://cloudgames.com/app/img/logo-horizontal.png" - it is sponsor logo. It can load & use it only if used  that way I described above (no matter if I set it to "" or "anonymous" , but if I omit this line: img.crossOrigin = ""; I get this error ... which is exactly what that tutorial was about (how to use CORS texture in WebGL)

CORS.thumb.png.8f45c034c5ca997d3e6f559b2bffee0c.png

Now, I am struggling with http x https. If URL for image is https it works, if it is http, then it does not work. Looks like sponsors emulator is returning http, but luckily production looks to return https (I debugged one PIXI game there).

 

 

Share this post


Link to post
Share on other sites

When you're setting an image's crossOrigin property directly, an empty string or any string value besides use-credentials is the same as anonymous. That's why the tutorial code works.

But for Phaser's Loader#crossOrigin, an empty string is the same as false, meaning don't set the crossOrigin property.

The server also has to accept the cross-origin request, but I guess that's not the problem in your case.

So I'm not sure why your two-step method works. :) It looks similar to what Phaser is doing in loadImageTag.

Share this post


Link to post
Share on other sites

@samme - thanks for explanation & help! It works for me now. I tried "anonymous" before without luck, but it was messed with second problem - sever is returning URL link without protokol (like //cloudgames.com/ ....) and if I converted it into absolute URL, it added http (because my dev server is http). But server is returning image only if asked for with https. If I ask for it with correct protokol, then everything works.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.