我通過(guò)在Xcode中使用一個(gè)html文件進(jìn)行展示我發(fā)現(xiàn)將很多圖片進(jìn)行循環(huán)可以展示出3D全景效果接下來(lái)我們深入研究一下如何使用html和JS代碼將全景地圖效果展示出來(lái)
1.我們需要在Xcode中創(chuàng)建一個(gè)html文件
//寫(xiě)入<!DOCTYPE html>這句代碼使用來(lái)聲明html文件的
2.<html> </html>
<head> </head>
<meta charset=utf-8">
這三句代碼就是html文件的標(biāo)簽元素跟我理解的xml文件標(biāo)簽元素是一樣的
注意:<title>Full 360 degree View</title>這句代碼設(shè)置html文件在預(yù)覽器中的標(biāo)題
3.接下來(lái)我們需要深一步了解JS代碼
首先我們創(chuàng)建JS標(biāo)簽<script></script> 然后我們將在標(biāo)簽中寫(xiě)入JS用來(lái)展示全景效果圖此處注意所有JS標(biāo)簽全部被html標(biāo)簽所包含所有JS代碼全部被JS標(biāo)簽包含(包含也就是放到標(biāo)簽里面去)
var ctx = null; // 顯示證書(shū)全局變量2 d上下文
var frame = 1; // 設(shè)置位置信息
var width = 0;//寬度
var height = 0;//高度
var started = false;//判斷是否開(kāi)始
var images = new Array();//設(shè)置圖片數(shù)組
var startedX = -1;
//設(shè)置函數(shù)體
window.onload = function() {
var canvas = document.getElementById("fullview_canvas");
canvas.width = 440;//寬度/帆;
canvas.height = 691;//高度/帆;
width = canvas.width;//設(shè)置寬度帆
height = canvas.height;//設(shè)置高度帆
//等到元素loadProgressBar
var bar = document.getElementById('loadProgressBar');
//循環(huán)圖片數(shù)組images
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");//獲得2d證書(shū)內(nèi)容
// 通過(guò)鼠標(biāo)滑動(dòng)網(wǎng)頁(yè)進(jìn)行滾動(dòng)圖片操作(必須在圖片開(kāi)啟滾動(dòng)的情況)
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);
}
</head>
<body>
//設(shè)置標(biāo)簽元素進(jìn)度條最大值為23
<progress id="loadProgressBar" value="0" max="23"></progress>
//設(shè)置函數(shù)標(biāo)簽元素
<canvas id="fullview_canvas"></canvas>
//設(shè)置按鈕點(diǎn)擊函數(shù)體和按鈕標(biāo)題
<button onclick="loaded()">Auto Play</button>
</body>
</html>
4.我們已經(jīng)了解如何生成html文件接下來(lái)我們可以在工程中通過(guò)OC代碼與html文件和JS代碼進(jìn)行交互使用顯示全景效果