HTML5 Canvas完成360度全景图图的示例编码

2021-02-23 10:53 jianzhan

许多买东西网站如今都适用360商品全景图图象,能够360度随意挑选查询样品,这样对选购者来讲是1个很好的消費体验,在网上有许多这样的软件全是根据jQuery完成的有收费的也是有完全免费的,实际上很功能强大的1个叫3deye.js的软件。该软件适用桌面上与挪动终端设备iOS与Android, 它的demo程序流程:http://www.voidcanvas.com/demo/28823deye/

自身玩了玩这个Demo之后,照着它的思路,用HTML5 Canvas也完成了相近的作用。

因此先说1下它的360度全景图图的基本原理

1. 最先必须对商品照相,间距是每张相片转动15度,因此必须23张相片。
2.相片提前准备好了之后,尽可能挑选JPG文件格式,剪裁到适度尺寸。
3.JavaScript中预载入全部相片,能够相互配合进度条显示信息载入精度
4.建立/获得Canvas目标,再加电脑鼠标监视恶性事件,当电脑鼠标上下挪动情况下,适当的绘图不一样帧。大概的基本原理便是这样,简易吧!

完成编码:

<!DOCTYPE html>  
<html>  
<head>  
  <meta charset=utf⑻">  
  <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 = ⑴;  
      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('loadProgressBar');  
        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('mousemove', doMouseMove, false);  
        canvas.addEventListener('mouseup',   doMouseUp, false);  
        // loaded();  
          
        // frame = 1  
        frame = 1;  
        images[frame].onload = function() {  
            redraw();  
            bar.style.display = 'none';  
        }  
    }  
    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 = ⑴;  
            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演试文档免费下载详细地址-> fullview_jb51.rar

以上便是本文的所有內容,期待对大伙儿的学习培训有一定的协助,也期待大伙儿多多适用脚本制作之家。