bookcover-3d-web

中文文档
0.24.0发布于 2026/11/03MIT-0TypeScriptES Module零依赖

Create 3D images of books by providing the book cover designs. Part of the bookcover generation system.

原始使用文档
文档目录4 节
  1. generate(svgs, options?): Promise<Uint8Array>
  2. Book3DRenderer
  3. Full API
  4. Photo compositing

bookcover-3d-web

Create 3D images of books by providing the book cover designs. Part of the bookcover generation system.

npm install bookcover-3d-web

Example 3D image

This is a WebGL 3D book renderer with no external graphics dependencies.

import {Book3DRenderer, generate} from 'bookcover-3d-web'
import type {BookFaces, CoverType, GenerateOptions} from 'bookcover-3d-web'

Both exports take the same SVG face inputs and produce the same kind of output. The difference is lifecycle: generate is a one-shot helper that creates and destroys a renderer internally, while Book3DRenderer keeps the WebGL context alive so you can re-render cheaply (e.g. on mouse drag) without reloading textures.

Use generate for static exports. Use Book3DRenderer for interactive previews.

generate(svgs, options?): Promise<Uint8Array>

Creates a renderer, loads the SVGs, renders once, returns PNG bytes, then cleans up.

import {generate} from 'bookcover-3d-web'

const png = await generate(
    {front: frontSvg, back: backSvg, spine: spineSvg},
    {cover_type: 'paperback', azimuth: -30, width: 800, height: 600},
)
interface BookFaces {
    front: string   // SVG string
    back: string    // SVG string
    spine?: string  // SVG string (optional — depth_mm used if absent)
}

interface GenerateOptions {
    cover_type?: 'paperback'|'paperback_coil'|'paperback_wire'|'paperback_stitch'|'hardcover'|'hardcover_jacket'
    azimuth?: number     // horizontal camera angle in degrees (default: -30)
    elevation?: number   // vertical camera angle in degrees (default: 20)
    roll?: number        // clockwise rotation in degrees (default: 0)
    width?: number       // canvas width in pixels (default: 800)
    height?: number      // canvas height in pixels (default: 600)
}

Book3DRenderer

Persistent renderer for interactive use. Load SVGs once; call render() repeatedly with different angles without reloading textures each time.

import {Book3DRenderer} from 'bookcover-3d-web'

// Create once
const renderer = new Book3DRenderer(800, 600)

// Load SVGs and build geometry (do this when the cover changes)
await renderer.load({front: frontSvg, back: backSvg, spine: spineSvg}, 'paperback')

// Render at any angle — fast, no texture reload
renderer.render(-30, 20)   // azimuth, elevation

// Export the current frame
const png = await renderer.to_png()

// Free GPU resources when done
renderer.destroy()

Full API

const renderer = new Book3DRenderer(width?, height?)

// Load cover face SVGs and build 3D geometry
await renderer.load(svgs: BookFaces, cover_type?: CoverType, depth_mm?: number)

// Render at camera angles (all in degrees)
renderer.render(azimuth?, elevation?, zoom?, roll?, light_az?, light_el?, ambient?, exposure?)

// Resize without recreating WebGL context
renderer.resize(width, height)

// Get projected width/height ratio at default viewing angle
renderer.get_projected_aspect(): number

// Composite onto a background photo
await renderer.composite_photo(background: ImageBitmap, options?: PhotoCompositeOptions)

// Export current frame
await renderer.snapshot(): Promise<ImageBitmap>
await renderer.to_png(): Promise<Uint8Array>

// Free GPU resources
renderer.destroy()

Photo compositing

composite_photo renders the book at a certain angle and composites it over a background image, with a shadow derived from the light direction. It returns an ImageBitmap at the background's native resolution.

Example photo

The library describes a set of background photos via BACKGROUNDS (metadata only — the JPGs themselves are not bundled in the npm package; they live in the repo's assets tree for you to host alongside your other static assets). Each entry has an id (to identify which image to load) and pre-tuned camera/lighting options that make the book look natural in that scene. Pass the background entry directly as the options argument — it extends PhotoCompositeOptions.

import {Book3DRenderer, BACKGROUNDS} from 'bookcover-3d-web'
import type {Background} from 'bookcover-3d-web'

// Find the background you want
const bg: Background = BACKGROUNDS.find(b => b.id === 'coffee_table')!

// Load the image however you serve your assets
const img = await fetch(`/assets/3d/backgrounds/${bg.id}.jpg`)
const blob = await img.blob()
const bitmap = await createImageBitmap(blob)

// Render the book, then composite — pass the background entry as options
await renderer.load({front: frontSvg, back: backSvg, spine: spineSvg})
const result = await renderer.composite_photo(bitmap, bg)

// result is an ImageBitmap at the background's native resolution

Available background IDs: table_with_book, wood, coffee_table, table_with_laptop, table_side.

You can also composite onto your own image by passing a plain PhotoCompositeOptions object (all fields optional — defaults give a flat overhead perspective):

interface PhotoCompositeOptions {
    azimuth?: number      // horizontal camera angle in degrees (default: -15)
    elevation?: number    // vertical camera angle in degrees (default: 35)
    zoom?: number
    roll?: number         // clockwise rotation in degrees (default: 0)
    book_scale?: number   // book width as fraction of background width (default: 0.55)
    offset_x?: number     // position offset as fraction of background width (0 = centred)
    offset_y?: number     // position offset as fraction of background height (0 = centred)
    light_az?: number     // light horizontal angle in degrees
    light_el?: number     // light vertical angle in degrees
    ambient?: number      // ambient light level 0–1
}

版本与发布记录

bookcover-3d-web 共发布 28 个版本,首次发布于 2026/07/16;过去 12 个月发布了 28 个版本。

版本发布日期距今
0.24.0latest2026/11/03今天
0.23.02026/10/267 天前
0.22.32026/10/2311 天前
0.22.22026/10/2212 天前
0.22.12026/10/2212 天前
0.22.02026/10/2212 天前

关于 bookcover-3d-web 的常见问题

bookcover-3d-web 是什么?

通过提供书籍封面设计,创建书籍的3D图像。部分 封套 一代系统。

如何安装 bookcover-3d-web?

在项目目录运行 npm install bookcover-3d-web,或使用 pnpm add / yarn add / bun add。

bookcover-3d-web 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

bookcover-3d-web 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

bookcover-3d-web 可以商用吗?

bookcover-3d-web 使用 MIT-0 许可。声明的许可为 MIT-0,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

bookcover-3d-web 还在维护吗?

最新版本 0.24.0 发布于 2026/11/03(今天);至今共发布 28 个版本,其中过去 12 个月发布了 28 个;首次发布于 2026/07/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。