@abasiakanmbat/stpageflip
v2.1.0
Published
Powerful, simple and flexible JS Library for creating realistic and beautiful page turning effect
Downloads
42
Maintainers
Readme
StPageFlip
Powerful, simple, and flexible JS Library for creating realistic and beautiful page turning effect.

Features
- Works with simple images on canvas and complex HTML blocks
- Has simple API and flexible configuration
- Compatible with mobile devices
- Supports landscape and portrait screen mode
- Supports soft and hard page types (only in HTML mode)
- No dependencies
Demo and docs: https://nodlik.github.io/StPageFlip/
For React.JS you can use: https://nodlik.github.io/react-pageflip/
Docs (generated by TypeDoc): https://nodlik.github.io/StPageFlip/docs/index.html
Local development
This repository includes a small browser playground for testing changes to the library.
Install dependencies and start the Webpack watch build:
pnpm install --frozen-lockfile
pnpm run build-globalLeave that terminal running. In a second terminal, start the local static server:
pnpm run serve-demoOpen http://localhost:8080/demo/ in a browser. Edit files in src/, wait for Webpack to rebuild dist/js/pageFlip.browser.js, then refresh the page.
Installation
Install the maintained fork from npm:
npm install @abasiakanmbat/stpageflipThe package includes the browser bundle, ES module bundle, and TypeScript declarations. If your npm username is different, replace @abasiakanmbat with your own npm scope.
Usage
If you've installed the package from npm, import PageFlip from the package, or use the browser bundle with a <script> tag:
<script src="{path/to/scripts}/page-flip.browser.js"></script>To create a new PageFlip object:
import { PageFlip } from '@abasiakanmbat/stpageflip';
const pageFlip = new PageFlip(htmlParentElement, settings);
// or if you're using a script tag and page-flip.browser.js:
const pageFlip = new St.PageFlip(htmlParentElement, settings);Publishing updates
This repository is the reusable library. Keep your website, authentication, book database, uploads, and API in a separate application repository. Publish library changes here, then install the package in that application.
Before the first release, make sure the npm name in package.json uses a scope that you own. The default scope in this fork is @abasiakanmbat.
Build and inspect the exact files that npm will publish:
npm install
npm run build
npm pack --dry-runThe package preview must contain dist/js/page-flip.browser.js, dist/js/page-flip.module.js, dist/PageFlip.d.ts, README.md, and LICENSE. The prepublishOnly script runs the production build automatically during publishing.
After updating the version according to semantic versioning, publish it with:
npm login
npm publish --access publicConsumers can then install the new version with npm install @abasiakanmbat/stpageflip.
Using it in an application
Use React, Next.js, or Angular as a separate frontend project. Fetch ordered page-image URLs from your backend and pass them to loadFromImages():
const response = await fetch('/api/books/book-1/');
const book = await response.json();
pageFlip.loadFromImages(book.pages);The backend should expose GET /api/books/:id/, GET /api/books/:id/progress/, and PUT /api/books/:id/progress/. Listen for the flip event to save the zero-based page index:
pageFlip.on('flip', (event) => {
// Save event.data through your authenticated API.
});In React and Next.js, initialize the library in a client-side effect and destroy it during cleanup. In Angular, initialize it after the browser view is rendered and destroy it in the component destroy hook. This library uses browser DOM and canvas APIs, so it must not be initialized during server rendering.
For Vercel, deploy the frontend and API from Git-connected projects. Next.js route handlers are the simplest single-project option; Express and Django can also be deployed as separate backend projects. Store page images in object storage or a CDN and keep book records and progress in a persistent database. Configure the API's CORS allow-list to contain only the deployed frontend origin.
htmlParentElement - HTMLElement- root element, where the book will be created
settings: object - configuration object.
To draw on a canvas, use loadFromImages:
pageFlip.loadFromImages(['path/to/image1.jpg', 'path/to/image2.jpg' ... ]);To load page from html elements - use loadFromHtml:
pageFlip.loadFromHtml(items);For example:
<div id="book">
<div class="my-page" data-density="hard">
Page Cover
</div>
<div class="my-page">
Page one
</div>
<div class="my-page">
Page two
</div>
<div class="my-page">
Page three
</div>
<div class="my-page">
Page four
</div>
<div class="my-page" data-density="hard">
Last page
</div>
</div>const pageFlip = new PageFlip(document.getElementById('book'), {
width: 400, // required parameter - base page width
height: 600, // required parameter - base page height
});
pageFlip.loadFromHTML(document.querySelectorAll('.my-page'));Use data-density="hard" attribute to specify page type (soft | hard) and define flipping animation.
Config
To set configuration define these parameters when creating an object:
width: number- requiredheight: number- requiredsize: ("fixed", "stretch")- default:"fixed"Whether the book will be stretched under the parent element or notminWidth, maxWidth, minHeight, maxHeight: numberYou must set threshold values with size:"stretch"drawShadow: bool- default:trueDraw shadows or not when page flippingflippingTime: number(milliseconds) - default:1000Flipping animation timeusePortrait: bool- default:trueEnable switching to portrait mode. !This mode uses cloning of html elements (pages)startZIndex: number- default:0Initial value to z-indexstartPage: number- default:0Page number from which to start viewingautoSize: bool- default:trueIf this value is true, the parent element will be equal to the size of the bookmaxShadowOpacity: number [0..1]- default:1Shadow intensity (1: max intensity, 0: hidden shadows)showCover: boolean- default:falseIf this value is true, the first and the last pages will be marked as hard and will be shown in single page modemobileScrollSupport: boolean- default:truedisable content scrolling when touching a book on mobile devicesswipeDistance: number- default:30(px) minimum distance to detect swipe (new on 1.1.0)clickEventForward: boolean- default:trueforwarding click events to the page children html elements (only foraandbuttontags) (new on 1.1.0)useMouseEvents: boolean- default:trueusing mouse and touch events to page flipping (new on 1.2.0)disableFlipByClick: boolean- default:falseif this value is true, flipping by clicking on the whole book will be locked. Clicking will only work in corners (new on 2.0.3)
Events
To listen events use the method on:
pageFlip.on('flip', (e) => {
// callback code
alert(e.data); // current page number
});Available events:
flip: number- triggered by page turningchangeOrientation: ("portrait", "landscape")- triggered when page orientation changeschangeState: ("user_fold", "fold_corner", "flipping", "read")- triggered when the state of the book changesinit: ({page: number, mode: 'portrait', 'landscape'})- triggered when the book is init and the start page is loaded. Listen (on) this event before using the "loadFrom..." methodsupdate: ({page: number, mode: 'portrait', 'landscape'})- triggered when the book pages are updated (using the "updateFrom..." methods)
Event object has two fields: data: number | string and object: PageFlip
Methods
getPageCount: number- Get number of all pagesgetOrientation: 'portrait', 'landscape'- Get the current orientation: portrait or landscapegetBoundsRect: PageRect- Get current book sizes and positiongetCurrentPageIndex: number- Get the current page number (starts at 0)turnToPage(pageNum: number)- Turn to the specified page number (without animation)turnToNextPage()- Turn to the next page (without animation)turnToPrevPage()- Turn to the previous page (without animation)flipNext(corner: 'top' | 'bottom')- Turn to the next page (with animation)flipPrev(corner: 'top' | 'bottom')- Turn to the previous page (with animation)flip(pageNum: number, corner: 'top' | 'bottom')- Turn to the specified page (with animation)loadFromImages(images: ['path-to-image1.jpg', ...])- Load page from imagesloadFromHtml(items: NodeListOf | HTMLElement[])- Load page from html elementsupdateFromHtml(items: NodeListOf | HTMLElement[])- Update page from html elements (new on 0.4.0)updateFromImages(images: ['path-to-image1.jpg', ...])- Update page from images (new on 0.4.0)destroy()- Destructor. Remove a root HTML element and all event handlers (new on 0.4.0)
Contacts
Oleg,
https://github.com/Nodlik/StPageFlip

