Avin Dev

Avin Dev: Transforming Ideas into stunning websites

3D Theme switch button

Introduction:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Who Can Do This:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Step 1: Setting Up the HTML Structure

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Step 2: Styling with CSS

HTML
				<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Theme Change Toggle Button</title>
    <link rel="stylesheet" href="style.css">
</head>
<body class="light">
    <button id="button" class="switch">
        <div id="three-container"></div>
    </button>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.7.0/gsap.min.js"></script>
  <script src="app.js"></script>
</body>
</html>

			

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

CSS
				* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    min-height: 100vh;
    overflow: hidden;
    background-color: #331D2C;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.35s;
}

body.light {
    background-color: #F6F1F1;
}

button {
    width: 300px;
    height: 150px;
    border-radius: 20rem;
    border: none;
    outline: none;
    box-shadow: 0 0 15px -3px rgba(0, 0, 0, 0.6),
        inset 0 0 4px rgba(0, 0, 0, 0.2),
        5px 0 0 -3px rgba(0, 0, 0, 0.1),
        inset -5px 0 3px rgba(0, 0, 0, 0.9);
    background-color: #331D2C;
    cursor: pointer;
    transition: background-color 0.35s ease-in,
    box-shadow 0.3s, transform 0.3s;
}

button:hover {
    transform: scale(1.05);
}

body.light button {
    background-color: #F6F1F1;
    box-shadow: 0 0 15px -3px rgba(0, 0, 0, 0.6),
        inset 0 0 4px rgba(0, 0, 0, 0.2),
        5px 0 0 -3px rgba(0, 0, 0, 0.1),
        inset -5px 0 3px rgba(0, 0, 0, 0.6);
}
			

Step 3: Adding JavaScript Functionality

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

JavaScript
				let scene, camera, renderer, sphere, light,
  sunTexture, moonTexture, moonBump,
  sunMaterial, moonMaterial
let isLightTheme = true;

function init() {
  scene = new THREE.Scene();

  camera = new THREE.PerspectiveCamera(28, 300 / 150, 0.1, 1000);
  camera.position.z = 5;

  renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
  renderer.setSize(300, 150);
  document.getElementById("three-container")
  .appendChild(renderer.domElement);
  const textureLoader = new THREE.TextureLoader();
  sunTexture = textureLoader.load("sun.jpg");
  moonTexture = textureLoader.load("moon.jpg");
  moonBump = textureLoader.load('moonbump.jpg');

  sunMaterial = new THREE.MeshStandardMaterial({
    map: sunTexture,
    roughness: 1,
  });
  moonMaterial = new THREE.MeshStandardMaterial({
    map: moonTexture,
    displacementMap: moonBump,
    displacementScale: 0.15,
    roughness: 1,
  });

  const sphereGeometry = new THREE.SphereGeometry(1, 32, 32);
  sphere = new THREE.Mesh(sphereGeometry, sunMaterial);
  sphere.position.set(-1, 0, 0)
  scene.add(sphere);

  light1 = new THREE.PointLight(0xffffff, 1);
  light2 = new THREE.PointLight(0xffffff, 1);
  light3 = new THREE.PointLight(0xffffff, 1);
  light4 = new THREE.PointLight(0xffffff, 1);
  light1.position.set(2, 2, 2);
  light2.position.set(-2, -2, 2);
  light3.position.set(2, -2, 2);
  light4.position.set(-2, 2, 2);
  scene.add(light1);
  scene.add(light2);
  scene.add(light3);
  scene.add(light4);
}


function animate() {
  requestAnimationFrame(animate);
  sphere.rotation.y += 0.01;
  renderer.render(scene, camera);
}

function toggleTheme() {
  isLightTheme = !isLightTheme;
  const newTexture = isLightTheme ? sunMaterial : moonMaterial;
  const newPosition = isLightTheme ? -1 : 1;
  document.querySelector('body').classList.toggle('light')
  sphere.material = newTexture;

  gsap.to(sphere.position, {
    x: newPosition,
    duration: 0.5,
    ease: "back.out(1.7)"
  });
  sphere.material.needsUpdate = true;
}

function onWindowResize() {
  camera.aspect = window.innerWidth / window.innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize(window.innerWidth, window.innerHeight);
}
document.querySelector(".switch").addEventListener("click", toggleTheme);


init();
animate();

			

conclusion:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top