hover3d-bg-effects.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Interactive 3D image with selectable background effects and atomic rotation
  2. // Core interaction and effect selector
  3. document.querySelectorAll('[data-hover-3d]').forEach(container => {
  4. const img = container.querySelector('img');
  5. container.style.perspective = '1000px';
  6. img.style.transition = 'transform 0.05s ease';
  7. img.style.maxWidth = '100%';
  8. img.style.maxHeight = '100%';
  9. img.style.display = 'block';
  10. container.style.position = 'relative';
  11. let effectType = container.getAttribute('data-hover-3d');
  12. let currentBackground = null;
  13. if (container.hasAttribute('data-effect-selector')) {
  14. const select = document.createElement('select');
  15. select.classList.add('effect-selector');
  16. ['triangles', 'hexagons', 'circuits', 'atomic-circle'].forEach(effect => {
  17. const option = document.createElement('option');
  18. option.value = effect;
  19. option.textContent = effect.charAt(0).toUpperCase() + effect.slice(1);
  20. if (effect === effectType) option.selected = true;
  21. select.appendChild(option);
  22. });
  23. select.style.position = 'absolute';
  24. select.style.top = '10px';
  25. select.style.left = '10px';
  26. select.style.padding = '5px 10px';
  27. select.style.borderRadius = '5px';
  28. select.style.border = '1px solid #ccc';
  29. select.style.background = '#fff';
  30. select.style.cursor = 'pointer';
  31. select.style.zIndex = '0';
  32. container.appendChild(select);
  33. select.addEventListener('change', () => {
  34. effectType = select.value;
  35. container.setAttribute('data-hover-3d', effectType);
  36. container.querySelectorAll('.effect-background, .atomic-wrapper').forEach(el => el.remove());
  37. currentBackground = createEffectBackground(container, effectType);
  38. });
  39. }
  40. currentBackground = createEffectBackground(container, effectType);
  41. container.addEventListener('mousemove', e => {
  42. const rect = container.getBoundingClientRect();
  43. const x = (e.clientX - rect.left - rect.width / 2) / (rect.width / 2);
  44. const y = (e.clientY - rect.top - rect.height / 2) / (rect.height / 2);
  45. img.style.transform = `rotateX(${-y * 15}deg) rotateY(${x * 15}deg)`;
  46. });
  47. container.addEventListener('mouseleave', () => {
  48. img.style.transform = `rotateX(0deg) rotateY(0deg)`;
  49. });
  50. });
  51. function createEffectBackground(container, effectType) {
  52. if (effectType === 'triangles') return initTriangles(container);
  53. if (effectType === 'hexagons') return initHexagons(container);
  54. if (effectType === 'circuits') return initCircuits(container);
  55. if (effectType === 'pulsing-circles') return initPulsingCircles(container);
  56. if (effectType === 'atomic-circle') return initAtomicCircle(container);
  57. }
  58. // Triangles Effect
  59. function initTriangles(container) {
  60. const canvas = document.createElement('canvas');
  61. canvas.classList.add('effect-background');
  62. canvas.width = container.offsetWidth;
  63. canvas.height = container.offsetHeight;
  64. canvas.style.position = 'absolute';
  65. canvas.style.top = '0';
  66. canvas.style.left = '0';
  67. canvas.style.width = '100%';
  68. canvas.style.height = '100%';
  69. canvas.style.zIndex = '-1';
  70. container.appendChild(canvas);
  71. const ctx = canvas.getContext('2d');
  72. const triangles = [];
  73. for (let i = 0; i < 20; i++) {
  74. triangles.push({
  75. x: Math.random() * canvas.width,
  76. y: Math.random() * canvas.height,
  77. size: 40 + Math.random() * 30,
  78. speedX: (Math.random() - 0.5) * 0.5,
  79. speedY: (Math.random() - 0.5) * 0.5
  80. });
  81. }
  82. function animate() {
  83. ctx.clearRect(0, 0, canvas.width, canvas.height);
  84. triangles.forEach(t => {
  85. t.x += t.speedX;
  86. t.y += t.speedY;
  87. if (t.x < 0 || t.x > canvas.width) t.speedX *= -1;
  88. if (t.y < 0 || t.y > canvas.height) t.speedY *= -1;
  89. ctx.beginPath();
  90. ctx.moveTo(t.x, t.y);
  91. ctx.lineTo(t.x + t.size, t.y + t.size / 2);
  92. ctx.lineTo(t.x, t.y + t.size);
  93. ctx.closePath();
  94. ctx.fillStyle = 'rgba(0,212,255,0.2)';
  95. ctx.fill();
  96. });
  97. requestAnimationFrame(animate);
  98. }
  99. animate();
  100. return canvas;
  101. }
  102. // Hexagons Effect
  103. function initHexagons(container) {
  104. const canvas = document.createElement('canvas');
  105. canvas.classList.add('effect-background');
  106. canvas.width = container.offsetWidth;
  107. canvas.height = container.offsetHeight;
  108. canvas.style.position = 'absolute';
  109. canvas.style.top = '0';
  110. canvas.style.left = '0';
  111. canvas.style.width = '100%';
  112. canvas.style.height = '100%';
  113. canvas.style.zIndex = '-1';
  114. container.appendChild(canvas);
  115. const ctx = canvas.getContext('2d');
  116. const hexagons = [];
  117. for (let i = 0; i < 15; i++) {
  118. hexagons.push({
  119. x: Math.random() * canvas.width,
  120. y: Math.random() * canvas.height,
  121. size: 30 + Math.random() * 20,
  122. speedX: (Math.random() - 0.5) * 0.7,
  123. speedY: (Math.random() - 0.5) * 0.7
  124. });
  125. }
  126. function drawHexagon(x, y, size) {
  127. const angleStep = Math.PI / 3;
  128. ctx.beginPath();
  129. for (let i = 0; i < 6; i++) {
  130. const angle = i * angleStep;
  131. const px = x + size * Math.cos(angle);
  132. const py = y + size * Math.sin(angle);
  133. if (i === 0) ctx.moveTo(px, py);
  134. else ctx.lineTo(px, py);
  135. }
  136. ctx.closePath();
  137. ctx.fillStyle = 'rgba(255, 100, 0, 0.2)';
  138. ctx.fill();
  139. }
  140. function animate() {
  141. ctx.clearRect(0, 0, canvas.width, canvas.height);
  142. hexagons.forEach(h => {
  143. h.x += h.speedX;
  144. h.y += h.speedY;
  145. if (h.x < 0 || h.x > canvas.width) h.speedX *= -1;
  146. if (h.y < 0 || h.y > canvas.height) h.speedY *= -1;
  147. drawHexagon(h.x, h.y, h.size);
  148. });
  149. requestAnimationFrame(animate);
  150. }
  151. animate();
  152. return canvas;
  153. }
  154. // Circuits Effect
  155. function initCircuits(container) {
  156. const canvas = document.createElement('canvas');
  157. canvas.classList.add('effect-background');
  158. canvas.width = container.offsetWidth;
  159. canvas.height = container.offsetHeight;
  160. canvas.style.position = 'absolute';
  161. canvas.style.top = '0';
  162. canvas.style.left = '0';
  163. canvas.style.width = '100%';
  164. canvas.style.height = '100%';
  165. canvas.style.zIndex = '-1';
  166. container.appendChild(canvas);
  167. const ctx = canvas.getContext('2d');
  168. const lines = [];
  169. for (let i = 0; i < 20; i++) {
  170. lines.push({
  171. x: Math.random() * canvas.width,
  172. y: Math.random() * canvas.height,
  173. length: 30 + Math.random() * 40,
  174. speed: 0.5 + Math.random(),
  175. vertical: Math.random() > 0.5
  176. });
  177. }
  178. function animate() {
  179. ctx.clearRect(0, 0, canvas.width, canvas.height);
  180. lines.forEach(l => {
  181. if (l.vertical) l.y += l.speed;
  182. else l.x += l.speed;
  183. if (l.x > canvas.width || l.y > canvas.height) {
  184. l.x = Math.random() * canvas.width;
  185. l.y = Math.random() * canvas.height;
  186. }
  187. ctx.beginPath();
  188. if (l.vertical) ctx.moveTo(l.x, l.y - l.length);
  189. else ctx.moveTo(l.x - l.length, l.y);
  190. ctx.lineTo(l.x, l.y);
  191. ctx.strokeStyle = 'rgba(0, 255, 100, 0.3)';
  192. ctx.lineWidth = 2;
  193. ctx.stroke();
  194. });
  195. requestAnimationFrame(animate);
  196. }
  197. animate();
  198. return canvas;
  199. }
  200. // Atomic Circle Effect
  201. function initAtomicCircle(container) {
  202. const img = container.querySelector('img');
  203. const containerRect = container.getBoundingClientRect();
  204. container.style.perspective = '1000px';
  205. container.style.transformStyle = 'preserve-3d';
  206. const wrapper = document.createElement('div');
  207. wrapper.classList.add('atomic-wrapper');
  208. wrapper.style.position = 'absolute';
  209. wrapper.style.top = '50%';
  210. wrapper.style.left = '50%';
  211. wrapper.style.transformStyle = 'preserve-3d';
  212. wrapper.style.transform = 'translate(-50%, -50%)';
  213. container.appendChild(wrapper);
  214. const orbitCount = 2;
  215. const orbitRadii = [1.2, 1.3, 1.4];
  216. const orbitAxes = ['X', 'Y', 'Z'];
  217. const orbitBaseSize = Math.min(containerRect.width, containerRect.height) * 0.9;
  218. for (let i = 0; i < orbitCount; i++) {
  219. const orbit = document.createElement('div');
  220. orbit.classList.add('atomic-orbit');
  221. orbit.style.position = 'absolute';
  222. orbit.style.top = '50%';
  223. orbit.style.left = '50%';
  224. orbit.style.width = `${orbitBaseSize * orbitRadii[i]}px`;
  225. orbit.style.height = `${orbitBaseSize * orbitRadii[i]}px`;
  226. orbit.style.border = '2px solid rgba(0, 212, 255, 0.6)';
  227. orbit.style.borderRadius = '50%';
  228. orbit.style.transform = 'translate(-50%, -50%)';
  229. orbit.style.animation = `rotateOrbit${orbitAxes[i]} ${6 + i * 2}s linear infinite`;
  230. wrapper.appendChild(orbit);
  231. }
  232. img.style.position = 'relative';
  233. img.style.zIndex = '1';
  234. img.style.maxWidth = '100%';
  235. img.style.maxHeight = '100%';
  236. img.style.display = 'block';
  237. return wrapper;
  238. }
  239. // 3D Orbit Animations
  240. const style = document.createElement('style');
  241. style.textContent = `
  242. @keyframes rotateOrbitX {
  243. from { transform: translate(-50%, -50%) rotateX(0deg); }
  244. to { transform: translate(-50%, -50%) rotateX(360deg); }
  245. }
  246. @keyframes rotateOrbitY {
  247. from { transform: translate(-50%, -50%) rotateY(0deg); }
  248. to { transform: translate(-50%, -50%) rotateY(360deg); }
  249. }
  250. @keyframes rotateOrbitZ {
  251. from { transform: translate(-50%, -50%) rotateZ(0deg); }
  252. to { transform: translate(-50%, -50%) rotateZ(360deg); }
  253. }
  254. `;
  255. document.head.appendChild(style);
  256. // ✅ FIX: Recalculate effects on resize/orientation change
  257. function recalcEffects() {
  258. document.querySelectorAll('[data-hover-3d]').forEach(container => {
  259. container.querySelectorAll('.effect-background, .atomic-wrapper').forEach(el => el.remove());
  260. const effectType = container.getAttribute('data-hover-3d');
  261. createEffectBackground(container, effectType);
  262. });
  263. }
  264. window.addEventListener('resize', recalcEffects);
  265. window.addEventListener('orientationchange', recalcEffects);