【发布时间】:2020-10-16 09:51:40
【问题描述】:
我正在尝试对 FabricJS 中的大图像应用过滤器。
图像大约为 3000 x 4000,超过了默认的 2048 纹理大小。我可以将它增加到 4096,它适用于这张图片。
当前,当应用过滤器时,图像会被剪切。我尝试使用以下方法减小图像的大小:
let obj = this.canvas.getActiveObject();
if (Math.max(obj.width, obj.height) > 2048) {
let scale = 2048 / Math.max(obj.width, obj.height);
obj.filters.push(
new fabric.Image.filters.Resize({ scaleX: scale, scaleY: scale })
);
}
obj.filters.push(new fabric.Image.filters.Grayscale());
obj.applyFilters();
this.canvas.renderAll();
但这对我不起作用。图像仍被剪切和旋转。
这就是我将图像添加到画布的方式:
//in a parent component
let url = URL.createObjectURL(e.target.files[0]);
fabric.Image.fromURL(url, (image) => {
this.setState({
image,
});
});
//following is in a child component
let canvasWidth = this.canvasWrapperRef.current.clientWidth;
let canvasHeight = this.canvasWrapperRef.current.clientHeight;
//create canvas
this.canvas = new fabric.Canvas("canvas", {
selection: false,
backgroundColor: "black",
hoverCursor: "context-menu",
height: canvasHeight,
width: canvasWidth,
});
let image = this.props.image;
image.set({ selectable: false });
this.canvas.centerObject(image);
this.canvas.setActiveObject(image);
this.canvas.add(image);
this.canvas.renderAll();
有没有办法可以减小原始图像的大小/质量,以便对其应用过滤器?
【问题讨论】: