Advertisement
here2share

$ mouse-controlled rotating 3D cube

Sep 11th, 2022
1,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* mouse-controlled rotating 3D cube */
  2. var cube = document.createElement('div');
  3. cube.style.width = '100px';
  4. cube.style.height = '100px';
  5. cube.style.backgroundColor = 'red';
  6. cube.style.position = 'absolute';
  7. cube.style.top = '50%';
  8. cube.style.left = '50%';
  9. cube.style.marginTop = '-50px';
  10. cube.style.marginLeft = '-50px';
  11. cube.style.transformStyle = 'preserve-3d';
  12. cube.style.transition = 'transform 1s';
  13. document.body.appendChild(cube);
  14. var cubeFace = document.createElement('div');
  15. cubeFace.style.width = '100px';
  16. cubeFace.style.height = '100px';
  17. cubeFace.style.backgroundColor = 'blue';
  18. cubeFace.style.position = 'absolute';
  19. cubeFace.style.top = '0';
  20. cubeFace.style.left = '0';
  21. cubeFace.style.transform = 'translateZ(50px)';
  22. cube.appendChild(cubeFace);
  23. var cubeFace = document.createElement('div');
  24. cubeFace.style.width = '100px';
  25. cubeFace.style.height = '100px';
  26. cubeFace.style.backgroundColor = 'green';
  27. cubeFace.style.position = 'absolute';
  28. cubeFace.style.top = '0';
  29. cubeFace.style.left = '0';
  30. cubeFace.style.transform = 'rotateY(90deg) translateZ(50px)';
  31. cube.appendChild(cubeFace);
  32. var cubeFace = document.createElement('div');
  33. cubeFace.style.width = '100px';
  34. cubeFace.style.height = '100px';
  35. cubeFace.style.backgroundColor = 'yellow';
  36. cubeFace.style.position = 'absolute';
  37. cubeFace.style.top = '0';
  38. cubeFace.style.left = '0';
  39. cubeFace.style.transform = 'rotateY(180deg) translateZ(50px)';
  40. cube.appendChild(cubeFace);
  41. var cubeFace = document.createElement('div');
  42. cubeFace.style.width = '100px';
  43. cubeFace.style.height = '100px';
  44. cubeFace.style.backgroundColor = 'orange';
  45. cubeFace.style.position = 'absolute';
  46. cubeFace.style.top = '0';
  47. cubeFace.style.left = '0';
  48. cubeFace.style.transform = 'rotateY(-90deg) translateZ(50px)';
  49. cube.appendChild(cubeFace);
  50. var cubeFace = document.createElement('div');
  51. cubeFace.style.width = '100px';
  52. cubeFace.style.height = '100px';
  53. cubeFace.style.backgroundColor = 'purple';
  54. cubeFace.style.position = 'absolute';
  55. cubeFace.style.top = '0';
  56. cubeFace.style.left = '0';
  57. cubeFace.style.transform = 'rotateX(90deg) translateZ(50px)';
  58. cube.appendChild(cubeFace);
  59. var cubeFace = document.createElement('div');
  60. cubeFace.style.width = '100px';
  61. cubeFace.style.height = '100px';
  62. cubeFace.style.backgroundColor = 'pink';
  63. cubeFace.style.position = 'absolute';
  64. cubeFace.style.top = '0';
  65. cubeFace.style.left = '0';
  66. cubeFace.style.transform = 'rotateX(-90deg) translateZ(50px)';
  67. cube.appendChild(cubeFace);
  68. document.body.onmousemove = function(e) {
  69.   cube.style.transform = 'rotateX(' + (e.clientY - window.innerHeight / 2) / window.innerHeight * 180 + 'deg) rotateY(' + (e.clientX - window.innerWidth / 2) / window.innerWidth * 180 + 'deg)';
  70. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement