Innovenergy_trunk/frontend/node_modules/testcafe/lib/screenshots/utils.js

37 lines
6.7 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.copyImagePart = exports.generateScreenshotMark = void 0;
const pngjs_1 = require("pngjs");
const lodash_1 = require("lodash");
const nanoid_1 = require("nanoid");
const constants_1 = require("./constants");
const ALPHABET = '01';
function generateScreenshotMark() {
// NOTE: 32-bit id
const id = (0, nanoid_1.customAlphabet)(ALPHABET, constants_1.MARK_LENGTH)();
// NOTE: array of RGB values
const markSeed = (0, lodash_1.flatten)((0, lodash_1.map)(id, bit => bit === '0' ? [0, 0, 0, 255] : [255, 255, 255, 255]));
// NOTE: macOS browsers can't display an element, if it's CSS height is lesser than 1.
// It happens on Retina displays, because they have more than 1 physical pixel in a CSS pixel.
// So increase mark size by prepending transparent pixels before the actual mark.
const imageData = (0, lodash_1.times)(constants_1.MARK_BYTES_PER_PIXEL * constants_1.MARK_LENGTH * (constants_1.MARK_HEIGHT - 1), (0, lodash_1.constant)(0)).concat(markSeed);
const imageDataBuffer = Buffer.from(imageData);
const pngImage = new pngjs_1.PNG({ width: constants_1.MARK_LENGTH, height: constants_1.MARK_HEIGHT });
imageDataBuffer.copy(pngImage.data);
const markData = 'data:image/png;base64,' + pngjs_1.PNG.sync.write(pngImage).toString('base64');
return { markSeed, markData };
}
exports.generateScreenshotMark = generateScreenshotMark;
function copyImagePart(pngImage, { clipLeft, clipTop, clipRight, clipBottom }) {
const width = clipRight - clipLeft;
const height = clipBottom - clipTop;
const dstImage = new pngjs_1.PNG({ width, height });
const stride = dstImage.width * constants_1.MARK_BYTES_PER_PIXEL;
for (let i = 0; i < height; i++) {
const srcStartIndex = (pngImage.width * (i + clipTop) + clipLeft) * constants_1.MARK_BYTES_PER_PIXEL;
pngImage.data.copy(dstImage.data, stride * i, srcStartIndex, srcStartIndex + stride);
}
return dstImage;
}
exports.copyImagePart = copyImagePart;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9u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