pdf-to-png-converter
Version:
Node.js utility to convert PDF file/buffer pages to PNG files/buffers with no native dependencies.
48 lines (47 loc) • 2.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeCanvasFactory = void 0;
const canvas_1 = require("@napi-rs/canvas");
const node_assert_1 = require("node:assert");
class NodeCanvasFactory {
/**
* Creates a new canvas context with the specified width and height.
* @param width The width of the canvas.
* @param height The height of the canvas.
* @returns A new canvas context with the specified width and height.
* @throws An error if the width or height is less than or equal to zero.
*/
create(width, height) {
node_assert_1.strict.ok(width > 0 && height > 0, 'Canvas width and height must be greater than zero');
const canvas = new canvas_1.Canvas(width, height);
return {
canvas,
context: canvas.getContext('2d'),
};
}
/**
* Resets the canvas to the specified width and height.
* @param canvasAndContext - The canvas and its context.
* @param width - The new width of the canvas.
* @param height - The new height of the canvas.
*/
reset(canvasAndContext, width, height) {
node_assert_1.strict.ok(canvasAndContext.canvas, 'Canvas object is required');
node_assert_1.strict.ok(width > 0 && height > 0, 'Canvas width and height must be greater than zero');
canvasAndContext.canvas.width = width;
canvasAndContext.canvas.height = height;
}
/**
* Destroys the canvas and its context by setting the canvas width and height to 0 and
* setting the canvas and context properties to undefined.
* @param canvasAndContext - The canvas and its context to be destroyed.
*/
destroy(canvasAndContext) {
node_assert_1.strict.ok(canvasAndContext.canvas, 'Canvas object is required');
canvasAndContext.canvas.width = 0;
canvasAndContext.canvas.height = 0;
canvasAndContext.canvas = null;
canvasAndContext.context = null;
}
}
exports.NodeCanvasFactory = NodeCanvasFactory;