DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5詳解
HTML5 transform 打造 3D 立方體:可調尺寸、位置與三軸旋轉
編輯:HTML5詳解     

為了真正吃透 CSS transform 的精髓,最好的練習就是做一個 3D 立方體。本文示範用六個 div 拼出長方體,並提供長寬高、位置、三軸旋轉角度的即時控制介面。其中有個難題尚未完美解決,文末一併討論。

做法概述

六個面各是一個絕對定位的 div。關鍵在 transform-origin 的 Z 值——把旋轉軸往 Z 軸推半個深度,該面就會翻到正確的位置上,六面合起來就成一個封閉的方體。

CSS 與建構子骨架

<style>
.cuboid_side_div {
  position: absolute;
  border: 1px solid #333;
  -webkit-transition: ease all 1s;
}
</style>

<script>
function cuboidModel(left_init, top_init, long_init, width_init, height_init) {
    var left = left_init, top = top_init;
    var long = long_init, width = width_init, height = height_init;

    var rotateX = 0, rotateY = 0, rotateZ = 0, zIndex = 0;

    var div_front, div_behind, div_left, div_right, div_top, div_bottom;
    // ...以下為方法定義
}
</script>

建立六個面

this.init = function () {
    var defs = [
        ['front',  "div front",  "#f1b2b2"],
        ['behind', "div behind", "#bd91eb"],
        ['left',   "div left",   "#64a3c3"],
        ['right',  "div right",  "#78e797"],
        ['top',    "div top",    "#e7db78"],
        ['bottom', "div bottom", "#e79c78"]
    ];
    for (var i = 0; i < defs.length; i++) {
        var d = document.createElement("div");
        d.className = "cuboid_side_div";
        d.innerHTML = defs[i][1];
        d.style.backgroundColor = defs[i][2];
        document.body.appendChild(d);
        this['div_' + defs[i][0]] = d;
    }
    this.refresh();
};

定位每個面:transform-origin 是全部關鍵

this.refresh = function () {
    // 正面與背面:旋轉軸往後推半個寬度
    div_front.style.left   = left + "px";
    div_front.style.top    = top  + "px";
    div_front.style.width  = long   + "px";
    div_front.style.height = height + "px";
    div_front.style.webkitTransformOrigin = "50% 50% " + (-width / 2) + "px";

    div_behind.style.width  = div_front.style.width;
    div_behind.style.height = div_front.style.height;
    div_behind.style.webkitTransformOrigin = "50% 50% " + (-width / 2) + "px";

    // 左面與右面:本身是 height × width,軸往左推半個長度
    div_left.style.left   = left + ((long - height) / 2) + "px";
    div_left.style.top    = top  + ((height - width) / 2) + "px";
    div_left.style.width  = height + "px";
    div_left.style.height = width  + "px";
    div_left.style.webkitTransformOrigin = "50% 50% " + (-long / 2) + "px";

    div_right.style.left   = div_left.style.left;
    div_right.style.top    = div_left.style.top;
    div_right.style.width  = div_left.style.width;
    div_right.style.height = div_left.style.height;
    div_right.style.webkitTransformOrigin = "50% 50% " + (-long / 2) + "px";

    // 上面與下面:本身是 long × width,軸往上推半個高度
    div_top.style.left   = left + "px";
    div_top.style.top    = top + ((height - width) / 2) + "px";
    div_top.style.width  = long  + "px";
    div_top.style.height = width + "px";
    div_top.style.webkitTransformOrigin = "50% 50% " + (-height / 2) + "px";

    div_bottom.style.left   = div_top.style.left;
    div_bottom.style.top    = div_top.style.top;
    div_bottom.style.width  = div_top.style.width;
    div_bottom.style.height = div_top.style.height;
    div_bottom.style.webkitTransformOrigin = "50% 50% " + (-height / 2) + "px";

    this.rotate(rotateX, rotateY, rotateZ);
};

旋轉:每個面的角度要各自換算

立方體整體旋轉 (X, Y, Z) 之後,個別面的角度並不相同。例如背面相當於正面再繞 Y 軸轉 180 度;上面比正面多繞 X 軸 90 度。

