本文主要介紹HTML5 Canvas實現360度全景圖的範例程式碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟著小編過來看看吧,希望能幫助大家。
許多購物網站現在都支援360實體全景圖像,可以360度任意選擇查看樣品,這樣對購買者來說是一個很好的消費體驗,網上有很多這樣的插件都是基於jQuery實現的有收費的也有免費的,其實很好用的一個叫3deye.js的插件。該外掛支援桌面與行動終端iOS與Android,它的demo程式:http://www.voidcanvas.com/demo/28823deye/
自己玩了玩這個Demo以後,照著它的思路,用HTML5 Canvas也實作了類似的功能。
所以先說它的360度全景圖的原理
#1. 首先需要對實體拍照,間隔是每張照片旋轉15度,所以需要23張照片。
2.照片準備好了以後,盡量選擇JPG格式,裁切到適當大小。
3.JavaScript中預先載入所有照片,可以配合進度條顯示載入精度
4.建立/取得Canvas對象,加上滑鼠監聽事件,當滑鼠左右移動時候,適度的繪製不同畫面。大致的原理就是這樣,簡單吧!
實作程式碼:
<!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('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 = -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示範檔下載位址-> fullview_jb51.rar
相關推薦:
以上是HTML5 Canvas實現360度全景方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!