HTML5Canvas实现360度全景图的⽰例代码
⾃⼰玩了玩这个Demo以后,照着它的思路,⽤HTML5 Canvas也实现了类似的功能。
所以先说⼀下它的360度全景图的原理
1. ⾸先需要对实物拍照,间隔是每张照⽚旋转15度,所以需要23张照⽚。
2.照⽚准备好了以后,尽量选择JPG格式,裁剪到适当⼤⼩。
3.JavaScript中预加载所有照⽚,可以配合进度条显⽰加载精度
4.创建/获取Canvas对象,加上⿏标监听事件,当⿏标左右移动时候,适度的绘制不同帧。⼤致的原理就是这样,简单吧!实现代码:
<!DOCTYPE html>
jquery下载文件进度条<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;
var canvas = ElementById("fullview_canvas");
canvas.width = 440;// window.innerWidth;
canvas.height = 691;//window.innerHeight;
width = canvas.width;
height = canvas.height;
var bar = ElementById('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 = Context("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();
giteabar.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;c语言数据常量定义规则
}
redraw();
}
}
}
function doMouseUp(event) {
console.log("mouse up now");
if (started) {
doMouseMove(event);
startedX = -1;
started = false;
}
}
function getPointOnCanvas(canvas, x, y) {
var bbox = BoundingClientRect();
return { x: x - bbox.left * (canvas.width / bbox.width),
y: y - p * (canvas.height / bbox.height)
};
}
function loaded() {
setTimeout( update, 1000/8);
}
function redraw()
{
// var imageObj = ateElement("img");
// var imageObj = new Image();
var imageObj = images[frame];linux怎么撤销上一步命令
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演⽰⽂件下载地址->
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论