@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
184 lines (183 loc) • 7.26 kB
TypeScript
export interface RenderOptions {
/** Width of the rendered image in pixels */
width?: number;
/** Height of the rendered image in pixels */
height?: number;
/** Time to wait for scene to render (ms) */
renderWaitTime?: number;
/** Time to wait for canvas element to appear and stabilize (ms). Default: 30000 for CI reliability with SwiftShader */
canvasTimeout?: number;
/** Use fast mode - reduces wait times and reuses page (default: false) */
fastMode?: boolean;
/** Camera distance from subject */
cameraDistance?: number;
/** Camera angle (radians) */
cameraAlpha?: number;
cameraBeta?: number;
/** Clip region for screenshot - crops out borders from the page */
clip?: {
x: number;
y: number;
width: number;
height: number;
};
/** Image format: 'png' or 'jpeg' (default: 'png') */
imageFormat?: "png" | "jpeg";
/** JPEG quality 0-100 (default: 80). Only applies when imageFormat is 'jpeg' */
jpegQuality?: number;
/** Force a fresh context even if viewport hasn't changed. Use when content at the same URL has changed. */
forceNewContext?: boolean;
/** Force a page reload even when reusing context. Use for multi-angle renders where only URL params change. */
forceReload?: boolean;
/**
* When rendering blocks, skip the gradient skybox and paint a uniform light
* sky-blue clearColor instead. Used by the block sprite atlas pipeline so
* composited thumbnails don't carry a gradient background.
*/
flatBackground?: boolean;
}
export interface RenderResult {
/** Image data as Uint8Array (PNG or JPEG depending on options) */
imageData: Uint8Array | undefined;
/** Error message if rendering failed */
error?: string;
/** Browser used for rendering */
browserUsed?: string;
/** Image format of the returned data */
imageFormat?: "png" | "jpeg";
}
export default class PlaywrightPageRenderer {
private _baseUrl;
private _browser;
private _browserName;
private _playwright;
private _persistentContext;
private _persistentPage;
private _persistentViewport;
private _lastModelPath;
private _lastFullUrl;
constructor(baseUrl?: string);
/**
* Reset the persistent page/context. Call this when switching between
* different model types to ensure clean state.
*/
resetPersistentPage(): Promise<void>;
/**
* Get the list of browser configurations to try, in order of preference.
*/
private _getBrowserConfigs;
/**
* Initialize the browser with fallback strategy.
* Tries multiple browser configurations until one works.
*/
initialize(): Promise<boolean>;
/**
* Warm up the browser by creating and destroying a test context.
* This ensures the browser is fully ready before the first real render.
* Call this after initialize() to improve first-render reliability.
*/
warmUp(): Promise<boolean>;
/**
* Check if the browser is connected and responsive.
*/
isBrowserReady(): boolean;
/**
* Render a model to PNG.
*
* @param modelPath - URL path to the model viewer page
* @param options - Rendering options
*/
renderModel(modelPath: string, options?: RenderOptions): Promise<RenderResult>;
/**
* Fast render method that reuses page for batch operations.
* Much faster than renderModel() because it skips context creation overhead.
*
* @param modelPath - URL path to navigate to
* @param options - Rendering options
*/
renderModelFast(modelPath: string, options?: RenderOptions): Promise<RenderResult>;
/**
* Render a block to PNG using the BlockViewer.
* Uses headless mode to hide UI chrome and get full-viewport canvas.
*/
renderBlock(blockName: string, options?: RenderOptions): Promise<RenderResult>;
/**
* Render multiple blocks efficiently, reusing the browser instance and page.
* Uses fast mode by default for significantly better performance.
* @param blocks - Array of { name, outputPath } objects
* @param options - Rendering options (fastMode defaults to true for batch)
* @param onProgress - Optional callback for progress reporting
* @returns Array of results with block names
*/
renderBlocks(blocks: Array<{
name: string;
outputPath: string;
}>, options?: RenderOptions, onProgress?: (blockName: string, index: number, total: number) => void): Promise<Array<{
name: string;
success: boolean;
error?: string;
}>>;
/**
* Render a mob/entity to PNG using the MobViewer.
* Uses headless mode to hide UI chrome and get full-viewport canvas.
*/
renderMob(mobId: string, options?: RenderOptions): Promise<RenderResult>;
/**
* Render an item/attachable to PNG using the ItemViewer.
* Uses headless mode to hide UI chrome and get full-viewport canvas.
*/
renderItem(itemId: string, options?: RenderOptions): Promise<RenderResult>;
/**
* Render multiple mobs efficiently, reusing the browser instance and page.
* Uses fast mode by default for significantly better performance.
* @param mobs - Array of { name, outputPath } objects
* @param options - Rendering options (fastMode defaults to true for batch)
* @param onProgress - Optional callback for progress reporting
* @returns Array of results with mob names
*/
renderMobs(mobs: Array<{
name: string;
outputPath: string;
}>, options?: RenderOptions, onProgress?: (mobName: string, index: number, total: number) => void): Promise<Array<{
name: string;
success: boolean;
error?: string;
}>>;
/**
* Render multiple items/attachables efficiently, reusing the browser instance and page.
* Uses fast mode by default for significantly better performance.
* @param items - Array of { name, outputPath } objects
* @param options - Rendering options (fastMode defaults to true for batch)
* @param onProgress - Optional callback for progress reporting
* @returns Array of results with item names
*/
renderItems(items: Array<{
name: string;
outputPath: string;
}>, options?: RenderOptions, onProgress?: (itemName: string, index: number, total: number) => void): Promise<Array<{
name: string;
success: boolean;
error?: string;
}>>;
/**
* Shared batch rendering logic for mobs, items, or any entity type.
* Reuses the browser instance and page for efficiency.
*/
private _renderBatch;
/**
* Render a custom model geometry with texture to PNG.
* Uses a custom route that accepts geometry and texture data.
*
* @param geometryId - Identifier for the geometry (used in URL)
* @param options - Rendering options
*/
renderCustomModel(geometryId: string, options?: RenderOptions): Promise<RenderResult>;
/**
* Close the browser and clean up resources.
*/
close(): Promise<void>;
/**
* Get information about the current browser being used.
*/
getBrowserInfo(): string;
}