超酷的元素週期表

官方演示地址:【http://www.hewebgl.com/code/show/4.html

對於一個後端Java開發來說,炫酷的網頁是非常有吸引力的。各種特效、動態效果也很容易吸引住網友的注意力。
最近一直想給自己的博客【www.tecchen.xyz】 做個獨立的主頁, 博客僅僅作爲其中的一個欄目,所以搜索了各種個人網站的首頁演示效果、代碼及其他大神的博客和主頁。
在看到webgl和three.js時,對其中的特效很感興趣,就copy了官方演示效果的【http://www.hewebgl.com/code/show/4.html】 源碼,並參考【TWaver版3D化學元素週期表-https://www.cnblogs.com/twaver/p/3842177.html】 的效果,得到了下面的代碼。

個人代碼演示地址

http://show.tecchen.xyz/periodicTable.html

TODO工作

  • 1、一字長蛇特效
  • 2、點擊翻轉特效(參考輕鬆一下效果)
  • 3、雙擊放大特效
  • 4、點擊打開鏈接效果
  • 5、實戰(個人網站首頁)
  • 6、元素中文及讀音(已完成)

源碼

<!DOCTYPE html>
<html>
<head>
    <title>超酷的元素週期表 periodic table - three.js css3d</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"/>
    <style>
        body {
            margin: 0;
            background-color: #000;
            color: #fff;
            font-family: Monospace;
            font-size: 13px;
            line-height: 24px;
        }

        a {
            color: #ff0;
            text-decoration: none;
        }

        a:hover {
            text-decoration: underline;
        }

        button {
            cursor: pointer;
            text-transform: uppercase;
        }

        canvas {
            display: block;
        }

        #info {
            position: absolute;
            top: 0px;
            width: 100%;
            padding: 10px;
            box-sizing: border-box;
            text-align: center;
            z-index: 1;
        }

        .dg.ac {
            z-index: 2 !important;
        }

        a {
            color: #8ff;
        }

        #menu {
            position: absolute;
            bottom: 20px;
            width: 100%;
            text-align: center;
        }

        .element {
            width: 120px;
            height: 160px;
            box-shadow: 0px 0px 12px rgba(0, 255, 255, 0.5);
            border: 1px solid rgba(127, 255, 255, 0.25);
            font-family: Helvetica, sans-serif;
            text-align: center;
            line-height: normal;
            cursor: default;
        }

        .element:hover {
            box-shadow: 0px 0px 12px rgba(0, 255, 255, 0.75);
            border: 1px solid rgba(127, 255, 255, 0.75);
        }

        .element .name {
            position: absolute;
            top: 18px;
            left: 20px;
            font-size: 12px;
            color: rgba(127, 255, 255, 0.75);
        }

        .element .pronunciation {
            position: absolute;
            top: 20px;
            left: 45px;
            font-size: 12px;
            color: rgba(127, 255, 255, 0.75);
        }

        .element .number {
            position: absolute;
            top: 20px;
            right: 20px;
            font-size: 12px;
            color: rgba(127, 255, 255, 0.75);
        }

        .element .symbol {
            position: absolute;
            top: 40px;
            left: 0px;
            right: 0px;
            font-size: 60px;
            font-weight: bold;
            color: rgba(255, 255, 255, 0.75);
            text-shadow: 0 0 10px rgba(0, 255, 255, 0.95);
        }

        .element .details {
            position: absolute;
            bottom: 15px;
            left: 0px;
            right: 0px;
            font-size: 12px;
            color: rgba(127, 255, 255, 0.75);
        }

        button {
            color: rgba(127, 255, 255, 0.75);
            background: transparent;
            outline: 1px solid rgba(127, 255, 255, 0.75);
            border: 0px;
            padding: 5px 10px;
            cursor: pointer;
        }

        button:hover {
            background-color: rgba(0, 255, 255, 0.5);
        }

        button:active {
            color: #000000;
            background-color: rgba(0, 255, 255, 0.75);
        }
    </style>
</head>
<body>
<script src="https://threejs.org/examples/js/libs/tween.min.js"></script>
<div id="info">
    <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> css3d - 超酷的元素週期表 periodic table.
</div>
<div id="container"></div>
<div id="menu">
    <button id="table">表格方式</button>
    <button id="sphere">球形方式</button>
    <button id="helix">螺旋方式</button>
    <button id="grid">網格方式</button>
    <button id="snake">一字長蛇</button>
