filmout_manager/dist/testimage/index.js

92 lines
3.5 KiB
JavaScript

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestImage = void 0;
const PImage = __importStar(require("pureimage"));
const fs_1 = require("fs");
const os_1 = require("os");
const path_1 = require("path");
const uuid_1 = require("uuid");
class TestImage {
static async Frame(width, height, edge = 2) {
const id = (0, uuid_1.v4)();
const filePath = (0, path_1.join)((0, os_1.tmpdir)(), `frame-${id}.png`);
const img = PImage.make(width, height);
const ctx = img.getContext('2d');
const halfW = Math.round(width / 2) - (edge > 1 ? Math.round(edge / 2) : 0);
const halfH = Math.round(height / 2) - (edge > 1 ? Math.round(edge / 2) : 0);
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, width, edge);
ctx.fillRect(0, 0, edge, height);
ctx.fillRect(0, height - edge, width, edge);
ctx.fillRect(width - edge, 0, edge, height);
ctx.fillRect(halfW, 0, edge, height);
ctx.fillRect(0, halfH, width, edge);
try {
await PImage.encodePNGToStream(img, (0, fs_1.createWriteStream)(filePath));
}
catch (err) {
throw err;
}
return filePath;
}
static async Focus(width, height, count = 20) {
const id = (0, uuid_1.v4)();
const filePath = (0, path_1.join)((0, os_1.tmpdir)(), `frame-${id}.png`);
const img = PImage.make(width, height);
const ctx = img.getContext('2d');
const half = Math.round(count / 2);
const dpr = 1;
const w = width / dpr;
const h = height / dpr;
const longest = w >= h ? w * 1.5 : h * 1.5;
const opp = Math.tan(360 / (count * 32)) * longest / 10;
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, w, h);
ctx.fillStyle = 'white';
for (let i = 0; i < count; i++) {
ctx.beginPath();
ctx.moveTo(w / 2, h / 2);
ctx.lineTo((w / 2) + opp, longest);
ctx.lineTo((w / 2) - opp, longest);
ctx.fill();
ctx.translate(w / 2, h / 2);
ctx.rotate((360 / count) * Math.PI / 180);
ctx.translate(-w / 2, -h / 2);
}
try {
await PImage.encodePNGToStream(img, (0, fs_1.createWriteStream)(filePath));
}
catch (err) {
throw err;
}
return filePath;
}
}
exports.TestImage = TestImage;
module.exports = { TestImage };
//# sourceMappingURL=index.js.map