All files dyn-image.js

48.64% Statements 36/74
51.85% Branches 14/27
76.92% Functions 10/13
50% Lines 36/72

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122  2x   2x 2x 2x 2x 2x 2x 1x       2x 2x 1x     2x 2x               2x     2x           2x         2x 2x 2x 2x         2x       1x       1x 1x 1x   1x 1x   1x 1x                       1x 1x 1x                                                             1x 1x       19x       8x    
export function loadImage(src, doExif) {
    Iif (!src) { throw 'Source image missing'; }
 
    const img = new Image();
    img.style.opacity = '0';
    return new Promise( (resolve, reject) => {
        const _resolve = () => {
            img.style.opacity = '1';
            setTimeout(() => {
                resolve(img);
            }, 1);
        }
 
        img.removeAttribute('crossOrigin');
        if (src.match(/^https?:\/\/|^\/\//)) {
            img.setAttribute('crossOrigin', 'anonymous');
        }
 
        img.onload = function () {
            Iif (doExif) {
                if(EXIF) {
                    EXIF.getData(img, () => _resolve());
                } else {
                    reject("Option 'exif' need global Object 'EXIF' from exif-js library");
                }
            }
            else {
                _resolve();
            }
        };
        img.onerror = function (ev) {
            img.style.opacity = 1;
            setTimeout(function () {
                reject(ev);
            }, 1);
        };
        img.src = src;
    });
}
 
export function naturalImageDimensions(img, ornt) {
    let w = img.naturalWidth;
    let h = img.naturalHeight;
    const orient = ornt || getExifOrientation(img);
    Iif (orient && orient >= 5) {
        const x= w;
        w = h;
        h = x;
    }
    return { width: w, height: h };
}
 
export function getExifOrientation (img) {
    return img.exifdata && img.exifdata.Orientation ? num(img.exifdata.Orientation) : 1;
}
 
export function drawCanvas(canvas, img, orientation) {
    const width = img.width,
        height = img.height,
        ctx = canvas.getContext('2d');
 
    canvas.width = img.width;
    canvas.height = img.height;
 
    ctx.save();
    switch (orientation) {
        case 2:
            ctx.translate(width, 0);
            ctx.scale(-1, 1);
            break;
 
        case 3:
            ctx.translate(width, height);
            ctx.rotate(180*Math.PI/180);
            break;
 
        case 4:
            ctx.translate(0, height);
            ctx.scale(1, -1);
            break;
 
        case 5:
            canvas.width = height;
            canvas.height = width;
            ctx.rotate(90*Math.PI/180);
            ctx.scale(1, -1);
            break;
 
        case 6:
            canvas.width = height;
            canvas.height = width;
            ctx.rotate(90*Math.PI/180);
            ctx.translate(0, -height);
            break;
 
        case 7:
            canvas.width = height;
            canvas.height = width;
            ctx.rotate(-90*Math.PI/180);
            ctx.translate(-width, height);
            ctx.scale(1, -1);
            break;
 
        case 8:
            canvas.width = height;
            canvas.height = width;
            ctx.translate(0, width);
            ctx.rotate(-90*Math.PI/180);
            break;
    }
    ctx.drawImage(img, 0,0, width, height);
    ctx.restore();
}
 
export function fix(v, decimalPoints) {
    return parseFloat(v).toFixed(decimalPoints || 0);
}
 
export function num(v) {
    return parseInt(v, 10);
}