Giter Site home page Giter Site logo

react_vr_pdf's Introduction

show pdf in react 360

You can use pdf.js, and use texture in Plane/Box/Cylinder/Model/Plane/Sphere component. use canvas in client.js or NativeModules

1. add pdf.js in index.html
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.0.550/pdf.min.js"></script>

2. add PdfModule

import {Module} from 'react-360-web';
let pagenum = 1 // pdf cunrrentNum
let pageRendering = true // is pdf rendering
export default class PdfModule extends Module{
    constructor(context){
        super('PdfModule')
        this._context = context
    }

    $showPdf(pdfUrl  ,width, height , scale  ){
        this.pdfUrl = pdfUrl
        this.width = width
        this.height = height
        this.scale = scale

        this.canvas = document.createElement('canvas');
        this.canvas.width = width;
        this.canvas.height = height;
        this.cx = this.canvas.getContext('2d');

        this.showPage(pagenum)

        this._context.registerTextureSource('fps', this.canvas, {updateOnFrame: true}); // Needs an update each frame
    }

 showPage  (num)  {
    const url = this.pdfUrl
    const width = this.width
    const height = this.height
    const scale = this.scale
    const canvas = this.canvas
    const ctx = this.cx
    pageRendering = true;
    // document.getElementById('content').style.display = 'block';//show pdf loading
    pdfjsLib.getDocument(url).then(function(pdf){
        if(num === 'next'){
            pagenum ++;
        }else if(num === 'last'){
            pagenum --;
        }
        if(pagenum < 1){
            pagenum = 0
            return
        }else if(pagenum > pdf.numPages){
            pagenum = pdf.numPages
            return
        }

        pdf.getPage(pagenum).then(function(page){
            const viewport = page.getViewport(scale);
            canvas.height = height;
            canvas.width = width;
            // Render PDF page into canvas context
            const renderContext = {
            canvasContext: ctx,
            viewport: viewport
            };
            var renderTask = page.render(renderContext);
            // Wait for rendering to finish
            renderTask.promise.then(function () {
            pageRendering = false;
            // document.getElementById('content').style.display = 'none';//hide pdf loading
            })
            .catch(e=>{
            pageRendering = false;
            // document.getElementById('content').style.display = 'none';//hide pdf loading
            });
        }).catch(e=>console.log(e));
    })
  }
}
3. pass pdfurl/ width / height / pdf scale to module
4. then you can use pdfmodule in your code

see index.js

componentWillMount(){
    PDF.showPdf('http://localhost:8081/output.pdf',512,1024,2)
  }

  render() {
    return (
      <View style={styles.panel}>
        <Plane
        dimWidth={512}
        dimHeight={512}
        dimDepth={100}
        texture={texture('fps')} // Use our custom texture
      />
        <View style={styles.greetingBox}>
          <VrButton onClick={()=>{
            PDF.showPage('next') // show next page
            // PDF.showPage('last') // show last page
          }}>
            <Text style={styles.text}>
              enter
            </Text>

          </VrButton>
        </View>
      </View>
    );
  }

```# react_vr_pdf
# react_vr_pdf

react_vr_pdf's People

Contributors

liuc520 avatar

Watchers

张江平 avatar

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.