smoothretro82

moveable battery charge display HUD

Nov 29th, 2025 (edited)
50
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. (() => {
  2. if (window.__batteryHUD) return; window.__batteryHUD = true;
  3.  
  4. // Create HUD container
  5. const hud = document.createElement('div');
  6. hud.id = 'batteryHUD';
  7. Object.assign(hud.style,{
  8. position:'fixed', top:'30px', left:'30px', width:'120px', height:'120px',
  9. background:'rgba(0,0,0,0.4)', borderRadius:'50%', display:'flex', alignItems:'center', justifyContent:'center',
  10. cursor:'move', zIndex:'999999', userSelect:'none', boxShadow:'0 0 20px rgba(0,0,0,0.5)'
  11. });
  12. document.body.appendChild(hud);
  13.  
  14. // Create SVG for circular progress
  15. const svgNS = "http://www.w3.org/2000/svg";
  16. const svg = document.createElementNS(svgNS,'svg');
  17. svg.setAttribute('width','100');
  18. svg.setAttribute('height','100');
  19. svg.setAttribute('viewBox','0 0 100 100');
  20. hud.appendChild(svg);
  21.  
  22. // Background circle
  23. const bgCircle = document.createElementNS(svgNS,'circle');
  24. bgCircle.setAttribute('cx','50'); bgCircle.setAttribute('cy','50'); bgCircle.setAttribute('r','45');
  25. bgCircle.setAttribute('stroke','#555'); bgCircle.setAttribute('stroke-width','10'); bgCircle.setAttribute('fill','none');
  26. svg.appendChild(bgCircle);
  27.  
  28. // Progress circle
  29. const progCircle = document.createElementNS(svgNS,'circle');
  30. progCircle.setAttribute('cx','50'); progCircle.setAttribute('cy','50'); progCircle.setAttribute('r','45');
  31. progCircle.setAttribute('stroke','limegreen'); progCircle.setAttribute('stroke-width','10'); progCircle.setAttribute('fill','none');
  32. progCircle.setAttribute('stroke-linecap','round');
  33. progCircle.setAttribute('transform','rotate(-90 50 50)');
  34. progCircle.style.transition = 'stroke-dashoffset 0.5s, stroke 0.5s';
  35. svg.appendChild(progCircle);
  36.  
  37. const circumference = 2 * Math.PI * 45;
  38. progCircle.style.strokeDasharray = circumference;
  39. progCircle.style.strokeDashoffset = circumference;
  40.  
  41. // Text
  42. const batteryText = document.createElement('div');
  43. batteryText.style.position='absolute';
  44. batteryText.style.color='white';
  45. batteryText.style.fontWeight='bold';
  46. batteryText.style.fontSize='18px';
  47. batteryText.style.textAlign='center';
  48. batteryText.style.pointerEvents='none';
  49. hud.appendChild(batteryText);
  50.  
  51. // Charging bolt
  52. const bolt = document.createElement('div');
  53. bolt.innerText='⚡';
  54. Object.assign(bolt.style,{
  55. position:'absolute', top:'10px', right:'10px', fontSize:'24px', color:'yellow', textShadow:'0 0 8px yellow', display:'none'
  56. });
  57. hud.appendChild(bolt);
  58.  
  59. // Drag
  60. let dragging=false,offsetX=0,offsetY=0;
  61. hud.addEventListener('mousedown', e=>{dragging=true; offsetX=e.clientX-hud.offsetLeft; offsetY=e.clientY-hud.offsetTop;});
  62. document.addEventListener('mousemove', e=>{if(dragging){hud.style.left=(e.clientX-offsetX)+'px'; hud.style.top=(e.clientY-offsetY)+'px';}});
  63. document.addEventListener('mouseup', ()=>dragging=false);
  64.  
  65. // Battery update
  66. (async()=>{
  67. if(!navigator.getBattery){batteryText.innerText='N/A'; return;}
  68. const b = await navigator.getBattery();
  69.  
  70. function updateBattery(){
  71. let level = b.level;
  72. batteryText.innerText = Math.round(level*100)+'%';
  73. bolt.style.display = b.charging?'block':'none';
  74.  
  75. // Update circular progress
  76. let offset = circumference * (1 - level);
  77. progCircle.style.strokeDashoffset = offset;
  78.  
  79. // Color: green normal, yellow charging, red low
  80. if(b.charging) progCircle.setAttribute('stroke','limegreen');
  81. else if(level<0.2) progCircle.setAttribute('stroke','red');
  82. else progCircle.setAttribute('stroke','green');
  83. }
  84.  
  85. updateBattery();
  86. b.addEventListener('chargingchange',updateBattery);
  87. b.addEventListener('levelchange',updateBattery);
  88. b.addEventListener('chargingtimechange',updateBattery);
  89. b.addEventListener('dischargingtimechange',updateBattery);
  90. })();
  91. })();
  92.  
Advertisement
Comments
  • smoothretro82
    244 days
    # text 0.22 KB | 0 0
    1. Features in this version:
    2.  
    3. Circular progress ring with smooth gradient fill.
    4.  
    5. Charging bolt appears as a glowing yellow icon.
    6.  
    7. Shadow and glow effects for depth.
    8.  
    9. Smooth animation on level change.
    10.  
    11. Draggable HUD.
Add Comment
Please, Sign In to add comment