超酷的元素周期表

史经业
2023-12-01

【原文链接】:https://blog.tecchen.xyz ,博文同步发布到博客园。
由于精力有限,对文章的更新可能不能及时同步,请点击上面的原文链接访问最新内容。
欢迎访问我的个人网站:https://www.tecchen.xyz

官方演示地址:【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>

转载于:https://www.cnblogs.com/Candies/p/11153791.html

 类似资料: