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;
    }
    return false;
};
const isCube = (point, points3d) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, _64, _65, _66, _67, _68, _69, _70, _71, _72, _73, _74, _75, _76, _77, _78, _79, _80, _81, _82, _83, _84, _85, _86, _87, _88, _89, _90, _91, _92, _93, _94, _95, _96, _97, _98, _99, _100, _101, _102, _103, _104, _105, _106, _107, _108, _109, _110, _111, _112, _113, _114, _115, _116, _117, _118, _119, _120, _121, _122, _123, _124, _125, _126, _127, _128, _129, _130, _131, _132, _133, _134, _135, _136, _137, _138, _139, _140, _141, _142, _143;
    // If at back left
    if ((((_c = (_b = (_a = points3d[point.y]) === null || _a === void 0 ? void 0 : _a[point.x]) === null || _b === void 0 ? void 0 : _b[point.z + 1]) === null || _c === void 0 ? void 0 : _c.value) === point.value) &&
        (((_f = (_e = (_d = points3d[point.y]) === null || _d === void 0 ? void 0 : _d[point.x + 1]) === null || _e === void 0 ? void 0 : _e[point.z]) === null || _f === void 0 ? void 0 : _f.value) === point.value) &&
        (((_j = (_h = (_g = points3d[point.y]) === null || _g === void 0 ? void 0 : _g[point.x + 1]) === null || _h === void 0 ? void 0 : _h[point.z + 1]) === null || _j === void 0 ? void 0 : _j.value) === point.value) &&
        (((_m = (_l = (_k = points3d[point.y + 1]) === null || _k === void 0 ? void 0 : _k[point.x]) === null || _l === void 0 ? void 0 : _l[point.z]) === null || _m === void 0 ? void 0 : _m.value) === point.value) &&
        (((_q = (_p = (_o = points3d[point.y + 1]) === null || _o === void 0 ? void 0 : _o[point.x]) === null || _p === void 0 ? void 0 : _p[point.z + 1]) === null || _q === void 0 ? void 0 : _q.value) === point.value) &&
        (((_t = (_s = (_r = points3d[point.y + 1]) === null || _r === void 0 ? void 0 : _r[point.x + 1]) === null || _s === void 0 ? void 0 : _s[point.z]) === null || _t === void 0 ? void 0 : _t.value) === point.value) &&
        (((_w = (_v = (_u = points3d[point.y + 1]) === null || _u === void 0 ? void 0 : _u[point.x + 1]) === null || _v === void 0 ? void 0 : _v[point.z + 1]) === null || _w === void 0 ? void 0 : _w.value) === point.value)) {
        return true;
    }
    // If at back right
    if ((((_z = (_y = (_x = points3d[point.y]) === null || _x === void 0 ? void 0 : _x[point.x]) === null || _y === void 0 ? void 0 : _y[point.z - 1]) === null || _z === void 0 ? void 0 : _z.value) === point.value) &&
        (((_2 = (_1 = (_0 = points3d[point.y]) === null || _0 === void 0 ? void 0 : _0[point.x + 1]) === null || _1 === void 0 ? void 0 : _1[point.z]) === null || _2 === void 0 ? void 0 : _2.value) === point.value) &&
        (((_5 = (_4 = (_3 = points3d[point.y]) === null || _3 === void 0 ? void 0 : _3[point.x + 1]) === null || _4 === void 0 ? void 0 : _4[point.z - 1]) === null || _5 === void 0 ? void 0 : _5.value) === point.value) &&
        (((_8 = (_7 = (_6 = points3d[point.y + 1]) === null || _6 === void 0 ? void 0 : _6[point.x]) === null || _7 === void 0 ? void 0 : _7[point.z]) === null || _8 === void 0 ? void 0 : _8.value) === point.value) &&
        (((_11 = (_10 = (_9 = points3d[point.y + 1]) === null || _9 === void 0 ? void 0 : _9[point.x]) === null || _10 === void 0 ? void 0 : _10[point.z - 1]) === null || _11 === void 0 ? void 0 : _11.value) === point.value) &&
        (((_14 = (_13 = (_12 = points3d[point.y + 1]) === null || _12 === void 0 ? void 0 : _12[point.x + 1]) === null || _13 === void 0 ? void 0 : _13[point.z]) === null || _14 === void 0 ? void 0 : _14.value) === point.value) &&
        (((_17 = (_16 = (_15 = points3d[point.y + 1]) === null || _15 === void 0 ? void 0 : _15[point.x + 1]) === null || _16 === void 0 ? void 0 : _16[point.z - 1]) === null || _17 === void 0 ? void 0 : _17.value) === point.value)) {
        return true;
    }
    // If at front left
    if ((((_20 = (_19 = (_18 = points3d[point.y]) === null || _18 === void 0 ? void 0 : _18[point.x]) === null || _19 === void 0 ? void 0 : _19[point.z + 1]) === null || _20 === void 0 ? void 0 : _20.value) === point.value) &&
        (((_23 = (_22 = (_21 = points3d[point.y]) === null || _21 === void 0 ? void 0 : _21.........完整代码请登录后点击上方下载按钮下载查看

网友评论0