index.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <link rel="icon" href="/favicon.ico" />
  6. <meta name="viewport"
  7. content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, viewport-fit=cover">
  8. <meta http-equiv="Cache-control" content="no-cache">
  9. <meta http-equiv="Cache" content="no-cache">
  10. <meta name="apple-mobile-web-app-capable" content="yes" />
  11. <title>点名</title>
  12. <link rel="stylesheet" href="./css/index.css">
  13. <script src="js/three.min.js"></script>
  14. <script src="js/tween.min.js"></script>
  15. <!-- <script src="js/stats.min.js"></script> -->
  16. <script src="js/TrackballControls.js"></script>
  17. <script src="js/CSS3DRenderer.js"></script>
  18. <script src="js/vue.min.js"></script>
  19. <!-- 创建星星 -->
  20. <!-- <script src="js/star.js"></script> -->
  21. <!-- 数据位置 -->
  22. <script src="js/data.js"></script>
  23. </head>
  24. <body>
  25. <div id="container" style="pointer-events: none;"></div>
  26. <div id="vueBoxs">
  27. <div class="bss" @dblclick="()=>{animationStatus?closes():start()}">
  28. <div class="pageTitle"></div>
  29. <div v-if="platform!=='modal'" class="iconBack" @click.stop="onBack()"></div>
  30. <div id="menu" :class="[platform==='modal'?(imagePos==='right'?'menuRight':'menuLeft'):'']">
  31. <div id="table" :class="tableIndex <= 1 ? 'disabled' : ''" v-show="!animationStatus"
  32. @click.stop="start()"></div>
  33. <div id="sphere" v-show="animationStatus" @click.stop="closes()"></div>
  34. <div v-if="platform==='modal'" id="backBtn" @click.stop="onBack()"></div>
  35. <!-- <button id="reset" style="margin-left:40px;" @click="resets()">照片墙</button> -->
  36. <!-- <button id="lists" @click="listShow = true">中奖名单</button> -->
  37. </div>
  38. </div>
  39. </div>
  40. </body>
  41. </html>
  42. <script>
  43. var table = getData();
  44. var tableLens = table.length;
  45. var camera, scene, renderer;
  46. var controls;
  47. var objects = [];
  48. var targets = { chaos: [], table: [], sphere: [], helix: [], grid: [] };
  49. var ang = 0, moving = false, objectsss, starBG, isMoving = false; //objectsss 为切换的图片的
  50. var cpx = 0, cpy = 0, cpz = 0, crx = 0, cry = 0, crz = 0;
  51. function init() {
  52. camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000);
  53. camera.position.z = 1500;
  54. scene = new THREE.Scene();
  55. // table
  56. for (var i = 0; i < table.length; i++) {
  57. // 每个图标的盒子
  58. var element = document.createElement('div');
  59. element.className = 'element';
  60. element.style.backgroundColor = 'rgba(255,255,255)';
  61. element.style.borderRadius = '12px'
  62. if (i >= 32) {
  63. element.className = 'element hide'
  64. }
  65. // 图片
  66. var symbolBox = document.createElement('div');
  67. var symbol = document.createElement('img');
  68. symbolBox.className = 'symbolBox';
  69. symbol.className = 'symbol';
  70. symbol.src = table[i].img;
  71. symbolBox.appendChild(symbol);
  72. element.appendChild(symbolBox);
  73. // 姓名
  74. var details = document.createElement('div');
  75. details.className = 'details';
  76. details.innerHTML = table[i].name;
  77. element.appendChild(details);
  78. // 图标变成3d内的对象,放入场景中
  79. var object = new THREE.CSS3DObject(element);
  80. object.position.x = Math.random() * 3400 - 1700;
  81. object.position.y = Math.random() * 3400 - 1700;
  82. object.position.z = Math.random() * 3400 - 1700;
  83. object.name = table[i].name;
  84. scene.add(object);
  85. objects.push(object);
  86. // 根据索引,定制位置
  87. var object = new THREE.Object3D();
  88. var ix = (i) % 8;
  89. object.position.y = 300;
  90. if (table.length === 1) {
  91. object.position.x = 0;
  92. } else if (table.length === 2) {
  93. object.position.x = (ix * 172) - (172 * 1 / 2);
  94. } else if (table.length === 3) {
  95. object.position.x = (ix * 172) - (172 * 2 / 2);
  96. } else if (table.length === 4) {
  97. object.position.x = (ix * 172) - (172 * 3 / 2);
  98. } else if (table.length === 5) {
  99. object.position.x = (ix * 172) - (172 * 4 / 2);
  100. } else if (table.length === 6) {
  101. object.position.x = (ix * 172) - (172 * 5 / 2);
  102. } else if (table.length === 7) {
  103. object.position.x = (ix * 172) - (172 * 6 / 2);
  104. } else {
  105. var iy = Math.floor((i) / 8);
  106. object.position.x = (ix * 172) - 600;
  107. object.position.y = - (iy * 192) + 300;
  108. }
  109. targets.table.push(object);
  110. }
  111. // 创建切换图片
  112. var elements = document.createElement('div');
  113. elements.className = 'changeImgBoxs';
  114. // element.style.backgroundImage = "url(./img/pic.png)";
  115. elements.style.backgroundColor = 'rgba(255,255,255)';
  116. var symbolBox = document.createElement('div');
  117. var symbol = document.createElement('img');
  118. symbol.setAttribute("id", "changeImg");
  119. symbolBox.className = 'symbolBox2';
  120. symbol.className = 'symbol2';
  121. symbol.src = table[0].img;
  122. symbolBox.appendChild(symbol);
  123. elements.appendChild(symbolBox);
  124. var details = document.createElement('div');
  125. details.setAttribute("id", "detailss");
  126. details.className = 'details';
  127. details.innerHTML = table[0].name;
  128. elements.appendChild(details);
  129. objectsss = new THREE.CSS3DObject(elements);
  130. objectsss.position.x = 0;
  131. objectsss.position.y = 20000;
  132. objectsss.position.z = 0;
  133. scene.add(objectsss);
  134. // 散乱随机位置
  135. var vector = new THREE.Vector3();
  136. for (var i = 0, l = objects.length; i < l; i++) {
  137. var phi = Math.acos(-1 + (2 * i) / l);
  138. var theta = Math.sqrt(l * Math.PI) * phi;
  139. var object = new THREE.Object3D();
  140. var py = Math.random() * 3400 - 1700;
  141. if (py < 400 && py > -400) { //防止停止时,图片位置不好挡住中央的图片
  142. if (py < 0) {
  143. py -= 400;
  144. } else {
  145. py += 400;
  146. }
  147. }
  148. object.position.x = Math.random() * 3400 - 1700;
  149. object.position.y = py;
  150. object.position.z = Math.random() * 3400 - 1700;
  151. object.lookAt(vector);
  152. targets.chaos.push(object);
  153. }
  154. // 球
  155. var vector = new THREE.Vector3();
  156. for (var i = 0, l = objects.length; i < l; i++) {
  157. var phi = Math.acos(-1 + (2 * i) / l);
  158. var theta = Math.sqrt(l * Math.PI) * phi;
  159. var object = new THREE.Object3D();
  160. object.position.x = 750 * Math.cos(theta) * Math.sin(phi);
  161. object.position.y = 750 * Math.sin(theta) * Math.sin(phi);
  162. object.position.z = 750 * Math.cos(phi);
  163. vector.copy(object.position).multiplyScalar(2);
  164. object.lookAt(vector);
  165. targets.sphere.push(object);
  166. }
  167. //渲染
  168. renderer = new THREE.CSS3DRenderer();
  169. renderer.setSize(window.innerWidth, window.innerHeight);
  170. renderer.domElement.style.position = 'absolute';
  171. document.getElementById('container').appendChild(renderer.domElement);
  172. //控制器
  173. controls = new THREE.TrackballControls(camera, renderer.domElement);
  174. controls.rotateSpeed = 0.5;
  175. controls.minDistance = 500;
  176. controls.maxDistance = 6000;
  177. controls.addEventListener('change', render);
  178. // 初始化
  179. transform(targets.table, 500);
  180. window.addEventListener('resize', onWindowResize, false);
  181. cpx = camera.position.x;
  182. cpy = camera.position.y;
  183. cpz = camera.position.z;
  184. crx = camera.rotation.x;
  185. cry = camera.rotation.y;
  186. crz = camera.rotation.z;
  187. }
  188. // 切换状态时动画
  189. function transform(targets, duration, type) {
  190. var scale = 1;
  191. if (type == undefined) {
  192. type = 0;
  193. }
  194. TWEEN.removeAll();
  195. for (var i = 0; i < objects.length; i++) {
  196. var object = objects[i];
  197. var target = targets[i];
  198. new TWEEN.Tween(object.position)
  199. .to({ x: target.position.x, y: target.position.y, z: target.position.z }, Math.random() * duration + duration)
  200. .easing(TWEEN.Easing.Exponential.InOut)
  201. .start();
  202. new TWEEN.Tween(object.rotation)
  203. .to({ x: target.rotation.x, y: target.rotation.y, z: target.rotation.z }, Math.random() * duration + duration)
  204. .easing(TWEEN.Easing.Exponential.InOut)
  205. .start();
  206. }
  207. new TWEEN.Tween(this)
  208. .to({}, duration * 2)
  209. .onUpdate(render)
  210. .start()
  211. // console.log(document.querySelectorAll('.element'), '1212')
  212. setTimeout(() => {
  213. if (moving || type === 2) {
  214. const elements = document.querySelectorAll('.element')
  215. if (elements.length > 0) {
  216. elements.forEach((e, index) => {
  217. if (type === 2) {
  218. if (index >= 32) {
  219. e.classList.add('hide')
  220. }
  221. } else {
  222. e.classList.remove('hide')
  223. }
  224. })
  225. }
  226. }
  227. }, 400);
  228. }
  229. // 屏幕大小适配
  230. function onWindowResize() {
  231. camera.aspect = window.innerWidth / window.innerHeight;
  232. camera.updateProjectionMatrix();
  233. renderer.setSize(window.innerWidth, window.innerHeight);
  234. render();
  235. }
  236. function animate() {
  237. requestAnimationFrame(animate);
  238. TWEEN.update();
  239. // 旋转
  240. if (moving) {
  241. movings();
  242. }
  243. controls.update();
  244. // 星空位置与相机位置相对
  245. cpx = camera.position.x;
  246. cpy = camera.position.y;
  247. cpz = camera.position.z;
  248. crx = camera.rotation.x;
  249. cry = camera.rotation.y;
  250. crz = camera.rotation.z;
  251. }
  252. function render() {
  253. renderer.render(scene, camera);
  254. }
  255. var numsss = 0, srcss = '', txtsss = '';
  256. function movings() {
  257. // 相机旋转
  258. ang += Math.PI / 50;
  259. camera.position.x = Math.cos(ang) * 2000;
  260. camera.position.z = Math.sin(ang) * 2000;
  261. camera.position.y = 0;
  262. // 相机方向重置
  263. camera.up.x = 0;
  264. camera.up.y = 1;
  265. camera.up.z = 0;
  266. // 图片方向固定
  267. objectsss.rotation.y = -ang + Math.PI / 2;
  268. //中间图片切换
  269. numsss = Math.floor(Math.random() * tableLens);
  270. if (numsss == tableLens) {
  271. numsss = tableLens - 1;
  272. }
  273. srcss = table[numsss].img;
  274. txtsss = table[numsss].name;
  275. changeImg.src = srcss;
  276. detailss.innerHTML = txtsss;
  277. }
  278. // 停止时,图片放大动画
  279. function objDeal(obj, nums) {
  280. var option = {
  281. x: obj.scale.x,
  282. y: obj.scale.y,
  283. z: obj.scale.z,
  284. };
  285. var tween = new TWEEN.Tween(option).to({
  286. x: nums,
  287. y: nums,
  288. z: nums,
  289. }, 300).delay(100).onUpdate(function () {
  290. obj.scale.x = this.x;
  291. obj.scale.y = this.y;
  292. obj.scale.z = this.z;
  293. isMoving = true;
  294. }).onComplete(function () {
  295. isMoving = false;
  296. }).start();
  297. }
  298. // 初始化数据
  299. function renderData(data) {
  300. table = data
  301. tableLens = data.length
  302. vm.tableIndex = tableLens
  303. init();
  304. animate();
  305. console.log(vm.tableIndex, 'renderData')
  306. }
  307. //点击事件及部分判断
  308. var vm = new Vue({
  309. el: '#vueBoxs',
  310. data: {
  311. animationStatus: false,
  312. tableIndex: 0,
  313. listShow: false,
  314. platform: "",
  315. imagePos: ""
  316. },
  317. methods: {
  318. keyDowns: function () {
  319. this.closes();
  320. },
  321. //开始
  322. start: function () {
  323. if (this.tableIndex <= 1) {
  324. return
  325. }
  326. moving = true;
  327. this.animationStatus = true;
  328. objectsss.position.y = 0;
  329. transform(targets.sphere, 1000);
  330. objDeal(objectsss, 1);
  331. // music.play();
  332. // this.ckPrice();
  333. },
  334. //结束
  335. closes: function () {
  336. // music.pause();
  337. if (!moving) {
  338. return;
  339. }
  340. // music2.play();
  341. moving = false;
  342. this.animationStatus = false;
  343. // this.choosePerson();
  344. transform(targets.chaos, 250, 1);
  345. objDeal(objectsss, 1.8);
  346. },
  347. //回到照片墙的状态
  348. resets: function () {
  349. moving = false;
  350. camera.position.z = 1500;
  351. camera.position.x = 0;
  352. camera.position.y = 0;
  353. camera.up.x = 0;//相机以哪个方向为上方
  354. camera.up.y = 1;
  355. camera.up.z = 0;
  356. //图片重置方向
  357. objectsss.rotation.y = 0;
  358. objectsss.position.y = 20000;
  359. objectsss.scale.set(1, 1, 1);
  360. // 星空背景图重置
  361. transform(targets.table, 400, 2);
  362. // music.pause();
  363. // this.ckPrice();
  364. },
  365. onBack() {
  366. // 返回时先结束
  367. this.closes()
  368. this.resets()
  369. window.parent.postMessage({
  370. api: 'callBack',
  371. loading: false,
  372. }, '*');
  373. },
  374. handleUrlParmas(){
  375. const params=getUrlParams(location.href)
  376. params?.platform&&(this.platform = params?.platform)
  377. params?.imagePos&&(this.imagePos = params?.imagePos)
  378. }
  379. },
  380. created: function () {
  381. // init();
  382. // animate();
  383. // getStar();
  384. // this.ckPrice();
  385. this.handleUrlParmas()
  386. }
  387. })
  388. function getUrlParams(url) {
  389. let urlStr = url.split('?')[1]
  390. const urlSearchParams = new URLSearchParams(urlStr)
  391. const result = Object.fromEntries(urlSearchParams.entries())
  392. return result
  393. }
  394. </script>