生成多邊菱形

生成多邊菱形

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        /*n邊形的外角和爲360  360/n
			n變形的內角 180 - 360/n*/
        
        * {
            margin: 0;
            padding: 0;
        }
        
        html,
        body {
            height: 100%;
            overflow: hidden;
        }
        
        #wrap {
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            margin: auto;
        }
        
        #wrap>.box {
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            margin: auto;
            transition: 10s transform;
            transform-style: preserve-3d;
        }
        
        #wrap>.box>div {
            position: absolute;
            background: pink;
            text-align: center;
            font: 50px/300px "微軟雅黑";
            backface-visibility: hidden;
        }
        
        #wrap:hover>.box {
            transform: rotateY(360DEG);
        }
    </style>
</head>

<body>
    <div id="wrap">
        <div class="box">
        </div>
    </div>
</body>
<script type="text/javascript">
    //n:棱數
    window.onload = function() {
        createLZ(10, 300, 300);
    }

    function createLZ(n, width, height) {
        var boxNode = document.querySelector("#wrap > .box");
        var stlyleNode = document.createElement("style");
        //外角
        var degOut = 360 / n;
        //內角
        var degIn = 180 - 360 / n;

        var text = "";
        var cssText = "";
        for (var i = 0; i < n; i++) {
            text += "<div>" + (i + 1) + "</div>";
            cssText += "#wrap > .box > div:nth-child(" + (i + 1) + "){transform: rotateY(" + (i * degOut) + "deg);}";
        }

        boxNode.innerHTML = text;
        var mianNode = document.querySelector("#wrap > .box > div");

        // 修改基準
        cssText += `#wrap > .box{
			transform-origin: center center -${( (width/ 2 )* Math.tan((degIn / 2) * Math.PI / 180)) }px;
			width:${width}px;
			height:${height}px;
			}`;
        cssText += `#wrap > .box > div{
			transform-origin: center center -${( width / 2 )* Math.tan((degIn / 2) * Math.PI / 180)}px;
			width:${width}px;
			height:${height}px;
			}`;

        //    修改景深

        cssText += `#wrap{
			perspective:  ${width}px;
			}`;
        stlyleNode.innerHTML = cssText;
        document.head.appendChild(stlyleNode);
    }
</script>

</html>

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章