jquery - 在文本区域的更改事件上使用 Canvas 上的 fabric.js 添加文本

标签 jquery html css fabricjs

在文本区域的更改事件中,我在 Canvas 上添加文本如何使用以下代码在 Canvas 上设置最终文本我得到很多字符串。

enter image description here

   $('#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);
}

这是未经测试的当前形式,因为它来 self 编写的一个更大的命名空间应用程序。但它应该工作。您正朝着正确的方向前进,但实际上您在 keyup/change 上添加了一个新的文本节点。我希望这可以帮助别人。

关于jquery - 在文本区域的更改事件上使用 Canvas 上的 fabric.js 添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18459733/

相关文章:

javascript - 确认框从alertify插件和jquery返回值

javascript - 鼠标悬停在不覆盖整个 Div 的区域上

php - 使用 JQuery/CSS 或可能的 PHP/CSS 将数据转换为图像。 (评分系统)

javascript - jquery 在自定义函数中使用 (this)

javascript - 无法让 JSFiddle 工作

html - CSS更改表格单元格的高度,其中包含<a>

php - 将 MySQL 计数列添加到 PHP/HTML 表

css - Bootstrap 网格 : Floating columns vertically

css - 仅当 CSS 过渡启动时才在 div 中显示文本?

javascript - 更改堆积条形图的颜色