</div>
<script type="module">

    import * as THREE from 'https://threejs.org/build/three.module.js';

    import { TrackballControls } from 'https://threejs.org/examples/jsm/controls/TrackballControls.js';
    import { CSS3DRenderer, CSS3DObject } from 'https://threejs.org/examples/jsm/renderers/CSS3DRenderer.js';

    var table = [
        "1", "氫", "(qīng)",     "H", "Hydrogen", "1.00794", 1, 1,
        "2", "氦", "(hài)",     "He", "Helium", "4.002602", 18, 1,
        "3", "鋰", "(lǐ)",     "Li", "Lithium", "6.941", 1, 2,
        "4", "鈹", "(pí)",     "Be", "Beryllium", "9.012182", 2, 2,
        "5", "硼", "(péng)",     "B", "Boron", "10.811", 13, 2,
        "6", "碳", "(tàn)",     "C", "Carbon", "12.0107", 14, 2,
        "7", "氮", "(dàn)",     "N", "Nitrogen", "14.0067", 15, 2,
        "8", "氧", "(yǎng)",     "O", "Oxygen", "15.9994", 16, 2,
        "9", "氟", "(fú)",     "F", "Fluorine", "18.9984032", 17, 2,
        "10", "氖", "(nǎi)",     "Ne", "Neon", "20.1797", 18, 2,
        "11", "鈉", "(nà)",     "Na", "Sodium", "22.98976...", 1, 3,
        "12", "鎂", "(měi)",     "Mg", "Magnesium", "24.305", 2, 3,
        "13", "鋁", "(lǚ)",     "Al", "Aluminium", "26.9815386", 13, 3,
        "14", "硅", "(guī)",     "Si", "Silicon", "28.0855", 14, 3,
        "15", "磷", "(lín)",     "P", "Phosphorus", "30.973762", 15, 3,
        "16", "硫", "(liú)",     "S", "Sulfur", "32.065", 16, 3,
        "17", "氯", "(lǜ)",     "Cl", "Chlorine", "35.453", 17, 3,
        "18", "氬", "(yà)",     "Ar", "Argon", "39.948", 18, 3,
        "19", "鉀", "(jiǎ)",     "K", "Potassium", "39.948", 1, 4,
        "20", "鈣", "(gài)",     "Ca", "Calcium", "40.078", 2, 4,
        "21", "鈧", "(kàng)",     "Sc", "Scandium", "44.955912", 3, 4,
        "22", "鈦", "(tài)",     "Ti", "Titanium", "47.867", 4, 4,
        "23", "釩", "(fán)",     "V", "Vanadium", "50.9415", 5, 4,
        "24", "鉻", "(gè)",     "Cr", "Chromium", "51.9961", 6, 4,
        "25", "錳", "(měng)",     "Mn", "Manganese", "54.938045", 7, 4,
        "26", "鐵", "(tiě)",     "Fe", "Iron", "55.845", 8, 4,
        "27", "鈷", "(gǔ)",     "Co", "Cobalt", "58.933195", 9, 4,
        "28", "鎳", "(niè)",     "Ni", "Nickel", "58.6934", 10, 4,
        "29", "銅", "(tóng)",     "Cu", "Copper", "63.546", 11, 4,
        "30", "鋅", "(xīn)",     "Zn", "Zinc", "65.38", 12, 4,
        "31", "鎵", "(jiā)",     "Ga", "Gallium", "69.723", 13, 4,
        "32", "鍺", "(zhě)",     "Ge", "Germanium", "72.63", 14, 4,
        "33", "砷", "(shēn)",     "As", "Arsenic", "74.9216", 15, 4,
        "34", "硒", "(xī)",     "Se", "Selenium", "78.96", 16, 4,
        "35", "溴", "(xiù)",     "Br", "Bromine", "79.904", 17, 4,
        "36", "氪", "(kè)",     "Kr", "Krypton", "83.798", 18, 4,
        "37", "銣", "(rú)",     "Rb", "Rubidium", "85.4678", 1, 5,
        "38", "鍶", "(sī)",     "Sr", "Strontium", "87.62", 2, 5,
        "39", "釔", "(yǐ)",     "Y", "Yttrium", "88.90585", 3, 5,
        "40", "鋯", "(gào)",     "Zr", "Zirconium", "91.224", 4, 5,
        "41", "鈮", "(ní)",     "Nb", "Niobium", "92.90628", 5, 5,
        "42", "鉬", "(mù)",     "Mo", "Molybdenum", "95.96", 6, 5,
        "43", "鍀", "(dé)",     "Tc", "Technetium", "(98)", 7, 5,
        "44", "釕", "(liǎo)",     "Ru", "Ruthenium", "101.07", 8, 5,
        "45", "銠", "(lǎo)",     "Rh", "Rhodium", "102.9055", 9, 5,
        "46", "鈀", "(bǎ)",     "Pd", "Palladium", "106.42", 10, 5,
        "47", "銀", "(yín)",     "Ag", "Silver", "107.8682", 11, 5,
        "48", "鎘", "(gé)",     "Cd", "Cadmium", "112.411", 12, 5,
        "49", "銦", "(yīn)",     "In", "Indium", "114.818", 13, 5,
        "50", "錫", "(xī)",     "Sn", "Tin", "118.71", 14, 5,
        "51", "銻", "(tī)",     "Sb", "Antimony", "121.76", 15, 5,
        "52", "碲", "(dì)",     "Te", "Tellurium", "127.6", 16, 5,
        "53", "碘", "(diǎn)",     "I", "Iodine", "126.90447", 17, 5,
        "54", "氙", "(xiān)",     "Xe", "Xenon", "131.293", 18, 5,
        "55", "銫", "(sè)",     "Cs", "Caesium", "132.9054", 1, 6,
        "56", "鋇", "(bèi)",     "Ba", "Barium", "132.9054", 2, 6,
        "57", "鑭", "(lán)",      "La", "Lanthanum", "138.90547", 4, 9,
        "58", "鈰", "(shì)",      "Ce", "Cerium", "140.116", 5, 9,
        "59", "鐠", "(pǔ)",      "Pr", "Praseodymium", "140.90765", 6, 9,
        "60", "釹", "(nǚ)",      "Nd", "Neodymium", "144.242", 7, 9,
        "61", "鉕", "(pǒ)",      "Pm", "Promethium", "(145)", 8, 9,
        "62", "釤", "(shān)",      "Sm", "Samarium", "150.36", 9, 9,
        "63", "銪", "(yǒu)",      "Eu", "Europium", "151.964", 10, 9,
        "64", "釓", "(gá)",      "Gd", "Gadolinium", "157.25", 11, 9,
        "65", "鋱", "(tè)",      "Tb", "Terbium", "158.92535", 12, 9,
        "66", "鏑", "(dī)",      "Dy", "Dysprosium", "162.5", 13, 9,
        "67", "鈥", "(huǒ)",      "Ho", "Holmium", "164.93032", 14, 9,
        "68", "鉺", "(ěr)",      "Er", "Erbium", "167.259", 15, 9,
        "69", "銩", "(diū)",      "Tm", "Thulium", "168.93421", 16, 9,
        "70", "鐿", "(yì)",      "Yb", "Ytterbium", "173.054", 17, 9,
        "71", "鑥", "(lǔ)",      "Lu", "Lutetium", "174.9668", 18, 9,
        "72", "鉿", "(hā)",      "Hf", "Hafnium", "178.49", 4, 6,
        "73", "鉭", "(tǎn)",      "Ta", "Tantalum", "180.94788", 5, 6,
        "74", "鎢", "(wū)",      "W", "Tungsten", "183.84", 6, 6,
        "75", "錸", "(lái)",      "Re", "Rhenium", "186.207", 7, 6,
        "76", "鋨", "(é)",      "Os", "Osmium", "190.23", 8, 6,
        "77", "銥", "(yī)",      "Ir", "Iridium", "192.217", 9, 6,
        "78", "鉑", "(bó)",      "Pt", "Platinum", "195.084", 10, 6,
        "79", "金", "(jīn)",      "Au", "Gold", "196.966569", 11, 6,
        "80", "汞", "(gǒng)",      "Hg", "Mercury", "200.59", 12, 6,
        "81", "鉈", "(tā)",      "Tl", "Thallium", "204.3833", 13, 6,
        "82", "鉛", "(qiān)",      "Pb", "Lead", "207.2", 14, 6,
        "83", "鉍", "(bì)",      "Bi", "Bismuth", "208.9804", 15, 6,
        "84", "釙", "(pō)",      "Po", "Polonium", "(209)", 16, 6,
        "85", "砹", "(ài)",      "At", "Astatine", "(210)", 17, 6,
        "86", "氡", "(dōng)",      "Rn", "Radon", "(222)", 18, 6,
        "87", "鈁", "(fāng)",      "Fr", "Francium", "(223)", 1, 7,
        "88", "鐳", "(léi)",      "Ra", "Radium", "(226)", 2, 7,
        "89", "錒", "(ā)",         "Ac", "Actinium", "(227)", 4, 10,
        "90", "釷", "(tǔ)",         "Th", "Thorium", "232.03806", 5, 10,
        "91", "鏷", "(pú)",         "Pa", "Protactinium", "231.0588", 6, 10,
        "92", "鈾", "(yóu)",         "U", "Uranium", "238.02891", 7, 10,
        "93", "鎿", "(ná)",         "Np", "Neptunium", "(237)", 8, 10,
        "94", "鈈", "(bù)",         "Pu", "Plutonium", "(244)", 9, 10,
        "95", "鎇", "(méi)",         "Am", "Americium", "(243)", 10, 10,
        "96", "鋦", "(jú)",         "Cm", "Curium", "(247)", 11, 10,
        "97", "錇", "(péi)",         "Bk", "Berkelium", "(247)", 12, 10,
        "98", "鐦", "(kāi)",         "Cf", "Californium", "(251)", 13, 10,
        "99", "鎄", "(āi)",         "Es", "Einstenium", "(252)", 14, 10,
        "100", "鐨", "(fèi)",         "Fm", "Fermium", "(257)", 15, 10,
        "101", "鍆", "(mén)",         "Md", "Mendelevium", "(258)", 16, 10,
        "102", "鍩", "(nuò)",         "No", "Nobelium", "(259)", 17, 10,
        "103", "鐒", "(láo)",         "Lr", "Lawrencium", "(262)", 18, 10,
        "104", "釒盧", "(lú)",         "Rf", "Rutherfordium", "(267)", 4, 7,
        "105", "釒杜", "(dù)",         "Db", "Dubnium", "(268)", 5, 7,
        "106", "釒喜", "(xǐ)",         "Sg", "Seaborgium", "(271)", 6, 7,
        "107", "釒波", "(bō)",         "Bh", "Bohrium", "(272)", 7, 7,
        "108", "釒黑", "(hēi)",         "Hs", "Hassium", "(270)", 8, 7,
        "109", "釒麥", "(mài)",         "Mt", "Meitnerium", "(276)", 9, 7,
        "110", "釒達", "(dá)",         "Ds", "Darmstadium", "(281)", 10, 7,
        "111", "釒侖", "(lún)",         "Rg", "Roentgenium", "(280)", 11, 7,
        "112", "釒哥", "(ge)",         "Cn", "Copernicium", "(285)", 12, 7,
        "113", "釒爾", "(er)",         "Nh", "Nihonium", "(286)", 13, 7,
        "114", "釒夫", "(fu)",         "Fl", "Flerovium", "(289)", 14, 7,
        "115", "鏌", "(mò)",         "Mc", "Moscovium", "(290)", 15, 7,
        "116", "鉝", "(lì)",         "Lv", "Livermorium", "(293)", 16, 7,
        "117", "石田", "(tían)",         "Ts", "Tennessine", "(294)", 17, 7,
        "118", "奧氣", "(aò)",         "Og", "Oganesson", "(294)", 18, 7
    ];

    var camera, scene, renderer;
    var controls;

    var objects = [];
    var targets = { table: [], sphere: [], helix: [], grid: [], snake: [] };

    init();
    animate();

    function init() {
        camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 10000);
        camera.position.z = 3000;

        scene = new THREE.Scene();

        // 表格
        for (var i = 0; i < table.length; i += 8) {

            var element = document.createElement('div');
            element.className = 'element';
            // element.style.backgroundColor = 'rgba(0,127,127,' + ( Math.random() * 0.5 + 0.25 ) + ')';
            element.style.backgroundColor = 'rgba(12,87,200,' + (Math.random() * 0.5 + 0.25) + ')';

            var id = document.createElement('div');
            id.className = 'id';
            id.textContent = table[i];
            // element.appendChild( id );

            var name = document.createElement('div');
            name.className = 'name';
            name.textContent = table[i + 1];
            element.appendChild(name);

            var pronunciation = document.createElement('div');
            pronunciation.className = 'pronunciation';
            pronunciation.textContent = table[i + 2];
            element.appendChild(pronunciation);

            var number = document.createElement('div');
            number.className = 'number';
            number.textContent = (i / 8) + 1;
            element.appendChild(number);

            var symbol = document.createElement('div');
            symbol.className = 'symbol';
            symbol.textContent = table[i + 3];
            element.appendChild(symbol);

            var details = document.createElement('div');
            details.className = 'details';
            details.innerHTML = table[i + 4] + '<br>' + table[i + 5];
            element.appendChild(details);

            var object = new CSS3DObject(element);
            object.position.x = Math.random() * 4000 - 2000;
            object.position.y = Math.random() * 4000 - 2000;
            object.position.z = Math.random() * 4000 - 2000;
            scene.add(object);

            objects.push(object);


            var object = new THREE.Object3D();
            object.position.x = (table[i + 6] * 140) - 1330;
            object.position.y = -(table[i + 7] * 180) + 990;

            targets.table.push(object);
        }

        // 球形
        var vector = new THREE.Vector3();

        for (var i = 0, l = objects.length; i < l; i++) {
            var phi = Math.acos(-1 + (2 * i) / l);
            var theta = Math.sqrt(l * Math.PI) * phi;

            var object = new THREE.Object3D();

            object.position.setFromSphericalCoords(800, phi, theta);

            vector.copy(object.position).multiplyScalar(2);

            object.lookAt(vector);

            targets.sphere.push(object);
        }

        // 螺旋
        var vector = new THREE.Vector3();
        for (var i = 0, l = objects.length; i < l; i++) {
            var theta = i * 0.175 + Math.PI;
            var y = -(i * 8) + 450;

            var object = new THREE.Object3D();

            object.position.setFromCylindricalCoords(900, theta, y);

            vector.x = object.position.x * 2;
            vector.y = object.position.y;
            vector.z = object.position.z * 2;

            object.lookAt(vector);

            targets.helix.push(object);
        }

        // 網格
        for (var i = 0; i < objects.length; i++) {
            var object = new THREE.Object3D();

            object.position.x = ((i % 5) * 400) - 800;
            object.position.y = (-(Math.floor(i / 5) % 5) * 400) + 800;
            object.position.z = (-Math.floor(i / 25)) * 1000 + 2000;

            targets.grid.push(object);
        }

        // 
        renderer = new CSS3DRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.getElementById('container').appendChild(renderer.domElement);

        //
        controls = new TrackballControls(camera, renderer.domElement);
        controls.minDistance = 500;
        controls.maxDistance = 6000;
        controls.addEventListener('change', render);

        var button = document.getElementById('table');
        button.addEventListener('click', function () {
            transform(targets.table, 2000);
        }, false);

        var button = document.getElementById('sphere');
        button.addEventListener('click', function () {
            transform(targets.sphere, 2000);
        }, false);

        var button = document.getElementById('helix');
        button.addEventListener('click', function () {
            transform(targets.helix, 2000);
        }, false);

        var button = document.getElementById('grid');
        button.addEventListener('click', function () {
            transform(targets.grid, 2000);
        }, false);

        var button = document.getElementById('snake');
        button.addEventListener('click', function () {
            transform(targets.snake, 2000);
        }, false);

        transform(targets.table, 2000);

        //
        window.addEventListener('resize', onWindowResize, false);
    }

    function transform(targets, duration) {
        TWEEN.removeAll();

        for (var i = 0; i < objects.length; i++) {

            var object = objects[i];
            var target = targets[i];

            new TWEEN.Tween( object.position ).to( { x: target.position.x, y: target.position.y, z: target.position.z},Math.random() * duration + duration ).easing( TWEEN.Easing.Exponential.InOut ).start();

            new TWEEN.Tween( object.rotation ).to( {x: target.rotation.x, y: target.rotation.y, z: target.rotation.z},Math.random() * duration + duration ).easing( TWEEN.Easing.Exponential.InOut ).start();
        }

        new TWEEN.Tween(this).to({}, duration * 2).onUpdate(render).start();
    }

    function onWindowResize() {
        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();

        renderer.setSize(window.innerWidth, window.innerHeight);

        render();
    }

    function animate() {
        requestAnimationFrame(animate);

        TWEEN.update();

        controls.update();
    }

    function render() {
        renderer.render(scene, camera);
    }
</script>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章