Get base64 string of frame

For developers using the Construct 2 Javascript SDK

Post » Mon Jan 09, 2017 9:53 pm

Hello,
can someone help me to get base 64 string of sprite's frame ?
I have tried these function but it doesn't work...

Code: Select all
function toDataUrl(src, callback, outputFormat) {
  var img = new Image();
  img.crossOrigin = 'Anonymous';
  img.onload = function() {
    var canvas = document.createElement('CANVAS');
    var ctx = canvas.getContext('2d');
    var dataURL;
    canvas.height = this.height;
    canvas.width = this.width;
    ctx.drawImage(this, 0, 0);
    dataURL = canvas.toDataURL(outputFormat);
    callback(dataURL);
  };
  img.src = src;
  if (img.complete || img.complete === undefined) {
    img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
    img.src = src;
  }
}

//then execute :
var url = this.inst.cur_animation.frames[this.inst.cur_frame].texture_img.src;
toDataUrl(url, function(base64Img) {
  console.log(base64Img);
});


I have also tried this code:
Code: Select all
function getBase64FromImageUrl(url) {
    var img = new Image();

    img.setAttribute('crossOrigin', 'anonymous');

    img.onload = function () {
        var canvas = document.createElement("canvas");
        canvas.width =this.width;
        canvas.height =this.height;

        var ctx = canvas.getContext("2d");
        ctx.drawImage(this, 0, 0);

        var dataURL = canvas.toDataURL("image/png");

        alert(dataURL.replace(/^data:image\/(png|jpg);base64,/, ""));
    };

    img.src = url;
}

//then execute:
var url = this.inst.cur_animation.frames[this.inst.cur_frame].texture_img.src;
getBase64FromImageUrl(url);


Thanks fro your help!
B
17
S
10
G
19
Posts: 140
Reputation: 11,457

Post » Mon Jan 09, 2017 11:26 pm

I think you're trying to make what @Pode already made. So you can take a look at his code: behavior-extract-sprite-to-base64-string_t68494
Image
B
27
S
14
G
54
Posts: 853
Reputation: 31,110

Post » Tue Jan 10, 2017 5:13 pm

@BackendFreak Hi,
Thanks for your precious help, I had searched before posting but I have not found!
Really thanks
B
17
S
10
G
19
Posts: 140
Reputation: 11,457


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests