• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    HTML+CSS+JavaScript实现图片3D展览的示例代码

    发布者: Error | 发布时间: 2025-6-16 12:33| 查看数: 64| 评论数: 0|帖子模式

    一、图片3D展览效果
    上传图片的大小有限制,录制的GIF图展示效果不是很好

    录屏的效果见链接:https://www.bilibili.com/video/BV1Bi4y1E7wk/
    二、代码实现
    1. HTML代码
    1. <!DOCTYPE HTML>
    2. <html>
    3. <head>
    4.     <meta charset="utf-8">
    5.     <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    6.     <meta name="HandheldFriendly" content="true">
    7.     <meta name="description" content="图片3D展览屋">
    8.     <meta name="keywords" content="canvas,3D_picture,perspective,texturing,gallery">
    9.     <link href="res/slider-wb.css" rel="stylesheet">
    10.     <title>图片3D展览屋</title>
    11.     <style>
    12.         html
    13.         {
    14.             overflow:visible;
    15.             -ms-touch-action: none;
    16.             -ms-content-zooming: none;
    17.         }
    18.         body
    19.         {
    20.             position: absolute;
    21.             margin: 0px;
    22.             padding: 0px;
    23.             background: #fff;
    24.             width: 100%;
    25.             height: 100%;
    26.         }
    27.         #canvas
    28.         {
    29.             position: absolute;
    30.             width: 100%;
    31.             height: 100%;
    32.             background: #fff;
    33.         }
    34.     </style>
    35.     <script type="text/javascript" src="res/ge1doot.js"></script>
    36.     <script type="text/javascript">

    37.         "use strict";

    38.         (function () {
    39.             /* ==== definitions ==== */
    40.             var diapo = [], layers = [], ctx, pointer, scr, camera, light, fps = 0, quality = [1, 2],
    41.             // ---- poly constructor ----
    42.         Poly = function (parent, face) {
    43.             this.parent = parent;
    44.             this.ctx = ctx;
    45.             this.color = face.fill || false;
    46.             this.points = [];
    47.             if (!face.img) {
    48.                 // ---- create points ----
    49.                 for (var i = 0; i < 4; i++) {
    50.                     this.points[i] = new ge1doot.transform3D.Point(
    51.                                         parent.pc.x + (face.x[i] * parent.normalZ) + (face.z[i] * parent.normalX),
    52.                                         parent.pc.y + face.y[i],
    53.                                         parent.pc.z + (face.x[i] * parent.normalX) + (-face.z[i] * parent.normalZ)
    54.                                 );
    55.                 }
    56.                 this.points[3].next = false;
    57.             }
    58.         },
    59.             // ---- diapo constructor ----
    60.         Diapo = function (path, img, structure) {
    61.             // ---- create image ----
    62.             this.img = new ge1doot.transform3D.Image(
    63.                         this, path + img.img, 1, {
    64.                             isLoaded: function (img) {
    65.                                 img.parent.isLoaded = true;
    66.                                 img.parent.loaded(img);
    67.                             }
    68.                         }
    69.                 );
    70.             this.visible = false;
    71.             this.normalX = img.nx;
    72.             this.normalZ = img.nz;
    73.             // ---- point center ----
    74.             this.pc = new ge1doot.transform3D.Point(img.x, img.y, img.z);
    75.             // ---- target positions ----
    76.             this.tx = img.x + (img.nx * Math.sqrt(camera.focalLength) * 20);
    77.             this.tz = img.z - (img.nz * Math.sqrt(camera.focalLength) * 20);
    78.             // ---- create polygons ----
    79.             this.poly = [];
    80.             for (var i = -1, p; p = structure[++i]; ) {
    81.                 layers[i] = (p.img === true ? 1 : 2);
    82.                 this.poly.push(
    83.                                 new Poly(this, p)
    84.                         );
    85.             }
    86.         },
    87.             // ---- init section ----
    88.         init = function (json) {
    89.             // draw poly primitive
    90.             Poly.prototype.drawPoly = ge1doot.transform3D.drawPoly;
    91.             // ---- init screen ----
    92.             scr = new ge1doot.Screen({
    93.                 container: "canvas"
    94.             });
    95.             ctx = scr.ctx;
    96.             scr.resize();
    97.             // ---- init pointer ----
    98.             pointer = new ge1doot.Pointer({
    99.                 tap: function () {
    100.                     if (camera.over) {
    101.                         if (camera.over === camera.target.elem) {
    102.                             // ---- return to the center ----
    103.                             camera.target.x = 0;
    104.                             camera.target.z = 0;
    105.                             camera.target.elem = false;
    106.                         } else {
    107.                             // ---- goto diapo ----
    108.                             camera.target.elem = camera.over;
    109.                             camera.target.x = camera.over.tx;
    110.                             camera.target.z = camera.over.tz;
    111.                             // ---- adapt tesselation level to distance ----
    112.                             for (var i = 0, d; d = diapo[i++]; ) {
    113.                                 var dx = camera.target.x - d.pc.x;
    114.                                 var dz = camera.target.z - d.pc.z;
    115.                                 var dist = Math.sqrt(dx * dx + dz * dz);
    116.                                 var lev = (dist > 1500) ? quality[0] : quality[1];
    117.                                 d.img.setLevel(lev);
    118.                             }
    119.                         }
    120.                     }
    121.                 }
    122.             });
    123.             // ---- init camera ----
    124.             camera = new ge1doot.transform3D.Camera({
    125.                 focalLength: Math.sqrt(scr.width) * 10,
    126.                 easeTranslation: 0.025,
    127.                 easeRotation: 0.06,
    128.                 disableRz: true
    129.             }, {
    130.                 move: function () {
    131.                     this.over = false;
    132.                     // ---- rotation ----
    133.                     if (pointer.isDraging) {
    134.                         this.target.elem = false;
    135.                         this.target.ry = -pointer.Xi * 0.01;
    136.                         this.target.rx = (pointer.Y - scr.height * 0.5) / (scr.height * 0.5);
    137.                     } else {
    138.                         if (this.target.elem) {
    139.                             this.target.ry = Math.atan2(
    140.                                                         this.target.elem.pc.x - this.x,
    141.                                                         this.target.elem.pc.z - this.z
    142.                                                 );
    143.                         }
    144.                     }
    145.                     this.target.rx *= 0.9;
    146.                 }
    147.             });
    148.             camera.z = -10000;
    149.             camera.py = 0;
    150.             // ---- create images ----
    151.             for (var i = 0, img; img = json.imgdata[i++]; ) {
    152.                 diapo.push(
    153.                                 new Diapo(
    154.                                         json.options.imagesPath,
    155.                                         img,
    156.                                         json.structure
    157.                                 )
    158.                         );
    159.             }
    160.             // ---- start engine ---- >>>
    161.             setInterval(function () {
    162.                 quality = (fps > 50) ? [2, 3] : [1, 2];
    163.                 fps = 0;
    164.             }, 1000);
    165.             run();
    166.         },
    167.             // ---- main loop ----
    168.         run = function () {
    169.             // ---- clear screen ----
    170.             ctx.clearRect(0, 0, scr.width, scr.height);
    171.             // ---- camera ----
    172.             camera.move();
    173.             // ---- draw layers ----
    174.             for (var k = -1, l; l = layers[++k]; ) {
    175.                 light = false;
    176.                 for (var i = 0, d; d = diapo[i++]; ) {
    177.                     (l === 1 && d.draw()) ||
    178.                                 (d.visible && d.poly[k].draw());
    179.                 }
    180.             }
    181.             // ---- cursor ----
    182.             if (camera.over && !pointer.isDraging) {
    183.                 scr.setCursor("pointer");
    184.             } else {
    185.                 scr.setCursor("move");
    186.             }
    187.             // ---- loop ----
    188.             fps++;
    189.             requestAnimFrame(run);
    190.         };
    191.             /* ==== prototypes ==== */
    192.             Poly.prototype.draw = function () {
    193.                 // ---- color light ----
    194.                 var c = this.color;
    195.                 if (c.light || !light) {
    196.                     var s = c.light ? this.parent.light : 1;
    197.                     // ---- rgba color ----
    198.                     light = "rgba(" +
    199.                                 Math.round(c.r * s) + "," +
    200.                                 Math.round(c.g * s) + "," +
    201.                                 Math.round(c.b * s) + "," + (c.a || 1) + ")";
    202.                     ctx.fillStyle = light;
    203.                 }
    204.                 // ---- paint poly ----
    205.                 if (!c.light || this.parent.light < 1) {
    206.                     // ---- projection ----
    207.                     for (
    208.                                 var i = 0;
    209.                                 this.points[i++].projection();
    210.                         );
    211.                     this.drawPoly();
    212.                     ctx.fill();
    213.                 }
    214.             }
    215.             /* ==== image onload ==== */
    216.             Diapo.prototype.loaded = function (img) {
    217.                 // ---- create points ----
    218.                 var d = [-1, 1, 1, -1, 1, 1, -1, -1];
    219.                 var w = img.texture.width * 0.5;
    220.                 var h = img.texture.height * 0.5;
    221.                 for (var i = 0; i < 4; i++) {
    222.                     img.points[i] = new ge1doot.transform3D.Point(
    223.                                 this.pc.x + (w * this.normalZ * d[i]),
    224.                                 this.pc.y + (h * d[i + 4]),
    225.                                 this.pc.z + (w * this.normalX * d[i])
    226.                         );
    227.                 }
    228.             }
    229.             /* ==== images draw ==== */
    230.             Diapo.prototype.draw = function () {
    231.                 // ---- visibility ----
    232.                 this.pc.projection();
    233.                 if (this.pc.Z > -(camera.focalLength >> 1) && this.img.transform3D(true)) {
    234.                     // ---- light ----
    235.                     this.light = 0.5 + Math.abs(this.normalZ * camera.cosY - this.normalX * camera.sinY) * 0.6;
    236.                     // ---- draw image ----
    237.                     this.visible = true;
    238.                     this.img.draw();
    239.                     // ---- test pointer inside ----
    240.                     if (pointer.hasMoved || pointer.isDown) {
    241.                         if (
    242.                                         this.img.isPointerInside(
    243.                                                 pointer.X,
    244.                                                 pointer.Y
    245.                                         )
    246.                                 ) camera.over = this;
    247.                     }
    248.                 } else this.visible = false;
    249.                 return true;
    250.             }
    251.             return {
    252.                 // --- load data ----
    253.                 load: function (data) {
    254.                     window.addEventListener('load', function () {
    255.                         ge1doot.loadJS(
    256.                                         "res/imageTransform3D.js",
    257.                                         init, data
    258.                                 );
    259.                     }, false);
    260.                 }
    261.             }
    262.         })().load({
    263.             imgdata: [
    264.             // north
    265.                 {img: 'imgs/001.jpg', x: -1000, y: 0, z: 1500, nx: 0, nz: 1 },
    266.                 { img: 'imgs/002.jpg', x: 0, y: 0, z: 1500, nx: 0, nz: 1 },
    267.                 { img: 'imgs/003.jpg', x: 1000, y: 0, z: 1500, nx: 0, nz: 1 },
    268.             // east
    269.                 {img: 'imgs/004.jpg', x: 1500, y: 0, z: 1000, nx: -1, nz: 0 },
    270.                 { img: 'imgs/005.jpg', x: 1500, y: 0, z: 0, nx: -1, nz: 0 },
    271.                 { img: 'imgs/006.jpg', x: 1500, y: 0, z: -1000, nx: -1, nz: 0 },
    272.             // south
    273.                 {img: 'imgs/007.jpg', x: 1000, y: 0, z: -1500, nx: 0, nz: -1 },
    274.                 { img: 'imgs/008.jpg', x: 0, y: 0, z: -1500, nx: 0, nz: -1 },
    275.                 { img: 'imgs/009.jpg', x: -1000, y: 0, z: -1500, nx: 0, nz: -1 },
    276.             // west
    277.                 {img: 'imgs/010.jpg', x: -1500, y: 0, z: -1000, nx: 1, nz: 0 },
    278.                 { img: 'imgs/011.jpg', x: -1500, y: 0, z: 0, nx: 1, nz: 0 },
    279.                 { img: 'imgs/012.jpg', x: -1500, y: 0, z: 1000, nx: 1, nz: 0 }
    280.         ],
    281.             structure: [
    282.                 {
    283.                     // wall
    284.                     fill: { r: 255, g: 255, b: 255, light: 1 },
    285.                     x: [-1001, -490, -490, -1001],
    286.                     z: [-500, -500, -500, -500],
    287.                     y: [500, 500, -500, -500]
    288.                 }, {
    289.                     // wall
    290.                     fill: { r: 255, g: 255, b: 255, light: 1 },
    291.                     x: [-501, 2, 2, -500],
    292.                     z: [-500, -500, -500, -500],
    293.                     y: [500, 500, -500, -500]
    294.                 }, {
    295.                     // wall
    296.                     fill: { r: 255, g: 255, b: 255, light: 1 },
    297.                     x: [0, 502, 502, 0],
    298.                     z: [-500, -500, -500, -500],
    299.                     y: [500, 500, -500, -500]
    300.                 }, {
    301.                     // wall
    302.                     fill: { r: 255, g: 255, b: 255, light: 1 },
    303.                     x: [490, 1002, 1002, 490],
    304.                     z: [-500, -500, -500, -500],
    305.                     y: [500, 500, -500, -500]
    306.                 }, {
    307.                     // shadow
    308.                     fill: { r: 0, g: 0, b: 0, a: 0.2 },
    309.                     x: [-420, 420, 420, -420],
    310.                     z: [-500, -500, -500, -500],
    311.                     y: [150, 150, -320, -320]
    312.                 }, {
    313.                     // shadow
    314.                     fill: { r: 0, g: 0, b: 0, a: 0.2 },
    315.                     x: [-20, 20, 20, -20],
    316.                     z: [-500, -500, -500, -500],
    317.                     y: [250, 250, 150, 150]
    318.                 }, {
    319.                     // shadow
    320.                     fill: { r: 0, g: 0, b: 0, a: 0.2 },
    321.                     x: [-20, 20, 20, -20],
    322.                     z: [-500, -500, -500, -500],
    323.                     y: [-320, -320, -500, -500]
    324.                 }, {
    325.                     // shadow
    326.                     fill: { r: 0, g: 0, b: 0, a: 0.2 },
    327.                     x: [-20, 20, 10, -10],
    328.                     z: [-500, -500, -100, -100],
    329.                     y: [-500, -500, -500, -500]
    330.                 }, {
    331.                     // base
    332.                     fill: { r: 32, g: 32, b: 32 },
    333.                     x: [-50, 50, 50, -50],
    334.                     z: [-150, -150, -50, -50],
    335.                     y: [-500, -500, -500, -500]
    336.                 }, {
    337.                     // support
    338.                     fill: { r: 16, g: 16, b: 16 },
    339.                     x: [-10, 10, 10, -10],
    340.                     z: [-100, -100, -100, -100],
    341.                     y: [300, 300, -500, -500]
    342.                 }, {
    343.                     // frame
    344.                     fill: { r: 255, g: 255, b: 255 },
    345.                     x: [-320, -320, -320, -320],
    346.                     z: [0, -20, -20, 0],
    347.                     y: [-190, -190, 190, 190]
    348.                 }, {
    349.                     // frame
    350.                     fill: { r: 255, g: 255, b: 255 },
    351.                     x: [320, 320, 320, 320],
    352.                     z: [0, -20, -20, 0],
    353.                     y: [-190, -190, 190, 190]
    354.                 },
    355.                 { img: true },
    356.                 {
    357.                     // ceilingLight
    358.                     fill: { r: 255, g: 128, b: 0 },
    359.                     x: [-50, 50, 50, -50],
    360.                     z: [450, 450, 550, 550],
    361.                     y: [500, 500, 500, 500]
    362.                 }, {
    363.                     // groundLight
    364.                     fill: { r: 255, g: 128, b: 0 },
    365.                     x: [-50, 50, 50, -50],
    366.                     z: [450, 450, 550, 550],
    367.                     y: [-500, -500, -500, -500]
    368.                 }
    369.         ],
    370.             options: {
    371.                 imagesPath: ""
    372.             }
    373.         });
    374. </script>
    375. </head>
    376. <body>
    377. <a href="https://blog.csdn.net/fyfugoyfa/article/details/108845194" target="_blank">CSDN文章</a><br />
    378.     <canvas id="canvas">建议用谷歌浏览器打开</canvas>
    379. </body>
    380. </html>
    复制代码
    2. CSS代码
    1. #icon {
    2.         background: rgb(255, 144, 0); border-radius: 6px 0px 0px; transition:0.6s ease-in-out; left: 65px; top: 6px; width: 40px; height: 40px; overflow: hidden; position: relative; cursor: pointer; -moz-transition: all 0.6s ease-in-out 0s; -webkit-transition: all 0.6s ease-in-out 0s; -o-transition: all 0.6s ease-in-out 0s;
    3. }
    4. #icon div {
    5.         background: none; position: absolute;
    6. }
    7. #icon div:nth-child(1) {
    8.         border-width: 7px 0px 7px 8px; border-style: solid; border-color: transparent rgb(255, 255, 255); left: 20px; top: 50%; width: 0px; height: 0px; margin-top: -7px; position: absolute;
    9. }
    10. #icon div:nth-child(2) {
    11.         background: rgb(255, 255, 255); left: 12px; top: 50%; width: 8px; height: 6px; margin-top: -3px; position: absolute;
    12. }
    13. #nav {
    14.         top: 10px; width: 100px; height: 0px; position: absolute; -ms-user-select: none; -webkit-user-select: none; -moz-user-select: none;
    15. }
    16. #nav-switch {
    17.         display: none;
    18. }
    19. #nav .label {
    20.         display: block; cursor: pointer;
    21. }
    22. #nav .container {
    23.         transition:left 0.3s ease-in-out; left: -100px; width: 100%; position: absolute; -moz-transition: left 0.3s ease-in-out 0s; -webkit-transition: left 0.3s ease-in-out 0s; -o-transition: left 0.3s ease-in-out 0s;
    24. }
    25. #nav .container > div {
    26.         padding: 0px; width: 50%; float: left;
    27. }
    28. #nav .container .nav-on {
    29.         color: rgb(51, 51, 51); padding-left: 0px;
    30. }
    31. #nav .container .nav-off {
    32.         width: 40px; height: 40px; padding-right: 10px;
    33. }
    34. :checked#nav-switch + .label .container {
    35.         left: 10px;
    36. }
    37. :checked#nav-switch + .label #icon {
    38.         background: rgb(0, 101, 203); border-radius: 0px 0px 6px; transform: rotate(-180deg); -webkit-transform: rotate(-180deg); -moz-transform: rotate(-180deg); -o-transform: rotate(-180deg);
    39. }
    40. .title {
    41.         margin: 0px; padding: 0px; left: 170px; top: 2px; height: 40px; color: rgb(51, 51, 51); line-height: 40px; font-family: "Segoe UI Light", "Segoe UI", Frutiger, "Frutiger Linotype", "Dejavu Sans", "Helvetica Neue", Arial, sans-serif; font-size: 24px; font-weight: bold; white-space: nowrap; position: absolute; -ms-user-select: none; -webkit-user-select: none; -moz-user-select: none; -o-user-select: none; user-select: none;
    42. }
    43. .menu {
    44.         margin: 0px; padding: 0px; left: 0px; top: 6px; color: rgb(51, 51, 51); font-family: "Segoe UI Light", "Segoe UI", Frutiger, "Frutiger Linotype", "Dejavu Sans", "Helvetica Neue", Arial, sans-serif; font-size: 1em; font-weight: lighter; list-style-type: none; position: relative; cursor: pointer;
    45. }
    46. .menu li {
    47.         list-style: none; width: 100px; height: 40px; border-right-color: rgb(221, 221, 221); border-right-width: 1px; border-right-style: solid; position: relative; cursor: pointer;
    48. }
    49. .menu a {
    50.         color: rgb(51, 51, 51); line-height: 40px; padding-left: 40px; text-decoration: none; display: block; position: relative;
    51. }
    52. .menu li a:hover {
    53.         background: rgb(255, 144, 0);
    54. }
    55. .menu li a:focus {
    56.         background: rgb(255, 144, 0);
    57. }
    58. .menu li a:active {
    59.         background: rgb(255, 144, 0);
    60. }
    61. #nav li::before {
    62.         top: 50%; margin-left: 10px; position: absolute; content: "";
    63. }
    64. #nav li::after {
    65.         top: 50%; margin-left: 10px; position: absolute; content: "";
    66. }
    67. #nav li a::before {
    68.         top: 50%; margin-left: 10px; position: absolute; content: "";
    69. }
    70. #nav li a::after {
    71.         top: 50%; margin-left: 10px; position: absolute; content: "";
    72. }
    73. #nav .home a::before {
    74.         border-width: 8px 7px; border-style: solid; border-color: transparent transparent rgb(51, 51, 51); left: 2px; margin-top: -16px;
    75. }
    76. #nav .home a::after {
    77.         border-width: 3px 4px 0px; border-style: solid; border-color: rgb(51, 51, 51) rgb(51, 51, 51) transparent; left: 4px; width: 2px; height: 4px; margin-top: 0px;
    78. }
    79. #nav .arrow a::before {
    80.         border-width: 7px 0px 7px 8px; border-style: solid; border-color: transparent rgb(51, 51, 51); left: 8px; margin-top: -7px;
    81. }
    82. #nav .arrow a::after {
    83.         background: rgb(51, 51, 51); left: 0px; width: 8px; height: 6px; margin-top: -3px;
    84. }
    85. #nav .back.arrow a::before {
    86.         border-width: 7px 8px 7px 0px; left: 0px;
    87. }
    88. #nav .back.arrow a::after {
    89.         left: 8px;
    90. }
    91. #nav .list a::before {
    92.         background: none; border-width: 6px 0px; border-style: double; border-color: rgb(51, 51, 51); left: 5px; top: 14px; width: 12px; height: 2px;
    93. }
    94. #nav .list a::after {
    95.         background: none; border-width: 6px 0px; border-style: double; border-color: rgb(51, 51, 51); left: 1px; top: 14px; width: 2px; height: 2px;
    96. }
    复制代码
    3. JavaScript代码
    1. // ===== ge1doot global =====    js文件1
    2. var ge1doot = ge1doot || {
    3.         json: null,
    4.         screen: null,
    5.         pointer: null,
    6.         camera: null,
    7.         loadJS: function (url, callback, data) {
    8.                 if (typeof url == "string") url = [url];
    9.                 var load = function (src) {
    10.                         var script = document.createElement("script");
    11.                                 if (callback) {
    12.                                         if (script.readyState){
    13.                                                 script.onreadystatechange = function () {
    14.                                                         if (script.readyState == "loaded" || script.readyState == "complete"){
    15.                                                                 script.onreadystatechange = null;
    16.                                                                 if (--n === 0) callback(data || false);
    17.                                                         }
    18.                                                 }
    19.                                         } else {
    20.                                                 script.onload = function() {
    21.                                                         if (--n === 0) callback(data || false);
    22.                                                 }
    23.                                         }
    24.                                 }
    25.                                 script.src = src;
    26.                                 document.getElementsByTagName("head")[0].appendChild(script);
    27.                 }
    28.                 for (var i = 0, n = url.length; i < n; i++) load(url[i]);
    29.         }
    30. }

    31. // ===== html/canvas container =====
    32. ge1doot.Screen = function (setup) {
    33.         ge1doot.screen = this;
    34.         this.elem = document.getElementById(setup.container) || setup.container;
    35.         this.ctx = this.elem.tagName == "CANVAS" ? this.elem.getContext("2d") : false;
    36.         this.style = this.elem.style;
    37.         this.left = 0;
    38.         this.top = 0;
    39.         this.width = 0;
    40.         this.height = 0;
    41.         this.cursor = "default";
    42.         this.setup = setup;
    43.         this.resize = function () {
    44.                 var o = this.elem;
    45.                 this.width  = o.offsetWidth;
    46.                 this.height = o.offsetHeight;
    47.                 for (this.left = 0, this.top = 0; o != null; o = o.offsetParent) {
    48.                         this.left += o.offsetLeft;
    49.                         this.top  += o.offsetTop;
    50.                 }
    51.                 if (this.ctx) {
    52.                         this.elem.width  = this.width;
    53.                         this.elem.height = this.height;
    54.                 }
    55.                 this.setup.resize && this.setup.resize();
    56.         }
    57.         this.setCursor = function (type) {
    58.                 if (type !== this.cursor && 'ontouchstart' in window === false) {
    59.                         this.cursor = type;
    60.                         this.style.cursor = type;
    61.                 }
    62.         }
    63.         window.addEventListener('resize', function () {
    64.                 ge1doot.screen.resize();
    65.         }, false);
    66.         !this.setup.resize && this.resize();
    67. }

    68. // ==== unified touch events handler ====
    69. ge1doot.Pointer = function (setup) {
    70.         ge1doot.pointer = this;
    71.         var self        = this;
    72.         var body        = document.body;
    73.         var html        = document.documentElement;
    74.         this.setup      = setup;
    75.         this.screen     = ge1doot.screen;
    76.         this.elem       = this.screen.elem;
    77.         this.X          = 0;
    78.         this.Y          = 0;
    79.         this.Xi         = 0;
    80.         this.Yi         = 0;
    81.         this.bXi        = 0;
    82.         this.bYi        = 0;
    83.         this.Xr         = 0;
    84.         this.Yr         = 0;
    85.         this.startX     = 0;
    86.         this.startY     = 0;
    87.         this.scale      = 0;
    88.         this.wheelDelta = 0;
    89.         this.isDraging  = false;
    90.         this.hasMoved   = false;
    91.         this.isDown     = false;
    92.         this.evt        = false;
    93.         var sX          = 0;
    94.         var sY          = 0;
    95.         // prevent default behavior
    96.         if (setup.tap) this.elem.onclick = function () { return false; }
    97.         if (!setup.documentMove) {
    98.                 document.ontouchmove = function(e) { e.preventDefault(); }
    99.         }
    100.         document.addEventListener("MSHoldVisual", function(e) { e.preventDefault(); }, false);
    101.         if (typeof this.elem.style.msTouchAction != 'undefined') this.elem.style.msTouchAction = "none";

    102.         this.pointerDown = function (e) {
    103.                 if (!this.isDown) {
    104.                         if (this.elem.setCapture) this.elem.setCapture();
    105.                         this.isDraging = false;
    106.                         this.hasMoved = false;
    107.                         this.isDown = true;
    108.                         this.evt = e;
    109.                         this.Xr = (e.clientX !== undefined ? e.clientX : e.touches[0].clientX);
    110.                         this.Yr = (e.clientY !== undefined ? e.clientY : e.touches[0].clientY);
    111.                         this.X  = sX = this.Xr - this.screen.left;
    112.                         this.Y  = sY = this.Yr - this.screen.top + ((html && html.scrollTop) || body.scrollTop);
    113.                         this.setup.down && this.setup.down(e);
    114.                 }
    115.         }
    116.         this.pointerMove = function(e) {
    117.                 this.Xr = (e.clientX !== undefined ? e.clientX : e.touches[0].clientX);
    118.                 this.Yr = (e.clientY !== undefined ? e.clientY : e.touches[0].clientY);
    119.                 this.X  = this.Xr - this.screen.left;
    120.                 this.Y  = this.Yr - this.screen.top + ((html && html.scrollTop) || body.scrollTop);
    121.                 if (this.isDown) {
    122.                         this.Xi = this.bXi + (this.X - sX);
    123.                         this.Yi = this.bYi - (this.Y - sY);
    124.                 }
    125.                 if (Math.abs(this.X - sX) > 11 || Math.abs(this.Y - sY) > 11) {
    126.                         this.hasMoved = true;
    127.                         if (this.isDown) {
    128.                                 if (!this.isDraging) {
    129.                                         this.startX = sX;
    130.                                         this.startY = sY;
    131.                                         this.setup.startDrag && this.setup.startDrag(e);
    132.                                         this.isDraging = true;
    133.                                 } else {
    134.                                         this.setup.drag && this.setup.drag(e);
    135.                                 }
    136.                         } else {
    137.                                 sX = this.X;
    138.                                 sY = this.Y;
    139.                         }
    140.                 }
    141.                 this.setup.move && this.setup.move(e);
    142.         }
    143.         this.pointerUp = function(e) {
    144.                 this.bXi = this.Xi;
    145.                 this.bYi = this.Yi;
    146.                 if (!this.hasMoved) {
    147.                         this.X = sX;
    148.                         this.Y = sY;
    149.                         this.setup.tap && this.setup.tap(this.evt);
    150.                 } else {
    151.                         this.setup.up && this.setup.up(this.evt);
    152.                 }
    153.                 this.isDraging = false;
    154.                 this.isDown = false;
    155.                 this.hasMoved = false;
    156.                 this.setup.up && this.setup.up(this.evt);
    157.                 if (this.elem.releaseCapture) this.elem.releaseCapture();
    158.                 this.evt = false;
    159.         }
    160.         this.pointerCancel = function(e) {
    161.                 if (this.elem.releaseCapture) this.elem.releaseCapture();
    162.                 this.isDraging = false;
    163.                 this.hasMoved = false;
    164.                 this.isDown = false;
    165.                 this.bXi = this.Xi;
    166.                 this.bYi = this.Yi;
    167.                 sX = 0;
    168.                 sY = 0;
    169.         }
    170.         if ('ontouchstart' in window) {
    171.                 this.elem.ontouchstart      = function (e) { self.pointerDown(e); return false;  }
    172.                 this.elem.ontouchmove       = function (e) { self.pointerMove(e); return false;  }
    173.                 this.elem.ontouchend        = function (e) { self.pointerUp(e); return false;    }
    174.                 this.elem.ontouchcancel     = function (e) { self.pointerCancel(e); return false;}
    175.         }
    176.         document.addEventListener("mousedown", function (e) {
    177.                 if (
    178.                         e.target === self.elem ||
    179.                         (e.target.parentNode && e.target.parentNode === self.elem) ||
    180.                         (e.target.parentNode.parentNode && e.target.parentNode.parentNode === self.elem)
    181.                 ) {
    182.                         if (typeof e.stopPropagation != "undefined") {
    183.                                 e.stopPropagation();
    184.                         } else {
    185.                                 e.cancelBubble = true;
    186.                         }
    187.                         e.preventDefault();
    188.                         self.pointerDown(e);
    189.                 }
    190.         }, false);
    191.         document.addEventListener("mousemove", function (e) { self.pointerMove(e); }, false);
    192.         document.addEventListener("mouseup",   function (e) {
    193.                 self.pointerUp(e);
    194.         }, false);
    195.         document.addEventListener('gesturechange', function(e) {
    196.                 e.preventDefault();
    197.                 if (e.scale > 1) self.scale = 0.1; else if (e.scale < 1) self.scale = -0.1; else self.scale = 0;
    198.                 self.setup.scale && self.setup.scale(e);
    199.                 return false;
    200.         }, false);
    201.         if (window.navigator.msPointerEnabled) {
    202.                 var nContact = 0;
    203.                 var myGesture = new MSGesture();
    204.                 myGesture.target = this.elem;
    205.                 this.elem.addEventListener("MSPointerDown", function(e) {
    206.                         if (e.pointerType == e.MSPOINTER_TYPE_TOUCH) {
    207.                                 myGesture.addPointer(e.pointerId);
    208.                                 nContact++;
    209.                         }
    210.                 }, false);
    211.                 this.elem.addEventListener("MSPointerOut", function(e) {
    212.                         if (e.pointerType == e.MSPOINTER_TYPE_TOUCH) {
    213.                                 nContact--;
    214.                         }
    215.                 }, false);
    216.                 this.elem.addEventListener("MSGestureHold", function(e) {
    217.                         e.preventDefault();
    218.                 }, false);
    219.                 this.elem.addEventListener("MSGestureChange", function(e) {
    220.                         if (nContact > 1) {
    221.                                 if (e.preventDefault) e.preventDefault();
    222.                                 self.scale = e.velocityExpansion;
    223.                                 self.setup.scale && self.setup.scale(e);
    224.                         }
    225.                         return false;
    226.                 }, false);
    227.         }
    228.         if (window.addEventListener) this.elem.addEventListener('DOMMouseScroll', function(e) {
    229.                 if (e.preventDefault) e.preventDefault();
    230.                 self.wheelDelta = e.detail * 10;
    231.                 self.setup.wheel && self.setup.wheel(e);
    232.                 return false;
    233.         }, false);
    234.         this.elem.onmousewheel = function () {
    235.                 self.wheelDelta = -event.wheelDelta * .25;
    236.                 self.setup.wheel && self.setup.wheel(event);
    237.                 return false;
    238.         }
    239. }

    240. // ===== request animation frame =====
    241. window.requestAnimFrame = (function(){
    242.                 return  window.requestAnimationFrame ||
    243.                 window.webkitRequestAnimationFrame   ||
    244.                 window.mozRequestAnimationFrame      ||
    245.                 window.oRequestAnimationFrame        ||
    246.                 window.msRequestAnimationFrame       ||
    247.                 function( run ){
    248.                         window.setTimeout(run, 16);
    249.                 };
    250. })();
    复制代码
    1. // ===== ge1doot global =====    js文件1
    2. var ge1doot = ge1doot || {
    3.         json: null,
    4.         screen: null,
    5.         pointer: null,
    6.         camera: null,
    7.         loadJS: function (url, callback, data) {
    8.                 if (typeof url == "string") url = [url];
    9.                 var load = function (src) {
    10.                         var script = document.createElement("script");
    11.                                 if (callback) {
    12.                                         if (script.readyState){
    13.                                                 script.onreadystatechange = function () {
    14.                                                         if (script.readyState == "loaded" || script.readyState == "complete"){
    15.                                                                 script.onreadystatechange = null;
    16.                                                                 if (--n === 0) callback(data || false);
    17.                                                         }
    18.                                                 }
    19.                                         } else {
    20.                                                 script.onload = function() {
    21.                                                         if (--n === 0) callback(data || false);
    22.                                                 }
    23.                                         }
    24.                                 }
    25.                                 script.src = src;
    26.                                 document.getElementsByTagName("head")[0].appendChild(script);
    27.                 }
    28.                 for (var i = 0, n = url.length; i < n; i++) load(url[i]);
    29.         }
    30. }

    31. // ===== html/canvas container =====
    32. ge1doot.Screen = function (setup) {
    33.         ge1doot.screen = this;
    34.         this.elem = document.getElementById(setup.container) || setup.container;
    35.         this.ctx = this.elem.tagName == "CANVAS" ? this.elem.getContext("2d") : false;
    36.         this.style = this.elem.style;
    37.         this.left = 0;
    38.         this.top = 0;
    39.         this.width = 0;
    40.         this.height = 0;
    41.         this.cursor = "default";
    42.         this.setup = setup;
    43.         this.resize = function () {
    44.                 var o = this.elem;
    45.                 this.width  = o.offsetWidth;
    46.                 this.height = o.offsetHeight;
    47.                 for (this.left = 0, this.top = 0; o != null; o = o.offsetParent) {
    48.                         this.left += o.offsetLeft;
    49.                         this.top  += o.offsetTop;
    50.                 }
    51.                 if (this.ctx) {
    52.                         this.elem.width  = this.width;
    53.                         this.elem.height = this.height;
    54.                 }
    55.                 this.setup.resize && this.setup.resize();
    56.         }
    57.         this.setCursor = function (type) {
    58.                 if (type !== this.cursor && 'ontouchstart' in window === false) {
    59.                         this.cursor = type;
    60.                         this.style.cursor = type;
    61.                 }
    62.         }
    63.         window.addEventListener('resize', function () {
    64.                 ge1doot.screen.resize();
    65.         }, false);
    66.         !this.setup.resize && this.resize();
    67. }

    68. // ==== unified touch events handler ====
    69. ge1doot.Pointer = function (setup) {
    70.         ge1doot.pointer = this;
    71.         var self        = this;
    72.         var body        = document.body;
    73.         var html        = document.documentElement;
    74.         this.setup      = setup;
    75.         this.screen     = ge1doot.screen;
    76.         this.elem       = this.screen.elem;
    77.         this.X          = 0;
    78.         this.Y          = 0;
    79.         this.Xi         = 0;
    80.         this.Yi         = 0;
    81.         this.bXi        = 0;
    82.         this.bYi        = 0;
    83.         this.Xr         = 0;
    84.         this.Yr         = 0;
    85.         this.startX     = 0;
    86.         this.startY     = 0;
    87.         this.scale      = 0;
    88.         this.wheelDelta = 0;
    89.         this.isDraging  = false;
    90.         this.hasMoved   = false;
    91.         this.isDown     = false;
    92.         this.evt        = false;
    93.         var sX          = 0;
    94.         var sY          = 0;
    95.         // prevent default behavior
    96.         if (setup.tap) this.elem.onclick = function () { return false; }
    97.         if (!setup.documentMove) {
    98.                 document.ontouchmove = function(e) { e.preventDefault(); }
    99.         }
    100.         document.addEventListener("MSHoldVisual", function(e) { e.preventDefault(); }, false);
    101.         if (typeof this.elem.style.msTouchAction != 'undefined') this.elem.style.msTouchAction = "none";

    102.         this.pointerDown = function (e) {
    103.                 if (!this.isDown) {
    104.                         if (this.elem.setCapture) this.elem.setCapture();
    105.                         this.isDraging = false;
    106.                         this.hasMoved = false;
    107.                         this.isDown = true;
    108.                         this.evt = e;
    109.                         this.Xr = (e.clientX !== undefined ? e.clientX : e.touches[0].clientX);
    110.                         this.Yr = (e.clientY !== undefined ? e.clientY : e.touches[0].clientY);
    111.                         this.X  = sX = this.Xr - this.screen.left;
    112.                         this.Y  = sY = this.Yr - this.screen.top + ((html && html.scrollTop) || body.scrollTop);
    113.                         this.setup.down && this.setup.down(e);
    114.                 }
    115.         }
    116.         this.pointerMove = function(e) {
    117.                 this.Xr = (e.clientX !== undefined ? e.clientX : e.touches[0].clientX);
    118.                 this.Yr = (e.clientY !== undefined ? e.clientY : e.touches[0].clientY);
    119.                 this.X  = this.Xr - this.screen.left;
    120.                 this.Y  = this.Yr - this.screen.top + ((html && html.scrollTop) || body.scrollTop);
    121.                 if (this.isDown) {
    122.                         this.Xi = this.bXi + (this.X - sX);
    123.                         this.Yi = this.bYi - (this.Y - sY);
    124.                 }
    125.                 if (Math.abs(this.X - sX) > 11 || Math.abs(this.Y - sY) > 11) {
    126.                         this.hasMoved = true;
    127.                         if (this.isDown) {
    128.                                 if (!this.isDraging) {
    129.                                         this.startX = sX;
    130.                                         this.startY = sY;
    131.                                         this.setup.startDrag && this.setup.startDrag(e);
    132.                                         this.isDraging = true;
    133.                                 } else {
    134.                                         this.setup.drag && this.setup.drag(e);
    135.                                 }
    136.                         } else {
    137.                                 sX = this.X;
    138.                                 sY = this.Y;
    139.                         }
    140.                 }
    141.                 this.setup.move && this.setup.move(e);
    142.         }
    143.         this.pointerUp = function(e) {
    144.                 this.bXi = this.Xi;
    145.                 this.bYi = this.Yi;
    146.                 if (!this.hasMoved) {
    147.                         this.X = sX;
    148.                         this.Y = sY;
    149.                         this.setup.tap && this.setup.tap(this.evt);
    150.                 } else {
    151.                         this.setup.up && this.setup.up(this.evt);
    152.                 }
    153.                 this.isDraging = false;
    154.                 this.isDown = false;
    155.                 this.hasMoved = false;
    156.                 this.setup.up && this.setup.up(this.evt);
    157.                 if (this.elem.releaseCapture) this.elem.releaseCapture();
    158.                 this.evt = false;
    159.         }
    160.         this.pointerCancel = function(e) {
    161.                 if (this.elem.releaseCapture) this.elem.releaseCapture();
    162.                 this.isDraging = false;
    163.                 this.hasMoved = false;
    164.                 this.isDown = false;
    165.                 this.bXi = this.Xi;
    166.                 this.bYi = this.Yi;
    167.                 sX = 0;
    168.                 sY = 0;
    169.         }
    170.         if ('ontouchstart' in window) {
    171.                 this.elem.ontouchstart      = function (e) { self.pointerDown(e); return false;  }
    172.                 this.elem.ontouchmove       = function (e) { self.pointerMove(e); return false;  }
    173.                 this.elem.ontouchend        = function (e) { self.pointerUp(e); return false;    }
    174.                 this.elem.ontouchcancel     = function (e) { self.pointerCancel(e); return false;}
    175.         }
    176.         document.addEventListener("mousedown", function (e) {
    177.                 if (
    178.                         e.target === self.elem ||
    179.                         (e.target.parentNode && e.target.parentNode === self.elem) ||
    180.                         (e.target.parentNode.parentNode && e.target.parentNode.parentNode === self.elem)
    181.                 ) {
    182.                         if (typeof e.stopPropagation != "undefined") {
    183.                                 e.stopPropagation();
    184.                         } else {
    185.                                 e.cancelBubble = true;
    186.                         }
    187.                         e.preventDefault();
    188.                         self.pointerDown(e);
    189.                 }
    190.         }, false);
    191.         document.addEventListener("mousemove", function (e) { self.pointerMove(e); }, false);
    192.         document.addEventListener("mouseup",   function (e) {
    193.                 self.pointerUp(e);
    194.         }, false);
    195.         document.addEventListener('gesturechange', function(e) {
    196.                 e.preventDefault();
    197.                 if (e.scale > 1) self.scale = 0.1; else if (e.scale < 1) self.scale = -0.1; else self.scale = 0;
    198.                 self.setup.scale && self.setup.scale(e);
    199.                 return false;
    200.         }, false);
    201.         if (window.navigator.msPointerEnabled) {
    202.                 var nContact = 0;
    203.                 var myGesture = new MSGesture();
    204.                 myGesture.target = this.elem;
    205.                 this.elem.addEventListener("MSPointerDown", function(e) {
    206.                         if (e.pointerType == e.MSPOINTER_TYPE_TOUCH) {
    207.                                 myGesture.addPointer(e.pointerId);
    208.                                 nContact++;
    209.                         }
    210.                 }, false);
    211.                 this.elem.addEventListener("MSPointerOut", function(e) {
    212.                         if (e.pointerType == e.MSPOINTER_TYPE_TOUCH) {
    213.                                 nContact--;
    214.                         }
    215.                 }, false);
    216.                 this.elem.addEventListener("MSGestureHold", function(e) {
    217.                         e.preventDefault();
    218.                 }, false);
    219.                 this.elem.addEventListener("MSGestureChange", function(e) {
    220.                         if (nContact > 1) {
    221.                                 if (e.preventDefault) e.preventDefault();
    222.                                 self.scale = e.velocityExpansion;
    223.                                 self.setup.scale && self.setup.scale(e);
    224.                         }
    225.                         return false;
    226.                 }, false);
    227.         }
    228.         if (window.addEventListener) this.elem.addEventListener('DOMMouseScroll', function(e) {
    229.                 if (e.preventDefault) e.preventDefault();
    230.                 self.wheelDelta = e.detail * 10;
    231.                 self.setup.wheel && self.setup.wheel(e);
    232.                 return false;
    233.         }, false);
    234.         this.elem.onmousewheel = function () {
    235.                 self.wheelDelta = -event.wheelDelta * .25;
    236.                 self.setup.wheel && self.setup.wheel(event);
    237.                 return false;
    238.         }
    239. }

    240. // ===== request animation frame =====
    241. window.requestAnimFrame = (function(){
    242.                 return  window.requestAnimationFrame ||
    243.                 window.webkitRequestAnimationFrame   ||
    244.                 window.mozRequestAnimationFrame      ||
    245.                 window.oRequestAnimationFrame        ||
    246.                 window.msRequestAnimationFrame       ||
    247.                 function( run ){
    248.                         window.setTimeout(run, 16);
    249.                 };
    250. })();
    复制代码
    到此这篇关于HTML+CSS+JavaScript实现图片3D展览的示例代码的文章就介绍到这了,更多相关HTML图片3D展览内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!

    来源:https://www.jb51.net/html5/747978.html
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表