简体   繁体   English

如何在fabric.js中运行WebGl图像过滤器

[英]How do I run a WebGl Image filter in fabric.js

I'm trying to execute the following snippet of code [ http://fabricjs.com/fabric-intro-part-2#image_filters] 我正在尝试执行以下代码段[ http://fabricjs.com/fabric-intro-part-2#image_filters]

  fabric.Image.filters.Redify = fabric.util.createClass({

  type: 'Redify',

  /**
   * Fragment source for the redify program
   */
  fragmentSource: 'precision highp float;\n' +
    'uniform sampler2D uTexture;\n' +
    'varying vec2 vTexCoord;\n' +
    'void main() {\n' +
      'vec4 color = texture2D(uTexture, vTexCoord);\n' +
      'color.g = 0;\n' +
      'color.b = 0;\n' +
      'gl_FragColor = color;\n' +
    '}',

  applyTo2d: function(options) {
    var imageData = options.imageData,
        data = imageData.data, i, len = data.length;

    for (i = 0; i < len; i += 4) {
      data[i + 1] = 0;
      data[i + 2] = 0;
    }

  }
});

fabric.Image.filters.Brightness.fromObject = fabric.Image.filters.BaseFilter.fromObject;

But I get 但是我明白了

Error:
filter.applyTo is not a function

I've tried changing the fabric backend to const webglBackend = new fabric.WebglFilterBackend() fabric.filterBackend = webglBackend 我尝试将结构后端更改为const webglBackend = new fabric.WebglFilterBackend()fabric.filterBackend = webglBackend

but it still returns that error, is there an configuration option that I have missed somewhere? 但它仍然会返回该错误,我是否错过了某个配置选项? Changing the applyTo2d function to applyTo gets rid of the error but "options" won't have options.imageData. 将applyTo2d函数更改为applyTo可以消除错误,但“ options”将没有options.imageData。

You should create the filter by extending the BaseFilter class, like this 您应该通过扩展BaseFilter类来创建过滤器,如下所示

fabric.Image.filters.MyFilter = fabric.util.createClass(fabric.Image.filters.BaseFilter, { 
   ... 
})

Also have a look here https://github.com/fabricjs/fabric.js/blob/master/src/filters/filter_boilerplate.js 也可以在这里看看https://github.com/fabricjs/fabric.js/blob/master/src/filters/filter_boilerplate.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM