svg 操作

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style type="text/css">
      * {
        box-sizing: border-box;
      }

      .img_c {
        background-image: url("./images/1.jpg");
        width: 3024px;
        height: 590px;
      }
    </style>
  </head>
  <body>
    <div
      class="div1"
      style="width: 200px; height: 400px; top: 50px; position: absolute"
    >
      <svg
        viewBox="0 0 200 100"
        xmlns="http://www.w3.org/2000/svg"
        class="svg1"
      >
        <!-- <defs>
            <linearGradient id="Gradient01">
              <stop offset="20%" stop-color="#39F" />
              <stop offset="90%" stop-color="#F3F" />
            </linearGradient>
          </defs> -->
        <!-- LineTo commands with absolute coordinates -->
        <path
          fill="red"
          stroke="red"
          class="path1"
          d="M 10,10
                 L 10,90
                 L 90,90
                 L 90,10 
                 L 10,10"
        />

        <!-- LineTo commands with relative coordinates -->
        <path
          fill="none"
          stroke="red"
          d="M 110,10
                 l 80,80
                 v -70
                 h -70"
        />
      </svg>
      <svg viewBox="-10 -10 120 120">
        <mask id="myMask">
          <!-- Everything under a white pixel will be visible  白色可见  黑色不可见 -->
          <rect x="0" y="0" width="120" height="120" fill="white" stroke="1" />

          <!-- Everything under a black pixel will be invisible -->
        </mask>

        <!-- <polygon points="-10,110 110,110 110,-10" fill="orange" />
       -->
        <!-- with this mask applied, we "punch" a heart shape hole into the circle -->
        <circle cx="50" cy="50" r="50" mask="url(#myMask)" fill="orange" />
      </svg>
    </div>
  </body>
  <script>
    var div1 = document.getElementsByClassName("div1")[0];

    var svg1 = document.getElementsByClassName("svg1")[0];
    var path1 = svg1.getElementsByClassName("path1")[0];
    var defs1 = document.createElementNS("http://www.w3.org/2000/svg", "defs");

    defs1.innerHTML = ` <pattern id="image" x="0" y="0" patternUnits="userSpaceOnUse" height="100" width="100">
      <image x="0" y="0" height="100" width="100" xlink:href="./images/1.jpg"></image>
    </pattern>
    <linearGradient id="Gradient01">
              <stop offset="20%" stop-color="#39F" />
              <stop offset="90%" stop-color="#F3F" />
            </linearGradient>
    `;
    svg1.appendChild(defs1);
    //path1.style.fill="url(#image)";

    // console.log(path1);
    path1.addEventListener("click", function (e) {
      console.log(e.offsetX);
      console.log(e.offsetY);
      e.target.style.fill = "url(#image)";
    });
  </script>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值