placeholder-craft-cli
Version:
a cli tool to generate placeholder images
27 lines (26 loc) • 1.18 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPlaceholderImage = void 0;
const node_fs_1 = __importDefault(require("node:fs"));
const canvas_1 = require("canvas");
const createPlaceholderImage = ({ width, height, bgColor = "grey", textColor = "white", filename = "placeholder.png", }) => {
if (width <= 0 || height <= 0) {
throw new Error("Width and height must be greater than 0");
}
const canvas = (0, canvas_1.createCanvas)(width, height);
const ctx = canvas.getContext("2d");
ctx.fillStyle = bgColor;
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = textColor;
const fontSize = Math.min(width, height) / 2;
ctx.font = `${fontSize}px Arial`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(`${width} * ${height}`, width / 2, height / 2, width - 20);
const buffer = canvas.toBuffer("image/png");
node_fs_1.default.writeFileSync(filename, buffer);
};
exports.createPlaceholderImage = createPlaceholderImage;