2012-08-31 34 views
2

如何在canvas 2d中做等角立方體?我已經按照True Isometric Projection with HTML5 Canvas獲得了立方體的頂部,但你如何做左右兩側?html canvas中的等距立方體投影

注意:我想使用Kinetic.js對象,並且事件處理功能保持不變。

這就是我現在所擁有的:

<!DOCTYPE HTML> 
<html> 
    <head> 
    <style> 
     body { 
     margin: 0px; 
     padding: 0px; 
     } 
     canvas { 
     border: 1px solid #9C9898; 
     } 
    </style> 
    <script src="http://www.html5canvastutorials.com/libraries/kinetic-v4.0.0.js"></script> 
    <script> 
     window.onload = function() { 
     var stage = new Kinetic.Stage({ 
      container: "container", 
      width: 578, 
      height: 200, 
      //scale: [1, 0.5], 
      //scale: [1, 0.86062], 
      //rotation: -30 * Math.PI /180, 
     }); 

     var layer = new Kinetic.Layer({ 
      scale: [1,0.5], 
     }); 

     var foo = false; 

     layer.beforeDraw(function(){ 
      if (!foo) { 
       var context = this.getContext(); 
       var sx = 45*Math.PI/180; 
       // .75 horizontal shear 
       var sy = 0; 
       // no vertical shear 

       // apply custom transform 
       //context.transform(1, sy, sx, 1, 0, 0); 
       //context.scale(1, 0.5); 
       //context.rotate(45 * Math.PI /180); 
       //var angle = 30; 
       //context.setTransform(1, Math.tan(30), 0, 1, 0, 0); 
      } 
      foo = true; 

     }); 



     layer.afterDraw(function(){ 
      var context = this.getContext(); 

      //context.scale(1, 2); 
      //context.rotate(-45 * Math.PI /180); 

     }); 



     var rect = new Kinetic.Rect({ 
      x: 200, 
      y: 100, 
      width: 50, 
      height: 50, 
      fill: "blue", 
      stroke: "black", 
      strokeWidth: 4, 

      rotation: -45 * Math.PI /180, 
      //scale: [1, 0.5], 

     }); 





     rect.on("mouseover", function() { 
       //alert(this.getFill()); 
       this.setFill("red"); 

       layer.draw(); 
     }); 

     rect.on("mouseout", function() { 
       //alert(this.getFill()); 
       this.setFill("blue"); 

       layer.draw(); 
     }); 

     // add the shape to the layer 
     layer.add(rect); 

    // add the layer to the stage 
    stage.add(layer); 

    }; 

</script> 

演示:http://jsfiddle.net/F5SzS/

回答

0

也許使用多邊形呢?這是一個例子。

http://jsfiddle.net/Ygnbb/

我做了3個部分,但你可以做到這一點作爲一個ploygon我猜。位置和尺寸需要一些調整,但...