[关闭]
@Gizmosir 2016-03-19T04:31:44.000000Z 字数 12660 阅读 773

date: 2016-03-
categories: Computer graphics
tag: [SVG, 矢量图]
博客

title: 矢量图像实战——制作个性名片

前言

这是矢量图像的第四篇,如果你没有看过之前的文章,可以点击:

在这一篇中我们将来通过学习制作个性名片的方式来实战练习之前介绍过的SVG知识。首先来欣赏一些很棒的SVG个性名片。

要求

实际上个性名片可以天马行空的随意运用之前介绍过的任意一种滤镜或少数几种。但是为了能够更好地综合运用SVG知识,这里对我们最后的名片效果有几个要求:

  1. 名片尺寸满足长宽比为1.667,比如1000 * 600;
  2. 个人照头像(我们举例中的名片均有头像),并使用卷积矩阵滤镜对头像图片进行一定处理;
  3. 文本信息包含但不仅限于姓名和邮件;
  4. 名片有阴影效果;
  5. 使用一个或多个渐变滤镜;
  6. 添加网页链接;

流程

背景及阴影

首先我们来做比较简单的部分:名片背景以及阴影。首先生成一个1000*600的黑色矩形:

  1. <!--这里我使用黑色填充是因为名片的基色调为黑色。-->
  2. <rect x="0" y="0"
  3. width="1000" height="600"
  4. style="fill:black" />

接着用feGaussianBlur滤镜来生成名片的阴影:

  1. <!--首先生成一个高斯模糊滤镜,并模糊黑色的矩形输入,将其偏
  2. 移到后下角,接着调整其颜色使阴影变成灰色。-->
  3. <svg
  4. xmlns="http://www.w3.org/2000/svg"
  5. xmlns:xlink="http://www.w3.org/1999/xlink" >
  6. <defs>
  7. <filter id="shadow"
  8. color-interpolation-filters = "sRGB">
  9. <feGaussianBlur in = "SourceGraphic" stdDeviation = "10"/>
  10. <feOffset dx = "20" dy = "20" /> <!-- result = "blur" in = "blur" -->
  11. <feColorMatrix type="matrix" result = "offsetBlur"
  12. values="0 0 0 0 0.5
  13. 0 0 0 0 0.5
  14. 0 0 0 0 0.5
  15. 0 0 0 1 0" />
  16. </filter>
  17. </defs>
  18. <rect x="100" y="100"
  19. width="1000" height="600"
  20. style="filter:url(#shadow); " transform = "translate(500, 200) scale(0.8, 0.8) skewX(-20)"/>
  21. <rect x="100" y="100"
  22. width="1000" height="600"
  23. style="fill:black;"/>
  24. </svg>

名片当然最好能够加上所在公司的logo,可是一般的方法又直接降低了我们名片的逼格。一个很好的方法就是用feConvolveMatrix滤镜来提取logo的边缘,然后使用高斯滤镜模糊一点,最后设置合适的透明度并添加到背景矩形上。

  1. <filter id="logo"
  2. color-interpolation-filters="sRGB">
  3. <feImage xlink:href="http://7xjz11.com1.z0.glb.clouddn.com/logo.png?imageView/2/w/200/h/147" width="200" height="147" />
  4. <feConvolveMatrix
  5. kernelMatrix="1 1 1 1 -8 1 1 1 1"
  6. order="3 3"
  7. divisor="1"
  8. bias="0"
  9. preserveAlpha="true"/>
  10. <feGaussianBlur stdDeviation="3"/>
  11. <feColorMatrix type="luminanceToAlpha"/>
  12. <feTile x="100" y="100" width="1000" height="600"/>
  13. </filter>

文本信息

接下来我们来添加文本信息到名片上。为了延续我们名片高大上的格调,我们使用渐变字体并且给名字加上阴影。

  1. <filter id="text"
  2. color-interpolation-filters="sRGB">
  3. <feGaussianBlur in="SourceAlpha" stdDeviation="2"/>
  4. <feOffset dx="5" dy="5" result="text_2"/>
  5. <feBlend mode="normal" in="SourceGraphic" in2="text_2"/>
  6. </filter>
  7. <g fill="url(#lineargradient2)">
  8. <text id="nameText" x="540" y="260" style="font-size:60;font-family:verdana;font-weight:bold;filter:url(#text)">Zheng Jiongbin</text>
  9. <text x="600" y="380" style="font-size:32;font-family:arial;font-style:italic;" >
  10. <tspan x="600" >Nephovision Co.,Ltd</tspan>
  11. <tspan x="600" dy="60">iOS/Algorithm Engineer</tspan>
  12. <tspan x="600" dy="40">Gizmosir@Nephovision.com</tspan>
  13. </text>
  14. </g>

