js实现三维数据方块效果代码

代码语言:html

所属分类:三维

代码描述:js实现三维数据方块效果代码

代码标签: js 三维 数据 方块

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开


<!DOCTYPE html>
<html lang="en" >

<head>

 
<meta charset="UTF-8">
 
<link href="https://fonts.googleapis.com/css2?family=Nunito:wght@300;400;700&display=swap" rel="stylesheet">


 
 
<style>
html
,
body
{
 
width: 100%;
 
height: 100%;
 
margin: 0;
 
background: #2e3440;
 
transform: translateZ(0);
 
font-family: Nunito, Arial, sans-serif;
}
#stage {
 
width: 100%;
 
height: 100%;
 
display: flex;
 
align-items: center;
 
justify-content: center;
 
cursor: -webkit-grab;
 
cursor: grab;
}
#stage:active {
 
cursor: -webkit-grabbing;
 
cursor: grabbing;
}
#checkbox {
 
display: none;
 
visibility: hidden;
 
opacity: 0;
}
#checkbox:checked + .toggle {
 
background: #5e81ac;
}
#checkbox:checked + .toggle:before {
 
content: "Show Active Bits";
}
#checkbox:not(:checked) + .toggle {
 
background: #b48ead;
}
#checkbox:not(:checked) + .toggle:before {
 
content: "Show Inactive Bits";
}
.toggle {
 
position: fixed;
 
top: 16px;
 
left: 16px;
 
padding: 8px 16px;
 
border-radius: 99999px;
 
color: #fff;
 
font-weight: bold;
 
cursor: pointer;
}
.container {
 
position: relative;
 
transform-style: preserve-3d;
 
-webkit-user-select: none;
     
-moz-user-select: none;
     
-ms-user-select: none;
         
user-select: none;
 
will-change: transform;
}
.box {
 
position: absolute;
 
transform-style: preserve-3d;
}
.box:nth-child(0) .face {
 
opacity: 0.75;
}
.box .face {
 
position: absolute;
 
display: flex;
 
align-items: center;
 
justify-content: center;
 
transform-origin: center center;
 
border: 2px solid currentColor;
 
background: #d8dee9;
 
color: #434c5e;
 
-webkit-backface-visibility: hidden;
         
backface-visibility: hidden;
 
outline: 1px transparent;
 
font-weight: bold;
 
font-size: 1.25rem;
/* Front & Back */
/* Left & Right */
/* Top & Bottom */
}
.box .face.empty {
 
opacity: 0;
}
.box .face.horizontal-line {
 
background: #8fbcbb;
}
.box .face.vertical-line {
 
background: #88c0d0;
}
.box .face.cube {
 
background: #bf616a;
}
.box .face:nth-child(1) {
 
transform: translateZ(calc(var(--scale) * 0.5px));
}
.box .face:nth-child(2) {
 
transform: translateZ(calc(var(--scale) * -0.5px)) rotateY(180deg);
}
.box .face:nth-child(3) {
 
transform: rotateY(-90deg) translateZ(calc(var(--scale) * 0.5px));
}
.box .face:nth-child(4) {
 
transform: rotateY(-90deg) translateZ(calc(var(--scale) * -0.5px)) rotateY(180deg);
}
.box .face:nth-child(5) {
 
transform: rotateX(-90deg) translateZ(calc(var(--scale) * 0.5px));
}
.box .face:nth-child(6) {
 
transform: rotateX(-90deg) translateZ(calc(var(--scale) * -0.5px)) rotateY(180deg);
}
</style>



</head>

<body  >
 
<input type="checkbox" id="checkbox"/>
<label class="toggle" for="checkbox"></label>
<div id="stage"></div>

 
     
