fabricjs - 没有fragmentSource的FabricJS图像过滤器?
问题描述
我有一个与 WebGL 不兼容的过滤器。有没有办法跳过编写片段源?我尝试在过滤器子类上覆盖 fabric.Image.filters.BaseFilter applyTo 以仅调用this.applyTo2d(options);
但我没有得到任何图像数据。
applyTo: function(options) {
if (options.webgl) {
if (options.passes > 1 && this.isNeutralState(options)) {
// avoid doing something that we do not need
return;
}
this._setupFrameBuffer(options);
this.applyToWebGL(options);
this._swapTextures(options);
}
else if (!this.isNeutralState()) {
this.applyTo2d(options);
}
},
解决方案
我能够在 webgl_backend.class.js 中使用最少的代码以非优化方式完成此任务。
首先,webgl 后端需要非 webgl 过滤器的后备:
this.fallback2dBackend = new fabric.Canvas2dFilterBackend();
然后,而不是运行所有过滤器:fabric.filterBackend.applyFilters(filters, this._originalElement, sourceWidth, sourceHeight, this._element, this.cacheKey);
做这个:
var newSource = fabric.util.createCanvasElement();
newSource.width = sourceWidth;
newSource.height = sourceHeight;
var newSourceCtx = newSource.getContext('2d');
newSourceCtx.drawImage(this._originalElement, 0, 0, sourceWidth, sourceHeight);
filters.forEach(function(filter) {
if (!filter) {
return;
}
var backend = fabric.filterBackend;
if (filter.fragmentSource === null) {
backend = backend.fallback2dBackend;
}
backend.applyFilters(
[filter], newSource, sourceWidth, sourceHeight, this._element);
newSourceCtx.clearRect(0, 0, sourceWidth, sourceHeight);
newSourceCtx.drawImage(this._element, 0, 0, sourceWidth, sourceHeight);
}, this);
我已经发布了一个 fork,以防有人想将其重构为 PR。
推荐阅读
- python - 如何将有限的输入时间添加到我的代码中?
- node.js - 来自对象 TypeError 的反应显示数据:值映射不是函数
- android - 登录不打算在为用户角色分配的活动中
- oracle - ORA-00900: 无效的 SQL 语句 - 在另一个内部调用过程
- flutter - 在 Flutter 中使用 createUserWithEmailAndPassword 的新用户的默认个人资料图片
- python - 当超链接不可见时处理点击
- windows-10 - WIn10 wsl2:Ubuntu变得致命:无法访问'https:无法连接到github.com端口443:连接超时
- r - R循环分位数,除非
- c - 用 c 写“W”字母
- postgresql - Sequelize 如何构建应用程序的聊天部分?