HTML5的内联SVG

什么是svg?

1、svg指可伸缩矢量图形

2、svg用于定义用于网络的基于矢量的图形

3、svg使用xml格式定义图形

4、svg图像在放大和改变尺寸的情况下图形质量不会有损失

5、svg是万维网联盟的标准


svg的优势?

与其它图像格式相比(比如jpeg和gif),使用svg的优势在于

1、svg的图像可通过文本编辑器来创建和修改

2、svg图像可被搜索、索引、脚本化或者压缩

3、svg是可伸缩的

4、svg在任何分辨率下都可被高质量的打印

5、svg可在图像质量不下降的情况下被放大


svg和canvas的比较?

canvas

1、依赖分辨率 2、不支持事件处理器 3、弱的文本渲染能力 4、能够以.png和.jpg格式保存图像 5、最适合图像密集型游戏,其中许多对象需要被频繁重绘

svg

1、不依赖分辨率  2、支持事件处理器  3、最适合带有大型渲染区域的应用程序(比如谷歌地图)4、复杂度高会减慢渲染速度(任何过度使用 DOM 的应用都不快) 5、不适合游戏应用


svg语法

1、在html里内联svg

[html]  view plain  copy
  1. <svg  <span class="hl-var" style="border:0px;margin:0px;padding:0px;color:rgb(0,0,139);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">xmlns</span><span class="hl-code" style="border:0px;margin:0px;padding:0px;color:#808080;font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">=</span><span class="hl-quotes" style="border:0px;margin:0px;padding:0px;color:rgb(139,0,0);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">"</span><span class="hl-string" style="border:0px;margin:0px;padding:0px;color:rgb(170,17,17);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">http://www.w3.org/2000/svg</span><span class="hl-quotes" style="border:0px;margin:0px;padding:0px;color:rgb(139,0,0);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">"</span><span class="hl-code" style="border:0px;margin:0px;padding:0px;color:#808080;font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;"> </span><span class="hl-var" style="border:0px;margin:0px;padding:0px;color:rgb(0,0,139);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">version</span><span class="hl-code" style="border:0px;margin:0px;padding:0px;color:#808080;font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">=</span><span class="hl-quotes" style="border:0px;margin:0px;padding:0px;color:rgb(139,0,0);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">"</span><span class="hl-string" style="border:0px;margin:0px;padding:0px;color:rgb(170,17,17);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">1.1</span><span class="hl-quotes" style="border:0px;margin:0px;padding:0px;color:rgb(139,0,0);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">"</span>   width="400" height="600">      </svg>  

2、svg图形写在<defs></defs>里面,再通过 <use xlink:href="#你定义图形的id"/>调用 在<defs></defs>里的图形不显示

3、在svg里定义多个种类图形的时候可以通过<g></g>分类

4、

[html]  view plain  copy
  1. <rect id="tree_g"  x="15" y="120" width="10"  height="50"  />  

x和y是矩形左上角坐标 width和height是矩形的长和宽

 <path d="M 20,20

L 20,30

L 40,10

Z"/>M是起始点  L是线段   这个标签用来自定义图形

<text>你的文本</text>      用来定义文本


svg实例

