最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - using fabric.js ,increase fontSize of canvas text on button click - Stack Overflow

programmeradmin4浏览0评论

i am using fabric.js to work on canvas . i have created text on canvas .now,onclick of button i want to increase font-size .

canvas.set({fontSize,40});

this is not working ? any other method .i have created fiddle please Check

   var canvas = window._canvas = new fabric.Canvas('c');
var text = new fabric.Text('Sample', {
    left: 100,
    top: 100,
    fill: 'navy'
});

canvas.add(text);

document.getElementById('textinput').addEventListener('change', function (e) {
    var obj = canvas.getActiveObject();

    if (!obj) return;

    obj.setText(e.target.value);
    canvas.renderAll();
});

document.getElementById('btn').addEventListener('click', function (e) {
    var obj = canvas.getActiveObject();

   alert('button clicked');
     if (!obj) return;

    obj.set(fontSize,40);
    canvas.renderAll();
});

i am using fabric.js to work on canvas . i have created text on canvas .now,onclick of button i want to increase font-size .

canvas.set({fontSize,40});

this is not working ? any other method .i have created fiddle please Check

   var canvas = window._canvas = new fabric.Canvas('c');
var text = new fabric.Text('Sample', {
    left: 100,
    top: 100,
    fill: 'navy'
});

canvas.add(text);

document.getElementById('textinput').addEventListener('change', function (e) {
    var obj = canvas.getActiveObject();

    if (!obj) return;

    obj.setText(e.target.value);
    canvas.renderAll();
});

document.getElementById('btn').addEventListener('click', function (e) {
    var obj = canvas.getActiveObject();

   alert('button clicked');
     if (!obj) return;

    obj.set(fontSize,40);
    canvas.renderAll();
});
Share Improve this question edited Aug 28, 2013 at 5:55 Sasidharan 3,7503 gold badges20 silver badges37 bronze badges asked Aug 28, 2013 at 4:46 anamanam 3,91316 gold badges46 silver badges85 bronze badges 2
  • Change that as fontsize – Prasanna Aarthi Commented Aug 28, 2013 at 4:51
  • obj.set(fontSize,40) is not being executed, program stops before that – Prasanna Aarthi Commented Aug 28, 2013 at 5:31
Add a ment  | 

2 Answers 2

Reset to default 10

Here is a working code,

var canvas = window._canvas = new fabric.Canvas('c');
var text = new fabric.Text('Sample', {
  left: 100,
  top: 100,
  fontSize: 80,
  fill: 'navy'
});

canvas.add(text);
document
  .getElementById('btn')
  .addEventListener('click', function (e) {
    canvas.getActiveObject().set("fontSize", "30");
    canvas.renderAll();   
  });

First of all, I was able to make this work by changing

obj.set('fontSize',40);

to

obj.setFontSize(40);

Second, I think that it is not the fontSize of your object that is changed when you resize it, but its scaleX and scaleY. Hope this helps.

发布评论

评论列表(0)

  1. 暂无评论