hanyuxinting / Blog

记录点滴
1 stars 0 forks source link

SVG 学习笔记一:SVG基本语法及使用Demo #23

Open hanyuxinting opened 5 years ago

hanyuxinting commented 5 years ago

什么是SVG-Scalable Vector Graphics-可缩放矢量图

简介使用方法

使用方法1. 放在html里

<!DOCTYPE html>
<html>
<head></head>
<body>
<svg
  id="mysvg"
  xmlns="http://www.w3.org/2000/svg"
  viewBox="0 0 800 600"
  preserveAspectRatio="xMidYMid meet"
>
  <circle id="mycircle" cx="400" cy="300" r="50" />
<svg>
</body>
</html>

使用方法2. 放在标签里

<img src="circle.svg">
<object id="object" data="circle.svg" type="image/svg+xml"></object>
<embed id="embed" src="icon.svg" type="image/svg+xml">
<iframe id="iframe" src="icon.svg"></iframe>

<!--base64-->
<img src="data:image/svg+xml;base64,[data]">

使用方法3. 放在css里

.logo {
  background: url(icon.svg);
}

语法

svg 标签

circle

.fancy { fill: none; stroke: black; stroke-width: 3pt; }


#### others

[demo 展示源码](https://github.com/hanyuxinting/demos/blob/master/svg/index.html)

```html
<svg width="300" height="180">
    <!-- x1属性和y1属性,表示线段起点的横坐标和纵坐标;x2属性和y2属性,表示线段终点的横坐标和纵坐标;style属性表示线段的样式。 -->
    <line x1="0" y1="0" x2="200" y2="0" style="stroke:rgb(0,0,0);stroke-width:5" />
  </svg>

  <svg width="300" height="180">
    <!-- 折线:points属性指定了每个端点的坐标,横坐标与纵坐标之间与逗号分隔,点与点之间用空格分隔 -->
    <polyline points="3,3 30,28 3,53" fill="none" stroke="black" />
  </svg>

  <svg width="300" height="180">
    <!-- 矩形: x属性和y属性,指定了矩形左上角端点的横坐标和纵坐标;width属性和height属性指定了矩形的宽度和高度(单位像素)。 -->
    <rect x="0" y="0" height="100" width="200" style="stroke: #70d5dd; fill: #dd524b" />
  </svg>

  <svg width="300" height="180">
    <!-- 椭圆:cx属性和cy属性,指定了椭圆中心的横坐标和纵坐标(单位像素);rx属性和ry属性,指定了椭圆横向轴和纵向轴的半径(单位像素)。 -->
    <ellipse cx="60" cy="60" ry="40" rx="20" stroke="black" stroke-width="5" fill="silver" />
  </svg>

  <svg width="300" height="180">
    <!-- 多边形:points属性指定了每个端点的坐标,横坐标与纵坐标之间与逗号分隔,点与点之间用空格分隔。 -->
    <polygon fill="green" stroke="orange" stroke-width="1" points="0,0 100,0 100,100 0,100 0,0" />
  </svg>

  <svg width="300" height="180">
    <!-- 路径: d属性表示绘制顺序,它的值是一个长字符串,每个字母表示一个绘制动作,后面跟着坐标。M:移动到(moveto);    L:画直线到(lineto);    Z:闭合路径 -->
    <path d="
      M 18,3
      L 46,3
      L 46,40
      L 61,40
      L 32,68
      L 3,40
      L 18,40
      Z
    "></path>
  </svg>

  <svg width="300" height="180">
    <!-- 文字: x属性和y属性,表示文本区块基线(baseline)起点的横坐标和纵坐标。文字的样式可以用class或style属性指定。 -->
    <text x="50" y="30" style="font-size: 50px;">Hello World</text>
  </svg>

  <svg viewBox="0 0 30 10" xmlns="http://www.w3.org/2000/svg">
    <!-- 复制一个形状: <use>的href属性指定所要复制的节点,x属性和y属性是<use>左上角的坐标。另外,还可以指定width和height坐标。-->
    <circle id="myCircle" cx="5" cy="5" r="4" />

    <use href="#myCircle" x="10" y="0" fill="blue" />
    <use href="#myCircle" x="20" y="0" fill="white" stroke="blue" />
  </svg>

  <!-- <g>标签用于将多个形状组成一个组(group),方便复用。 -->
  <svg width="300" height="100">
    <g id="myCircles">
      <text x="25" y="20" style="font-size: 20px;">圆形</text>
      <circle cx="50" cy="50" r="20" />
    </g>

    <use href="#myCircles" x="100" y="0" fill="blue" />
    <use href="#myCircles" x="200" y="0" fill="white" stroke="blue" />
  </svg>

  <!-- <defs>标签用于自定义形状,它内部的代码不会显示,仅供引用。 -->
  <svg width="300" height="100">
    <defs>
      <g id="mCircle">
        <text x="25" y="20">圆形</text>
        <circle cx="50" cy="50" r="20" />
      </g>
    </defs>

    <use href="#mCircle" x="0" y="0" />
    <use href="#mCircle" x="100" y="0" fill="blue" />
    <use href="#mCircle" x="200" y="0" fill="white" stroke="blue" />
  </svg>

  <!-- <pattern>标签用于自定义一个形状,该形状可以被引用来平铺一个区域。 -->
  <!-- <pattern>标签将一个圆形定义为dots模式。patternUnits="userSpaceOnUse"表示<pattern>的宽度和长度是实际的像素值。然后,指定这个模式去填充下面的矩形。 -->
  <svg width="500" height="500">
    <defs>
      <pattern id="dots" x="0" y="0" width="100" height="100" patternUnits="userSpaceOnUse">
        <circle fill="#bee9e8" cx="50" cy="50" r="35" />
      </pattern>
    </defs>
    <rect x="0" y="0" width="100%" height="100%" fill="url(#dots)" />
  </svg>

  <!-- <image>标签用于插入图片文件。 -->
  <svg viewBox="0 0 100 100" width="100" height="100">
    <image xlink:href="path/to/image.jpg" width="50%" height="50%" />
  </svg>

  <!-- <animate>标签用于产生动画效果。 -->
  <svg width="500px" height="100px">
    <rect x="0" y="0" width="100" height="100" fill="#feac5e">
      <animate attributeName="x" from="0" to="500" dur="2s" repeatCount="indefinite" />
      <!-- <animate attributeName="width" to="500" dur="2s" repeatCount="indefinite" /> -->
    </rect>
  </svg>

  <!-- <animate>标签用于产生动画效果。 -->
  <svg width="500px" height="100px">
    <rect x="0" y="0" width="100" height="100" fill="#feac5e">
      <!-- <animate attributeName="x" from="0" to="500" dur="2s" repeatCount="indefinite" /> -->
      <animate attributeName="width" to="500" dur="2s" repeatCount="indefinite" />
    </rect>
  </svg>
  <!-- <animate>标签对 CSS 的transform属性不起作用,如果需要变形,就要使用<animateTransform>标签。 -->
  <!-- animateTransform 的效果为旋转(rotate),这时from和to属性值有三个数字,第一个数字是角度值,第二个值和第三个值是旋转中心的坐标。from="0 200 200"表示开始时,角度为0,围绕(200, 200)开始旋转;to="360 400 400"表示结束时,角度为360,围绕(400, 400)旋转。-->
  <svg width="500px" height="500px">
    <rect x="250" y="250" width="50" height="50" fill="#4bc0c8">
      <animateTransform attributeName="transform" type="rotate" begin="0s" dur="10s" from="0 200 200" to="360 400 400"
        repeatCount="indefinite" />
    </rect>
  </svg>

JS 操作

DOM 操作

<svg
  id="mysvg"
  xmlns="http://www.w3.org/2000/svg"
  viewBox="0 0 800 600"
  preserveAspectRatio="xMidYMid meet"
>
  <circle id="mycircle" cx="400" cy="300" r="50" />
<svg>

circle {
  stroke-width: 5;
  stroke: #f00;
  fill: #ff0;
}

circle:hover {
  stroke: #090;
  fill: #fff;
}

var mycircle = document.getElementById('mycircle');

mycircle.addEventListener('click', function(e) {
  console.log('circle clicked - enlarging');
  mycircle.setAttribute('r', 60);
}, false);

获取SVG DOM


// 使用<object>、<iframe>、<embed>标签插入 SVG 文件,可以获取 SVG DOM。
// 如果使用<img>标签插入 SVG 文件,就无法获取 SVG DOM。
var svgObject = document.getElementById('object').contentDocument;
var svgIframe = document.getElementById('iframe').contentDocument;
var svgEmbed = document.getElementById('embed').getSVGDocument();

读取 SVG 源码

<div id="svg-container">
  <svg
    xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xml:space="preserve" width="500" height="440"
  >
    <!-- svg code -->
  </svg>
</div>
// 使用XMLSerializer实例的serializeToString()方法,获取 SVG 元素的代码。
var svgString = new XMLSerializer().serializeToString(document.querySelector('svg'));

SVG 图像转为 Canvas 图像

// SVG 图像指定到该Image对象的src属性
var img = new Image();
var svg = new Blob([svgString], {type: "image/svg+xml;charset=utf-8"});

var DOMURL = self.URL || self.webkitURL || self;
var url = DOMURL.createObjectURL(svg);

img.src = url;

// 当图像加载完成后,再将它绘制到<canvas>元素
img.onload = function () {
  var canvas = document.getElementById('canvas');
  var ctx = canvas.getContext('2d');
  ctx.drawImage(img, 0, 0);
};

以上是ruanyifeng老师博客出品,整理下来的笔记。

补充 SVG

inline SVG 支持资源外链 支持CSS 支持JS img SVG 不支持资源外链 支持内部CSS 不支持JS background-img SVG 不支持资源外链 支持内部CSS 不支持JS background-img BASE64 SVG 不支持资源外链 支持内部CSS 不支持JS object SVG 支持资源外链 支持内部CSS 支持内部JS embed SVG 支持资源外链 支持内部CSS 支持内部JS iframe SVG 支持资源外链 支持内部CSS 支持内部JS

? Responsive Content

more elements

这里是更多SVG 元素的列表