Creating a responsive, mobile-friendly layered color wheel with a frosted glass blur effect that reacts to device accelerometer or mouse movements involves a combination of HTML, CSS, and JavaScript. Below is a complete example that you can use as a starting point.
Overview
- HTML: Structure the color wheel using multiple concentric
div elements.
- CSS: Style the color wheel with gradients, layering, and the frosted glass effect using
backdrop-filter.
- JavaScript: Add interactivity by tracking mouse or device orientation to rotate the color wheel accordingly.
- Responsiveness: Ensure the color wheel scales appropriately on different screen sizes using relative units and media queries.
Complete Code Example
1. HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Responsive Layered Color Wheel</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<div class="color-wheel" id="colorWheel">
<div class="layer layer1"></div>
<div class="layer layer2"></div>
<div class="layer layer3"></div>
<div class="layer layer4"></div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
2. CSS (styles.css)
/* Reset default margins and paddings */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body, html {
height: 100%;
width: 100%;
background: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
}
.container {
position: relative;
width: 80vmin;
height: 80vmin;
max-width: 500px;
max-height: 500px;
}
.color-wheel {
position: relative;
width: 100%;
height: 100%;
perspective: 1000px;
}
.layer {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border-radius: 50%;
background: conic-gradient(
red, yellow, lime, aqua, blue, magenta, red
);
opacity: 0.7;
backdrop-filter: blur(10px);
border: 2px solid rgba(255, 255, 255, 0.3);
animation: rotate 20s linear infinite;
}
/* Different layers with varying rotation speeds and sizes */
.layer1 {
transform: rotate(0deg);
animation-duration: 20s;
}
.layer2 {
transform: rotate(90deg);
animation-duration: 30s;
}
.layer3 {
transform: rotate(180deg);
animation-duration: 25s;
}
.layer4 {
transform: rotate(270deg);
animation-duration: 35s;
}
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
/* Responsive adjustments */
@media (max-width: 600px) {
.container {
width: 90vmin;
height: 90vmin;
}
}
3. JavaScript (script.js)
const colorWheel = document.getElementById('colorWheel');
// Function to handle mouse movement
function handleMouseMove(event) {
const { innerWidth, innerHeight } = window;
const x = (event.clientX / innerWidth) - 0.5; // Range: -0.5 to 0.5
const y = (event.clientY / innerHeight) - 0.5;
const rotationX = y * 30; // Limit rotation to 30 degrees
const rotationY = x * 30;
colorWheel.style.transform = `rotateX(${rotationX}deg) rotateY(${rotationY}deg)`;
}
// Function to handle device orientation
function handleDeviceOrientation(event) {
const { beta, gamma } = event; // beta: front-back tilt, gamma: left-right tilt
const rotationX = beta - 0; // Adjust as needed
const rotationY = gamma - 0;
colorWheel.style.transform = `rotateX(${rotationX}deg) rotateY(${rotationY}deg)`;
}
// Detect device capability
if (window.DeviceOrientationEvent) {
window.addEventListener('deviceorientation', handleDeviceOrientation, true);
} else {
// Fallback for devices without accelerometer
window.addEventListener('mousemove', handleMouseMove, false);
}
Explanation
HTML
- Container: A
div with the class container centers the color wheel on the page.
- Color Wheel: The
color-wheel div contains multiple layer divs to create a layered effect.
CSS
- Reset and Base Styling: Ensures consistent styling across browsers and centers the container.
- Color Wheel Layers: Each
.layer uses a conic-gradient to create a color wheel effect. The backdrop-filter: blur(10px) adds the frosted glass effect.
- Animation: Each layer rotates at different speeds to create a dynamic, layered appearance.
- Responsiveness: The container scales with viewport units (
vmin) and adjusts for smaller screens using media queries.
JavaScript
- Interactivity:
- Mouse Movement: On devices without an accelerometer, the color wheel responds to mouse movements by rotating along the X and Y axes.
- Device Orientation: On mobile devices with an accelerometer, the color wheel responds to device tilting.
- Event Listeners: The script checks if
DeviceOrientationEvent is supported and adds the appropriate event listener.
Usage
- Create Files:
- Create an
index.html file and paste the HTML content.
- Create a
styles.css file and paste the CSS content.
- Create a
script.js file and paste the JavaScript content.
- Folder Structure:
project-folder/
├── index.html
├── styles.css
└── script.js
- Run the Project: Open
index.html in a web browser. On desktop, move the mouse to see the wheel react. On mobile devices, tilt the device to interact with the color wheel.
Additional Enhancements
- Touch Controls: For more granular control on touch devices, you can add touch event listeners.
- Accessibility: Ensure that the visual effects do not hinder the accessibility of your page.
- Performance Optimization: If the animations are too heavy, consider reducing the number of layers or simplifying the gradients.
Live Demo
To see a live demo without setting up files, you can use platforms like CodePen, JSFiddle, or JSBin. Here's how you can set it up on CodePen:
- Go to CodePen.
- Create a new Pen.
- Paste the HTML, CSS, and JavaScript code into their respective sections.
- Save and view the live preview.
This setup provides an interactive, visually appealing color wheel that adapts to both desktop and mobile environments, offering an engaging user experience.