个人头像

首先把个人头像从照片上“扣”下来。主要有三种不同的做法,第一种是用裁剪路径。

如果你不想手动一个一个算裁剪路径点的话,这里推荐使用一款叫SVGEdit的能够轻松在网页上编辑SVG的工具。只需将你的图片插入到网页上,然后点点点就能轻松生成裁剪路径,然后再把路径粘贴到你的代码中。

  1. <!--首先定义我在网页上生成的剪裁路径,接着读取图片,并声明一个色彩矩阵用于将头像外的背景透明化,然后将色彩矩阵与头像复合并应用到一个黑色矩形上。-->
  2. <svg
  3. xmlns="http://www.w3.org/2000/svg"
  4. xmlns:xlink="http://www.w3.org/1999/xlink"
  5. width="800"
  6. height="800" >
  7. <defs>
  8. <path fill="white" id="path_area" d="m332,176l-118.66667,85.33333l20,92l-18.66667,6.66667l20,89.33333l20,1.33333l22.66667,69.33333l56,44l49.33333,2.66667l66.66667,-56l16,-56l21.33333,-5.33333l20,-73.33333l-13.33333,-16l17.33333,-82.66667l-52,-77.33333l-126.66667,-24z"/>
  9. <filter id="photo_with_background"
  10. color-interpolation-filters="sRGB">
  11. <feImage x="-2%" y="7%" width="70%" height="70%" xlink:href="me.jpg" result="photo"/>
  12. <feImage xlink:href="#path_area" result="gradient"/>
  13. <feColorMatrix type="luminanceToAlpha" in="gradient" result="alpha"/>
  14. <feComposite in="photo" in2="alpha" operator="in" result="face_circle"/>
  15. <feFlood x="0" y="0" width="800" height="800" flood-color="black" result="black_area"/>
  16. <feMerge>
  17. <feMergeNode in="black_area"/>
  18. <feMergeNode in="face_circle"/>
  19. </feMerge>
  20. </filter>
  21. </defs>
  22. <rect x="0" y="0" width="1000" height="600"
  23. style="filter:url(#photo_with_background)"/>
  24. </svg>

第二种是使用径向渐变滤镜。

  1. <!--首先定义了一个矩形的径向渐变,接着将其作为图像进行色彩矩阵将渐变圈外的背景变透明,然后再跟头像进行复合并生成最后的效果。-->
  2. <svg
  3. xmlns="http://www.w3.org/2000/svg"
  4. xmlns:xlink="http://www.w3.org/1999/xlink"
  5. width="800"
  6. height="800" >
  7. <defs>
  8. <radialGradient id="face_gradient">
  9. <stop offset="0" style= "stop-color:white"/>
  10. <stop offset=".55" style= "stop-color:white"/>
  11. <stop offset=".60" style= "stop-color:grey"/>
  12. <stop offset=".63" style= "stop-color:black"/>
  13. </radialGradient>
  14. <rect width="800" height="800" fill="url(#face_gradient)" id="gradient_area" transform="translate(-40, -120) scale(1.4, 1.6)"/>
  15. <filter id="photo_with_background"
  16. color-interpolation-filters="sRGB">
  17. <feFlood x="0" y="0" width="800" height="800" flood-color="black" result="black_area"/>
  18. <feImage x="1.0%" y="0%" width="80%" height="80%" xlink:href="me.jpg" result="photo"/>
  19. <feImage xlink:href="#gradient_area" result="gradient"/>
  20. <feColorMatrix type="luminanceToAlpha" in="gradient" result="alpha"/>
  21. <feComposite in="photo" in2="alpha" operator="in" result="face_circle"/>
  22. <feMerge>
  23. <feMergeNode in="black_area"/>
  24. <feMergeNode in="face_circle"/>
  25. </feMerge>
  26. </filter>
  27. </defs>
  28. <rect x="0" y="0" width="800" height="800"
  29. filter="url(#photo_with_background)"/>
  30. </svg>

不难发现这种方法比第一种效果好很多,但是由于我这里使用的头像背景是白色,名片是黑色,直接使用这种方法效果可能也不是特别好。

头像效果处理

