HTML+CSS+JavaScript实现图片3D展览的示例代码
程序员文章站
2022-03-04 09:50:56
HTML+CSS+JavaScript实现图片3D展览的示例代码这篇文章主要介绍了HTML+CSS+JavaScript实现图片3D展览的示例代码。文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起... 20-10-12...
一、图片3d展览效果
上传图片的大小有限制,录制的gif图展示效果不是很好
录屏的效果见链接:https://www.bilibili.com/video/bv1bi4y1e7wk/
二、代码实现
1. html代码
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> <meta name="handheldfriendly" content="true"> <meta name="description" content="图片3d展览屋"> <meta name="keywords" content="canvas,3d_picture,perspective,texturing,gallery"> <link href="res/slider-wb.css" rel="stylesheet"> <title>图片3d展览屋</title> <style> html { overflow:visible; -ms-touch-action: none; -ms-content-zooming: none; } body { position: absolute; margin: 0px; padding: 0px; background: #fff; width: 100%; height: 100%; } #canvas { position: absolute; width: 100%; height: 100%; background: #fff; } </style> <script type="text/javascript" src="res/ge1doot.js"></script> <script type="text/javascript"> "use strict"; (function () { /* ==== definitions ==== */ var diapo = [], layers = [], ctx, pointer, scr, camera, light, fps = 0, quality = [1, 2], // ---- poly constructor ---- poly = function (parent, face) { this.parent = parent; this.ctx = ctx; this.color = face.fill || false; this.points = []; if (!face.img) { // ---- create points ---- for (var i = 0; i < 4; i++) { this.points[i] = new ge1doot.transform3d.point( parent.pc.x + (face.x[i] * parent.normalz) + (face.z[i] * parent.normalx), parent.pc.y + face.y[i], parent.pc.z + (face.x[i] * parent.normalx) + (-face.z[i] * parent.normalz) ); } this.points[3].next = false; } }, // ---- diapo constructor ---- diapo = function (path, img, structure) { // ---- create image ---- this.img = new ge1doot.transform3d.image( this, path + img.img, 1, { isloaded: function (img) { img.parent.isloaded = true; img.parent.loaded(img); } } ); this.visible = false; this.normalx = img.nx; this.normalz = img.nz; // ---- point center ---- this.pc = new ge1doot.transform3d.point(img.x, img.y, img.z); // ---- target positions ---- this.tx = img.x + (img.nx * math.sqrt(camera.focallength) * 20); this.tz = img.z - (img.nz * math.sqrt(camera.focallength) * 20); // ---- create polygons ---- this.poly = []; for (var i = -1, p; p = structure[++i]; ) { layers[i] = (p.img === true ? 1 : 2); this.poly.push( new poly(this, p) ); } }, // ---- init section ---- init = function (json) { // draw poly primitive poly.prototype.drawpoly = ge1doot.transform3d.drawpoly; // ---- init screen ---- scr = new ge1doot.screen({ container: "canvas" }); ctx = scr.ctx; scr.resize(); // ---- init pointer ---- pointer = new ge1doot.pointer({ tap: function () { if (camera.over) { if (camera.over === camera.target.elem) { // ---- return to the center ---- camera.target.x = 0; camera.target.z = 0; camera.target.elem = false; } else { // ---- goto diapo ---- camera.target.elem = camera.over; camera.target.x = camera.over.tx; camera.target.z = camera.over.tz; // ---- adapt tesselation level to distance ---- for (var i = 0, d; d = diapo[i++]; ) { var dx = camera.target.x - d.pc.x; var dz = camera.target.z - d.pc.z; var dist = math.sqrt(dx * dx + dz * dz); var lev = (dist > 1500) ? quality[0] : quality[1]; d.img.setlevel(lev); } } } } }); // ---- init camera ---- camera = new ge1doot.transform3d.camera({ focallength: math.sqrt(scr.width) * 10, easetranslation: 0.025, easerotation: 0.06, disablerz: true }, { move: function () { this.over = false; // ---- rotation ---- if (pointer.isdraging) { this.target.elem = false; this.target.ry = -pointer.xi * 0.01; this.target.rx = (pointer.y - scr.height * 0.5) / (scr.height * 0.5); } else { if (this.target.elem) { this.target.ry = math.atan2( this.target.elem.pc.x - this.x, this.target.elem.pc.z - this.z ); } } this.target.rx *= 0.9; } }); camera.z = -10000; camera.py = 0; // ---- create images ---- for (var i = 0, img; img = json.imgdata[i++]; ) { diapo.push( new diapo( json.options.imagespath, img, json.structure ) ); } // ---- start engine ---- >>> setinterval(function () { quality = (fps > 50) ? [2, 3] : [1, 2]; fps = 0; }, 1000); run(); }, // ---- main loop ---- run = function () { // ---- clear screen ---- ctx.clearrect(0, 0, scr.width, scr.height); // ---- camera ---- camera.move(); // ---- draw layers ---- for (var k = -1, l; l = layers[++k]; ) { light = false; for (var i = 0, d; d = diapo[i++]; ) { (l === 1 && d.draw()) || (d.visible && d.poly[k].draw()); } } // ---- cursor ---- if (camera.over && !pointer.isdraging) { scr.setcursor("pointer"); } else { scr.setcursor("move"); } // ---- loop ---- fps++; requestanimframe(run); }; /* ==== prototypes ==== */ poly.prototype.draw = function () { // ---- color light ---- var c = this.color; if (c.light || !light) { var s = c.light ? this.parent.light : 1; // ---- rgba color ---- light = "rgba(" + math.round(c.r * s) + "," + math.round(c.g * s) + "," + math.round(c.b * s) + "," + (c.a || 1) + ")"; ctx.fillstyle = light; } // ---- paint poly ---- if (!c.light || this.parent.light < 1) { // ---- projection ---- for ( var i = 0; this.points[i++].projection(); ); this.drawpoly(); ctx.fill(); } } /* ==== image onload ==== */ diapo.prototype.loaded = function (img) { // ---- create points ---- var d = [-1, 1, 1, -1, 1, 1, -1, -1]; var w = img.texture.width * 0.5; var h = img.texture.height * 0.5; for (var i = 0; i < 4; i++) { img.points[i] = new ge1doot.transform3d.point( this.pc.x + (w * this.normalz * d[i]), this.pc.y + (h * d[i + 4]), this.pc.z + (w * this.normalx * d[i]) ); } } /* ==== images draw ==== */ diapo.prototype.draw = function () { // ---- visibility ---- this.pc.projection(); if (this.pc.z > -(camera.focallength >> 1) && this.img.transform3d(true)) { // ---- light ---- this.light = 0.5 + math.abs(this.normalz * camera.cosy - this.normalx * camera.siny) * 0.6; // ---- draw image ---- this.visible = true; this.img.draw(); // ---- test pointer inside ---- if (pointer.hasmoved || pointer.isdown) { if ( this.img.ispointerinside( pointer.x, pointer.y ) ) camera.over = this; } } else this.visible = false; return true; } return { // --- load data ---- load: function (data) { window.addeventlistener('load', function () { ge1doot.loadjs( "res/imagetransform3d.js", init, data ); }, false); } } })().load({ imgdata: [ // north {img: 'imgs/001.jpg', x: -1000, y: 0, z: 1500, nx: 0, nz: 1 }, { img: 'imgs/002.jpg', x: 0, y: 0, z: 1500, nx: 0, nz: 1 }, { img: 'imgs/003.jpg', x: 1000, y: 0, z: 1500, nx: 0, nz: 1 }, // east {img: 'imgs/004.jpg', x: 1500, y: 0, z: 1000, nx: -1, nz: 0 }, { img: 'imgs/005.jpg', x: 1500, y: 0, z: 0, nx: -1, nz: 0 }, { img: 'imgs/006.jpg', x: 1500, y: 0, z: -1000, nx: -1, nz: 0 }, // south {img: 'imgs/007.jpg', x: 1000, y: 0, z: -1500, nx: 0, nz: -1 }, { img: 'imgs/008.jpg', x: 0, y: 0, z: -1500, nx: 0, nz: -1 }, { img: 'imgs/009.jpg', x: -1000, y: 0, z: -1500, nx: 0, nz: -1 }, // west {img: 'imgs/010.jpg', x: -1500, y: 0, z: -1000, nx: 1, nz: 0 }, { img: 'imgs/011.jpg', x: -1500, y: 0, z: 0, nx: 1, nz: 0 }, { img: 'imgs/012.jpg', x: -1500, y: 0, z: 1000, nx: 1, nz: 0 } ], structure: [ { // wall fill: { r: 255, g: 255, b: 255, light: 1 }, x: [-1001, -490, -490, -1001], z: [-500, -500, -500, -500], y: [500, 500, -500, -500] }, { // wall fill: { r: 255, g: 255, b: 255, light: 1 }, x: [-501, 2, 2, -500], z: [-500, -500, -500, -500], y: [500, 500, -500, -500] }, { // wall fill: { r: 255, g: 255, b: 255, light: 1 }, x: [0, 502, 502, 0], z: [-500, -500, -500, -500], y: [500, 500, -500, -500] }, { // wall fill: { r: 255, g: 255, b: 255, light: 1 }, x: [490, 1002, 1002, 490], z: [-500, -500, -500, -500], y: [500, 500, -500, -500] }, { // shadow fill: { r: 0, g: 0, b: 0, a: 0.2 }, x: [-420, 420, 420, -420], z: [-500, -500, -500, -500], y: [150, 150, -320, -320] }, { // shadow fill: { r: 0, g: 0, b: 0, a: 0.2 }, x: [-20, 20, 20, -20], z: [-500, -500, -500, -500], y: [250, 250, 150, 150] }, { // shadow fill: { r: 0, g: 0, b: 0, a: 0.2 }, x: [-20, 20, 20, -20], z: [-500, -500, -500, -500], y: [-320, -320, -500, -500] }, { // shadow fill: { r: 0, g: 0, b: 0, a: 0.2 }, x: [-20, 20, 10, -10], z: [-500, -500, -100, -100], y: [-500, -500, -500, -500] }, { // base fill: { r: 32, g: 32, b: 32 }, x: [-50, 50, 50, -50], z: [-150, -150, -50, -50], y: [-500, -500, -500, -500] }, { // support fill: { r: 16, g: 16, b: 16 }, x: [-10, 10, 10, -10], z: [-100, -100, -100, -100], y: [300, 300, -500, -500] }, { // frame fill: { r: 255, g: 255, b: 255 }, x: [-320, -320, -320, -320], z: [0, -20, -20, 0], y: [-190, -190, 190, 190] }, { // frame fill: { r: 255, g: 255, b: 255 }, x: [320, 320, 320, 320], z: [0, -20, -20, 0], y: [-190, -190, 190, 190] }, { img: true }, { // ceilinglight fill: { r: 255, g: 128, b: 0 }, x: [-50, 50, 50, -50], z: [450, 450, 550, 550], y: [500, 500, 500, 500] }, { // groundlight fill: { r: 255, g: 128, b: 0 }, x: [-50, 50, 50, -50], z: [450, 450, 550, 550], y: [-500, -500, -500, -500] } ], options: { imagespath: "" } }); </script> </head> <body> <a href="https://blog.csdn.net/fyfugoyfa/article/details/108845194" target="_blank">csdn文章</a><br /> <canvas id="canvas">建议用谷歌浏览器打开</canvas> </body> </html>
2. css代码
#icon { 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; } #icon div { background: none; position: absolute; } #icon div:nth-child(1) { 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; } #icon div:nth-child(2) { background: rgb(255, 255, 255); left: 12px; top: 50%; width: 8px; height: 6px; margin-top: -3px; position: absolute; } #nav { top: 10px; width: 100px; height: 0px; position: absolute; -ms-user-select: none; -webkit-user-select: none; -moz-user-select: none; } #nav-switch { display: none; } #nav .label { display: block; cursor: pointer; } #nav .container { 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; } #nav .container > div { padding: 0px; width: 50%; float: left; } #nav .container .nav-on { color: rgb(51, 51, 51); padding-left: 0px; } #nav .container .nav-off { width: 40px; height: 40px; padding-right: 10px; } :checked#nav-switch + .label .container { left: 10px; } :checked#nav-switch + .label #icon { 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); } .title { 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; } .menu { 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; } .menu li { 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; } .menu a { color: rgb(51, 51, 51); line-height: 40px; padding-left: 40px; text-decoration: none; display: block; position: relative; } .menu li a:hover { background: rgb(255, 144, 0); } .menu li a:focus { background: rgb(255, 144, 0); } .menu li a:active { background: rgb(255, 144, 0); } #nav li::before { top: 50%; margin-left: 10px; position: absolute; content: ""; } #nav li::after { top: 50%; margin-left: 10px; position: absolute; content: ""; } #nav li a::before { top: 50%; margin-left: 10px; position: absolute; content: ""; } #nav li a::after { top: 50%; margin-left: 10px; position: absolute; content: ""; } #nav .home a::before { border-width: 8px 7px; border-style: solid; border-color: transparent transparent rgb(51, 51, 51); left: 2px; margin-top: -16px; } #nav .home a::after { 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; } #nav .arrow a::before { border-width: 7px 0px 7px 8px; border-style: solid; border-color: transparent rgb(51, 51, 51); left: 8px; margin-top: -7px; } #nav .arrow a::after { background: rgb(51, 51, 51); left: 0px; width: 8px; height: 6px; margin-top: -3px; } #nav .back.arrow a::before { border-width: 7px 8px 7px 0px; left: 0px; } #nav .back.arrow a::after { left: 8px; } #nav .list a::before { background: none; border-width: 6px 0px; border-style: double; border-color: rgb(51, 51, 51); left: 5px; top: 14px; width: 12px; height: 2px; } #nav .list a::after { background: none; border-width: 6px 0px; border-style: double; border-color: rgb(51, 51, 51); left: 1px; top: 14px; width: 2px; height: 2px; }
3. javascript代码
// ===== ge1doot global ===== js文件1 var ge1doot = ge1doot || { json: null, screen: null, pointer: null, camera: null, loadjs: function (url, callback, data) { if (typeof url == "string") url = [url]; var load = function (src) { var script = document.createelement("script"); if (callback) { if (script.readystate){ script.onreadystatechange = function () { if (script.readystate == "loaded" || script.readystate == "complete"){ script.onreadystatechange = null; if (--n === 0) callback(data || false); } } } else { script.onload = function() { if (--n === 0) callback(data || false); } } } script.src = src; document.getelementsbytagname("head")[0].appendchild(script); } for (var i = 0, n = url.length; i < n; i++) load(url[i]); } } // ===== html/canvas container ===== ge1doot.screen = function (setup) { ge1doot.screen = this; this.elem = document.getelementbyid(setup.container) || setup.container; this.ctx = this.elem.tagname == "canvas" ? this.elem.getcontext("2d") : false; this.style = this.elem.style; this.left = 0; this.top = 0; this.width = 0; this.height = 0; this.cursor = "default"; this.setup = setup; this.resize = function () { var o = this.elem; this.width = o.offsetwidth; this.height = o.offsetheight; for (this.left = 0, this.top = 0; o != null; o = o.offsetparent) { this.left += o.offsetleft; this.top += o.offsettop; } if (this.ctx) { this.elem.width = this.width; this.elem.height = this.height; } this.setup.resize && this.setup.resize(); } this.setcursor = function (type) { if (type !== this.cursor && 'ontouchstart' in window === false) { this.cursor = type; this.style.cursor = type; } } window.addeventlistener('resize', function () { ge1doot.screen.resize(); }, false); !this.setup.resize && this.resize(); } // ==== unified touch events handler ==== ge1doot.pointer = function (setup) { ge1doot.pointer = this; var self = this; var body = document.body; var html = document.documentelement; this.setup = setup; this.screen = ge1doot.screen; this.elem = this.screen.elem; this.x = 0; this.y = 0; this.xi = 0; this.yi = 0; this.bxi = 0; this.byi = 0; this.xr = 0; this.yr = 0; this.startx = 0; this.starty = 0; this.scale = 0; this.wheeldelta = 0; this.isdraging = false; this.hasmoved = false; this.isdown = false; this.evt = false; var sx = 0; var sy = 0; // prevent default behavior if (setup.tap) this.elem.onclick = function () { return false; } if (!setup.documentmove) { document.ontouchmove = function(e) { e.preventdefault(); } } document.addeventlistener("msholdvisual", function(e) { e.preventdefault(); }, false); if (typeof this.elem.style.mstouchaction != 'undefined') this.elem.style.mstouchaction = "none"; this.pointerdown = function (e) { if (!this.isdown) { if (this.elem.setcapture) this.elem.setcapture(); this.isdraging = false; this.hasmoved = false; this.isdown = true; this.evt = e; this.xr = (e.clientx !== undefined ? e.clientx : e.touches[0].clientx); this.yr = (e.clienty !== undefined ? e.clienty : e.touches[0].clienty); this.x = sx = this.xr - this.screen.left; this.y = sy = this.yr - this.screen.top + ((html && html.scrolltop) || body.scrolltop); this.setup.down && this.setup.down(e); } } this.pointermove = function(e) { this.xr = (e.clientx !== undefined ? e.clientx : e.touches[0].clientx); this.yr = (e.clienty !== undefined ? e.clienty : e.touches[0].clienty); this.x = this.xr - this.screen.left; this.y = this.yr - this.screen.top + ((html && html.scrolltop) || body.scrolltop); if (this.isdown) { this.xi = this.bxi + (this.x - sx); this.yi = this.byi - (this.y - sy); } if (math.abs(this.x - sx) > 11 || math.abs(this.y - sy) > 11) { this.hasmoved = true; if (this.isdown) { if (!this.isdraging) { this.startx = sx; this.starty = sy; this.setup.startdrag && this.setup.startdrag(e); this.isdraging = true; } else { this.setup.drag && this.setup.drag(e); } } else { sx = this.x; sy = this.y; } } this.setup.move && this.setup.move(e); } this.pointerup = function(e) { this.bxi = this.xi; this.byi = this.yi; if (!this.hasmoved) { this.x = sx; this.y = sy; this.setup.tap && this.setup.tap(this.evt); } else { this.setup.up && this.setup.up(this.evt); } this.isdraging = false; this.isdown = false; this.hasmoved = false; this.setup.up && this.setup.up(this.evt); if (this.elem.releasecapture) this.elem.releasecapture(); this.evt = false; } this.pointercancel = function(e) { if (this.elem.releasecapture) this.elem.releasecapture(); this.isdraging = false; this.hasmoved = false; this.isdown = false; this.bxi = this.xi; this.byi = this.yi; sx = 0; sy = 0; } if ('ontouchstart' in window) { this.elem.ontouchstart = function (e) { self.pointerdown(e); return false; } this.elem.ontouchmove = function (e) { self.pointermove(e); return false; } this.elem.ontouchend = function (e) { self.pointerup(e); return false; } this.elem.ontouchcancel = function (e) { self.pointercancel(e); return false;} } document.addeventlistener("mousedown", function (e) { if ( e.target === self.elem || (e.target.parentnode && e.target.parentnode === self.elem) || (e.target.parentnode.parentnode && e.target.parentnode.parentnode === self.elem) ) { if (typeof e.stoppropagation != "undefined") { e.stoppropagation(); } else { e.cancelbubble = true; } e.preventdefault(); self.pointerdown(e); } }, false); document.addeventlistener("mousemove", function (e) { self.pointermove(e); }, false); document.addeventlistener("mouseup", function (e) { self.pointerup(e); }, false); document.addeventlistener('gesturechange', function(e) { e.preventdefault(); if (e.scale > 1) self.scale = 0.1; else if (e.scale < 1) self.scale = -0.1; else self.scale = 0; self.setup.scale && self.setup.scale(e); return false; }, false); if (window.navigator.mspointerenabled) { var ncontact = 0; var mygesture = new msgesture(); mygesture.target = this.elem; this.elem.addeventlistener("mspointerdown", function(e) { if (e.pointertype == e.mspointer_type_touch) { mygesture.addpointer(e.pointerid); ncontact++; } }, false); this.elem.addeventlistener("mspointerout", function(e) { if (e.pointertype == e.mspointer_type_touch) { ncontact--; } }, false); this.elem.addeventlistener("msgesturehold", function(e) { e.preventdefault(); }, false); this.elem.addeventlistener("msgesturechange", function(e) { if (ncontact > 1) { if (e.preventdefault) e.preventdefault(); self.scale = e.velocityexpansion; self.setup.scale && self.setup.scale(e); } return false; }, false); } if (window.addeventlistener) this.elem.addeventlistener('dommousescroll', function(e) { if (e.preventdefault) e.preventdefault(); self.wheeldelta = e.detail * 10; self.setup.wheel && self.setup.wheel(e); return false; }, false); this.elem.onmousewheel = function () { self.wheeldelta = -event.wheeldelta * .25; self.setup.wheel && self.setup.wheel(event); return false; } } // ===== request animation frame ===== window.requestanimframe = (function(){ return window.requestanimationframe || window.webkitrequestanimationframe || window.mozrequestanimationframe || window.orequestanimationframe || window.msrequestanimationframe || function( run ){ window.settimeout(run, 16); }; })();
// ===== ge1doot global ===== js文件1 var ge1doot = ge1doot || { json: null, screen: null, pointer: null, camera: null, loadjs: function (url, callback, data) { if (typeof url == "string") url = [url]; var load = function (src) { var script = document.createelement("script"); if (callback) { if (script.readystate){ script.onreadystatechange = function () { if (script.readystate == "loaded" || script.readystate == "complete"){ script.onreadystatechange = null; if (--n === 0) callback(data || false); } } } else { script.onload = function() { if (--n === 0) callback(data || false); } } } script.src = src; document.getelementsbytagname("head")[0].appendchild(script); } for (var i = 0, n = url.length; i < n; i++) load(url[i]); } } // ===== html/canvas container ===== ge1doot.screen = function (setup) { ge1doot.screen = this; this.elem = document.getelementbyid(setup.container) || setup.container; this.ctx = this.elem.tagname == "canvas" ? this.elem.getcontext("2d") : false; this.style = this.elem.style; this.left = 0; this.top = 0; this.width = 0; this.height = 0; this.cursor = "default"; this.setup = setup; this.resize = function () { var o = this.elem; this.width = o.offsetwidth; this.height = o.offsetheight; for (this.left = 0, this.top = 0; o != null; o = o.offsetparent) { this.left += o.offsetleft; this.top += o.offsettop; } if (this.ctx) { this.elem.width = this.width; this.elem.height = this.height; } this.setup.resize && this.setup.resize(); } this.setcursor = function (type) { if (type !== this.cursor && 'ontouchstart' in window === false) { this.cursor = type; this.style.cursor = type; } } window.addeventlistener('resize', function () { ge1doot.screen.resize(); }, false); !this.setup.resize && this.resize(); } // ==== unified touch events handler ==== ge1doot.pointer = function (setup) { ge1doot.pointer = this; var self = this; var body = document.body; var html = document.documentelement; this.setup = setup; this.screen = ge1doot.screen; this.elem = this.screen.elem; this.x = 0; this.y = 0; this.xi = 0; this.yi = 0; this.bxi = 0; this.byi = 0; this.xr = 0; this.yr = 0; this.startx = 0; this.starty = 0; this.scale = 0; this.wheeldelta = 0; this.isdraging = false; this.hasmoved = false; this.isdown = false; this.evt = false; var sx = 0; var sy = 0; // prevent default behavior if (setup.tap) this.elem.onclick = function () { return false; } if (!setup.documentmove) { document.ontouchmove = function(e) { e.preventdefault(); } } document.addeventlistener("msholdvisual", function(e) { e.preventdefault(); }, false); if (typeof this.elem.style.mstouchaction != 'undefined') this.elem.style.mstouchaction = "none"; this.pointerdown = function (e) { if (!this.isdown) { if (this.elem.setcapture) this.elem.setcapture(); this.isdraging = false; this.hasmoved = false; this.isdown = true; this.evt = e; this.xr = (e.clientx !== undefined ? e.clientx : e.touches[0].clientx); this.yr = (e.clienty !== undefined ? e.clienty : e.touches[0].clienty); this.x = sx = this.xr - this.screen.left; this.y = sy = this.yr - this.screen.top + ((html && html.scrolltop) || body.scrolltop); this.setup.down && this.setup.down(e); } } this.pointermove = function(e) { this.xr = (e.clientx !== undefined ? e.clientx : e.touches[0].clientx); this.yr = (e.clienty !== undefined ? e.clienty : e.touches[0].clienty); this.x = this.xr - this.screen.left; this.y = this.yr - this.screen.top + ((html && html.scrolltop) || body.scrolltop); if (this.isdown) { this.xi = this.bxi + (this.x - sx); this.yi = this.byi - (this.y - sy); } if (math.abs(this.x - sx) > 11 || math.abs(this.y - sy) > 11) { this.hasmoved = true; if (this.isdown) { if (!this.isdraging) { this.startx = sx; this.starty = sy; this.setup.startdrag && this.setup.startdrag(e); this.isdraging = true; } else { this.setup.drag && this.setup.drag(e); } } else { sx = this.x; sy = this.y; } } this.setup.move && this.setup.move(e); } this.pointerup = function(e) { this.bxi = this.xi; this.byi = this.yi; if (!this.hasmoved) { this.x = sx; this.y = sy; this.setup.tap && this.setup.tap(this.evt); } else { this.setup.up && this.setup.up(this.evt); } this.isdraging = false; this.isdown = false; this.hasmoved = false; this.setup.up && this.setup.up(this.evt); if (this.elem.releasecapture) this.elem.releasecapture(); this.evt = false; } this.pointercancel = function(e) { if (this.elem.releasecapture) this.elem.releasecapture(); this.isdraging = false; this.hasmoved = false; this.isdown = false; this.bxi = this.xi; this.byi = this.yi; sx = 0; sy = 0; } if ('ontouchstart' in window) { this.elem.ontouchstart = function (e) { self.pointerdown(e); return false; } this.elem.ontouchmove = function (e) { self.pointermove(e); return false; } this.elem.ontouchend = function (e) { self.pointerup(e); return false; } this.elem.ontouchcancel = function (e) { self.pointercancel(e); return false;} } document.addeventlistener("mousedown", function (e) { if ( e.target === self.elem || (e.target.parentnode && e.target.parentnode === self.elem) || (e.target.parentnode.parentnode && e.target.parentnode.parentnode === self.elem) ) { if (typeof e.stoppropagation != "undefined") { e.stoppropagation(); } else { e.cancelbubble = true; } e.preventdefault(); self.pointerdown(e); } }, false); document.addeventlistener("mousemove", function (e) { self.pointermove(e); }, false); document.addeventlistener("mouseup", function (e) { self.pointerup(e); }, false); document.addeventlistener('gesturechange', function(e) { e.preventdefault(); if (e.scale > 1) self.scale = 0.1; else if (e.scale < 1) self.scale = -0.1; else self.scale = 0; self.setup.scale && self.setup.scale(e); return false; }, false); if (window.navigator.mspointerenabled) { var ncontact = 0; var mygesture = new msgesture(); mygesture.target = this.elem; this.elem.addeventlistener("mspointerdown", function(e) { if (e.pointertype == e.mspointer_type_touch) { mygesture.addpointer(e.pointerid); ncontact++; } }, false); this.elem.addeventlistener("mspointerout", function(e) { if (e.pointertype == e.mspointer_type_touch) { ncontact--; } }, false); this.elem.addeventlistener("msgesturehold", function(e) { e.preventdefault(); }, false); this.elem.addeventlistener("msgesturechange", function(e) { if (ncontact > 1) { if (e.preventdefault) e.preventdefault(); self.scale = e.velocityexpansion; self.setup.scale && self.setup.scale(e); } return false; }, false); } if (window.addeventlistener) this.elem.addeventlistener('dommousescroll', function(e) { if (e.preventdefault) e.preventdefault(); self.wheeldelta = e.detail * 10; self.setup.wheel && self.setup.wheel(e); return false; }, false); this.elem.onmousewheel = function () { self.wheeldelta = -event.wheeldelta * .25; self.setup.wheel && self.setup.wheel(event); return false; } } // ===== request animation frame ===== window.requestanimframe = (function(){ return window.requestanimationframe || window.webkitrequestanimationframe || window.mozrequestanimationframe || window.orequestanimationframe || window.msrequestanimationframe || function( run ){ window.settimeout(run, 16); }; })();
到此这篇关于html+css+javascript实现图片3d展览的示例代码的文章就介绍到这了,更多相关html图片3d展览内容请搜索以前的文章或继续浏览下面的相关文章,希望大家以后多多支持!
上一篇: HTML5逐步分析实现拖放功能的方法
下一篇: 使用HTML5做的导航条详细步骤