如何在kinetic js版本5.1.0中将文本转换为图像

时间:2015-02-10 09:24:29

标签: angularjs html5 kineticjs

我的以下代码与kineticjs版本4.4.3配合使用,但在kineticjs版本5.1.0中出错(如屏幕截图所示)
enter image description here 代码是:

var simpleText = new Kinetic.Text({
      x:50,
      y:50,
      文字:'测试',
      fontSize:16,
      填:“白”
    });
    simpleText.toImage({
        宽度:50,
        高度:50,
        x:50,
        y:50,
        回调:函数(img){
          var yodaImg = new Kinetic.Image({
              image:img,
              x:0,
              y:0,
              宽度:50,
              身高:50,
              名称:'图像'
          });
          的console.log(yodaImg.src);
        }
    });

出于某些原因,我必须使用这个最新版本 任何解决方案?
提前致谢

1 个答案:

答案 0 :(得分:1)

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将在base64字符串中。然后,您可以将此图像应用于Kinetic.Image()对象。