Advertisement
Wolfbeast

CSS transparent stop gradient animation

Apr 9th, 2016
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. I think Tab's main objection is that a transparent stop itself (especially
  2. when "in the middle") would be more difficult to animate to another
  3. (not-transparent) color because it would influence the surrounding gradients
  4. rather sharply.
  5.  
  6. The simple animation scenario indeed applies without issue, but only if you're
  7. talking about animating the adjacent colors [a]- but considering the nature of
  8. the transparent stop being dependent on the adjacent stop already, the
  9. transparent stop will simply follow whatever color the adjacent stop animates
  10. to, to keep the gradient a single hue.
  11.  
  12. If you're actually animating the transparent stop itself to something not
  13. transparent it's still simple on the edge [b] and only if you're looking at a
  14. transparent stop in the middle [c] it's more complex but certainly not
  15. impossible. The trick there is that you need 3 stops in your implementation.
  16. Why? because you're actually looking at a *sharp edge* at the transparent
  17. point that would influence the gradients with a naive averaging approach.
  18.  
  19. Nobody said animating accurate gradients was simple in all cases ;)
  20.  
  21. However, I don't see problems implementing any of these scenarios:
  22.  
  23. [a] is extremely simple and just needs the color value of the adjacent stop
  24. copied to the color value of the transparent stop each animation step if it
  25. remains a transparent stop. If you previously optimized the transparent stop
  26. to be a single one (for equal adjacent colors either side) and adjacent colors
  27. animate to different ones, then you need to create a second stop for unequal
  28. color values either side of transparent.
  29.  
  30. [b] is also very simple since you can just transition from the transparent
  31. adjacent color to the target color.
  32.  
  33. [c] is a little more complex. It's still some straightforward math to make an
  34. animation-dependent average mid point. When it comes down to a usable
  35. approach, all you have to do is look at what you start with and what you need
  36. to end with.
  37. Since you start off with a sharp edge as far as color is concerned (although
  38. not visually), you need to not just double-up, but create an intermediate,
  39. sliding stop on either side (going from 50% position to 25%||75% at
  40. completion) that starts at the transparent stop and ends at halfway between
  41. the transparent stop and the adjacent color at the midpoint values.
  42.  
  43. If absolutely needed I can take some time to write a paper how to do this (I'd
  44. rather not, I'm quite busy as it is already), but basically you'd end up with
  45. the following:
  46. start of animation (0):
  47. L - T - R -> rendered as L - TL||TR - R
  48. We're going to transition T(ransparent) to a new color C with alpha A
  49. for animation, the starting situation should be considered as
  50. L - TL||T||TR - R, where T = the target color C with alpha 0
  51. at 20% animation:
  52. L - (90%L+10%C, 20%A)@45% - (C, 20%A)@50% - (90%R+10%C, 20%A)@55% - R
  53. at 50% animation:
  54. L - (75%L+25%C, 50%A)@37.5% - (C, 50%A)@50% - (75%R+25%C, 50%A)@62.5% - R
  55. at 100% animation:
  56. L - (50%L+50%C, 100%A)@25% - (C, 100%A)@50% - (50%R+50%C, 100%A)@75% - R
  57. which is exactly the same as an L - C - R gradient.
  58.  
  59. I think all of these situations/actions will lead to smooth animations. So it
  60. most certainly remains animatable although with a little bit more math
  61. involved due to the sharp edge in the latter case.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement