JS实现图片转换SVG

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>SVG图片生成器</title>
  <style>
    body {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      background-color: #f7f7f7;
      font-family: Arial, sans-serif;
    }
      
    .container {
      text-align: center;
      padding: 20px;
      background-color: #fff;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }
  
    h1 {
      margin-top: 0;
    }
  
    input[type="file"] {
      margin-bottom: 10px;
      display: none;
    }
  
    label {
      display: inline-block;
      padding: 10px 20px;
      background-color: #007bff;
      color: #fff;
      cursor: pointer;
    }
  
    .avatar {
      margin-top: 10px;
      max-width: 100%;
      height: auto;
    }
  
    .download-button {
      margin-top: 10px;
      display: none;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>SVG图片生成器</h1>
    <label for="upload">选择图片</label>
    <input id="upload" type="file" required accept="image/gif, image/jpeg, image/png">
    <img class="avatar" src="" alt="Avatar Preview">
    <a class="download-button" href="#" download="noavatar.svg">下载 SVG</a>
  </div>
  
<script>
document.getElementById('upload').addEventListener('change', function() {
  var file = this.files[0];
  if (file) {
    var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.addEventListener('load', function() {
      var dataURL = this.result;
      var svgCode = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="20" height="20"><image xlink:href="'+dataURL+'" height="20" width="20"/></svg>';
      var blob = new Blob([svgCode], {type: 'image/svg+xml'});
      var url = URL.createObjectURL(blob);
      document.querySelector('.avatar').src = url;
      document.querySelector('.download-button').style.display = 'inline-block';
      document.querySelector('.download-button').href = url;
    });
  }
});
</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值