如何"切换"在使用fabricjs canvas选择IText对象(文本段落)之间?

时间:2017-10-23 15:03:50

标签: javascript jquery canvas fabricjs

我希望能够从下拉列表中选择"切换"在我的fabricjs画布上设置样式的IText段落之间,却遇到了麻烦。我尝试借用这个,这与图像类似,没有运气:

//oImgObj bread and butter, kudos @grunt
function replaceImage(oImgObj, imgUrl) {
  if (!isImageLoaded) return; //return if initial image not loaded
  var imgElem = oImgObj._element; //reference to actual image element
  imgElem.src = imgUrl; //set image source
  imgElem.onload = () => canvas.renderAll(); //render on image load
}

如何使用可编辑的文本块实现此目的?

1 个答案:

答案 0 :(得分:1)

您可以使用属性text将文本设置为文本对象。



var canvas = new fabric.Canvas('c');
var text = new fabric.IText('FabricJs is Awsome',{
  left:50,top:50
});
canvas.add(text);
function replace(option){
 var val = document.getElementsByTagName('textarea')[0].value;
 text.set('text',val);
 canvas.renderAll();
}

canvas {
 border: blue dotted 2px;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.19/fabric.min.js"></script>
<canvas id="c" width="400" height="400"></canvas>
<br>
<textarea></textarea><button onclick='replace()'>Replace</button>
&#13;
&#13;
&#13;