Get image source from sprite

Hi!

I am creating base64 strings from textures but I need to do it from a sprite as well.
Is there a ‘shortcut’ I might be missing or do I have to go full out and get it from the textureatlas’ texture?

Thanks,
Pieter

Never mind - I couldnt wait and I went full out. Maybe there is a better faster way but in the end this didn’t took a lot of time. Here is my result if anyone needs it.

Utils.prototype.ConvertSpriteToBase64 = function(aSprite) {
    var atlas = aSprite.atlas;
    var texture = atlas.texture;
    var frame = atlas.frames[aSprite.frameKeys[0]];
    
    var canvas = document.createElement( 'canvas' );
    var ctx = canvas.getContext( '2d' );
    
    canvas.width = frame.rect.z;
    canvas.height = frame.rect.w;
    
    var img = new Image();
    img.src = texture.getSource().src;
    img.setAttribute( 'crossOrigin', 'anonymous' );
    
    var sx = frame.rect.x;
    var sy = texture.height - frame.rect.y - frame.rect.w;
    ctx.drawImage( img, sx, sy, canvas.width, canvas.height, 0, 0, canvas.width, canvas.height);
    
    var result = canvas.toDataURL( "image/png" );
    return result;
};

Its a function in my Utils class.

-Pieter

1 Like