this.rotate = function (x, y, z) {
    rotateX = x; rotateY = y; rotateZ = z;

    var rotateX_front = rotateX, rotateY_front = rotateY, rotateZ_front = rotateZ;

    var rotateX_behind = rotateX_front + 180;
    var rotateY_behind = rotateY_front * (-1);
    var rotateZ_behind = rotateZ_front * (-1);

    var rotateX_top = rotateX_front + 90;
    var rotateY_top = rotateZ_front;
    var rotateZ_top = rotateY_front * (-1);

    var rotateX_bottom = rotateX_front - 90;
    var rotateY_bottom = rotateZ_front * (-1);
    var rotateZ_bottom = rotateY_front;

    var rotateX_left  = rotateX_front + 90;
    var rotateY_left  = rotateZ_front - 90;
    var rotateZ_left  = rotateY_front * (-1);

    var rotateX_right = rotateX_front + 90;
    var rotateY_right = rotateZ_front + 90;
    var rotateZ_right = rotateY_front * (-1);

    // 套用 perspective 與旋轉
    div_front.style.webkitTransform =
        "perspective(500px) rotateX(" + rotateX_front + "deg) rotateY(" +
        rotateY_front + "deg) rotateZ(" + rotateZ_front + "deg)";
    div_behind.style.webkitTransform =
        "perspective(500px) rotateX(" + rotateX_behind + "deg) rotateY(" +
        rotateY_behind + "deg) rotateZ(" + rotateZ_behind + "deg)";
    div_left.style.webkitTransform =
        "perspective(500px) rotateX(" + rotateX_left + "deg) rotateZ(" +
        rotateZ_left + "deg) rotateY(" + rotateY_left + "deg)";
    div_right.style.webkitTransform =
        "perspective(500px) rotateX(" + rotateX_right + "deg) rotateZ(" +
        rotateZ_right + "deg) rotateY(" + rotateY_right + "deg)";
    div_top.style.webkitTransform =
        "perspective(500px) rotateX(" + rotateX_top + "deg) rotateZ(" +
        rotateZ_top + "deg) rotateY(" + rotateY_top + "deg)";
    div_bottom.style.webkitTransform =
        "perspective(500px) rotateX(" + rotateX_bottom + "deg) rotateZ(" +
        rotateZ_bottom + "deg) rotateY(" + rotateY_bottom + "deg)";
};

控制介面

function transform() {
    cuboid.resize(parseInt(document.getElementById("long").value),
                  parseInt(document.getElementById("width").value),
                  parseInt(document.getElementById("height").value));
    cuboid.move(parseInt(document.getElementById("left").value),
                parseInt(document.getElementById("top").value));
    cuboid.rotate(parseInt(document.getElementById("rotatex").value),
                  parseInt(document.getElementById("rotatey").value),
                  parseInt(document.getElementById("rotatez").value));
}
</script>

<div style="position:absolute; border:1px solid #333; top:240px; left:100px;
            width:1000px; height:360px;">
  left:    <input id="left" value="100"></input>px<br><br>
  top:     <input id="top" value="50"></input>px<br><br>
  long:    <input id="long" value="100"></input>px<br><br>
  width:   <input id="width" value="60"></input>px<br><br>
  height:  <input id="height" value="80"></input>px<br><br>
  rotateX: <input id="rotatex" value="0"></input>deg<br><br>
  rotateY: <input id="rotatey" value="0"></input>deg<br><br>
  rotateZ: <input id="rotatez" value="0"></input>deg<br><br>
  <input type="button" value="確定" onclick="transform()"></input>
</div>

那個未解的難題:面的遮擋順序

原範例試圖用 zIndex 矩陣,配合把旋轉角除以 90 度取模得到的象限值,來推算六個面之間的前後關係。想法是對的,但實作上仍有缺陷——當旋轉角度落在象限邊界附近時,zIndex 會跳階,導致某些角度下遠側的面蓋住近側的面。

好消息是,這個問題在今天有標準答案了:不要自己算 zIndex,改用 transform-style: preserve-3d,讓瀏覽器自己處理深度排序。

.scene {
  perspective: 500px;
  width: 200px; height: 200px;
}
.cuboid {
  position: relative;
  width: 100%; height: 100%;
  transform-style: preserve-3d;   /* 這一行取代整套 zIndex 計算 */
}
.face {
  position: absolute;
  width: 200px; height: 200px;
  backface-visibility: hidden;    /* 背對鏡頭的面自動隱藏 */
}
.front  { transform: translateZ(100px); }
.back   { transform: rotateY(180deg) translateZ(100px); }
.right  { transform: rotateY(90deg)  translateZ(100px); }
.left   { transform: rotateY(-90deg) translateZ(100px); }
.top    { transform: rotateX(90deg)  translateZ(100px); }
.bottom { transform: rotateX(-90deg) translateZ(100px); }

用這種寫法,六個面的定位不需要任何三角運算,深度排序交給 GPU,backface-visibility 還順便解決了背面穿透的問題。程式碼從幾百行縮到幾十行,效能也更好。

還可以延伸的模型

目前版本只有長方體。常見的還包括線段、平面、正錐體、球體與橢球體。球體比較特別——它無法用平面多邊形拼出,通常得靠透微投影把經緯網格畫上去,或用大量三角面近似。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved