@objectifthunes/react-three-book 0.1.1 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/index.d.ts +132 -2
  2. package/dist/index.js +744 -583
  3. package/package.json +1 -1
package/dist/index.d.ts CHANGED
@@ -385,13 +385,16 @@ export declare interface BookState {
385
385
  }
386
386
 
387
387
  /**
388
- * Creates a 512×512 `THREE.CanvasTexture` suitable for use as a book page.
388
+ * Creates a `THREE.CanvasTexture` suitable for use as a book page.
389
+ *
390
+ * When `pageWidth` and `pageHeight` are given the canvas matches the page
391
+ * aspect ratio (256 px per world unit). Otherwise defaults to 512×512.
389
392
  *
390
393
  * - Fills the background with `color`.
391
394
  * - If `image` is provided, draws it using `fitMode` and `fullBleed`.
392
395
  * - Otherwise, renders `label` as centred text (useful for debugging).
393
396
  */
394
- export declare function createPageTexture(color: string, label: string, image: HTMLImageElement | null, fitMode: ImageFitMode, fullBleed: boolean): THREE.Texture;
397
+ export declare function createPageTexture(color: string, label: string, image: HTMLImageElement | null, fitMode: ImageFitMode, fullBleed: boolean, pageWidth?: number, pageHeight?: number): THREE.Texture;
395
398
 
