SVG 入门教程(三) 绘制

原文出处:https://www6.software.ibm.com/developerworks/cn/education/xml/x-svg/tutorial/index.html

 

笔划与填充<nobr>第 1 页(共5 页)</nobr>



整个教程到目前为止,示例已经演示了围绕对象的笔划或线以及对象内部区域的填充。这些属性实际上还有子属性,也可以设置子属性来创建不同的效果。这些属性包括:

  • fill:该属性指定用来填充对象内部区域的颜料。大多数情况下,该属性只是一种颜色,但它也可以是渐变或图案(会在图案中介绍)。这个值通常是关键字、颜色说明或指向预定义元素的 URI。
  • fill-opacity:该属性指定元素的透明性。值的范围从完全透明(0)到完全不透明(1)。
  • stroke:该属性指定元素外边框的外观。象 fill 一样,它引用颜料,尽管通常将它指定为一种简单颜色。
  • stroke-width:该属性指定笔划线的宽度。
  • stroke-linecap:该属性确定线末端的形状,可取的值有粗端(缺省值)、圆和正方形。
  • stroke-linejoin:该属性确定对象各角的外观。允许的值有直角(缺省值)、圆和斜角,它如示例中所示将尖角的边缘“剪掉”。
  • stroke-dasharray:该属性是一个整数系列(如 3、2、3、2、4、3、2 和 3),它允许对虚线中每一划的相对长度进行控制。
  • stroke-opacity:类似于 fill-opacity,该属性确定元素笔划线的相对透明性。

您可以在下面看到这些属性的一些示例:

 
xml 代码
  1. xml version="1.0"?>  
  2.   "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">  
  3. <svg width="400" height="200" xmlns="http://www.w3.org/2000/svg"  
  4.                       xmlns:xlink="http://www.w3.org/1999/xlink">  
  5.      
  6.   <desc>Stroke and filldesc>  
  7.   <defs>  
  8.   
  9.      <linearGradient id="lineGradient">  
  10.         <stop offset="0%" stop-color="red" />  
  11.         <stop offset="100%" stop-color="yellow" />  
  12.      linearGradient>  
  13.         
  14.      <polygon id="lens" points="65,50 185,50 185,75, 150,100    
  15.                                           100,100 65,75"   
  16.              fill="pink" stroke="purple" stroke-width="4"  
  17.              fill-opacity=".5"/>  
  18.        
  19. . . .    
  20.   
  21.   defs>  
  22.      
  23.   <g>  
  24.   
  25. . . .   
  26.                     
  27.     <line x1="65" y1="50" x2="310" y2="50"    
  28.                           stroke="plum" stroke-width="2"/>       
  29.         
  30.                              
  31.     <rect x="50" y="125" width="275" height="40" fill="orange"  
  32.            stroke-width="6" stroke="url(#lineGradient)" />  
  33.   
  34.       
  35.     <line x1="65" y1="190" x2="310" y2="190"    
  36.                           stroke="purple" stroke-width="20"  
  37.                           stroke-linecap="round"/>     
  38.                              
  39.                                  
  40.     <polygon points="50,250 100,225 300,225 200,275" stroke="blue"  
  41.           fill="none" stroke-width="10" stroke-linejoin="bevel" />    
  42.               
  43.   g>  
  44. svg>  

 

颜色<nobr>第 2 页(共5 页)</nobr>


颜色对于 SVG 图像是极其重要的。单个颜色可以直接使用它们的 RGB 值指定,或者使用差不多 150 个颜色关键字中的一个来间接指定,该关键字也引用 RGB 值。

RGB 值在 0 到 255 数值范围内指定一种颜色的红、绿、蓝成分的相对亮度。例如:

 
 
