{"size": 2492, "ext": "js", "max_stars_repo_path": "image-helper.js", "max_stars_repo_name": "Phalpo88/skribbl-io-autodraw", "max_stars_repo_stars_event_min_datetime": "2019-01-21T18:35:59.000Z", "max_stars_repo_stars_event_max_datetime": "2021-08-07T15:05:45.000Z", "max_issues_repo_path": "image-helper.js", "max_issues_repo_name": "Phalpo88/skribbl-io-autodraw", "max_issues_repo_issues_event_min_datetime": "2019-02-18T18:03:46.000Z", "max_issues_repo_issues_event_max_datetime": "2019-03-22T03:46:33.000Z", "max_forks_repo_path": "image-helper.js", "max_forks_repo_name": "Phalpo88/skribbl-io-autodraw", "max_forks_repo_forks_event_min_datetime": "2019-02-18T12:53:18.000Z", "max_forks_repo_forks_event_max_datetime": "2021-08-07T15:05:28.000Z"}
{"max_stars_count": 12, "max_issues_count": 1, "max_forks_count": 18, "avg_line_length": 32.7894736842, "max_line_length": 95, "alphanum_fraction": 0.5754414125}
let createImageHelper = function (image) {
    let createCanvas = function (canvasSize) {
        let canvas = document.createElement("canvas");
        canvas.width = canvasSize.width;
        canvas.height = canvasSize.height;
        return canvas;
    };

    let fill = function (canvas, context) {
        let factor = Math.max(canvas.width / image.width, canvas.height / image.height);

        let dw = factor * image.width;
        let dh = factor * image.height;

        let dx = (canvas.width - dw) / 2;
        let dy = (canvas.height - dh) / 2;

        context.drawImage(image, dx, dy, dw, dh);
    };

    let fit = function (canvas, context) {
        let factor = Math.min(canvas.width / image.width, canvas.height / image.height);

        let dw = factor * image.width;
        let dh = factor * image.height;

        let dx = (canvas.width - dw) / 2;
        let dy = (canvas.height - dh) / 2;

        context.drawImage(image, dx, dy, dw, dh);
    };

    let getImageData = function (canvas, context) {
        let imageData = context.getImageData(0, 0, canvas.width, canvas.height);

        imageData.getRgbObject = function (coords) {
            return {
                r: this.data[4 * (coords.x + coords.y * this.width) + 0],
                g: this.data[4 * (coords.x + coords.y * this.width) + 1],
                b: this.data[4 * (coords.x + coords.y * this.width) + 2]
            };
        };

        return imageData;
    };

    let clear = function (canvas, context, color) {
        context.fillStyle = `rgb(${color.r}, ${color.g}, ${color.b})`;
        context.fillRect(0, 0, canvas.width, canvas.height);
    };

    let normalizeImageData = function (normalize, size, backgroundColor) {
        let canvas = createCanvas(size);
        let context = canvas.getContext("2d");

        if (backgroundColor) {
            clear(canvas, context, backgroundColor);
        }

        normalize(canvas, context);
        return getImageData(canvas, context);
    };

    return {
        fillImageData: function (size, backgroundColor) {
            log(`Normalizing image to ${size.width} x ${size.height} using fill transform...`);
            return normalizeImageData(fill, size, backgroundColor);
        },

        fitImageData: function (size, backgroundColor) {
            log(`Normalizing image to ${size.width} x ${size.height} using fit transform...`);
            return normalizeImageData(fit, size, backgroundColor);
        }
    };
};