396
399
  /**
397
400
  * Ported from Book.cs — Cylinder struct (lines ~3805-3902).
@@ -987,6 +990,9 @@ export declare class PaperUVMargin {
987
990
  clone(): PaperUVMargin;
988
991
  }
989
992
 
993
+ /** Pixels per world unit — used to compute canvas size from page dimensions. */
994
+ export declare const PX_PER_UNIT = 256;
995
+
990
996
  export declare class RendererFactory {
991
997
  private m_Root;
992
998
  private m_CreateMeshCollider;
@@ -1093,6 +1099,119 @@ export declare class StapleSetup {
1093
1099
  set quality(value: number);
1094
1100
  }
1095
1101
 
1102
+ export declare class TextBlock {
1103
+ x: number;
1104
+ y: number;
1105
+ width: number;
1106
+ text: string;
1107
+ fontFamily: string;
1108
+ fontSize: number;
1109
+ fontWeight: 'normal' | 'bold';
1110
+ fontStyle: 'normal' | 'italic';
1111
+ color: string;
1112
+ lineHeight: number;
1113
+ textAlign: 'left' | 'center' | 'right';
1114
+ opacity: number;
1115
+ shadowColor: string;
1116
+ shadowBlur: number;
1117
+ constructor(options?: TextBlockOptions);
1118
+ private _font;
1119
+ /**
1120
+ * Word-wrap `text` into lines that fit within `width` pixels.
1121
+ * Respects explicit newlines.
1122
+ */
1123
+ wrapLines(ctx: CanvasRenderingContext2D): string[];
1124
+ /** Total rendered height in canvas pixels. */
1125
+ measureHeight(ctx: CanvasRenderingContext2D): number;
1126
+ /** Returns true if the point (px, py) is within the text bounding box. */
1127
+ hitTest(ctx: CanvasRenderingContext2D, px: number, py: number): boolean;
1128
+ private _maxLineWidth;
1129
+ draw(ctx: CanvasRenderingContext2D): void;
1130
+ }
1131
+
1132
+ /**
1133
+ * A styled text block rendered onto a 2D canvas.
1134
+ *
1135
+ * Positions and dimensions are in canvas pixels. Word-wrapping is handled
1136
+ * manually via `ctx.measureText()` when `width` > 0.
1137
+ */
1138
+ export declare interface TextBlockOptions {
1139
+ /** Left edge of the text box in canvas pixels. */
1140
+ x?: number;
1141
+ /** Top edge of the text box in canvas pixels. */
1142
+ y?: number;
1143
+ /** Maximum width before wrapping. 0 = no wrapping (default 0). */
1144
+ width?: number;
1145
+ /** Text content. */
1146
+ text?: string;
1147
+ /** Font family (default 'Georgia'). */
1148
+ fontFamily?: string;
1149
+ /** Font size in canvas pixels (default 24). */
1150
+ fontSize?: number;
1151
+ /** Font weight (default 'normal'). */
1152
+ fontWeight?: 'normal' | 'bold';
1153
+ /** Font style (default 'normal'). */
1154
+ fontStyle?: 'normal' | 'italic';
1155
+ /** CSS fill colour (default '#222'). */
1156
+ color?: string;
1157
+ /** Line height multiplier (default 1.4). */
1158
+ lineHeight?: number;
1159
+ /** Text alignment within the box (default 'left'). */
1160
+ textAlign?: 'left' | 'center' | 'right';
1161
+ /** Opacity 0–1 (default 1). */
1162
+ opacity?: number;
1163
+ /** Optional text shadow colour for readability. */
1164
+ shadowColor?: string;
1165
+ /** Shadow blur radius in pixels (default 0). */
1166
+ shadowBlur?: number;
1167
+ }
1168
+
1169
+ export declare class TextOverlayContent implements IPageContent {
1170
+ readonly canvas: HTMLCanvasElement;
1171
+ readonly texts: TextBlock[];
1172
+ private readonly ctx;
1173
+ private readonly _texture;
1174
+ private readonly _textureST;
1175
+ private _source;
1176
+ get texture(): THREE.Texture;
1177
+ get textureST(): THREE.Vector4;
1178
+ /** The base layer drawn beneath text blocks. */
1179
+ get source(): HTMLCanvasElement | HTMLImageElement | null;
1180
+ set source(v: HTMLCanvasElement | HTMLImageElement | null);
1181
+ constructor(options?: TextOverlayContentOptions);
1182
+ addText(options?: TextBlockOptions): TextBlock;
1183
+ removeText(text: TextBlock): void;
1184
+ /** Update a text block by index. Only provided fields are changed. */
1185
+ updateText(index: number, options: Partial<TextBlockOptions>): void;
1186
+ /**
1187
+ * Re-composite the canvas: source layer + text blocks.
1188
+ * Call every frame (or when content changes).
1189
+ *
1190
+ * @param root Optional THREE.Object3D to traverse for texture sync
1191
+ * (same pattern as SpriteScene — needed because three-book
1192
+ * clones material textures).
1193
+ */
1194
+ update(root?: THREE.Object3D): void;
1195
+ /**
1196
+ * Traverse `root` and set `needsUpdate = true` on every material map whose
1197
+ * source image is this overlay's canvas.
1198
+ */
1199
+ syncMaterials(root: THREE.Object3D): void;
1200
+ isPointOverUI(_textureCoord: THREE.Vector2): boolean;
1201
+ init(_bookContent: BookContent): void;
1202
+ setActive(_active: boolean): void;
1203
+ dispose(): void;
1204
+ }
1205
+
1206
+ export declare interface TextOverlayContentOptions {
1207
+ /** Canvas width in pixels (default 512). */
1208
+ width?: number;
1209
+ /** Canvas height in pixels (default 512). */
1210
+ height?: number;
1211
+ /** Source canvas or image to draw as the base layer. */
1212
+ source?: HTMLCanvasElement | HTMLImageElement | null;
1213
+ }
1214
+
1096
1215
  export declare class ThreeBook extends THREE.Group {
1097
1216
  private static s_Instances;
1098
1217
  static get instances(): ThreeBook[];
@@ -1263,4 +1382,15 @@ export declare interface UsePageTurningOptions {
1263
1382
  /** Returns the nearest ancestor Book instance. Throws outside a <Book> tree. */
1264
1383
  export declare function useRequiredBook(): ThreeBook;
1265
1384
 
1385
+ /**
1386
+ * Creates and manages a TextOverlayContent instance with per-frame compositing.
1387
+ *
1388
+ * The returned overlay's canvas is re-composited every frame (source + text blocks).
1389
+ * Material sync is performed automatically if inside a `<Book>` tree.
1390
+ *
1391
+ * @param options Initial options (width, height, source).
1392
+ * @returns The TextOverlayContent instance (stable ref).
1393
+ */
1394
+ export declare function useTextOverlay(options?: TextOverlayContentOptions): TextOverlayContent;
1395
+
1266
1396
  export { }