Giter VIP home page Giter VIP logo

flipbook-viewer's Introduction

Flipbook Viewer

Amazing flip book component with animated pages.

demo

This is a tiny library that can show flip books from any source (including PDF’s, images, etc).

Advantages

  1. Tiny (18 Kb). For comparison, the amazing page-flip is 10 Mb (x1000 times bigger!).
  2. Can use any input as a book simply by plugging in a “book provider”. An example PDF book using the amazing pdfjs from Mozilla can be found in the test folder—book-pdf.js (referenced usage: test-pdf.js)
  3. Supports Panning, Zooming, Liking, Sharing, along with page turning effects.
  4. Raises events to track which pages are being viewed by user.

Usage

Below shows the flip book on the given div with the id div-id:

'use strict'

import { init as flipbook } from 'flipbook-viewer';

...

flipbook(book, 'div-id', (err, viewer) => {
  if(err) console.error(err);

  console.log('Number of pages: ' + viewer.page_count);
  viewer.on('seen', n => console.log('page number: ' + n));

  next.onclick = () => viewer.flip_forward();
  prev.onclick = () => viewer.flip_back();
  zoom.onclick = () => viewer.zoom();

});

The viewer can show any flip book. All you need to do is provide a book interface:

{
  numPages: () => {
    /* return number of pages */
  },
  getPage: (num, cb) => {
    /* return page number 'num'
     * in the callback 'cb'
     * as any CanvasImageSource:
     * (CSSImageValue, HTMLImageElement, 
     *  SVGImageElement, HTMLVideoElement,
     *  HTMLCanvasElement, ImageBitmap,
     *  OffscreenCanvas)
     */
  }
}

Options

An optional opts parameter can be passed in to change the UI:

const opts = {
  backgroundColor: "#353535",
  boxColor: "#353535",
  width: 800,
  height: 600,
}

flipbook(book, 'div-id', opts, (err, viewer) => ...

Events

You can listen on the viewer for which pages were seen:

viewer.on('seen', n => ...)

Programmatic API

The returned viewer can be used to programmatically control the viewer:

viewer.flip_forward()
viewer.flip_back()
viewer.zoom()

Single Page View

Finally, sometimes it makes sense to just show the book as a simple, scrollable view. To pass in only a singlepage:true option:

flipbook(book, 'div-id', {singlepage:true}, (err, viewer) => ...

This will generate a series of canvases with class="flipbook__page" and id="flipbook__pgnum_<n>" that you can style using CSS. The single page view will raise the same seen event that the flipbook viewer does for tracking which pages the user actually flips through.

The single page viewer is currently experimental and very simple. It should work for many PDF's but is not optimized for handling PDF's with a large number of pages.

Enjoy!


flipbook-viewer's People

Contributors

theproductiveprogrammer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

flipbook-viewer's Issues

Demo not working

I'm trying hard to get the demo working. Here my page with just the repo code inside: https://flip.design-to-use.de/test/test-pdf.html

  1. I had to fix
    <script src="/test-pdf.js"></script>
    to <script src="./test-pdf.js"></script>, because the file was not found.
  2. I had to fix the same line to <script type="module" src="/test-pdf.js"></script>, because I had a browser error "Uncaught SyntaxError: import declarations may only appear at top level of a module".

Now I have a next error on my browser console: Uncaught TypeError: The specifier "@tpp/htm-x" was a bare specifier but was not remapped to anything. Relative module specifiers must begin with "./", "../" or "/". How to solve?

Package manager support

Hello,

is there an option to install via npm? Or how are you supposed to install it? Just download manually?

No flipping animation

Hi, i see no animation at all.

My code:

 const flipbookPages = flipbookEl.querySelectorAll('.flip-page');

    const opts = {
        backgroundColor: "#000",
        boxColor: "#000",
      }
      
    
    const book = {
        numPages: () => flipbookPages.length,
        getPage: (n, cb) => {
            const cache = []
            if(!n || n > flipbookPages.length) return cb()
            if(cache[n]) return cb(null, cache[n])
        
            const img = new Image()
            img.src = flipbookPages[n-1].src
            img.addEventListener("load", () => {
              cache[n] = {
                img,
                num: n,
                width: img.width,
                height: img.height,
              }
              cb(null, cache[n])
            }, false)
        },
    }

    flipbook(book, 'flipbook', opts, (err, viewer) => {
        if(err) console.error(err);
    
        console.log('Number of pages: ' + viewer.page_count);
        viewer.on('seen', n => console.log('page number: ' + n));
    
        flipNext.onclick = () => viewer.flip_forward();
        flipPrev.onclick = () => viewer.flip_back();
        // zoom.onclick = () => viewer.zoom();
    
    });

Inside flipbookviewer.js i see showFlip() but it's not being called or i don't know how

How to use with vue

Hi,

I want to try to see if I can use your package on my website. But I'm struggling a bit to get it to work with Vue3. So I created a simple component:

template>
    <Pagesection>
        <div id="flipbookApp" class="bg-openpeople-grey min-h-screen"></div>
        <button id="flipbookNext">next</button>
        <button id="flipbookPrev">prev</button>
        <button id="flipbookZoom">zoom</button>
    </Pagesection>
</template>

<script setup>
import { init as flipbook } from 'flipbook-viewer';

const app = document.getElementById('flipbookApp')
const next = document.getElementById('flipbookNext')
const prev = document.getElementById('flipbookPrev')
const zoom = document.getElementById('flipbookZoom')


onMounted(async () => {
    flipbook("/fp.pdf", app, (err, viewer) => {
        if (err) console.error(err);

        console.log('Number of pages: ' + viewer.page_count);
        viewer.on('seen', n => console.log('page number: ' + n));

        next.onclick = () => viewer.flip_forward();
        prev.onclick = () => viewer.flip_back();
        zoom.onclick = () => viewer.zoom();
    });
})
</script>

And I' rendering it on the page with a client-only marker to prevent SSR, but everytime I try to load that component it returns an error:

ReferenceError: self is not defined
    at Object.<anonymous> (/Users/gerard/dev/openpeople-content/node_modules/flipbook-viewer/dist/flipbook-viewer.js:1:209)
    at Module._compile (node:internal/modules/cjs/loader:1119:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
    at Module.load (node:internal/modules/cjs/loader:997:32)
    at Module._load (node:internal/modules/cjs/loader:838:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:170:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:528:24)
    at async __instantiateModule__ (file:///Users/gerard/dev/openpeople-content/.nuxt/dist/server/server.mjs:25281:3)

Could you point me in the right direction, probably missing something simple.

No bundler

Hi!
Is it possible to use this library as vanilla JS?

I'm trying to run your test folder as it is but it is not working
Besides that, the way it runs things is different from the example in the readme

Can you provide a working example?

Thanks

maximum dimensions

Hi, great project.

Maybe I missed something obvious but I couldn't see a way to set the flipbook to use the maximum height and width of the viewport. I could only see a way to se absolute dimensions in pixels.

Error when initiating with singlepage-viewer option

Hi, thank you for this compact and awesome flipbook.

I am running into some trouble when setting up a viewer in singlepage mode. The error message reads: Uncaught TypeError: o is undefined [dist/flipbook-viewer.js:1:16674], which would translate to this line in the source code which means pdf is not defined. Could webpack produce some variable scope issues? I would answer this question myself, if I knew anything about webpack.
Does anybody else run into similar problems with the dist code?

Thanks for any help!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.