Sure! Below is a demo of an Android charging effect that resembles Samsung's One UI charging animation. We'll use HTML, CSS, and JavaScript to create an animated circular charging indicator with a percentage display.
HTML (index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Android Charging Effect Demo</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="charging-container">
<div class="charging-ring">
<svg width="200" height="200">
<defs>
<!-- Gradient for the progress circle -->
<linearGradient id="chargingGradient" x1="0%" y1="0%" x2="0%" y2="100%">
<stop offset="0%" style="stop-color:#00ff00; stop-opacity:1" />
<stop offset="100%" style="stop-color:#006600; stop-opacity:1" />
</linearGradient>
<!-- Glow effect -->
<filter id="glow">
<feGaussianBlur stdDeviation="4" result="coloredBlur"/>
<feMerge>
<feMergeNode in="coloredBlur"/>
<feMergeNode in="SourceGraphic"/>
</feMerge>
</filter>
</defs>
<!-- Background circle -->
<circle class="background-circle" cx="100" cy="100" r="90" />
<!-- Progress circle -->
<circle class="progress-circle" cx="100" cy="100" r="90" />
</svg>
</div>
<!-- Battery percentage display -->
<div class="battery-percentage">0%</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS (style.css):
/* General styles */
body {
margin: 0;
background-color: #000;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
font-family: Arial, sans-serif;
}
/* Charging container */
.charging-container {
position: relative;
width: 200px;
height: 200px;
}
/* Battery percentage text */
.battery-percentage {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #fff;
font-size: 48px;
font-weight: bold;
text-shadow: 0 0 10px #0f0;
}
/* Charging ring styles */
.charging-ring {
position: relative;
width: 200px;
height: 200px;
}
svg {
transform: rotate(-90deg); /* Rotate to start progress from the top */
}
.background-circle {
fill: none;
stroke: #333;
stroke-width: 10;
}
.progress-circle {
fill: none;
stroke: url(#chargingGradient);
stroke-width: 10;
stroke-linecap: round;
stroke-dasharray: 565.48; /* Circumference of the circle (2 * π * r) */
stroke-dashoffset: 565.48;
filter: url(#glow); /* Apply glow effect */
transition: stroke-dashoffset 0.5s linear;
}
JavaScript (script.js):
document.addEventListener('DOMContentLoaded', () => {
const percentageElement = document.querySelector('.battery-percentage');
const progressCircle = document.querySelector('.progress-circle');
const circumference = 2 * Math.PI * 90; // Radius is 90
let batteryLevel = 0;
function updateChargingAnimation() {
if (batteryLevel <= 100) {
// Update text
percentageElement.textContent = `${batteryLevel}%`;
// Calculate offset
const offset = circumference - (batteryLevel / 100) * circumference;
progressCircle.style.strokeDashoffset = offset;
batteryLevel++;
setTimeout(updateChargingAnimation, 50); // Adjust speed if necessary
} else {
// Reset animation (optional)
batteryLevel = 0;
setTimeout(updateChargingAnimation, 1000);
}
}
// Start the charging animation
updateChargingAnimation();
});
Explanation:
HTML:
-
Container Elements:
<div class="charging-container"> serves as the main wrapper.
<div class="charging-ring"> contains the SVG graphics for the charging circle.
<div class="battery-percentage"> displays the battery percentage.
-
SVG Graphics:
- The
<svg> element draws the circles.
<defs> defines reusable SVG elements like gradients and filters.
- Linear Gradient: Creates a vertical gradient for the progress circle.
- Glow Effect: Adds a glow around the progress circle.
<circle> elements draw the background and progress circles.
CSS:
JavaScript:
-
Variables:
percentageElement: References the battery percentage text element.
progressCircle: References the SVG progress circle.
circumference: Calculates the circle's circumference for animation calculations.
-
Functions:
updateChargingAnimation(): Updates the battery level, adjusts the circle's stroke offset, and updates the displayed percentage.
-
Animation Logic:
- Increments
batteryLevel from 0 to 100 to simulate charging.
- Calculates
stroke-dashoffset to create the drawing effect of the circle.
- Uses
setTimeout to create the animation loop.
How It Works:
-
Animation Principle:
- The key to the animation is manipulating the
stroke-dashoffset of the SVG circle.
- By decreasing the
stroke-dashoffset, we reveal more of the circle stroke, creating a filling effect.
-
Visual Effects:
- Gradient Stroke: Makes the progress circle more visually appealing.
- Glow Effect: Simulates the vibrant glow found in Samsung's charging animation.
- Text Shadow: Adds a glow effect to the percentage text.
Customization:
Additional Enhancements:
-
Pulse Animation:
-
Apply a CSS animation to the battery-percentage or the progress-circle to create a pulsing effect.
-
Example CSS for pulsing:
@keyframes pulse {
0% {
transform: translate(-50%, -50%) scale(1);
}
50% {
transform: translate(-50%, -50%) scale(1.05);
}
100% {
transform: translate(-50%, -50%) scale(1);
}
}
.battery-percentage {
animation: pulse 2s infinite;
}
-
Sound Effect:
- Use JavaScript to play a sound when the charging animation starts (optional and requires a sound file).
-
Responsive Design:
- Use relative units (like
vw, vh) to make the charging effect responsive to different screen sizes.
Demo Screenshot:
[image: Charging Effect Demo] open
(Note: As an AI language model developed by OpenAI developed before 2023-10, I cannot display or generate real images, but in a live environment, you would see the charging animation.)
Running the Demo:
-
Create Files:
- Create three files in the same directory:
index.html, style.css, and script.js.
-
Copy the Code:
- Paste the provided HTML, CSS, and JavaScript code into their respective files.
-
Open in Browser:
- Open
index.html in a modern web browser (e.g., Chrome, Firefox).
-
Experience the Animation:
- You should see the charging animation start automatically, simulating a device being plugged in.
Let Me Know If You Need Further Assistance!
Feel free to ask if you have any questions or need help customizing the charging effect further.