JSFiddle - React, Tailwind, and code Playground

by eithe

JavaScript

function chunk(arr, n) {
        return arr.reduce(function(p, cur, i) {
            (p[i/n|0] = p[i/n|0] || []).push(cur);
            return p;
        },[]);
    }

    function calculate_weight(pixel, args)
    {
        switch(args.algorithm)
        {
            case 'surrounding':
                // get the weight of itself
                return (pixel[0]*256*256 + pixel[1]*256 + pixel[2]) + 
                       (pixel[1]*256*256 + pixel[2]*256 + pixel[0]) + 
                       (pixel[2]*256*256 + pixel[0]*256 + pixel[1]) +
                // add 1/2 of surrounding weight
                    args.surrounding*(pixel.location.left ? calculate_weight(pixel.location.left, {algorightm: 'balanced'}) : 0) +
                args.surrounding*(pixel.location.right ? calculate_weight(pixel.location.right, {algorightm: 'balanced'}): 0) +
                args.surrounding*(pixel.location.top ? calculate_weight(pixel.location.top, {algorightm: 'balanced'}) : 0) +
                args.surrounding*(pixel.location.bottom ? calculate_weight(pixel.location.bottom, {algorightm: 'balanced'}) : 0)

                
            break;
                
            case 'balanced': default:
                return (args.noise || 1) * (pixel[0]*256*256 + pixel[1]*256 + pixel[2]) + 
                       (pixel[1]*256*256 + pixel[2]*256 + pixel[0]) + 
                       (pixel[2]*256*256 + pixel[0]*256 + pixel[1])
            break;
        }
    }

    function f(im_1, im_2, args)
    {
        if (!args)
            args = {};
        
        if (!args.speed)
            args.speed = 100;

        if (!args.algorithm)
            args.algorithm = 'balanced';

        if (!args.surrounding)
            args.surrounding = 0.5;
        
        if (!args.noise)
            args.noise = 1;

        if (typeof im_1 == 'string')
        {
            var img_1 = new Image();
            img_1.crossOrigin = "Anonymous";
            img_1.src = im_1.indexOf("http://") != -1 ?...