首页 > 解决方案 > Rect.setGradient 不是 fabricjs 中的函数

问题描述

我看到有关在fabricjs中将背景设置为渐变的问题。他们说要在 fabric.Rect 的对象上使用 setGradient 但是当我使用它时会导致错误 rect.setGradient is not a function。有什么解决办法吗?谢谢

let rect = new fabric.Rect({
        top: 0, left : 0, width : $("canvas").width(), height: $("canvas").height(), fill : "#000"
    });
rect.setGradient("fill",{
             properties here
}) 

标签: javascripthtmlhtml5-canvasfabricjs

解决方案


setGradient方法在 Fabric v4 中被删除(参见http://fabricjs.com/v4-break-changes)。

这是设置渐变填充的当前方法。

obj.set('fill', new fabric.Gradient({
  //gradient options
  type: 'linear',
  gradientUnits: 'pixels', // or 'percentage'
  coords: { x1: 0, y1: 0, x2: 50, y2: 0 },
  colorStops:[
    { offset: 0, color: 'red' },
    { offset: 1, color: 'green'}
  ]
}));

推荐阅读