[html]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset="UTF-8">  
  5.         <title></title>  
  6.         <style>  
  7.             svg  
  8.             {  
  9.                 border: black;  
  10.                 border-style: solid;  
  11.             }  
  12.         </style>  
  13.         <script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js">  
  14.         </script>  
  15.           
  16.           
  17.           
  18.     </head>  
  19.     <body>  
  20.           
  21.         <button id="add" onclick="f()">添加</button>  
  22.         <svg  <span class="hl-var" style="border:0px;margin:0px;padding:0px;color:rgb(0,0,139);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">xmlns</span><span class="hl-code" style="border:0px;margin:0px;padding:0px;color:#808080;font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">=</span><span class="hl-quotes" style="border:0px;margin:0px;padding:0px;color:rgb(139,0,0);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">"</span><span class="hl-string" style="border:0px;margin:0px;padding:0px;color:rgb(170,17,17);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">http://www.w3.org/2000/svg</span><span class="hl-quotes" style="border:0px;margin:0px;padding:0px;color:rgb(139,0,0);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">"</span><span class="hl-code" style="border:0px;margin:0px;padding:0px;color:#808080;font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;"> </span><span class="hl-var" style="border:0px;margin:0px;padding:0px;color:rgb(0,0,139);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">version</span><span class="hl-code" style="border:0px;margin:0px;padding:0px;color:#808080;font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">=</span><span class="hl-quotes" style="border:0px;margin:0px;padding:0px;color:rgb(139,0,0);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">"</span><span class="hl-string" style="border:0px;margin:0px;padding:0px;color:rgb(170,17,17);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">1.1</span><span class="hl-quotes" style="border:0px;margin:0px;padding:0px;color:rgb(139,0,0);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">"</span>   width="400" height="600">  
  23.             <defs>  
  24.               
  25.             <pattern id="ll" patternUnits="userSpaceOnUse" x="0" y="300" width="100" height="27"  
  26.                 vieeBox="0 300 100 67">  
  27.             <image x="0",y="300" width=100 height=67 xlink:href="img/l.jpg"/>  
  28.             </pattern>  
  29.             <g id="tree"> <!--树和树干 -->  
  30.                 <path   
  31.                     d="M 20,30  
  32.                     L 30,60  
  33.                     L 25,60  
  34.                     L 35,90  
  35.                     L 30,90  
  36.                     L 40,120  
  37.                     L 0,120  
  38.                     L 10,90  
  39.                     L 5,90  
  40.                     L 15,60  
  41.                     L 10,60  
  42.                     Z" id="tree_head"   
  43.                     />  
  44.                       
  45.                     <rect id="tree_g"  x="15" y="120" width="10"  height="50"  />  
  46.                 </g>  
  47.                   
  48.                   
  49.                 <g id="treeshadow">  
  50.                     <use xlink:href="#tree_head" fill="grey"/>  
  51.                     <use xlink:href="#tree_g" fill="grey"/>  
  52.                 </g>  
  53.                   
  54.                   
  55.             </defs>  
  56.               
  57.             <g stroke-width="20"  
  58.                 stroke-linejoin="round"  
  59.                 stroke="url(#ll)"  
  60.                 >  
  61.                   <path  d="M0,300 Q150,100 200,200 Q300,300 400,60" fill="none"/>  
  62.             </g>  
  63.               
  64.             <g>  
  65.                 <text x="80" y="100" font-size="40px" font-family="Droid Sans" stroke="#00f" fill="#0ff" font-weight="bold">  
  66.                     Select this text!  
  67.                 </text>  
  68.                   
  69.                 <text id="tc" x="80" y="140" font-size="40px" font-family="Droid Sans" stroke="#00f" fill="#0ff" font-weight="bold"  >  
  70.                 </text>  
  71.                   
  72.             </g>  
  73.             <g id="ctree">  
  74.             <use xlink:href="#tree_head"  stroke=none fill="#339900"/>  
  75.             <use xlink:href="#tree_g"  stroke=none fill="#552200"/>  
  76.             </g>  
  77.             <use xlink:href="#tree_head" transform="translate(200,250) scale(2,2)" stroke=none fill="#339900"/>  
  78.             <use xlink:href="#tree_g" transform="translate(200,250) scale(2,2)" stroke=none fill="#552200"/>  
  79.             <use xlink:href="#treeshadow" transform="translate(60,68) scale(1,0.6) skewX(-18)"/>  
  80.             <use xlink:href="#treeshadow" transform="translate(375,370) scale(1,1.3) skewX(-40)"/>  
  81.               
  82.         </svg>  
  83.           
  84.           
  85.           
  86.         <script>  
  87.               
  88.             function removeTree(e)  
  89.             {  
  90.                 var elt=e.target;  
  91.                 if(elt.correspondingUseElement)  
  92.                 {  
  93.                     elt=elt.correspondingUseElement;  
  94.                 }  
  95.                 elt.parentNode.removeChild(elt);  
  96.                 updateTress();  
  97.             }  
  98.               
  99.             function f(){  
  100.             var x=Math.floor(Math.random() *400);  
  101.             var y=Math.floor(Math.random() *600);  
  102.             var scale=Math.random() + .5;  
  103.             var translate="translate("+x+","+y+")";  
  104.             var tree=document.createElementNS("http://www.w3.org/2000/svg","use");  
  105.             tree.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href","#ctree");  
  106.             tree.setAttribute("transform",translate+"scale("+scale+")");  
  107.             document.querySelector("svg").appendChild(tree);  
  108.             updateTress();    
  109.          }  
  110.               
  111.               
  112.             function updateTress()  
  113.             {  
  114.                 var list=document.querySelectorAll("use");  
  115.                 var treeCount=0;  
  116.                 for(var i=0;i<list.length;i++)  
  117.                 {  
  118.                     if(list[i].getAttribute("xlink:href")=="#ctree")  
  119.                     {  
  120.                         treeCount++;  
  121.                         list[i].onclick=removeTree;  
  122.                     }  
  123.                 }  
  124.                 var co=document.getElementById("tc");  
  125.             co.textContent=treeCount+"tree in the forest";  
  126.           
  127.             }  
  128.             updateTress();  
  129.         </script>  
  130.           
  131.     </body>  
  132. </html>  

什么是svg?

