Home >Web Front-end >H5 Tutorial >HTML5 Canvas implementation of 360-degree panorama method

HTML5 Canvas implementation of 360-degree panorama method

小云云
小云云Original
2018-01-31 10:52:446378browse

This article mainly introduces the sample code for implementing 360-degree panorama in HTML5 Canvas. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

Many shopping websites now support 360 panoramic images of real objects, and you can choose to view samples at 360 degrees. This is a good consumer experience for buyers. There are many such plug-ins on the Internet based on jQuery. There are paid ones and free ones. In fact, a plug-in called 3deye.js is very useful. The plug-in supports desktop and mobile terminals iOS and Android. Its demo program: http://www.voidcanvas.com/demo/28823deye/

After playing this demo myself, following its ideas, Similar functions are also implemented using HTML5 Canvas.

So let’s talk about the principle of its 360-degree panorama first

1. First, you need to take photos of the actual object. The interval is to rotate each photo 15 degrees, so it takes 23 a photograph.
2. After the photo is ready, try to choose JPG format and crop it to the appropriate size.
3. Preload all photos in JavaScript, and you can display the loading accuracy with the progress bar
4. Create/obtain the Canvas object, add mouse listening events, and draw different frames appropriately when the mouse moves left and right. The general principle is this, simple!

Implementation code:


<!DOCTYPE html>  
<html>  
<head>  
  <meta charset=utf-8">  
  <title>Full 360 degree View</title>  
  <script>  
        var ctx = null; // global variable 2d context  
        var frame = 1; // 23  
        var width = 0;  
        var height = 0;  
        var started = false;  
        var images = new Array();  
        var startedX = -1;  
      window.onload = function() {  
        var canvas = document.getElementById("fullview_canvas");  
        canvas.width = 440;// window.innerWidth;  
        canvas.height = 691;//window.innerHeight;  
        width = canvas.width;  
        height = canvas.height;  
        var bar = document.getElementById(&#39;loadProgressBar&#39;);  
        for(var i=1; i<24; i++)  
        {  
            bar.value = i;  
            if(i<10)  
            {  
                images[i] = new Image();  
                images[i].src = "0" + i + ".jpg";  
            }  
            else   
            {  
                images[i] = new Image();  
                images[i].src = i + ".jpg";  
            }  
        }  
        ctx = canvas.getContext("2d");  
          
        // mouse event  
        canvas.addEventListener("mousedown", doMouseDown, false);  
        canvas.addEventListener(&#39;mousemove&#39;, doMouseMove, false);  
        canvas.addEventListener(&#39;mouseup&#39;,   doMouseUp, false);  
        // loaded();  
          
        // frame = 1  
        frame = 1;  
        images[frame].onload = function() {  
            redraw();  
            bar.style.display = &#39;none&#39;;  
        }  
    }  
    function doMouseDown(event) {  
        var x = event.pageX;  
        var y = event.pageY;  
        var canvas = event.target;  
        var loc = getPointOnCanvas(canvas, x, y);  
        console.log("mouse down at point( x:" + loc.x + ", y:" + loc.y + ")");  
        startedX = loc.x;  
        started = true;  
    }  
      
    function doMouseMove(event) {  
        var x = event.pageX;  
        var y = event.pageY;  
        var canvas = event.target;  
        var loc = getPointOnCanvas(canvas, x, y);  
        if (started) {  
            var count = Math.floor(Math.abs((startedX - loc.x)/30));  
            var frameIndex = Math.floor((startedX - loc.x)/30);  
            while(count > 0)  
            {                 
                console.log("frameIndex = " + frameIndex);  
                count--;      
                if(frameIndex > 0)  
                {  
                    frameIndex--;  
                    frame++;  
                } else if(frameIndex < 0)  
                {  
                    frameIndex++;  
                    frame--;  
                }  
                else if(frameIndex == 0)  
                {  
                    break;  
                }  
                                  
                if(frame >= 24)  
                {  
                    frame = 1;  
                }  
                if(frame <= 0)  
                {  
                    frame = 23;  
                }  
                redraw();  
            }  
        }  
    }  
      
    function doMouseUp(event) {  
        console.log("mouse up now");  
        if (started) {  
            doMouseMove(event);  
            startedX = -1;  
            started = false;  
        }  
    }  
  
    function getPointOnCanvas(canvas, x, y) {  
        var bbox = canvas.getBoundingClientRect();  
        return { x: x - bbox.left * (canvas.width  / bbox.width),  
                y: y - bbox.top  * (canvas.height / bbox.height)  
                };  
    }  
      
    function loaded() {  
        setTimeout( update, 1000/8);  
    }  
    function redraw()  
    {  
        // var imageObj = document.createElement("img");  
        // var imageObj = new Image();  
        var imageObj = images[frame];  
        ctx.clearRect(0, 0, width, height)  
        ctx.drawImage(imageObj, 0, 0, width, height);  
    }  
    function update() {  
        redraw();  
        frame++;  
        if (frame >= 23) frame = 1;  
        setTimeout( update, 1000/8);  
    }  
  </script>  
</head>  
<body>  
<progress id="loadProgressBar" value="0" max="23"></progress>   
<canvas id="fullview_canvas"></canvas>  
<button onclick="loaded()">Auto Play</button>  
</body>  
</html>

Demo demo file download address-> fullview_jb51.rar

Related recommendations:

How to use H5 Canvas to implement 3D dynamic Chart

html2 canvas to implement browser screenshots

JS+canvas drawing Dynamic mechanical watch animation effect

The above is the detailed content of HTML5 Canvas implementation of 360-degree panorama method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn