script.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. Button = (function () {
  2. var scene,
  3. camera,
  4. renderer,
  5. button,
  6. theButtonThe;
  7. function makeAScene() {
  8. scene = new THREE.Scene();
  9. camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
  10. renderer = new THREE.WebGLRenderer({
  11. antialias: true,
  12. transparent: true,
  13. alpha: true
  14. });
  15. renderer.setSize(window.innerWidth - 15, window.innerHeight - 15);
  16. var container = document.querySelector('.container');
  17. container.appendChild(renderer.domElement);
  18. var buttonOuterGeometry = new THREE.TorusGeometry(5, .5, 25, 50),
  19. buttonMaterial = new THREE.MeshPhongMaterial({
  20. color: 0xec6115,
  21. emissive: 0xbd411e,
  22. shininess: 60
  23. }),
  24. extrudeSettings = {
  25. amount: 0.485,
  26. bevelEnabled: false
  27. };
  28. button = new THREE.Mesh(buttonOuterGeometry, buttonMaterial);
  29. var buttonInnerShape = new THREE.Shape();
  30. buttonInnerShape.moveTo(5, 0);
  31. buttonInnerShape.absarc(0, 0, 5, 0, Math.PI * 2, false),
  32. holePath1 = new THREE.Path(),
  33. holePath2 = new THREE.Path(),
  34. holePath3 = new THREE.Path(),
  35. holePath4 = new THREE.Path();
  36. holePath1.moveTo(-1, 1.5);
  37. holePath1.absarc(-1.53, 1.5, .5, 0, Math.PI * 2, true);
  38. buttonInnerShape.holes.push(holePath1);
  39. holePath2.moveTo(-2, 1.5);
  40. holePath2.absarc(-1.5, -1.5, .5, 0, Math.PI * 2, true);
  41. buttonInnerShape.holes.push(holePath2);
  42. holePath3.moveTo(2, -1.5);
  43. holePath3.absarc(1.5, -1.5, .5, 0, Math.PI * 2, true);
  44. buttonInnerShape.holes.push(holePath3);
  45. holePath4.moveTo(2, 1.5);
  46. holePath4.absarc(1.5, 1.5, .5, 0, Math.PI * 2, true);
  47. buttonInnerShape.holes.push(holePath4);
  48. var buttonInnerGeometry = new THREE.ExtrudeGeometry(buttonInnerShape, extrudeSettings),
  49. buttonInner = new THREE.Mesh(buttonInnerGeometry, buttonMaterial);
  50. button.add(buttonInner);
  51. button.rotation.set(Math.PI / 2, 0, 0);
  52. scene.add(button);
  53. var light = new THREE.DirectionalLight(0xffffff, 1);
  54. light.position.set(0, 1, 0);
  55. scene.add(light);
  56. var cameraZPosition = window.innerHeight < 500 ? 10 : 17;
  57. camera.position.set(0, 0, cameraZPosition);
  58. render();
  59. }
  60. function update() {
  61. renderer.setSize(window.innerWidth - 15, window.innerHeight - 15);
  62. camera.aspect = window.innerWidth / window.innerHeight;
  63. camera.updateProjectionMatrix();
  64. var cameraZPosition = window.innerHeight < 500 ? 10 : 17;
  65. camera.position.set(0, 0, cameraZPosition);
  66. }
  67. function render() {
  68. requestAnimationFrame(render);
  69. renderer.render(scene, camera);
  70. };
  71. function itsAButton() {
  72. this.className += ' click';
  73. theButtonThe.removeEventListener('mousedown', itsAButton);
  74. var click = new Audio('click.mp3');
  75. click.volume = .3;
  76. click.preload = true;
  77. click.play();
  78. var flip = new Audio('whoosh.mp3');
  79. flip.volume = .75;
  80. flip.preload = true;
  81. flip.play();
  82. var currentRotation = button.rotation.x;
  83. TweenMax.to(button.rotation, 8, {
  84. x: currentRotation + (Math.PI * 2), ease: Power2.easeInOut, onComplete: function () {
  85. theButtonThe.removeAttribute('class');
  86. theButtonThe.addEventListener('mousedown', itsAButton);
  87. }
  88. });
  89. }
  90. function init() {
  91. theButtonThe = document.getElementsByTagName('button')[0];
  92. theButtonThe.addEventListener('mousedown', itsAButton);
  93. window.addEventListener('resize', update);
  94. makeAScene();
  95. }
  96. document.addEventListener("DOMContentLoaded", init);
  97. })();