Search code examples
cocos2d-xcocos2d-x-3.0cocos2d-js

How to get byteArray or base64 string from RenderTexture Cocos2d-JS


I am making a game on cocos2d-JS for facebook in which there is a requirement of sharing a screenshot of the game.

I am able to take the screenshot but now I am unable to upload it in the Parse.com server because it requires base64 format or byte array. I am unable to find any solution of converting Sprite in to this format.. Here's my code so result when I do addchild its coming proper .. I have also added my commented code so that it will help to understand that I have tried lot of things but couldnt achieve the same.

shareToSocialNetworking: function () {

    cc.director.setNextDeltaTimeZero(true);

    var newsize = cc.director.getVisibleSize();

    var renderText = new cc.RenderTexture(newsize.width,newsize.height);

    renderText.begin();
    cc.director.getRunningScene().visit();
    renderText.end();
    var result = cc.Sprite.create(renderText.getSprite().getTexture());
    result.flippedY = true;
    this._mainViewNode.addChild(result,6000);

    //renderText.saveToFile("screenshot.jpg",cc.IMAGE_FORMAT_PNG);
    //var based = renderText.getSprite().getTexture().getStringForFormat().toString();
    //var data = based.getData();
    var file = new Parse.File("screen.jpg", { base64: this.getBase64(result) });
    //var file = new Parse.File("screen.jpg", data, "image/png");
    var self = this;

    file.save().then(function() {
        // The file has been saved to Parse.
        alert(file.url);
     this.onSharePictureInfoLink(file.url());

    }, function(error) {
        // The file either could not be read, or could not be saved to Parse.
    });


    //
    //var ccImage = renderText.newCCImage();
    //
    //var str = ccImage.getData();

},

is there any workaround that can be done


Solution

  • there is a private variable called _cacheCanvas, which is the instance of the offscreen canvas

    you can simply do renderText._cacheCanvas.toDataURL()