react-3d-flipbook 1.1.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.
@@ -0,0 +1,158 @@
1
+ import type { FlipbookPage } from "../types";
2
+ export interface FlipbookSizeOptions {
3
+ /** Maximum width constraint for the container (default: 1200) */
4
+ maxWidth?: number;
5
+ /** Maximum height constraint for the container (default: 900) */
6
+ maxHeight?: number;
7
+ /** Whether the flipbook is in single-page mode (default: false) */
8
+ singlePageMode?: boolean;
9
+ }
10
+ export interface FlipbookSize {
11
+ /** Calculated container width in pixels */
12
+ width: number;
13
+ /** Calculated container height in pixels */
14
+ height: number;
15
+ /** The aspect ratio of the container (width / height) */
16
+ aspectRatio: number;
17
+ /** Whether the page is landscape orientation */
18
+ isLandscape: boolean;
19
+ }
20
+ /**
21
+ * Calculate optimal flipbook container size based on page dimensions
22
+ *
23
+ * This function calculates the best container size that:
24
+ * - Maintains the PDF page aspect ratio
25
+ * - Fits within the specified max width/height constraints
26
+ * - Accounts for single-page vs two-page spread modes
27
+ *
28
+ * @param pageWidth - The PDF page width (in points or pixels)
29
+ * @param pageHeight - The PDF page height (in points or pixels)
30
+ * @param options - Sizing options
31
+ * @returns Calculated container dimensions
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * // Get first page info from PDF
36
+ * const pdfInfo = await getPdfInfo('document.pdf');
37
+ * const firstPage = pdfInfo.pageInfos[0];
38
+ *
39
+ * // Calculate optimal size for single-page mode
40
+ * const size = calculateFlipbookSize(firstPage.width, firstPage.height, {
41
+ * singlePageMode: true,
42
+ * maxWidth: 800,
43
+ * maxHeight: 600,
44
+ * });
45
+ *
46
+ * // Use with Flipbook component
47
+ * <Flipbook pages={pages} width={size.width} height={size.height} singlePageMode />
48
+ * ```
49
+ */
50
+ export declare function calculateFlipbookSize(pageWidth: number, pageHeight: number, options?: FlipbookSizeOptions): FlipbookSize;
51
+ /**
52
+ * Calculate optimal flipbook size from a FlipbookPage array
53
+ * Uses the first page's dimensions for calculation
54
+ *
55
+ * @param pages - Array of FlipbookPage objects (must have at least one page with width/height)
56
+ * @param options - Sizing options
57
+ * @returns Calculated container dimensions, or null if pages don't have size info
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * const pages = await pdfToFlipbookPages('document.pdf');
62
+ * const size = calculateFlipbookSizeFromPages(pages, { singlePageMode: true });
63
+ *
64
+ * if (size) {
65
+ * <Flipbook pages={pages} width={size.width} height={size.height} />
66
+ * }
67
+ * ```
68
+ */
69
+ export declare function calculateFlipbookSizeFromPages(pages: FlipbookPage[], options?: FlipbookSizeOptions): FlipbookSize | null;
70
+ /**
71
+ * Set the PDF.js worker source URL
72
+ * Must be called before using any PDF functions
73
+ *
74
+ * @example
75
+ * // For Vite
76
+ * import workerSrc from 'pdfjs-dist/build/pdf.worker.mjs?url';
77
+ * setPdfWorkerSrc(workerSrc);
78
+ *
79
+ * // For CDN
80
+ * setPdfWorkerSrc('https://unpkg.com/pdfjs-dist@4.0.379/build/pdf.worker.mjs');
81
+ */
82
+ export declare function setPdfWorkerSrc(workerSrc: string): void;
83
+ export interface PdfToImagesOptions {
84
+ /** Scale factor for rendering (default: 2 for high DPI) */
85
+ scale?: number;
86
+ /** Image format: 'png' or 'jpeg' (default: 'png') */
87
+ format?: "png" | "jpeg";
88
+ /** JPEG quality 0-1 (default: 0.92) */
89
+ quality?: number;
90
+ /** Callback for progress updates */
91
+ onProgress?: (current: number, total: number) => void;
92
+ /** Specific page numbers to render (1-indexed). If not provided, renders all pages */
93
+ pageNumbers?: number[];
94
+ }
95
+ export interface PdfPageInfo {
96
+ /** Page number (1-indexed) */
97
+ pageNumber: number;
98
+ /** Original PDF page width in points */
99
+ width: number;
100
+ /** Original PDF page height in points */
101
+ height: number;
102
+ /** Detected orientation */
103
+ orientation: "portrait" | "landscape";
104
+ /** Data URL of the rendered image */
105
+ dataUrl: string;
106
+ }
107
+ /**
108
+ * Load a PDF and convert pages to images
109
+ *
110
+ * @param source - PDF source: URL string, ArrayBuffer, or Uint8Array
111
+ * @param options - Conversion options
112
+ * @returns Promise resolving to array of FlipbookPage objects
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * import { pdfToFlipbookPages, setPdfWorkerSrc } from 'react-3d-flipbook/utils';
117
+ *
118
+ * // Set up worker first
119
+ * setPdfWorkerSrc('https://unpkg.com/pdfjs-dist@4.0.379/build/pdf.worker.mjs');
120
+ *
121
+ * // Convert PDF to flipbook pages
122
+ * const pages = await pdfToFlipbookPages('https://example.com/document.pdf', {
123
+ * scale: 2,
124
+ * onProgress: (current, total) => console.log(`Rendering page ${current}/${total}`)
125
+ * });
126
+ *
127
+ * // Use with Flipbook component
128
+ * <Flipbook pages={pages} width={800} height={600} />
129
+ * ```
130
+ */
131
+ export declare function pdfToFlipbookPages(source: string | ArrayBuffer | Uint8Array, options?: PdfToImagesOptions): Promise<FlipbookPage[]>;
132
+ /**
133
+ * Get PDF document info without rendering pages
134
+ */
135
+ export declare function getPdfInfo(source: string | ArrayBuffer | Uint8Array): Promise<{
136
+ numPages: number;
137
+ pageInfos: Array<{
138
+ pageNumber: number;
139
+ width: number;
140
+ height: number;
141
+ orientation: "portrait" | "landscape";
142
+ }>;
143
+ }>;
144
+ /**
145
+ * Load PDF pages lazily (one at a time) for memory efficiency
146
+ * Returns an async generator that yields pages one by one
147
+ *
148
+ * @example
149
+ * ```typescript
150
+ * const pages: FlipbookPage[] = [];
151
+ * for await (const page of pdfToFlipbookPagesLazy(pdfUrl)) {
152
+ * pages.push(page);
153
+ * // Update UI progressively
154
+ * setPages([...pages]);
155
+ * }
156
+ * ```
157
+ */
158
+ export declare function pdfToFlipbookPagesLazy(source: string | ArrayBuffer | Uint8Array, options?: PdfToImagesOptions): AsyncGenerator<FlipbookPage, void, unknown>;
package/package.json ADDED
@@ -0,0 +1,122 @@
1
+ {
2
+ "name": "react-3d-flipbook",
3
+ "version": "1.1.1",
4
+ "description": "A modern React library for creating beautiful, interactive 3D flipbooks with realistic page-turning effects powered by WebGL and Three.js. Supports PDF loading, automatic page sizing, and customizable camera controls.",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.esm.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "README.md",
11
+ "LICENSE"
12
+ ],
13
+ "sideEffects": [
14
+ "**/*.css"
15
+ ],
16
+ "scripts": {
17
+ "build": "rollup -c",
18
+ "dev": "rollup -c -w",
19
+ "test": "jest",
20
+ "test:watch": "jest --watch",
21
+ "test:coverage": "jest --coverage",
22
+ "lint": "eslint src --ext .ts,.tsx",
23
+ "lint:fix": "eslint src --ext .ts,.tsx --fix",
24
+ "format": "prettier --write \"src/**/*.{ts,tsx,css}\"",
25
+ "typecheck": "tsc --noEmit",
26
+ "prepare": "npm run build",
27
+ "prepublishOnly": "npm run build",
28
+ "storybook": "storybook dev -p 6006",
29
+ "build-storybook": "storybook build",
30
+ "version:patch": "npm version patch --no-git-tag-version",
31
+ "version:minor": "npm version minor --no-git-tag-version",
32
+ "version:major": "npm version major --no-git-tag-version",
33
+ "release:patch": "npm run version:patch && npm run release:tag",
34
+ "release:minor": "npm run version:minor && npm run release:tag",
35
+ "release:major": "npm run version:major && npm run release:tag",
36
+ "release:tag": "git add . && git commit -m \"chore(release): v$(node -p \"require('./package.json').version\")\" && git tag v$(node -p \"require('./package.json').version\") && git push origin HEAD --tags"
37
+ },
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "git+https://github.com/hdn-james/react-3d-flipbook.git"
41
+ },
42
+ "bugs": {
43
+ "url": "https://github.com/hdn-james/react-3d-flipbook/issues"
44
+ },
45
+ "homepage": "https://github.com/hdn-james/react-3d-flipbook#readme",
46
+ "keywords": [
47
+ "react",
48
+ "flipbook",
49
+ "3d",
50
+ "webgl",
51
+ "page-flip",
52
+ "book",
53
+ "pdf",
54
+ "pdf-viewer",
55
+ "animation",
56
+ "three.js",
57
+ "threejs",
58
+ "page-turner",
59
+ "magazine",
60
+ "catalog",
61
+ "ebook",
62
+ "reader",
63
+ "typescript",
64
+ "react-component",
65
+ "page-curl",
66
+ "book-viewer"
67
+ ],
68
+ "author": "Dai-Nhan Huynh <huynhdainhan242@gmail.com>",
69
+ "license": "MIT",
70
+ "peerDependencies": {
71
+ "react": ">=17.0.0",
72
+ "react-dom": ">=17.0.0",
73
+ "three": ">=0.150.0"
74
+ },
75
+ "peerDependenciesMeta": {
76
+ "pdfjs-dist": {
77
+ "optional": true
78
+ }
79
+ },
80
+ "optionalDependencies": {
81
+ "pdfjs-dist": "^4.0.0"
82
+ },
83
+ "devDependencies": {
84
+ "@rollup/plugin-commonjs": "^25.0.7",
85
+ "@rollup/plugin-node-resolve": "^15.2.3",
86
+ "@rollup/plugin-typescript": "^11.1.5",
87
+ "@storybook/addon-docs": "^10.1.10",
88
+ "@storybook/addon-links": "^10.1.10",
89
+ "@storybook/react": "^10.1.10",
90
+ "@storybook/react-vite": "^10.1.10",
91
+ "@testing-library/jest-dom": "^6.1.0",
92
+ "@testing-library/react": "^14.1.0",
93
+ "@testing-library/user-event": "^14.6.1",
94
+ "@types/jest": "^29.5.11",
95
+ "@types/react": "^18.2.45",
96
+ "@types/react-dom": "^18.2.18",
97
+ "@types/three": "^0.160.0",
98
+ "@typescript-eslint/eslint-plugin": "^6.16.0",
99
+ "@typescript-eslint/parser": "^6.16.0",
100
+ "eslint": "^8.56.0",
101
+ "eslint-plugin-react": "^7.33.2",
102
+ "eslint-plugin-react-hooks": "^4.6.0",
103
+ "identity-obj-proxy": "^3.0.0",
104
+ "jest": "^29.7.0",
105
+ "jest-environment-jsdom": "^29.7.0",
106
+ "prettier": "^3.1.0",
107
+ "react": "^18.2.0",
108
+ "react-dom": "^18.2.0",
109
+ "rollup": "^4.9.1",
110
+ "rollup-plugin-dts": "^6.1.0",
111
+ "rollup-plugin-peer-deps-external": "^2.2.4",
112
+ "rollup-plugin-postcss": "^4.0.2",
113
+ "storybook": "^10.1.10",
114
+ "three": "^0.160.0",
115
+ "ts-jest": "^29.1.1",
116
+ "typescript": "^5.3.3",
117
+ "vite": "^5.0.0"
118
+ },
119
+ "engines": {
120
+ "node": ">=20.0.0"
121
+ }
122
+ }