接下来对头像进行一定的处理,首先来试试看模糊效果的卷积矩阵滤镜。

  1. <feConvolveMatrix
  2. result="face"
  3. kernelMatrix=".11 .11 .11 .11 .11 .11 .11 .11 .11"
  4. order="3 3"
  5. divisor="1"
  6. bias="0"
  7. preserveAlpha="true" />

效果不太好,由于模糊了看上去就跟丢了神一样。边缘效果肯定是不可行的,毕竟还是要看到头像的嘛,那来试下锐化好了。

  1. <feConvolveMatrix
  2. result="face"
  3. kernelMatrix="-1 -1 -1 -1 9 -1 -1 -1 -1"
  4. order="3 3"
  5. divisor="1"
  6. bias="0"
  7. preserveAlpha="true" />

不错不错,看上去很精神!有点名片应该有点样子。接下来用色彩矩阵滤镜调整颜色,降低饱和度让头像看上去“冷峻”一点。

  1. <feColorMatrix type="saturate" values="0.4" result="face" />

接下来用渐变滤镜来给脸部添加一些阴影。

  1. <linearGradient id="face_gradient1">
  2. <stop offset= "0.4" style= "stop-color:white"/>
  3. <stop offset="0.9" style= "stop-color:black"/>
  4. </linearGradient>
  5. <rect width="350" height="450" fill="url(#face_gradient1)" id="gradient_area1" opacity="0.95"/>
  6. <feImage xlink:href="#gradient_area1" result="gradient1"/>
  7. <feBlend mode="multiply" in="face" in2="gradient1" result="faceOut" />

不错有点好莱坞大片即视感。

接下来给我头像添加光照。首先尝试SpecularLighting+PointLight的组合。

  1. <feSpecularLighting result="specular_light" in="faceMid" specularExponent="200" lighting-color="lightgrey">
  2. <fePointLight x="250" y="200" z="200"/>
  3. </feSpecularLighting>

效果不是特别好,原因可能是PhotoLigh光源太集中。再尝试下DiffuseLighting+SpotLight的组合。

  1. <feDiffuseLighting result="diffuse_light" in="SourceGraphic"
  2. diffuseConstant="100" lighting-color="white" specularExponent="100" >
  3. <feSpotLight x = "-100" y = "-100" z = "400" pointsAtX = "250" pointsAtY = "250" pointsAtZ = "0" limitingConeAngle = "7"/>
  4. </feDiffuseLighting>

发现效果更差!主要原因是spotlight需要调整六个参数来改变灯光的效果,非常考验经验与耐心。所以我们还是试下DiffuseLighting+Distanctlight的组合吧。

  1. <feDiffuseLighting result="diffuse_light" in="faceMid"
  2. diffuseConstant="100" lighting-color="lightgrey" specularExponent="100">
  3. <feDistantLight azimuth = "45" elevation = "135"/>
  4. </feDiffuseLighting>

效果同样不是特别好,看来Lighting不是个特别好的滤镜效果。那我们来试下渐变滤镜好了。

  1. <feImage xlink:href="#face_gradient2" />
  2. <feColorMatrix type="luminanceToAlpha" result="face_4"/>
  3. <feFlood width="300" height="300" flood-color="white" result="face_5"/>
  4. <feComposite in="face_5" in2="face_4" result="face_6" operator="in" />

好像稍微好点,最后来尝试下比较复杂的方法:首先生成一个渐变滤镜,然后转换成透明度,再基于透明度来修改原图像的色彩值,最后在还原透明度。

  1. <feImage xlink:href="#face_gradient2" />
  2. <feColorMatrix type="luminanceToAlpha" result="face_4"/>
  3. <feComposite in="face_1" in2="face_4" result="face_6" operator="in" />
  4. <feColorMatrix type="matrix" in="face_7" result="face_17"
  5. values="1 0 0 1 0
  6. 0 1 0 1 0
  7. 0 0 1 1 0
  8. 0 0 0 1 0 " />
  9. <feMerge result="face_27">
  10. <feMergeNode in="face_3"/>
  11. <feMergeNode in="face_17"/>
  12. </feMerge>

效果看上去是不是很棒?恩恩,因为不是添加的光源,而是基于图像本身进行的亮度调整,所以看上去更加真实,那就决定使用这种方法吧。

个人链接(Optional)

本来名片设计到这里就全部完成了,但是由于我们是使用SVG生成的名片。也就是说大多数情况下别人都会在网页上打开你的名片,那么为何不尝试在名片上添加个链接让对你感兴趣的人可以进一步了解你呢?