xml 代码
  1. xml version="1.0"?>  
  2.   "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">  
  3. <svg width="400" height="200" xmlns="http://www.w3.org/2000/svg">  
  4.      
  5.   <desc>Colorsdesc>  
  6.   <defs>  
  7.   defs>  
  8.      
  9.   <g>  
  10.   
  11.      <text x="20" y="50" font-size="30">Colors can be specifiedtext>  
  12.      <text x="20" y="100" font-size="30">by their   
  13.         <tspan fill="rgb(255,0,0)">Rtspan>  
  14.         <tspan fill="rgb(0,255,0)">Gtspan>  
  15.         <tspan fill="rgb(0,0,255)">Btspan>  
  16.      valuestext>  
  17.      <text x="20" y="150" font-size="30">or by keywords such astext>  
  18.      <text x="20" y="200" font-size="30">  
  19.         <tspan fill="lightsteelblue">lightsteelbluetspan>,   
  20.      text>  
  21.      <text x="20" y="250" font-size="30">  
  22.         <tspan fill="mediumseagreen">mediumseagreentspan>,   
  23.      text>  
  24.      <text x="20" y="300" font-size="30">and   
  25.         <tspan fill="darkorchid">darkorchidtspan>.   
  26.      text>  
  27.   
  28.   g>  
  29. svg>  
  30.   
  31.   
 
 
渐变<nobr>第 3 页(共5 页)</nobr>


正象您在前面的示例中看到的,渐变提供了将颜色混合在一起的能力。渐变有两种。对于每种情况,代码都指定沿着渐变向量的颜色“停止”或颜色点,渐变到这些点就成为某种颜色。例如,指定红色在 0% 停止,白色在 50% 停止而蓝色在 100% 停止的渐变将逐渐由红色变为白色再变为蓝色,白色在渐变向量的中心。

可以推断或者直接指定渐变向量。以线性渐变为例,假设它从要填充区域的左缘开始到右缘结束。可以用 x1、y1、x2 和 y2 属性更改这一向量。也可以(依照)使用 gradientTransform 属性对这一向量进行变换。

以放射性渐变为例,渐变基于一个圆,可以用 cxcyr 属性调整外部圆(渐变向量终止的地方)的圆心和半径。可以使用 fxfy 属性调整焦点(渐变向量起始的地方)。

考虑下面这些线性和放射性渐变的示例:

 
 
xml 代码
  1. xml version="1.0"?>  
  2.   "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">  
  3. <svg width="400" height="200" xmlns="http://www.w3.org/2000/svg">  
  4.      
  5.   <desc>Colorsdesc>  
  6.   <defs>  
  7.      <linearGradient id="linear1">  
  8.         <stop offset="0%" stop-color="red"/>  
  9.         <stop offset="50%" stop-color="white"/>  
  10.         <stop offset="100%" stop-color="blue"/>  
  11.      linearGradient>  
  12.      <linearGradient id="linear2" x1="100%" y1="0%" x2="0%"    
  13.                                     y2="100%">  
  14. . . .   
  15.      linearGradient>  
  16.      <linearGradient id="linear3" gradientTransform="rotate(90)">  
  17. . . .   
  18.      linearGradient>  
  19.      <radialGradient id="radial1">  
  20. . . .   
  21.      radialGradient>  
  22.      <radialGradient id="radial2" fx="225" fy="225">  
  23.   
  24. . . .   
  25.      radialGradient>  
  26.      <radialGradient id="radial3" cx="25%" cy="25%" r="75%">  
  27. . . .   
  28.      radialGradient>  
  29.      
  30.   defs>  
  31.      
  32.   <g>  
  33.   
  34.        
  35.      <rect x="10" y="10" height="100" width="100" stroke="black"    
  36.                                     fill="url(#linear1)"/>  
  37.      <rect x="125" y="10" height="100" width="100" stroke="black"     
  38.                                     fill="url(#linear2)"/>  
  39.      <rect x="240" y="10" height="100" width="100" stroke="black"     
  40.                                     fill="url(#linear3)"/>  
  41.   
  42.        
  43.      <rect x="10" y="125" height="100" width="100" stroke="black"    
  44.                                     fill="url(#radial1)"/>  
  45.      <rect x="125" y="125" height="100" width="100" stroke="black"    
  46.                                     fill="url(#radial2)"/>  
  47.      <rect x="240" y="125" height="100" width="100" stroke="black"    
  48.                                     fill="url(#radial3)"/>  
  49.   
  50.   g>  
  51. svg>  

 

图案<nobr>第 4 页(共5 页)</nobr>


用图案填充对象在很多地方与用渐变填充类似。两种情况下,都定义填充然后从填充属性内部调用它。

