不使用WebGL创建倾斜视图的地图?

时间:2015-07-17 16:21:08

标签: javascript html css canvas plugins

我想创建一个地图查看器,从某个角度查看地图。如果角度是90度(如谷歌地图),很明显如何创建它。但是,我想要一个小于90的角度和而不使用WebGL 。像这样:

Shogun Total War

也许使用CSS或画布有一些解决方案,黑客和技巧? 谢谢!

1 个答案:

答案 0 :(得分:1)

仅使用JavaScript按钮和窗口边缘进行导航。需要为您的屏幕显示配置一些:只需找到以Adjust

开头的奖励

Result

var mapWidth = -4500 + 900, // #Adjust: the same as #second width
    mapHeight = -2234 + 600, // #Adjust: the same as #second height
    $map;

function moveScreen(dx, dy) {
  var positionX = $map.css("backgroundPositionX"),
      positionY = $map.css("backgroundPositionY");

  positionX = parseInt(positionX.substring(0, positionX.length - 2)) + dx;
  positionY = parseInt(positionY.substring(0, positionY.length - 2)) + dy;

  if (positionX < mapWidth) {
    positionX = mapWidth;
  }
  if (positionX > 0) {
    positionX = 0;
  }
  if (positionY < mapHeight) {
    positionY = mapHeight;
  }
  if (positionY > 0) {
    positionY = 0;
  }

  $map.css("backgroundPositionX", positionX + "px");
  $map.css("backgroundPositionY", positionY + "px");
}

$(document).ready(function(){
  $map = $("#second");

  var moverLeft = null, moverUp = null, moverRight = null, moverDown = null;
  var clearMover = function(code) {
    if (code == 37) {
      clearInterval(moverLeft);
      moverLeft = null;
    }
    if (code == 38) {
      clearInterval(moverUp);
      moverUp = null;
    }
    if (code == 39) {
      clearInterval(moverRight);
      moverRight = null;
    }
    if (code == 40) {
      clearInterval(moverDown);
      moverDown = null;
    }
  }
  var moveScreenTop = function(){
    if (moverUp == null) {
      moverUp = setInterval(function(){ moveScreen(0, 10)}, 10);
    }
  };
  var moveScreenBottom = function(){
    if (moverDown == null) {
      moverDown = setInterval(function(){ moveScreen(0, -10) }, 10);
    }
  };
  var moveScreenLeft = function(){
    if (moverLeft == null) {
      moverLeft = setInterval(function(){ moveScreen(10, 0) }, 10);
    }
  };
  var moveScreenRight = function(){
    if (moverRight == null) {
      moverRight = setInterval(function(){ moveScreen(-10, 0) }, 10);
    }
  };
  $("#button_top").hover(moveScreenTop, function(){clearMover(38);});
  $("#button_bottom").hover(moveScreenBottom, function(){clearMover(40);});
  $("#button_left").hover(moveScreenLeft, function(){clearMover(37);});
  $("#button_right").hover(moveScreenRight, function(){clearMover(39);});
  $("#button_left_top").hover(function(){moveScreenLeft(); moveScreenTop();}, function(){clearMover(37);clearMover(38)});
  $("#button_right_top").hover(function(){moveScreenRight(); moveScreenTop();}, function(){clearMover(39);clearMover(38)});
  $("#button_right_bottom").hover(function(){moveScreenRight(); moveScreenBottom();}, function(){clearMover(39);clearMover(40)});
  $("#button_left_bottom").hover(function(){moveScreenLeft(); moveScreenBottom();}, function(){clearMover(37);clearMover(40)});
  $(document).keyup(function(e){
    clearMover(e.which);
  });
  $(document).keydown(function(e){
    if (e.which == 37) {
      moveScreenLeft();
    }
    if (e.which == 38) {
      moveScreenTop();
    }
    if (e.which == 39) {
      moveScreenRight();
    }
    if (e.which == 40) {
      moveScreenBottom();
    }
  });
});
body {
  margin: 0;
  overflow: hidden;
}

#first {
        /* Adjust perspective for your screen */
        -moz-perspective: 600px;
        -moz-perspective-origin: 50%;
        -webkit-perspective: 600px;
        -webkit-perspective-origin: 50%;
        perspective: 600px;
        perspective-origin: 50%;
        width: 100%;
        height: 100%;
    }

    #second {
        margin: 0 auto;
        /* Adjust width and height for your screen */
        width: 900px;
        height: 600px;
        background: url("http://img2.wikia.nocookie.net/__cb20131223222429/althistory/images/archive/b/bb/20140706210315!World_Map_(Ranjit_Singh_Lives).png") 0px 0px;

        /* Adjust translateZ for your screen */
        -moz-transform-style: preserve-3d;
        -webkit-transform-style: preserve-3d;
        transform-style: preserve-3d;
        -webkit-transform: translateZ(260px) rotateX( 20deg );
        -moz-transform: translateZ(260px) rotateX( 20deg );
        transform: translateZ(260px) rotateX( 20deg );
    }

.button_edge, .button_corner {
  opacity: 0.1;
  background-color: #999999;
  position: fixed;
}

#button_top {
  height: 30px;
  left: 30px;
  right: 30px;
  top: 0px;
}

#button_bottom {
  height: 30px;
  left: 30px;
  right: 30px;
  bottom: 0px;
}

#button_left {
  width: 30px;
  left: 0px;
  top: 30px;
  bottom: 30px;
}

#button_right {
  width: 30px;
  right: 0px;
  top: 30px;
  bottom: 30px;
}


.button_corner {
  width: 30px;
  height: 30px;
}

#button_left_top {
  left: 0px;
  top: 0px;
}

#button_right_top {
  right: 0px;
  top: 0px;
}

#button_right_bottom {
  right: 0px;
  bottom: 0px;
}

#button_left_bottom {
  left: 0px;
  bottom: 0px;
}
.tip {
  opacity: 0.9;
  color: white;
  background-color: #666666;
  padding: 10px;
  font-size: 14px;
  width: 200px;
  position: fixed;
  left: 45%;
  bottom: 50px;
}
.tip:hover {opacity: 0.2;}
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>

<div id="first">
  <div id="second"></div>
</div>
<div class="button_edge" id="button_top"></div>
<div class="button_edge" id="button_bottom"></div>
<div class="button_edge" id="button_left"></div>
<div class="button_edge" id="button_right"></div>
<div class="button_corner" id="button_left_top"></div>
<div class="button_corner" id="button_right_top"></div>
<div class="button_corner" id="button_right_bottom"></div>
<div class="button_corner" id="button_left_bottom"></div>
<div class="tip">Use arrow buttons or mouse for navigation</div>

P.S。:当然它不是最终版本:它需要自动调整用户的屏幕,但我喜欢当前的版本。)