1、svg指可伸缩矢量图形

2、svg用于定义用于网络的基于矢量的图形

3、svg使用xml格式定义图形

4、svg图像在放大和改变尺寸的情况下图形质量不会有损失

5、svg是万维网联盟的标准


svg的优势?

与其它图像格式相比(比如jpeg和gif),使用svg的优势在于

1、svg的图像可通过文本编辑器来创建和修改

2、svg图像可被搜索、索引、脚本化或者压缩

3、svg是可伸缩的

4、svg在任何分辨率下都可被高质量的打印

5、svg可在图像质量不下降的情况下被放大


svg和canvas的比较?

canvas

1、依赖分辨率 2、不支持事件处理器 3、弱的文本渲染能力 4、能够以.png和.jpg格式保存图像 5、最适合图像密集型游戏,其中许多对象需要被频繁重绘

svg

1、不依赖分辨率  2、支持事件处理器  3、最适合带有大型渲染区域的应用程序(比如谷歌地图)4、复杂度高会减慢渲染速度(任何过度使用 DOM 的应用都不快) 5、不适合游戏应用


svg语法

1、在html里内联svg

[html]  view plain  copy
  1. <svg  <span class="hl-var" style="border:0px;margin:0px;padding:0px;color:rgb(0,0,139);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">xmlns</span><span class="hl-code" style="border:0px;margin:0px;padding:0px;color:#808080;font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">=</span><span class="hl-quotes" style="border:0px;margin:0px;padding:0px;color:rgb(139,0,0);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">"</span><span class="hl-string" style="border:0px;margin:0px;padding:0px;color:rgb(170,17,17);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">http://www.w3.org/2000/svg</span><span class="hl-quotes" style="border:0px;margin:0px;padding:0px;color:rgb(139,0,0);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">"</span><span class="hl-code" style="border:0px;margin:0px;padding:0px;color:#808080;font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;"> </span><span class="hl-var" style="border:0px;margin:0px;padding:0px;color:rgb(0,0,139);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">version</span><span class="hl-code" style="border:0px;margin:0px;padding:0px;color:#808080;font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">=</span><span class="hl-quotes" style="border:0px;margin:0px;padding:0px;color:rgb(139,0,0);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">"</span><span class="hl-string" style="border:0px;margin:0px;padding:0px;color:rgb(170,17,17);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">1.1</span><span class="hl-quotes" style="border:0px;margin:0px;padding:0px;color:rgb(139,0,0);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">"</span>   width="400" height="600">      </svg>  

2、svg图形写在<defs></defs>里面,再通过 <use xlink:href="#你定义图形的id"/>调用 在<defs></defs>里的图形不显示

3、在svg里定义多个种类图形的时候可以通过<g></g>分类

4、

[html]  view plain  copy
  1. <rect id="tree_g"  x="15" y="120" width="10"  height="50"  />  

x和y是矩形左上角坐标 width和height是矩形的长和宽

 <path d="M 20,20

L 20,30

L 40,10

Z"/>M是起始点  L是线段   这个标签用来自定义图形

<text>你的文本</text>      用来定义文本


svg实例

