• 使用CSS实现多种Noise噪点效果


    在插画中添加噪点肌理可以营造出一种自然的氛围。噪点肌理可以用于塑造阴影、高光、深度以及更多细节,并优化插画质感,应用噪点肌理的方式在扁平插画中广受欢迎。

    前端开发过程中,我们也有可能遇到噪点插画风格的设计稿,应用基础的前端开发知识,能不能实现噪点风格的样式呢,本文主要内容主要就是通过几个示例来实现几种噪点效果。本文包含的知识点包括:CSS 属性 mask 遮罩、SVG 滤镜 feTurbulenceCSS 属性 filter 滤镜、CSS 属性 mix-blend-mode 元素混合、CSS 属性 image-rendering 图像缩放等。

    开始本文主要内容之前,我们先来欣赏几张设计师在插画作品中应用噪点肌理的优秀例子。

    作品链接 dribbble.com

    作品链接 dribbble.com

    作品链接 dribbble.com

    知识汇总

    PS 实现

    在 Photoshop 中增加噪点效果的基础操作方法:

    • 混合模式(溶解)+ 柔和笔刷(做暗灰亮)
    • 添加材质(正片叠底)
    • 图层样式(内阴影,投影等)
    • 噪点笔刷绘制

    知识点

    本文中将用到以下几个 CSS 特性,正式开发之前先简单了解下。

    💡 mask

    CSS 属性 mask 允许使用者通过遮罩或者裁切特定区域的图片的方式来隐藏一个元素的部分或者全部可见区域。

    基本用法

    1. // 使用位图来做遮罩
    2. mask: url(mask.png);
    3. // 使用 SVG 图形中的形状来做遮罩
    4. mask: url(masks.svg#star);

    🔗 详细用法可访问:MDN

    💡 feTurbulence

    在 SVG 中,feTurbulence 滤镜利用 Perlin 噪声函数创建了一个图像。它实现了人造纹理比如说云纹、大理石纹的合成。本文中将利用该滤镜生成噪点背景图片。

    🔗 详细用法可访问:MDN

    💡 filter

    CSS 属性 filter 将模糊或颜色偏移等图形效果应用于元素。可以为元素添加滤镜效果,本文中主要应用它来增强噪点视觉效果。

    🔗 详细用法可访问:MDN,也可访问我的另外一篇文章《CSS filter 有哪些神奇用途》,其中有 filter 应用的详细介绍。

    💡 mix-blend-mode

    CSS 属性 mix-blend-mode 描述了元素的内容应该与元素的直系父元素的内容和元素的背景如何混合。

    基本语法

    1. mix-blend-mode: normal;
    2. mix-blend-mode: multiply;
    3. mix-blend-mode: screen;
    4. mix-blend-mode: overlay;
    5. mix-blend-mode: darken;
    6. mix-blend-mode: lighten;
    7. mix-blend-mode: color-dodge
    8. mix-blend-mode: color-burn;
    9. mix-blend-mode: hard-light;
    10. mix-blend-mode: soft-light;
    11. mix-blend-mode: difference;
    12. mix-blend-mode: exclusion;
    13. mix-blend-mode: hue;
    14. mix-blend-mode: saturation;
    15. mix-blend-mode: color;
    16. mix-blend-mode: luminosity;
    17. mix-blend-mode: initial;
    18. mix-blend-mode: inherit;
    19. mix-blend-mode: unset;

    不同 mix-blend-mode 效果:

    🔗 详细用法可访问:MDN

    💡 image-rendering

    CSS 属性 image-rendering 用于设置图像缩放算法。它适用于元素本身,适用于元素其他属性中的图像,也应用于子元素。

    基本语法

    1. image-rendering: auto;
    2. image-rendering: crisp-edges;
    3. image-rendering: pixelated;

    其中:

    • auto:自动,自 Gecko 1.9 起,使用双线性算法进行重新采样。
    • crisp-edges:必须使用可有效保留对比度和图像中的边缘的算法来对图像进行缩放,并且,该算法既不会平滑颜色,又不会在处理过程中为图像引入模糊。
    • pixelated:放大图像时,使用最近邻居算法,因此,图像看着像是由大块像素组成的。缩小图像时,算法与 auto 相同。

    🔗 详细用法可访问:MDN

    效果

    以下页面是本文实现的多种噪点效果。

    实现

    定义 CSS 全局变量。

    1. :root {
    2. --black: #000000;
    3. --blue: #0072ff;
    4. --green: #03c03c;
    5. --yellow: #fffc00;
    6. --red: #ff4b2b;
    7. --orange: #f5af19;
    8. --pink: #ee9ca7;
    9. --purple: #a770ef;
    10. }

    创建一个 SVG 噪点背景,后续将使用创建的 SVG 元素作为其他元素的噪点背景。

    1. <svg viewBox="0 0 200 200" xmlns='http://www.w3.org/2000/svg'>
    2. <filter id='noise'>
    3. <feTurbulence type='fractalNoise' baseFrequency='0.65' numOctaves='3' stitchTiles='stitch' />
    4. </filter>
    5. <rect width='100%' height='100%' filter='url(#noiseFilter)' />
    6. </svg>

    类型0 🌚

    通过给 background 添加 linear-gradient 和噪点图片背景,就可实现渐变的噪点效果。

    <div class="noise_0"></div>
    
    1. .noise_0 {
    2. background: linear-gradient(to right bottom, var(--black), rgba(0, 0, 0, 0)), url(#noise);
    3. }

    类型1 🌑

    在上面的基础上,通过修改 filter 属性的对比度 contrast 和亮度 brightness,增强噪点效果得对比度。

    <div class="noise_1"></div>
    
    1. .noise_1 {
    2. filter: contrast(200%) brightness(150%);
    3. }

    类型2 🌒

    使用 mix-blend-mode: multiply 实现混合双色渐变的噪点效果。

    1. <div class="noise noise_2">
    2. <div class="isolate">
    3. <div class="gradient"></div>
    4. <div class="overlay"></div>
    5. </div>
    6. </div>
    1. .noise_2 {
    2. position: relative;
    3. }
    4. .noise_2 .isolate {
    5. isolation: isolate;
    6. position: relative;
    7. width: 100%;
    8. height: 100%;
    9. }
    10. .noise_2 .overlay {
    11. position: absolute;
    12. top: 0;
    13. width: 100%;
    14. height: 100%;
    15. background: var(--purple);
    16. mix-blend-mode: multiply;
    17. }
    18. .noise_2 .gradient {
    19. height: 100%;
    20. width: 100%;
    21. filter: contrast(200%) brightness(150%);
    22. background: linear-gradient(to right bottom, var(--black), rgba(0, 0, 0, 0)), url(#noise);
    23. }

    类型3 🌓

    圆形的噪点元素。

    1. <div class="noise_3">
    2. <div class="gradient"></div>
    3. </div>
    1. .noise_3 {
    2. border-radius: 50%;
    3. }
    4. .noise_3 {
    5. background: var(--black);
    6. }
    7. .noise_3 .gradient {
    8. width: 100%;
    9. height: 100%;
    10. background: radial-gradient(circle at 100% 0, transparent 20%, #cccccc 80%);
    11. mask: url(#noise), radial-gradient(circle at 100% 0, transparent 10%, #000 60%);
    12. }

    类型4 🌔

    球形的噪点元素,给圆形噪点添加地面阴影和光照效果就能形成立体的球体效果。

    1. <div class="noise noise_5">
    2. <div class="ground"><div class="ground-shadow"></div></div>
    3. <div class="ball">
    4. <div class="isolate">
    5. <div class="ball-shadow"></div>
    6. <div class="ball-light"></div>
    7. </div>
    8. </div>
    9. </div>
    1. <style>
    2. .noise_5 {
    3. position: relative;
    4. }
    5. .noise_5 .ball {
    6. position: relative;
    7. display: flex;
    8. align-items: center;
    9. justify-content: center;
    10. height: 100%;
    11. width: 100%;
    12. border-radius: 50%;
    13. overflow: hidden;
    14. z-index: 20;
    15. }
    16. .noise_5 .isolate {
    17. isolation: isolate;
    18. position: absolute;
    19. top: 0;
    20. height: 100%;
    21. width: 100%;
    22. }
    23. .noise_5 .ball-shadow {
    24. height: 100%;
    25. background: radial-gradient(circle at 65% 35%, rgba(0, 0, 0, 0), mediumblue), url(#noise);
    26. filter: contrast(120%) brightness(900%);
    27. }
    28. .noise_5 .ground {
    29. position: absolute;
    30. width: 150%;
    31. height: 140px;
    32. bottom: -10px;
    33. left: -65%;
    34. transform: rotateZ(7deg);
    35. mix-blend-mode: multiply;
    36. }
    37. .noise_5 .ground-shadow {
    38. width: 95%;
    39. height: 140px;
    40. border-radius: 50%;
    41. background: radial-gradient(ellipse at 70%, navy, rgba(0, 0, 0, 0)), url(#noise);
    42. filter: contrast(150%) brightness(700%);
    43. }
    44. .noise_5 .ball-light {
    45. position: absolute;
    46. top: 0;
    47. width: 100%;
    48. height: 100%;
    49. mix-blend-mode: multiply;
    50. background: radial-gradient(circle at 67% 30%, #ccc, var(--black));
    51. }
    52. style>

    类型5 🌕

    其他 3D 立体形状,包括球体、布丁 🍮、水滴 💧、花瓶 🏺、鸡蛋 🥚、红细胞 💉等,🖱 鼠标悬浮到元素上面可以看到三维旋转效果。

    1. <div class="item item--sphere item--color1"></div>
    2. <div class="item item--pudding item--color5"></div>
    3. <div class="item item--droplet item--color6"></div>
    4. <div class="item item--vase item--color7"></div>
    5. <div class="item item--egg item--color8"></div>
    6. <div class="item item--cell"></div>
    1. .item {
    2. background-color: #ffecd2;
    3. box-shadow: -5px 18px 25px -15px;
    4. overflow: hidden;
    5. transition: all 0.2s ease-out;
    6. image-rendering: pixelated;
    7. }
    8. .item::before {
    9. content: "";
    10. position: absolute;
    11. display: block;
    12. top: 0;
    13. left: 0;
    14. height: 100%;
    15. width: 400%;
    16. mask: url(#noise), radial-gradient(circle at 50% 0, transparent 5%, #000 80%) 0 0/50% 100%;
    17. }
    18. .item:hover {
    19. transform: scale(1.05);
    20. }
    21. .item:hover.item::before, .item:hover.item::after {
    22. animation: spin-round 3.5s linear infinite;
    23. }
    24. .item:active {
    25. animation: hue-rotate 7s infinite;
    26. }
    27. .item--sphere {
    28. border-radius: 50%;
    29. }
    30. .item--egg {
    31. width: 100.1px;
    32. border-radius: 50% 50% 50% 50%/60% 60% 40% 40%;
    33. }
    34. .item--pudding {
    35. width: 130px;
    36. height: 130px;
    37. border-radius: 50% 50% 10% 10%;
    38. }
    39. .item--pudding::after {
    40. content: "";
    41. position: absolute;
    42. top: 0;
    43. display: block;
    44. top: 0;
    45. left: 0;
    46. height: 100%;
    47. width: 400%;
    48. mask: url(#noise), radial-gradient(circle at 50% 0, transparent 55%, #000 85%) 0 0/50% 100%;
    49. }
    50. .item--droplet {
    51. width: 100.1px;
    52. height: 100.1px;
    53. border-radius: 5% 100% 50% 65%/5% 65% 50% 100%;
    54. transform: rotate(45deg);
    55. box-shadow: 5px 18px 25px -15px;
    56. }
    57. .item--droplet:hover {
    58. transform: rotate(45deg) scale(1.05);
    59. }
    60. .item--vase {
    61. position: relative;
    62. width: 80px;
    63. height: 130px;
    64. border-radius: 40% 40% 70% 70%/100%;
    65. }
    66. .item--vase::after {
    67. content: "";
    68. position: absolute;
    69. top: 0;
    70. display: block;
    71. top: 0;
    72. left: 0;
    73. height: 100%;
    74. width: 400%;
    75. mask: url(#noise), radial-gradient(circle at 50% 0, transparent 55%, #000 90%) 0 0/50% 100%;
    76. }
    77. .item--cell {
    78. width: 130px;
    79. height: 130px;
    80. border-radius: 50%;
    81. color: #ff4d5d;
    82. background: radial-gradient(circle at 100% 0, #ffa878 5%, transparent 75%), #ff4d5d;
    83. }
    84. .item--cell::before {
    85. top: 50%;
    86. left: 50%;
    87. transform: translate(-50%, -50%);
    88. width: 90px;
    89. height: 90px;
    90. border-radius: 50%;
    91. background: radial-gradient(circle at 0 75%, transparent 20%, #98000e 65%);
    92. mask: url(#noise), radial-gradient(circle at 50%, transparent 25%, #000 75%);
    93. }
    94. .item--cell:hover {
    95. animation: spin-flat-thick 3.5s linear infinite;
    96. }
    97. .item--cell:hover.item--cell::before {
    98. animation: spin-flat 3.5s linear infinite;
    99. }
    100. .item--cell:hover:active {
    101. animation: spin-flat-thick 3.5s linear infinite, hue-rotate 7s linear infinite;
    102. }
    103. .item--color1 {
    104. color: #7f5fff;
    105. }
    106. .item--color1::before {
    107. background: repeat-x 100% 0/50% 100% radial-gradient(circle at 50% 0, transparent 25%, #7f5fff 65%);
    108. }
    109. .item--color5 {
    110. color: #3d1635;
    111. background-color: #ffecd2;
    112. }
    113. .item--color5::before {
    114. background: repeat-x 100% 0/50% 100% radial-gradient(circle at 50% 0, transparent 15%, #e7627d 45%);
    115. }
    116. .item--color5::after {
    117. background: repeat-x 100% 0/50% 100% radial-gradient(circle at 50% 0, transparent 50%, #3d1635 75%);
    118. }
    119. .item--color6 {
    120. background-color: #72f9c1;
    121. color: #5193ff;
    122. }
    123. .item--color6::before {
    124. background: repeat-x 100% 0/50% 100% radial-gradient(circle at 50% 0, transparent 25%, #5193ff 65%);
    125. }
    126. .item--color7 {
    127. color: #5e21d6;
    128. background-color: #fec440;
    129. }
    130. .item--color7::before {
    131. background: repeat-x 100% 0/50% 100% radial-gradient(circle at 50% 0, transparent 15%, #fd2472 45%);
    132. }
    133. .item--color7::after {
    134. background: repeat-x 100% 0/50% 100% radial-gradient(circle at 50% 0, transparent 50%, #5e21d6 75%);
    135. }
    136. .item--color8 {
    137. color: #222;
    138. }
    139. .item--color8::before {
    140. background: repeat-x 100% 0/50% 100% radial-gradient(circle at 50% 0, transparent 25%, #222 65%);
    141. }

    类型6 🌝

    多个噪点元素地叠加可以生成非常惊艳的效果,比如通过将三个噪点元素层叠在一起可以实现光学三原色 RGB 效果。

    1. <div class="noise_6">
    2. <div class="circle noise-1"></div>
    3. <div class="circle noise-2"></div>
    4. <div class="circle noise-3"></div>
    5. </div>
    1. .noise_6 .circle {
    2. filter: contrast(145%) brightness(650%) invert(100%);
    3. mix-blend-mode: screen;
    4. }
    5. .noise_6 .noise-1 {
    6. position: absolute;
    7. top: 0;
    8. left: -10%;
    9. background: radial-gradient(circle at 50% 50%, rgba(0,255,255,1), rgba(0,0,0,0)), url(#noise);
    10. }
    11. .noise_6 .noise-2 {
    12. position: absolute;
    13. left: 10%;
    14. top: -30px;
    15. background: radial-gradient(circle at 50% 50%, rgba(255,0,255,1), rgba(0,0,0,0)), url(#noise);
    16. }
    17. .noise_6 .noise-3 {
    18. position: absolute;
    19. top: 50px;
    20. left: 4%;
    21. background: radial-gradient(circle at 50% 50%, rgba(255,255,0,1), rgba(0,0,0,0)), url(#noise);
    22. }

    类型7 🌙

    利用噪点元素还可以实现什么呢?中心扩散效果,太阳,涟漪,荷包蛋……

    1. <div class="noise_7">
    2. <div class="noise"></div>
    3. <div class="center">
    4. <div class="circle">
    5. </div>
    6. </div>
    7. </div>
    1. <style>
    2. .noise_7 {
    3. position: relative;
    4. width: 1000px;
    5. height: 1000px;
    6. margin: 0 auto;
    7. }
    8. .noise_7 .noise {
    9. width: 100%;
    10. height: 100%;
    11. background: radial-gradient(circle at 50% 50%, rgba(0, 0, 0, 1), rgba(0, 0, 0, 0)), url(#noise);
    12. filter: contrast(145%) brightness(650%) invert(100%);
    13. mix-blend-mode: screen;
    14. }
    15. .noise_7 .center {
    16. position: absolute;
    17. top: 0;
    18. width: 100%;
    19. height: 100%;
    20. display: flex;
    21. justify-content: center;
    22. align-items: center;
    23. }
    24. .noise_7 .circle {
    25. width: 200px;
    26. height: 200px;
    27. background-color: beige;
    28. border-radius: 50%;
    29. }
    30. style>

    背景噪点 🌜

    如果仔细观察的话,页面黑色背景也应用了噪点效果,是不是有一种黑白电视机的雪花屏复古感 😂

    <div class="bg"></div>
    
    1. .bg {
    2. position: fixed;
    3. top: -50%;
    4. left: -50%;
    5. right: -50%;
    6. bottom: -50%;
    7. width: 200%;
    8. height: 200vh;
    9. background: transparent url(#noise) repeat 0 0;
    10. background-repeat: repeat;
    11. animation: bg-animation .2s infinite;
    12. opacity: .9;
    13. visibility: visible;
    14. }
    15. @keyframes bg-animation {
    16. 0% { transform: translate(0,0) }
    17. 10% { transform: translate(-5%,-5%) }
    18. 20% { transform: translate(-10%,5%) }
    19. 30% { transform: translate(5%,-10%) }
    20. 40% { transform: translate(-5%,15%) }
    21. 50% { transform: translate(-10%,5%) }
    22. 60% { transform: translate(15%,0) }
    23. 70% { transform: translate(0,10%) }
    24. 80% { transform: translate(-15%,0) }
    25. 90% { transform: translate(10%,5%) }
    26. 100% { transform: translate(5%,0) }
    27. }

    📥 完整代码:GitHub - dragonir/noise: noise 噪点效果

    总结

    本文主要包含的知识点包括:

    • CSS 属性 mask 遮罩
    • SVG 滤镜 feTurbulence
    • CSS 属性 filter 滤镜
    • CSS 属性 mix-blend-mode 元素混合
    • CSS 属性 image-rendering 图像缩放
  • 相关阅读:
    BUUCTF Reverse/[QCTF2018]Xman-babymips
    mp4视频太大怎么压缩变小?
    mysql 数据库 期末复习题库
    Java项目:springboot医院管理系统
    Spark系列之Spark的Shuffle详解及相关参数调优
    Vault系列之:创建令牌
    Nginx 配置错误导致漏洞
    众佰诚:抖音店铺开网店前期需要投入多少
    《重磅发布 | 80+大屏&组件模板,即刻下载、快速构建应用!遥遥领先~》
    jdk工具之jstack
  • 原文地址:https://blog.csdn.net/sinat_40572875/article/details/128090513