我通过在Xcode中使用一个html文件进行展示我发现将很多图片进行循环可以展示出3D全景效果接下来我们深入研究一下如何使用html和JS代码将全景地图效果展示出来
1.我们需要在Xcode中创建一个html文件
//写入<!DOCTYPE html>这句代码使用来声明html文件的
2.<html> </html>
<head> </head>
<meta charset=utf-8">
这三句代码就是html文件的标签元素跟我理解的xml文件标签元素是一样的
注意:<title>Full 360 degree View</title>这句代码设置html文件在预览器中的标题
3.接下来我们需要深一步了解JS代码
首先我们创建JS标签<script></script> 然后我们将在标签中写入JS用来展示全景效果图此处注意所有JS标签全部被html标签所包含所有JS代码全部被JS标签包含(包含也就是放到标签里面去)
var ctx = null; // 显示证书全局变量2 d上下文
var frame = 1; // 设置位置信息
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;//宽度/帆;
canvas.height = 691;//高度/帆;
width = canvas.width;//设置宽度帆
height = canvas.height;//设置高度帆
//等到元素loadProgressBar
var bar = document.getElementById('loadProgressBar');
//循环图片数组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证书内容
// 通过鼠标滑动网页进行滚动图片操作(必须在图片开启滚动的情况)
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>
//设置标签元素进度条最大值为23
<progress id="loadProgressBar" value="0" max="23"></progress>
//设置函数标签元素
<canvas id="fullview_canvas"></canvas>
//设置按钮点击函数体和按钮标题
<button onclick="loaded()">Auto Play</button>
</body>
</html>
4.我们已经了解如何生成html文件接下来我们可以在工程中通过OC代码与html文件和JS代码进行交互使用显示全景效果