在Fabric JS中缩放时如何保持Rect的圆角

时间:2019-02-07 09:50:01

标签: javascript rounding fabricjs rect

我正在使用Fabric js 1.7.22版。

原始对象:

enter image description here

当前结果:

enter image description here

预期结果:

enter image description here

1 个答案:

答案 0 :(得分:5)

一种方法是监视scaling事件并将Rect的比例重置为1,改为修改其widthheight

const canvas = new fabric.Canvas('c')

const rect = new fabric.Rect({
  left: 10,
  top: 10,
  width: 100,
  height: 100,
  fill: 'blue',
  rx: 10,
  ry: 10,
  objectCaching: false,
})

rect.on('scaling', function() {
  this.set({
    width: this.width * this.scaleX,
    height: this.height * this.scaleY,
    scaleX: 1,
    scaleY: 1
  })
})

canvas.add(rect).renderAll()
canvas {
  border: 1px solid red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.22/fabric.js"></script>
<canvas id="c" width="300" height="250"></canvas>