簡體   English   中英

在文本區域發生更改事件時,在畫布上使用fabric.js添加文本

[英]on change event of text area add text using fabric.js on canvas

在文本區域發生更改時,我在畫布上添加文本,如何使用以下代碼在畫布上設置最終文本,我得到了很多字符串。

在此處輸入圖片說明

   $('#filedset').delegate(' textarea', 'change keyup', function() {

        console.log('text area change');
        var message = $(this).val();
 var text1 = new fabric.Text(message, { left: 100, top: 100 });
 canvas.add(text1);
    });
    $('#change_pos').click(function ()
    {
        console.log('button clicked');
        rect.set({ left: 120, top: 150 });
        canvas.renderAll();
    });

我知道這個問題有點老了,但是我認為我將盡力幫助自己,因為我必須解決確切的問題。

// Your Fabric Canvas
var canvas = new fabric.Canvas('YOURCANVASID');

// New event handler to take the value from the textarea and send to the addText utility function. 
$('.add-text').on('click', function(e){
    e.preventDefault();
    var text = $('#filedset').val();
    addText(text);
});

// Watches the keyup event handler in your textarea and updates the selected text node on canvas while you are typing. Creates a nice effect. 
// with whats being typed. 

 $('#filedset').on('keyup' , function(){

    var activeObject = canvas.getActiveObject();

     // We do a check to make sure there is an active canvas object, and see if it's text. 
    if (activeObject && activeObject.type === 'text') {
        if (!this.value) {
            return false;
        }
        else {
            activeObject.setText(this.value);
            console.log(this.value);
        }
    }

    canvas.renderAll();

});

// Function to add the text, just pass in a text string you want to add to the canvas. 
function addText(addtext) {
    var text = new fabric.Text(addtext, {  });
    canvas.add(text);
    // Center The New Text on the canvas or position it wherever
    canvas.centerObject(text);
}

這是未經測試的,因為它來自我編寫的一個更大的命名空間應用程序。 但它應該起作用。 您的方向是正確的,但實際上是在keyup / change上添加了一個新的文本節點。 我希望這可以幫助別人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM