Search code examples
angularjshtmlkineticjs

how to convert text to image in kinetic js version 5.1.0


My following code work fine with kineticjs version 4.4.3 but give error with kineticjs version 5.1.0 (as shown in screen shot)
enter image description here Code is:

var simpleText = new Kinetic.Text({
x: 50,
y: 50,
text: 'TESTING',
fontSize: 16,
fill: "white"
});
simpleText.toImage({
width:50,
height:50,
x : 50,
y : 50,
callback: function(img){
var yodaImg = new Kinetic.Image({
image: img,
x: 0,
y: 0,
width: 50,
height: 50,
name: 'image'
});
console.log(yodaImg.src);
}
});

For some reasons I have to use this latest version.
Any solution?
Thanks in advance


Solution

  • var textOnCanvas = new Kinetic.Text({
        x: 0,
        y: 0,
        text: fullText,
        fontFamily: "Arial",
        fontSize: 22,
        fill: '#000000',
        align: 'left',
        padding: 5,
        width: 1024
     });
    layer.add(textOnCanvas);
    layer.draw();
    
    var textImgSrc = textOnCanvas.toDataURL(); // base64 image of text
    
    var textImgObj = new Image();
    textImgObj.src = textImgSrc;
    var kineticIMG = new Kinetic.Image({
        image: textImgObj,
        x: 0,
        y: 0,
    });
    

    src will be in base64 string. Then you can apply this image to the Kinetic.Image() object.