[html]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset="UTF-8">  
  5.         <title></title>  
  6.         <style>  
  7.             svg  
  8.             {  
  9.                 border: black;  
  10.                 border-style: solid;  
  11.             }  
  12.         </style>  
  13.         <script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js">  
  14.         </script>  
  15.           
  16.           
  17.           
  18.     </head>  
  19.     <body>  
  20.           
  21.         <button id="add" onclick="f()">添加</button>  
  22.         <svg  <span class="hl-var" style="border:0px;margin:0px;padding:0px;color:rgb(0,0,139);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">xmlns</span><span class="hl-code" style="border:0px;margin:0px;padding:0px;color:#808080;font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">=</span><span class="hl-quotes" style="border:0px;margin:0px;padding:0px;color:rgb(139,0,0);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">"</span><span class="hl-string" style="border:0px;margin:0px;padding:0px;color:rgb(170,17,17);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">http://www.w3.org/2000/svg</span><span class="hl-quotes" style="border:0px;margin:0px;padding:0px;color:rgb(139,0,0);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">"</span><span class="hl-code" style="border:0px;margin:0px;padding:0px;color:#808080;font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;"> </span><span class="hl-var" style="border:0px;margin:0px;padding:0px;color:rgb(0,0,139);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">version</span><span class="hl-code" style="border:0px;margin:0px;padding:0px;color:#808080;font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">=</span><span class="hl-quotes" style="border:0px;margin:0px;padding:0px;color:rgb(139,0,0);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">"</span><span class="hl-string" style="border:0px;margin:0px;padding:0px;color:rgb(170,17,17);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">1.1</span><span class="hl-quotes" style="border:0px;margin:0px;padding:0px;color:rgb(139,0,0);font-family:Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace;font-size:13.2px;line-height:18.48px;white-space:pre-wrap;">"</span>   width="400" height="600">  
  23.             <defs>  
  24.               
  25.             <pattern id="ll" patternUnits="userSpaceOnUse" x="0" y="300" width="100" height="27"  
  26.                 vieeBox="0 300 100 67">  
  27.             <image x="0",y="300" width=100 height=67 xlink:href="img/l.jpg"/>  
  28.             </pattern>  
  29.             <g id="tree"> <!--树和树干 -->  
  30.                 <path   
  31.                     d="M 20,30  
  32.                     L 30,60  
  33.                     L 25,60  
  34.                     L 35,90  
  35.                     L 30,90  
  36.                     L 40,120  
  37.                     L 0,120  
  38.                     L 10,90  
  39.                     L 5,90  
  40.                     L 15,60  
  41.                     L 10,60  
  42.                     Z" id="tree_head"   
  43.                     />  
  44.                       
  45.                     <rect id="tree_g"  x="15" y="120" width="10"  height="50"  />  
  46.                 </g>  
  47.                   
  48.                   
  49.                 <g id="treeshadow">  
  50.                     <use xlink:href="#tree_head" fill="grey"/>  
  51.                     <use xlink:href="#tree_g" fill="grey"/>  
  52.                 </g>  
  53.                   
  54.                   
  55.             </defs>  
  56.               
  57.             <g stroke-width="20"  
  58.                 stroke-linejoin="round"  
  59.                 stroke="url(#ll)"  
  60.                 >  
  61.                   <path  d="M0,300 Q150,100 200,200 Q300,300 400,60" fill="none"/>  
  62.             </g>  
  63.               
  64.             <g>  
  65.                 <text x="80" y="100" font-size="40px" font-family="Droid Sans" stroke="#00f" fill="#0ff" font-weight="bold">  
  66.                     Select this text!  
  67.                 </text>  
  68.                   
  69.                 <text id="tc" x="80" y="140" font-size="40px" font-family="Droid Sans" stroke="#00f" fill="#0ff" font-weight="bold"  >  
  70.                 </text>  
  71.                   
  72.             </g>  
  73.             <g id="ctree">  
  74.             <use xlink:href="#tree_head"  stroke=none fill="#339900"/>  
  75.             <use xlink:href="#tree_g"  stroke=none fill="#552200"/>  
  76.             </g>  
  77.             <use xlink:href="#tree_head" transform="translate(200,250) scale(2,2)" stroke=none fill="#339900"/>  
  78.             <use xlink:href="#tree_g" transform="translate(200,250) scale(2,2)" stroke=none fill="#552200"/>  
  79.             <use xlink:href="#treeshadow" transform="translate(60,68) scale(1,0.6) skewX(-18)"/>  
  80.             <use xlink:href="#treeshadow" transform="translate(375,370) scale(1,1.3) skewX(-40)"/>  
  81.               
  82.         </svg>  
  83.           
  84.           
  85.           
  86.         <script>  
  87.               
  88.             function removeTree(e)  
  89.             {  
  90.                 var elt=e.target;  
  91.                 if(elt.correspondingUseElement)  
  92.                 {  
  93.                     elt=elt.correspondingUseElement;  
  94.                 }  
  95.                 elt.parentNode.removeChild(elt);  
  96.                 updateTress();  
  97.             }  
  98.               
  99.             function f(){  
  100.             var x=Math.floor(Math.random() *400);  
  101.             var y=Math.floor(Math.random() *600);  
  102.             var scale=Math.random() + .5;  
  103.             var translate="translate("+x+","+y+")";  
  104.             var tree=document.createElementNS("http://www.w3.org/2000/svg","use");  
  105.             tree.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href","#ctree");  
  106.             tree.setAttribute("transform",translate+"scale("+scale+")");  
  107.             document.querySelector("svg").appendChild(tree);  
  108.             updateTress();    
  109.          }  
  110.               
  111.               
  112.             function updateTress()  
  113.             {  
  114.                 var list=document.querySelectorAll("use");  
  115.                 var treeCount=0;  
  116.                 for(var i=0;i<list.length;i++)  
  117.                 {  
  118.                     if(list[i].getAttribute("xlink:href")=="#ctree")  
  119.                     {  
  120.                         treeCount++;  
  121.                         list[i].onclick=removeTree;  
  122.                     }  
  123.                 }  
  124.                 var co=document.getElementById("tc");  
  125.             co.textContent=treeCount+"tree in the forest";  
  126.           
  127.             }  
  128.             updateTress();  
  129.         </script>  
  130.           
  131.     </body>  
  132. </html>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值