$(document).ready(function() { calcTransf(80, 20, 40, 40, 100, 100); }); function calcTransf(x1, y1, z1, x2, y2, z2) { $(".cube").append('<svg class="C_R0"><path fill="rgba(80, 204, 04, 0.5)" stroke-width="0" d="M0,0 L20,20 L20,0 Z"></path></svg>'); $(".cube").append('<svg class="C_R1"><path fill="rgba(80, 204, 04, 0.5)" stroke-width="0" d="M0,0 L20,20 L20,0 Z"></path></svg>'); $(".cube").append('<svg class="C_R2"><path fill="rgba(80, 204, 04, 0.5)" stroke-width="0" d="M0,0 L20,20 L20,0 Z"></path></svg>'); $(".C_R0").css("transform", "translateX(0px) translateY(0px) translateZ(0px) rotateX(0deg) rotateY(0deg) rotateZ(0deg)"); $(".C_R1").css("transform", "translateX(" + x1 + "px) translateY(" + y1 + "px) translateZ(" + z1 + "px) rotateX(0deg) rotateY(0deg) rotateZ(0deg)"); $(".C_R2").css("transform", "translateX(" + x2 + "px) translateY(" + y2 + "px) translateZ(" + z2 + "px) rotateX(0deg) rotateY(0deg) rotateZ(0deg)"); var Yalpha = -Math.atan2(z1, x1); var LX = Math.sqrt(Math.pow(z1, 2) + Math.pow(x1, 2)); x1 = LX; y1 = y1; z1 = 0; DEGYalpha = Yalpha / Math.PI * 180; console.log("Yalpha " + DEGYalpha); var Zalpha = Math.atan2(y1, x1); var LX = Math.sqrt(Math.pow(y1, 2) + Math.pow(x1, 2)); x1 = LX; y1 = 0; z1 = 0; DEGZalpha = Zalpha / Math.PI * 180; console.log("Zalpha " + DEGZalpha); /* -----------------2. Punkt-------------------*/ var x2Ay = x2 * Math.cos(Yalpha) - z2 * Math.sin(Yalpha); var z2Ay = z2 * Math.cos(Yalpha) + x2 * Math.sin(Yalpha); x2 = x2Ay; y2 = y2; z2 = z2Ay; console.log("Ay: " + x2 + " " + y2 + " " + z2); Zalpha = -Zalpha; var x2Az = x2 * Math.cos(Zalpha) - y2 * Math.sin(Zalpha); var y2Az = y2 * Math.cos(Zalpha) + x2 * Math.sin(Zalpha); x2 = x2Az; y2 = y2Az; z2 = z2; console.log("Az: " + x2 + " " + y2 + " " + z2); //Winkel z-y var Xalpha = Math.atan2(z2, y2); DEGXalpha = Xalpha / Math.PI * 180; console.log("Xalpha " + DEGXalpha); var z2Ax = z2 * Math.cos(Xalpha) - y2 * Math.sin(Xalpha); var y2Ax = y2 * Math.cos(Xalpha) + z2 * Math.sin(Xalpha); x2 = x2; y2 = y2Ax; z2 = z2Ax; console.log("Ax: " + x2 + " " + y2 + " " + z2); $(".cube").append('<svg class="C_RE"><path fill="rgba(80, 4, 4, 0.5)" stroke-width="0" d="M0,0 L' + x1 + ',0 L' + x2 + ',' + y2 + ' Z"></path></svg>'); $(".C_RE").css("transform", 'translateX(0px) translateY(0px) translateZ(0px) rotateY(' + DEGYalpha + 'deg) rotateZ(' + DEGZalpha + 'deg) rotateX(' + DEGXalpha + 'deg)'); }
body { margin: 0; height: 100%; width: 100%; perspective: 500px; } .center { transform-style: preserve-3d; transform: translateX(50px) translateY(50px) translateZ(0px) rotateX(0deg) rotateY(0deg) rotateZ(0deg); } .cube { transform-style: preserve-3d; transform: translateX(0px) translateY(0px) translateZ(0px) rotateX(0deg) rotateY(0deg) rotateZ(0deg); } svg { transform-origin: left top; position: absolute; height: 150px; width: 150px; }
<!doctype html> <html> <head> <meta charset="utf-8"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </head> <body style="height: 150px; width: 150px;"> <div class="center" style="height: 50px; width: 50px;"> <div class="cube" style="height: 50px; width: 50px;"> </div> </div> </body> </html>