Fabric js: Increase font size instead of just scaling when resize with mouse

前端 未结 2 1384
慢半拍i
慢半拍i 2021-02-15 23:46

I am working on fabric js application & I need to increase/decrease the font size when we resize the font with mouse
my tried code

2条回答
  •  被撕碎了的回忆
    2021-02-16 00:28

    While perhaps not the best approach (I'm new to Fabric), this is working for me with version 4.3.0:

    canvas.on('text:changed', function (e) {
      console.log('Text:', e.target.text, e.target.text.length)
      let objectSelection = canvas.getActiveObject()
      switch (e.target.text.length) {
        case 10:
          console.log(`Text is ${e.target.text.length} characters in length.`)
          objectSelection.set('fontSize', 100)
          break
        case 15:
          console.log(`Text is ${e.target.text.length} characters in length.`)
          objectSelection.set('fontSize', 85)
          break
        case 25:
          console.log(`Text is ${e.target.text.length} characters in length.`)
          objectSelection.set('fontSize', 60)
          break
      }
    })
    canvas.on('text:editing:exited', function (e) {
      console.log('Text edited!')
    })
    

    I took inspiration from another answer, which does the job.

提交回复
热议问题