定义图案与定义任何其它作为 SVG 图像一部分出现的对象相似。它有位置、高度和宽度,通常还有一个或多个包含的对象。位置是基于整个文档还是基于正在被填充的对象由 patternUnits 属性确定,该属性可以设置为 objectBoundingBoxuserSpaceOnUse;这些属性分别设置基于对象和文档的坐标。与渐变相似,可以变换图案(使用 patternTransform 属性)。

请考虑下面的示例:

 
  
xml 代码
  1. xml version="1.0"?>  
  2.   "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">  
  3. <svg width="400" height="200" xmlns="http://www.w3.org/2000/svg">  
  4.      
  5.   <desc>Patternsdesc>  
  6.   <defs>  
  7.       <pattern id="notes" x="0" y="0" width="50" height="75"    
  8.                    patternTransform="rotate(15)"    
  9.                    patternUnits="userSpaceOnUse">  
  10.             
  11.          <ellipse cx="10" cy="30" rx="10" ry="5"/>  
  12.          <line x1="20" y1="30" x2="20" y2="0"    
  13.                    stroke-width="3" stroke="black"/>  
  14.          <line x1="20" y1="0" x2="30" y2="5"    
  15.                    stroke-width="3" stroke="black"/>  
  16.          
  17.       pattern>  
  18.   defs>  
  19.      
  20.   <g>  
  21.      <ellipse cx="175" cy="100" rx="125" ry="60"    
  22.            fill="url(#notes)" stroke="black" stroke-width="5"/>  
  23.   g>  
  24. svg>  

 

滤镜<nobr>第 5 页(共5 页)</nobr>


也许 SVG 最强大的功能之一就是给图像添加了滤镜效果。这些效果复制了昂贵的图形操作程序中的许多效果,如光照效果和高斯模糊。对这些滤镜的完整讨论超出了本教程的范围,不过本页讨论了一些基本滤镜。

对 SVG 图像的滤镜操作包括创建一系列滤镜原语操作,该系列中每个原语操作都有自己的目的。例如,偏移滤镜按指定信息将源图像左移或右移以及上移或下移。高斯模糊原语操作按要求对源图像进行模糊处理。

源图像不必一定是实际的 SVG 图像。例如,它可以是前一个原语操作的结果。下面的代码将几个滤镜应用到前一页中显示的图案。

 
   
xml 代码
  1. xml version="1.0"?>  
  2.   "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">  
  3. <svg width="400" height="200" xmlns="http://www.w3.org/2000/svg"  
  4.                       xmlns:xlink="http://www.w3.org/1999/xlink">  
  5.      
  6.   <desc>Filtersdesc>  
  7.   <defs>  
  8.      
  9.       <filter id="dropShadow" filterUnits="userSpaceOnUse"    
  10.                       x="0" y="0" width="400" height="200">  
  11.          <feOffset in="SourceAlpha" dx="5" dy="5" result="offset"/>  
  12.          <feGaussianBlur in="offset" stdDeviation="5" result="blur"/>  
  13.          <feMerge>  
  14.            <feMergeNode in="blur"/>  
  15.            <feMergeNode in="SourceGraphic"/>  
  16.          feMerge>    
  17.       filter>  
  18.   
  19. . . .   
  20.   defs>  
  21.      
  22.   <g>  
  23.      <ellipse filter="url(#dropShadow)" cx="175" cy="100"  
  24.            rx="125" ry="60"    
  25.            fill="url(#notes)" stroke="black" stroke-width="5"/>  
  26.   g>  
  27. svg>  

首先,偏移滤镜将原始椭圆及其图案的 alpha 通道作为源(使用 in 属性)。alpha 通道由与图像中每个非白色像素对应的黑色像素组成,并且用 SourceAlpha 关键字指定。偏移原语操作完成处理然后将结果输出到一个由 result 属性指定的缓冲区(本例中的缓冲区名为 offset )。

接下来,模糊原语操作接任。它将 in 参数指定的 offset 缓冲区的内容作为源。然后,它将其结果输出到 result 属性指定的名为 blur 的缓冲区。

这时,滤镜仅由经过偏移和模糊的图像组成。如果滤镜操作到此为止,那么页面上只出现模糊处理的图像。合并原语操作取得 blur 缓冲区的内容然后将它与原始源图形合并,正如当 in 属性引用 SourceGraphic 关键字时所指定的那样。

所有处理的结果是一幅具有阴影效果的原始图像:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值