这里为了增加别人点击链接的欲望,我往在链接的文本上添加了一个动画。

  1. <linearGradient id="lineargradient3">
  2. <stop offset= "0" style= "stop-color:black"/>
  3. <stop offset=".5" style= "stop-color:white"/>
  4. <stop offset="1.0" style= "stop-color:black"/>
  5. </linearGradient>
  6. <filter id="link"
  7. color-interpolation-filters="sRGB">
  8. <feImage xlink:href="#link_gradient" result="link_1"/>
  9. <feComposite in="SourceGraphic" in2="link_1" operator="in"/>
  10. </filter>
  11. <a xlink:href="http://gizmosir.com" >
  12. <text x="800" y="650" style="font-size:40;font-family:courier;font-style:oblique;font-weight:bold;filter:url(#link)">Visit More</text>
  13. </a>

总结

在几乎把所有的滤镜都尝试了也应用之后,我们的名片终于生成了,其最终效果如下。

或者你可以点击这里

另外需要注意的是,SVG在不同的浏览器以及不同的版本上效果大相径庭。我是就要Chrome 49.0.2623.87版本生成的这张名片,所以建议你复制链接到chrome浏览器上查看。

附录:源代码

  1. <svg
  2. xmlns="http://www.w3.org/2000/svg"
  3. xmlns:xlink="http://www.w3.org/1999/xlink" >
  4. <defs>
  5. <!--Gradient definition-->
  6. <radialGradient id="radialGradient1">
  7. <stop offset="0" style= "stop-color:white"/>
  8. <stop offset=".55" style= "stop-color:white"/>
  9. <stop offset=".60" style= "stop-color:grey"/>
  10. <stop offset=".63" style= "stop-color:black"/>
  11. </radialGradient>
  12. <radialGradient id="radialGradient2">
  13. <stop offset="0" style= "stop-color:#999999"/>
  14. <stop offset=".1" style= "stop-color:#888888"/>
  15. <stop offset=".3" style= "stop-color:#666666"/>
  16. <stop offset=".5" style= "stop-color:#444444"/>
  17. <stop offset=".7" style= "stop-color:#222222"/>
  18. <stop offset=".9" style= "stop-color:#111111"/>
  19. <stop offset="1" style= "stop-color:black"/>
  20. </radialGradient>
  21. <linearGradient id="lineargradient1">
  22. <stop offset= "0.4" style= "stop-color:white"/>
  23. <stop offset="0.9" style= "stop-color:black"/>
  24. </linearGradient>
  25. <linearGradient id="lineargradient2" x1="100%" y1="0%" x2="100%" y2="100%">
  26. <stop offset= "0.4" style= "stop-color:#cccccc"/>
  27. <stop offset="0.8" style= "stop-color:gray"/>
  28. </linearGradient>
  29. <linearGradient id="lineargradient3">
  30. <stop offset= "0" style= "stop-color:black"/>
  31. <stop offset=".5" style= "stop-color:white"/>
  32. <stop offset="1.0" style= "stop-color:black"/>
  33. </linearGradient>
  34. <!--Rectangle definition-->
  35. <rect id="face_area" width="350" height="450" fill="url(#radialGradient1)"
  36. transform="translate(-40, -120) scale(1.4, 1.6)"/>
  37. <rect id="link_gradient" width="400" height="400" fill="url(#lineargradient3)">
  38. <!--Text animation definition-->
  39. <animate attributeName="x"
  40. begin="0s" dur="2s"
  41. values="-400;0" repeatCount="indefinite"/>
  42. </rect>
  43. <rect id="face_gradient" width="400" height="500" fill="url(#lineargradient1)" opacity="0.95"/>
  44. <rect id="face_gradient2" width="350" height="350" fill="url(#radialGradient2)" opacity="0.5" transform="translate(-30, -40) scale(1.1, 1.1)"/>
  45. <!--Shadow filter definition-->
  46. <filter id="shadow"
  47. color-interpolation-filters="sRGB">
  48. <feGaussianBlur in="SourceGraphic" stdDeviation="10"/>
  49. <feOffset dx="20" dy="20" />
  50. <feColorMatrix type="matrix"
  51. values="0 0 0 0 0.5
  52. 0 0 0 0 0.5
  53. 0 0 0 0 0.5
  54. 0 0 0 1 0" />
  55. </filter>
  56. <!--Company logo filter definition-->
  57. <filter id="logo"
  58. color-interpolation-filters="sRGB">
  59. <feImage xlink:href="http://7xjz11.com1.z0.glb.clouddn.com/logo.png?imageView/2/w/200/h/147" width="200" height="147" />
  60. <feConvolveMatrix
  61. kernelMatrix="1 1 1 1 -8 1 1 1 1"
  62. order="3 3"
  63. divisor="1"
  64. bias="0"
  65. preserveAlpha="true"/>
  66. <feGaussianBlur stdDeviation="3"/>
  67. <feColorMatrix type="luminanceToAlpha"/>
  68. <feTile x="100" y="100" width="1000" height="600"/>
  69. </filter>
  70. <!--Text filter definition-->
  71. <filter id="text"
  72. color-interpolation-filters="sRGB">
  73. <feGaussianBlur in="SourceAlpha" stdDeviation="2"/>
  74. <feOffset dx="5" dy="5" result="text_2"/>
  75. <feBlend mode="normal" in="SourceGraphic" in2="text_2"/>
  76. </filter>
  77. <!--Link animition definition-->
  78. <filter id="link"
  79. color-interpolation-filters="sRGB">
  80. <feImage xlink:href="#link_gradient" result="link_1"/>
  81. <feComposite in="SourceGraphic" in2="link_1" operator="in"/>
  82. </filter>
  83. <!--Face filter definition-->
  84. <filter id="face"
  85. color-interpolation-filters="sRGB">
  86. <feImage xlink:href="http://7xjz11.com1.z0.glb.clouddn.com/me1.jpg" />
  87. <!--Sharpen and desaturate the face to make it look smarter-->
  88. <feConvolveMatrix
  89. kernelMatrix="-1 -1 -1 -1 9 -1 -1 -1 -1 "
  90. order="3 3"
  91. divisor="1"
  92. bias="0"
  93. preserveAlpha="true" />
  94. <feColorMatrix type="saturate" values="0.4" result="face_1"/>
  95. <!--Using a linear gradient to increase face layered-->
  96. <feImage xlink:href="#face_gradient" result="face_2"/>
  97. <feBlend mode="multiply" in="face_1" in2="face_2" result="face_3" />
  98. <feImage xlink:href="#face_gradient2" />
  99. <feColorMatrix type="luminanceToAlpha" result="face_4"/>
  100. <feComposite in="face_1" in2="face_4" result="face_6" operator="in" />
  101. <feColorMatrix type="matrix" in="face_7" result="face_17"
  102. values="1 0 0 1 0
  103. 0 1 0 1 0
  104. 0 0 1 1 0
  105. 0 0 0 1 0 " />
  106. <feMerge result="face_27">
  107. <feMergeNode in="face_3"/>
  108. <feMergeNode in="face_17"/>
  109. </feMerge>
  110. <!--Using an ellipse gradient to extract the face out-->
  111. <feImage xlink:href="#face_area" />
  112. <feColorMatrix type="luminanceToAlpha" result="face_8"/>
  113. <feComposite in="face_27" in2="face_8" operator="in"/>
  114. </filter>
  115. </defs>
  116. <rect x="100" y="100"
  117. width="1000" height="600"
  118. style="filter:url(#shadow); " transform="translate(500, 200) scale(0.8, 0.8) skewX(-20)"/>
  119. <rect x="100" y="100"
  120. width="1000" height="600"
  121. style="fill:#222222;"/> <!--Not pure black will look more actual-->
  122. <rect x="100" y="100"
  123. width="1000" height="600"
  124. style="filter:url(#logo);"/>
  125. <rect x="127" y="177" width="350" height="450"
  126. style="filter:url(#face);" transform="translate(80, 100) scale(0.8, 0.8)"/>
  127. <g fill="url(#lineargradient2)">
  128. <text id="nameText" x="540" y="260" style="font-size:60;font-family:verdana;font-weight:bold;filter:url(#text)">Zheng Jiongbin</text>
  129. <text x="600" y="380" style="font-size:32;font-family:arial;font-style:italic;" >
  130. <tspan x="600" >Nephovision Co.,Ltd</tspan>
  131. <tspan x="600" dy="60">iOS/Algorithm Engineer</tspan>
  132. <tspan x="600" dy="40">Gizmosir@Nephovision.com</tspan>
  133. </text>
  134. <a xlink:href="http://gizmosir.com" >
  135. <text x="800" y="650" style="font-size:40;font-family:courier;font-style:oblique;font-weight:bold;filter:url(#link)">Visit More</text>
  136. </a>
  137. </g>
  138. </svg>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注