[英]How can I blur an image on Canvas using fabricjs with React
I'm new to fabricjs, I've been trying to blur an image using a HTML slider(range from 0 - 1).我是fabricjs的新手,我一直在尝试使用HTML滑块(范围从0到1)来模糊图像。 The image has already been loaded on the canvas.
图像已经加载到 canvas 上。 But while applying the blur effect using Filters arrays, I don't see any visible change.
但是在使用过滤器 arrays 应用模糊效果时,我看不到任何可见的变化。
I'm using a controlled blur component in React as follows:我在 React 中使用了一个受控的模糊组件,如下所示:
<div>
Blur
<input
id="blur"
type="range"
min="0"
max="1"
step="0.1"
value={editor?.canvas?.getActiveObject()?.filters?.slice(-1)[0]?.blur || 0}
onChange={(e) => handleChangeSlider("blur", e?.target?.value)}
></input>
<span>{editor?.canvas?.getActiveObject()?.filters.slice(-1)[0]?.blur || 0}</span>
</div>
the onChange handler function is as follows: onChange 处理程序 function 如下:
function handleChangeSlider(){
setBlur(value)
editor?.canvas?.getActiveObject()?.filters?.push({
blur: 0.5,
horizontal: false,
aspectRatio: 1,
})
// editor?.canvas?.getActiveObject()?.applyFilters()
}
editor.canvas.renderAll()
}
The same approach works if I'm setting opacity on the image, like:如果我在图像上设置不透明度,则相同的方法有效,例如:
editor.canvas.getActiveObject().set({ opacity: value })
But, as far as I know, the Blur has to be inside the filters array, So I've tried doing the same, but it is not working at all.但是,据我所知,模糊必须在过滤器数组内,所以我尝试过这样做,但它根本不起作用。
Some Observations:一些观察:
Even after doing the editor.canvas.renderAll()
, I think editor?.canvas?.getActiveObject()?.applyFilters()
is necessary to apply the blur property.即使在做了
editor.canvas.renderAll()
之后,我认为editor?.canvas?.getActiveObject()?.applyFilters()
是应用模糊属性所必需的。 But if I uncomment that line from above code, i get the following error "filter.isNeutralState is not a function".但是,如果我从上面的代码中取消注释该行,我会收到以下错误“filter.isNeutralState 不是函数”。 While that line being kept commented, I don't see any error or any change.
虽然该行一直被评论,但我没有看到任何错误或任何变化。
I found this codepen link, where the author is literally doing the same, But I'm not sure what I'm doing wrong in my case.我找到了这个codepen链接,作者实际上是在做同样的事情,但我不确定在我的情况下我做错了什么。
My editor?.canvas?.getActiveObject()
我的
editor?.canvas?.getActiveObject()
Сreate a filter like this:创建一个像这样的过滤器:
new fabric.Image.filters.Blur({
blur: 0.5,
horizontal: false,
aspectRatio: 1,
});
In order to solve the cross-origin issue use the additional parameter:为了解决跨域问题,请使用附加参数:
fabric.Image.fromURL(
activeDragDropItem.src,
function (oImg) {
oImg.scale(0.2);
oImg.top = e.nativeEvent.layerY;
oImg.left = e.nativeEvent.layerX;
editor.canvas.add(oImg);
},
{
crossOrigin: "",
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.