All files load-image.ts

100% Statements 33/33
100% Branches 6/6
100% Functions 5/5
100% Lines 33/33

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 331x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 2x 2x 2x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x
 
/**
 * 加载图片
 * @param url - 图片地址
 */
export function loadImage(url: string): Promise<HTMLImageElement> {
    return new Promise<HTMLImageElement>((resolve, reject) => {
        const img = new Image();
        img.setAttribute('crossOrigin', 'Anonymous');
        img.onload = function() {
            resolve(img);
        };
 
        img.onerror = function(error) {
            reject(new Error(`load image failed: ${error}`));
        }
        img.src = url;
    });
}
 
/**
 * 加载图片像素数据
 * @param image - 图片
 */
export function loadImageData(image: HTMLImageElement): ImageData {
    const canvas = document.createElement('canvas');
    canvas.width = image.width;
    canvas.height = image.height;
    const context = canvas.getContext('2d')!;
    console.log(image)
    context.drawImage(image, image.width, image.height);
    return context.getImageData(0, 0, image.width, image.height);
}