前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SVG绘图

SVG绘图

作者头像
码客说
发布2021-12-05 09:49:06
2.1K0
发布2021-12-05 09:49:06
举报
文章被收录于专栏:码客码客

前言

图形

SVG

代码语言:javascript
复制
<svg id="test_1"
     xmlns="http://www.w3.org/2000/svg"
     xml:space="preserve"
     width="600"
     height="400"
     viewBox="0 0 300 200">
    <g style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1">
       <rect x="10" y="10" width="100" height="40" rx="20"/>
    </g>
</svg>

SVG中

  • width/height 是图形的宽/高
  • viewBox="0 0 300 200" 是画布的属性

如上面的示例 画布尺寸比图形小,那么我们看到的画布中的图形就会同比例放大。

圆形

代码语言:javascript
复制
<circle id="_x31_" 
        opacity="1" 
        fill="#d2fae3" 
        cx="332.081" cy="149.346" r="73" 
        stroke-width="1" stroke="#82eeb5"
        />

矩形

代码语言:javascript
复制
<rect id="_x31_"
      opacity="1"
      width="100" height="100"
      x="100" y="100"
      fill="#d2fae3"
      stroke-width="1" stroke="#82eeb5"
      />

菱形

代码语言:javascript
复制
<polygon points="0,50 50,0 100,50 50,100"
             style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1"/>

文字

代码语言:javascript
复制
<text x="0" y="15" font-size="18"
          style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1">Hello World</text>

不带边框

代码语言:javascript
复制
<text x="0" y="15" font-size="18"
          style="fill:#333333;">Hello World</text>

圆角矩形

代码语言:javascript
复制
<rect x="10" y="10" width="100" height="40" rx="4"
          style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1"/>

两侧为圆形的矩形

代码语言:javascript
复制
<rect x="10" y="10" width="100" height="40" rx="20"
          style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1"/>

Path

这里是画了个心形

代码语言:javascript
复制
<g style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1"
   transform="translate(100 0) scale(1 1)">
    <path id="heart" d="M 10,30 A 20,20 0,0,1 50,30 A 20,20 0,0,1 90,30 Q 90,60 50,90 Q 10,60 10,30 z"/>
</g>

相同样式的图形或者需要一起添加事件可以用g来分组

g不能设置宽高等位置属性,只能设置内部元素的样式及添加响应事件。

内部元素的定位也是相对于svg的。

代码语言:javascript
复制
<g style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1">
  <rect x="10" y="10" width="100" height="40" rx="20"/>
</g>

比如

代码语言:javascript
复制
<g stroke="#82eeb5" fill="#d2fae3" stroke-width="1">
    <circle cx="25" cy="25" r="15"/>
    <circle cx="40" cy="25" r="15"/>
    <circle cx="55" cy="25" r="15"/>
</g>

transform

代码语言:javascript
复制
<svg xmlns="http://www.w3.org/2000/svg"
     xml:space="preserve"
     width="600"
     height="400"
     viewBox="-1 -1 600 400">
    <rect x="50" y="20" width="100" height="40" rx="4"
          transform="translate(-50 -20)"
          style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1"/>
</svg>

事件

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>流程图-SVG版</title>
  </head>
  <body>

    <svg xmlns="http://www.w3.org/2000/svg"
         id="m_svg"
         xml:space="preserve"
         width="800"
         height="600"
         viewBox="-1 -1 800 600">
      <rect x="50" y="20" width="100" height="40" rx="4"
            transform="translate(-50 -20)"
            style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1"
            onmousedown="selectElement(event)"/>


      <rect x="100" y="120" width="100" height="40" rx="4"
            transform="translate(-50 -20)"
            style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1"
            onmousedown="selectElement(event)"/>

    </svg>

    <script type="text/javascript">
      var currentX = 0;
      var currentY = 0;
      selectedElement = null;
      let currentMatrix = {
        x: 0,
        y: 0
      };

      var m_svg = document.getElementById("m_svg");
      m_svg.setAttributeNS(null, "onmousemove", "moveElement(evt)");
      m_svg.setAttributeNS(null, "onmouseup", "deselectElement(evt)");

      function selectElement(evt) {
        selectedElement = evt.target;
        currentX = evt.clientX;
        currentY = evt.clientY;
        currentMatrix.x = parseInt(selectedElement.getAttributeNS(null, "x"));
        currentMatrix.y = parseInt(selectedElement.getAttributeNS(null, "y"));
      }

      function moveElement(evt) {
        var dx = evt.clientX - currentX;
        var dy = evt.clientY - currentY;
        currentMatrix.x += dx;
        currentMatrix.y += dy;

        selectedElement.setAttributeNS(null, "x", currentMatrix.x);
        selectedElement.setAttributeNS(null, "y", currentMatrix.y);
        currentX = evt.clientX;
        currentY = evt.clientY;
      }

      function deselectElement(evt) {
        if (selectedElement !== null) {
          selectedElement.removeAttributeNS(null, "onmousemove");
          selectedElement.removeAttributeNS(null, "onmouseout");
          selectedElement.removeAttributeNS(null, "onmouseup");
          selectedElement = null;
        }
      }

    </script>
  </body>
</html>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-12-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 图形
    • SVG
      • 圆形
        • 矩形
          • 菱形
            • 文字
              • 圆角矩形
                • 两侧为圆形的矩形
                  • Path
                  • transform
                  • 事件
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档