Search code examples
rotationzoomingpaperjs

issue with paperjs rotate


Using paperjs if I rotate p.view.rotate(update.deg); it is working fine with out issue.

If I refresh the page and call the above function p.view.rotate(update.deg); onload then the view is different. it is displaying a slight zoom.

default image rotation

enter image description here

After reloading the page I am rotating p.view with the previous value. then it is displaying as

enter image description here

Here is my js file

https://github.com/muralibobby35/annotateJs/blob/master/opentok-whiteboardnew.js


Solution

  • I was not able to run your code but I would suggest, for an easier project state preservation, that you use transformations (scale, rotation, ...) through layer rather than through view.
    That would allow you to easily export/import your project whithout having to manually restore state by calling view.rotate() like methods on window reload.

    Here is a fiddle demonstrating the solution.
    It simulates window reload by exporting/importing a project from one canvas to another empty one.

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>Debug Paper.js</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/paper.js/0.11.5/paper-full.min.js"></script>
        <style>
            html,
            body {
                margin   : 0;
                overflow : hidden;
                height   : 100%;
            }
    
            main {
                display : flex;
                height  : 100vh;
            }
    
            canvas {
                flex   : 1;
                border : 1px solid;
            }
        </style>
    </head>
    <body>
    <main>
        <canvas id="canvas1" resize></canvas>
        <canvas id="canvas2" resize></canvas>
    </main>
    <script type="text/paperscript" canvas="canvas1">
        // draw a square
        var rectangle = new Path.Rectangle({
            from: view.center - 50,
            to: view.center + 50,
            fillColor: 'orange'
        });
    
        // rotate layer rather than view so transformations are persisted when exporting
        project.activeLayer.rotate(30);
    
        // export datas and store them in global variable just for the demo, to simulate a page reload
        window.exportedDatas = project.exportJSON();
    </script>
    <script type="text/paperscript" canvas="canvas2">
        // import the exported datas
        project.importJSON(window.exportedDatas);
    
        // see that rotation is preserved
    </script>
    </body>
    </html>