Using the Caman.js Library in the Fabric.js Custom Filter

I am trying to create my own filter in Fabric.js. But I would like to use the Caman.js filter set. I feel like I'm uber here, but can't make it work. Does anyone have any experience combining the two or see where I can go wrong here?

There are no errors in the console. Right now I'm trying to install putImageDatawith new data received from Caman.js ... no luck. However, I can see the data in the console. Help?

(function(global) {

    'use strict';

    var fabric  = global.fabric || (global.fabric = { });

    /**
    * Lomo filter for fabricjs
    * Example: 
    * obj.filters.push(new fabric.Image.filters.Lomo(6));
    * obj.applyFilters(canvas.renderAll.bind(canvas));
    */
    fabric.Image.filters.Lomo = fabric.util.createClass(fabric.Image.filters.BaseFilter, {

        /**
         * Filter type
         * @param {String} type
         * @default
         */
        type: 'Lomo',

        /**
         * Applies filter to canvas element
         * @memberOf fabric.Image.filters.Lomo.prototype
         * @param {Object} canvas element to apply filter to
         */
        applyTo: function(canvas) {
            var context = canvas.getContext("2d");
            var width = canvas.width;
            var height = canvas.height;
            var imageData = context.getImageData(0, 0, width, height);

            var c = Caman(canvas, function(value) {
                this.lomo().render();
            });

            Caman.Event.listen(c, 'processComplete', function(job) {         
                console.log(this.imageData);
                context.putImageData(this.imageData, 0, 0);
            });
        }

    });

    fabric.Image.filters.Lomo.fromObject = function(object) {
        return new fabric.Image.filters.Lomo(object);
    };

})(typeof exports !== 'undefined' ? exports : this);
+4
source share

Source: https://habr.com/ru/post/1663299/


All Articles