)
}
}
)
(
}
{
)
)
(
)
(
(
{
}
)
(
)
}
)
)
{
(
(
)
)
}
)
(
}

Responsive Radial Gradient Animation

  1. function rect(x1, y1, x2, y2, col, blur=.1) {
  2.   const dx = x1 - x2;
  3.   const dy = y1 - y2;
  4.   const dist = Math.sqrt(dx * dx + dy * dy);
  5.   return `radial-gradient(circle at ${x1}% ${y1}%, ${col} 0, ${col} ${dist}%, transparent ${dist +
  6.     blur}%)`;
  7. }
  8.  
  9. const NUM = 8; 
  10.  
  11. let rects = [];
  12. const colors = ['rgba(0, 0, 0, 0.2)', 'white', 'black'];
  13.  
  14. let x = 0;
  15. let y = 0;
  16. let t = 8;
  17. function loop() { 
  18.   rects = [];
  19.  
  20.   for (let i = 0; i < NUM; i++) { 
  21.     x = 50 + 30 * Math.sin(t + i / 2);
  22.     y = 50 + 30 * Math.cos(t * 1.5 * i / 10);
  23.     rects.push(rect(x, y, x + 5, y + 5, 'rgba(255, 255, 255, 1)', 1));
  24.     rects.push(rect(x, y, x + 5, y + 5, 'rgba(0, 0, 0, 0.4)', 
  25.       8 + 6 * Math.cos(y / 10)));
  26.   }
  27.   t += .04;
  28.   document.body.style.backgroundImage = rects.join(', ');
  29.   window.requestAnimationFrame(loop);
  30. }
  31. loop()
  32.  
  33. document.body.innerHTML += `
  34.   <style>
  35.     body, html {
  36.       height: 100%;
  37.       background: #ccc;
  38.       margin: 0;
  39.       background-repeat: no-repeat;
  40.       width: 100%;
  41.     }
  42.   </style>
  43. `;

Animated variation on yesterdays post – many animating circles with no divs or canvas, just radial-gradients…

// animation // css // dom // graphics // hacks // javascript // math

Responsive Radial Gradient Background

  1. function rect(x1, y1, x2, y2, col) {
  2.   const dx = x1 - x2;
  3.   const dy = y1 - y2;
  4.   const dist = Math.sqrt(dx * dx + dy * dy);
  5.   return `radial-gradient(circle at ${x1}% ${y1}%, ${col} 0, ${col} ${dist}%, transparent ${dist +
  6.     0.1}%)`;
  7. }
  8.  
  9. const NUM = 90;
  10. const MAX_SIZE = 20;
  11.  
  12. const rects = [];
  13. const colors = ['rgba(0, 0, 0, 0.2)', 'white', 'black'];
  14.  
  15. for (let i = 0; i < NUM; i++) {
  16.   const x1 = Math.random() * 100; // %
  17.   const y1 = Math.random() * 100;
  18.   const size = Math.random() * Math.random() * MAX_SIZE;
  19.   const idx = Math.random() < 0.3 ? 1 + Math.round(Math.random()) : 0;
  20.   col = colors[idx];
  21.   rects.push(rect(x1, y1, x1 + size,  y1 + size, col));
  22. }
  23.  
  24. document.body.style.backgroundImage = rects.join(', ');
  25.  
  26. document.body.innerHTML += `
  27.   <style>
  28.     body, html {
  29.       height: 100%;
  30.       background-repeat: no-repeat;
  31.     }
  32.   </style>
  33. `;

Many circles with no divs or canvas, just radial-gradients…

Single Div Box Shadow CSS

  1. <div></div>
  2. <style>
  3.   div {
  4.     position: absolute;
  5.     left: 0; top: 0;
  6.     width: 1px; height: 1px;
  7.     box-shadow: 
  8.       140px 90px 0px 4px white,
  9.       140px 90px 0px 30px rgba(255, 0, 0, 0.2),
  10.       140px 90px 0px 10px teal,
  11.       90px 80px 0px 4px white,
  12.       90px 80px 0px 30px rgba(255, 0, 0, 0.2),
  13.       90px 80px 0px 10px teal,
  14.       70px 60px 0px 20px orange,
  15.       60px 50px 0px 20px blue,
  16.       50px 40px 0px 20px red,
  17.       40px 30px 0px 20px black;
  18.   }
  19. </style>

Many box shadows on a single div.

// css

Find preventDefault or stopPropagation JavaScript

  1. Event.prototype.preventDefault = () => {
  2.   debugger;  
  3. };
  4.  
  5. // @NOTE `stopPropagation` or `stopImmediatePropagation` could also be 
  6. // preventing events 
  7.  
  8. // Event.prototype.stopPropagation = () => {
  9. //   debugger;  
  10. // };

This is a real life saver – especially for large confusing legacy projects with lots of event logic. You can use this to track down an unwanted preventDefault and/or stopPropagation. I needed this recently to see which of many calls to preventDefault was preventing my touch events from working in a certain area.

I usually pop this into the console and step through things. If you’ve never seen debugger before read about it here…

Track Mouse and Touch Events

  1. const evts = [
  2.   'touchstart', 'touchmove', 'touchend',
  3.   'mousedown', 'mousemove', 'mouseup', 
  4.   'click', 'mousenter', 'mouseleave'
  5. ]
  6. evts.forEach(type => {
  7.   document.addEventListener(type, e => {
  8.     console.log('event: ', type)
  9.   })
  10. })

See which mouse events are able to fire on the document.

I used this recently when fixing some issues with Android Talkback. Screenreaders will swallow mouse events in some cases.

You can add pointer events too if you need them…

snippet.zone ~ 2021-24 /// {s/z}