javascript - 如何创建自己的交互式全景网站

标签 javascript html html5-canvas panoramas

我想制作一个房间的网站,我希望用户能够以有限的全景 View 查看该房间,例如上/下 30 度,左/右 45 度,我想将对象放置在用户可以与之交互的全景 View 中。

我发现谷歌街景可以提供全景效果,但我不太确定它是否适合我的需求,因为我想在其中放入物体。

是否有其他好的全景库可以为我提供工具来支持我想要实现的目标?

最佳答案

您基本上是在谈论围绕 View 平移。

您可以通过绘制具有水平和垂直偏移的 View 来做到这一点。

这里是带注释的代码和演示:http://jsfiddle.net/m1erickson/32Y5A/

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
    body{ background-color: ivory; padding:20px;}
    #canvas{border:1px solid red;}
</style>
<script>
$(function(){

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");
    ctx.strokeStyle="red";
    ctx.lineWidth=5;

    var canvasOffset=$("#canvas").offset();
    var offsetX=canvasOffset.left;
    var offsetY=canvasOffset.top;

    var lastX=0;
    var lastY=0;
    var panX=0;
    var panY=0;
    var dragging=[];
    var isDown=false;


    // create "draggable" rects
    var images=[];
    images.push({x:200,y:150,width:25,height:25,color:"green"});
    images.push({x:80,y:235,width:25,height:25,color:"gold"});

    // load the tiger image
    var tiger=new Image();
    tiger.onload=function(){
        draw();
    }
    tiger.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/tiger.png";


    function draw(){

        ctx.clearRect(0,0,canvas.width,canvas.height);

        // draw tiger
        ctx.globalAlpha=0.25;
        ctx.drawImage(tiger,panX,panY,tiger.width,tiger.height);

        // draw color images
        ctx.globalAlpha=1.00;
        for(var i=0;i<images.length;i++){
            var img=images[i];
            ctx.beginPath();
            ctx.rect(img.x+panX,img.y+panY,img.width,img.height);
            ctx.fillStyle=img.color;
            ctx.fill();
            ctx.stroke();
        }
    }

    // create an array of any "hit" colored-images
    function imagesHitTests(x,y){
        // adjust for panning
        x-=panX;
        y-=panY;
        // create var to hold any hits
        var hits=[];
        // hit-test each image
        // add hits to hits[]
        for(var i=0;i<images.length;i++){
            var img=images[i];
            if(x>img.x && x<img.x+img.width && y>img.y && y<img.y+img.height){
                hits.push(i);
            }
        }
        return(hits);
    }

    function handleMouseDown(e){

      // get mouse coordinates
      mouseX=parseInt(e.clientX-offsetX);
      mouseY=parseInt(e.clientY-offsetY);
      // set the starting drag position
      lastX=mouseX;
      lastY=mouseY;
      // test if we're over any of the images
      dragging=imagesHitTests(mouseX,mouseY);
      // set the dragging flag
      isDown=true;
    }

    function handleMouseUp(e){
      // clear the dragging flag
      isDown=false;
    }


    function handleMouseMove(e){

      // if we're not dragging, exit
      if(!isDown){return;}

      //get mouse coordinates
      mouseX=parseInt(e.clientX-offsetX);
      mouseY=parseInt(e.clientY-offsetY);

      // calc how much the mouse has moved since we were last here
      var dx=mouseX-lastX;
      var dy=mouseY-lastY;

      // set the lastXY for next time we're here
      lastX=mouseX;
      lastY=mouseY;

      // handle drags/pans
      if(dragging.length>0){
          // we're dragging images
          // move all affected images by how much the mouse has moved
          for(var i=0;i<dragging.length;i++){
              img=images[dragging[i]];
              img.x+=dx;
              img.y+=dy;
          }
      }else{
          // we're panning the tiger
          // set the panXY by how much the mouse has moved
          panX+=dx;
          panY+=dy;
      }
      draw();
    }

    // use jQuery to handle mouse events
    $("#canvas").mousedown(function(e){handleMouseDown(e);});
    $("#canvas").mousemove(function(e){handleMouseMove(e);});
    $("#canvas").mouseup(function(e){handleMouseUp(e);});

}); // end $(function(){});
</script>
</head>
<body>
    <h4>Drag the tiger and<br>independently drag the rectangles.</h4>
    <canvas id="canvas" width=300 height=300></canvas>
</body>
</html>

关于javascript - 如何创建自己的交互式全景网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25139515/

相关文章:

html - 在 Canvas 上播放视频并保留 Canvas 上的最后一帧/图像

javascript - 调整图像大小以获得特定的最大值。文件大小

javascript - WinJS:检查是否在 Debug模式下运行

javascript - 提交折叠 div 并更改 div 中的图像后的 Ajax 表单

html - IE9 怪高 - 无法修复

javascript - 如何按两位数而不是一位数排序?

html - 基于父元素类的 div 颜色

javascript - 如何使用 Fabric.js 将 URL 中的图像添加到 HTML Canvas 中并调整其大小?

javascript - 如何正确传递Parse.com Cloud Code参数?

javascript - 为移动设备禁用 javascript 视差效果