<script  >
"use strict";
const size = 8;
// Generate input with:
// Array.from({ length: 8 * 8 * 8 }, () => Math.random() > 0.5 ? 0 : 1).join("")
const input = "00100000100000100001000100010001100000010110110000010001001111100110000000000011001100000011000000011100101010000111000010000000011000000010101101000000000100001010010001010000100001010000010001001000110001100000001100100011000000001000000010100000010101001000010001000100110110110100110010100000000000000001000000001001010101100000010101000101000110100000000011000110101011100010110100000001010000101111001000000100010000010001010100000001100010011001001000100110010101001011000000001001011000010010001000110000";
class Shape {
    constructor(data, size) {
        this.data = data;
        this.size = size;
    }
    toPoints() {
        const output = [];
        for (let i = 0, x = 0, y = 0, z = 0; i < this.data.length; i++, x++) {
            if (i !== 0 && i % this.size === 0) {
                x = 0;
                y += 1;
            }
            if (y !== 0 && y % this.size === 0) {
                y = 0;
                z += 1;
            }
            output.push({ x, y, z, value: this.data[i] });
        }
        return output;
    }
    toPoints3d() {
        const output = [];
        const points = this.toPoints();
        for (const point of points) {
            if (output.length < point.y + 1) {
                output.push([]);
            }
            const row = output[point.y];
            if (row.length < point.x + 1) {
                row.push([]);
            }
            const col = row[point.x];
            col[point.z] = point;
        }
        return output;
    }
    map(f) {
        const points = this.toPoints();
        return points.map(f);
    }
}
const isHorizontalLine = (point, points3d) => {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    const prevPrevPoint = (_b = (_a = points3d[point.y]) === null || _a === void 0 ? void 0 : _a[point.x - 2]) === null || _b === void 0 ? void 0 : _b[point.z];
    const prevPoint = (_d = (_c = points3d[point.y]) === null || _c === void 0 ? void 0 : _c[point.x - 1]) === null || _d === void 0 ? void 0 : _d[point.z];
    const nextPoint = (_f = (_e = points3d[point.y]) === null || _e === void 0 ? void 0 : _e[point.x + 1]) === null || _f === void 0 ? void 0 : _f[point.z];
    const nextNextPoint = (_h = (_g = points3d[point.y]) === null || _g === void 0 ? void 0 : _g[point.x + 2]) === null || _h === void 0 ? void 0 : _h[point.z];
    if (((prevPrevPoint === null || prevPrevPoint === void 0 ? void 0 : prevPrevPoint.value) === point.value && (prevPrevPoint === null || prevPrevPoint === void 0 ? void 0 : prevPrevPoint.value) === point.value) ||
        ((prevPoint === null || prevPoint === void 0 ? void 0 : prevPoint.value) === point.value && (nextPoint === null || nextPoint === void 0 ? void 0 : nextPoint.value) === point.value) ||
        ((nextPoint === null || nextPoint === void 0 ? void 0 : nextPoint.value) === point.value && (nextNextPoint === null || nextNextPoint === void 0 ? void 0 : nextNextPoint.value) === point.value)) {
        return true;
    }
    return false;
};
const isHorizontalLineZ = (point, points3d) => {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    const prevPrevPoint = (_b = (_a = points3d[point.y]) === null || _a === void 0 ? void 0 : _a[point.x]) === null || _b === void 0 ? void 0 : _b[point.z - 2];
    const prevPoint = (_d = (_c = points3d[point.y]) === null || _c === void 0 ? void 0 : _c[point.x]) === null || _d === void 0 ? void 0 : _d[point.z - 1];
    const nextPoint = (_f = (_e = points3d[point.y]) === null || _e === void 0 ? void 0 : _e[point.x]) === null || _f === void 0 ? void 0 : _f[point.z + 1];
    const nextNextPoint = (_h = (_g = points3d[point.y]) === null || _g === void 0 ? void 0 : _g[point.x]) === null || _h === void 0 ? void 0 : _h[point.z + 2];
    if (((prevPrevPoint === null || prevPrevPoint === void 0 ? void 0 : prevPrevPoint.value) === point.value && (prevPrevPoint === null || prevPrevPoint === void 0 ? void 0 : prevPrevPoint.value) === point.value) ||
        ((prevPoint === null || prevPoint === void 0 ? void 0 : prevPoint.value) === point.value && (nextPoint === null || nextPoint === void 0 ? void 0 : nextPoint.value) === point.value) ||
        ((nextPoint === null || nextPoint === void 0 ? void 0 : nextPoint.value) === point.value && (nextNextPoint === null || nextNextPoint === void 0 ? void 0 : nextNextPoint.value) === point.value)) {
        return true;
    }
    return false;
};
const isVerticalLine = (point, points3d) => {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    const prevPrevPoint = (_b = (_a = points3d[point.y - 2]) === null || _a === void 0 ? void 0 : _a[point.x]) === null || _b === void 0 ? void 0 : _b[point.z];
    const prevPoint = (_d = (_c = points3d[point.y - 1]) === null || _c === void 0 ? void 0 : _c[point.x]) === null || _d === void 0 ? void 0 : _d[point.z];
    const nextPoint = (_f = (_e = points3d[point.y + 1]) === null || _e === void 0 ? void 0 : _e[point.x]) === null || _f === void 0 ? void 0 : _f[point.z];
    const nextNextPoint = (_h = (_g = points3d[point.y + 2]) === null || _g === void 0 ? void 0 : _g[point.x]) === null || _h === void 0 ? void 0 : _h[point.z];
    if (((prevPrevPoint === null || prevPrevPoint === void 0 ? void 0 : prevPrevPoint.value) === point.value && (prevPrevPoint === null || prevPrevPoint === void 0 ? void 0 : prevPrevPoint.value) === point.value) ||
        ((prevPoint === null || prevPoint === void 0 ? void 0 : prevPoint.value) === point.value && (nextPoint === null || nextPoint === void 0 ? void 0 : nextPoint.value) === point.value) ||
        ((nextPoint === null || nextPoint === void 0 ? void 0 : nextPoint.value) === point.value && (nextNextPoint === null || nextNextPoint === void 0 ? void 0 : nextNextPoint.value) === point.value)) {
        return true;
    }
  .........完整代码请登录后点击上方下载按钮下载查看

网友评论0