RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 589612
Accepted
Rennorb
Rennorb
Asked:2020-11-11 03:51:45 +0000 UTC2020-11-11 03:51:45 +0000 UTC 2020-11-11 03:51:45 +0000 UTC

在画布上重绘 svg

  • 772

你好!有一个任务 - 绘制和下载 svg,这样的结构

<svg id="canv" style="width: 220px; height: 220px;">       
  <g id="grid">
    <line x1="22" y1="0" x2="22" y2="220" stroke="black"></line>
    <line x1="44" y1="0" x2="44" y2="220" stroke="black"></line>
    <line x1="66" y1="0" x2="66" y2="220" stroke="black"></line>
    <line x1="88" y1="0" x2="88" y2="220" stroke="black"></line>
    <line x1="110" y1="0" x2="110" y2="220" stroke="black"></line>
    <line x1="132" y1="0" x2="132" y2="220" stroke="black"></line>
    <line x1="154" y1="0" x2="154" y2="220" stroke="black"></line>
    <line x1="176" y1="0" x2="176" y2="220" stroke="black"></line>
    <line x1="198" y1="0" x2="198" y2="220" stroke="black"></line>
    <line x1="0" y1="22" x2="220" y2="22" stroke="black"></line>
    <line x1="0" y1="44" x2="220" y2="44" stroke="black"></line>
    <line x1="0" y1="66" x2="220" y2="66" stroke="black"></line>
    <line x1="0" y1="88" x2="220" y2="88" stroke="black"></line>
    <line x1="0" y1="110" x2="220" y2="110" stroke="black"></line>
    <line x1="0" y1="132" x2="220" y2="132" stroke="black"></line>
    <line x1="0" y1="154" x2="220" y2="154" stroke="black"></line>
    <line x1="0" y1="176" x2="220" y2="176" stroke="black"></line>
    <line x1="0" y1="198" x2="220" y2="198" stroke="black"></line>
  </g>

  <line x1="165" y1="187" x2="121" y2="33" stroke="black"></line>
  <line x1="121" y1="33" x2="187" y2="55" stroke="black"></line>
  <line x1="187" y1="55" x2="99" y2="165" stroke="black"></line>
  <line x1="99" y1="165" x2="165" y2="187" stroke="black"></line>
</svg>

(组内有几行,组grid外有几行,颜色可以任意)

为此,我编写了以下代码:

function download()
{
  var temp = document.createElement('canvas');

  temp.style.width = canvas.width*cellSize;
  temp.style.height = canvas.height*cellSize;

  var ctx = temp.getContext('2d');

  for(var i=0; i<canv.getElementById('grid').children.length; i++)
  {
    var el = canv.getElementById('grid').children[i];
    console.log(el);

    ctx.strokeStyle = el.getAttribute('stroke');

    var x1 = el.getAttribute('x1'),
        x2 = el.getAttribute('x2'),
        y1 = el.getAttribute('y1'),
        y2 = el.getAttribute('y2');


    ctx.moveTo(x1, y1);
    ctx.lineTo(x2, y2);
    ctx.stroke();
  }

  for(var i=1; i<canv.children.length; i++)
  {
    var el = canv.children[i];
    console.log(el);
    ctx.strokeStyle = el.getAttribute('stroke');

    var x1 = el.getAttribute('x1'),
        x2 = el.getAttribute('x2'),
        y1 = el.getAttribute('y1'),
        y2 = el.getAttribute('y2');


    ctx.moveTo(x1, y1);
    ctx.lineTo(x2, y2);
    ctx.stroke();
  }

  document.body.appendChild(temp);

  var link = document.createElement('a');
  link.setAttribute('download', '');
  link.setAttribute('href', temp.toDataURL());
  link.click();
}

结果,下载了以下图像:

在此处输入图像描述

链接到 Jsfiddle(你需要点击图片)。

请帮我修一下。

javascript
  • 3 3 个回答
  • 10 Views

3 个回答

  • Voted
  1. Rustam Fatkullin
    2020-11-14T17:44:17Z2020-11-14T17:44:17Z

    下午好!我认为你给自己找了麻烦。这个问题可以通过两种方式解决:

    1. 下载svg格式的图片。可以在这里查看:https ://stackoverflow.com/questions/2483919/how-to-save-svg-canvas-to-local-filesystem 在答案中有一个解决方案应该可以在没有服务器的情况下工作。
    2. 下载png格式的图片。这将需要 canvg 库:

      var canvas = document.createElement('canvas');
      var svg = document.getElementById('canv');
      var svgWider = svg.innerHTML;
      canvg(canvas, svgWider);
      // Ваш код, который скачивает canvas как png файл.
      
    • 3
  2. Best Answer
    Apo-S
    2020-11-14T19:05:32Z2020-11-14T19:05:32Z

    事实上,默认的画布大小是 150x300

    删除

    temp.style.width = canvas.width*cellSize;
    temp.style.height = canvas.height*cellSize;
    

    之后

    var ctx = temp.getContext('2d');
    

    添加

    ctx.canvas.height = canvas.height * cellSize;
    ctx.canvas.width = canvas.width * cellSize;
    

    jsFiddle

    • 2
  3. Qwertiy
    2020-11-14T17:54:37Z2020-11-14T17:54:37Z
    1. width谁来指定 canvas和heightsvg的属性?
    2. 对于 svg 最好指定viewBox而不是widthand height。
    3. svg 图像有什么问题?
    • -2

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何停止编写糟糕的代码?

    • 3 个回答
  • Marko Smith

    onCreateView 方法重构

    • 1 个回答
  • Marko Smith

    通用还是非通用

    • 2 个回答
  • Marko Smith

    如何访问 jQuery 中的列

    • 1 个回答
  • Marko Smith

    *.tga 文件的组重命名(3620 个)

    • 1 个回答
  • Marko Smith

    内存分配列表C#

    • 1 个回答
  • Marko Smith

    常规赛适度贪婪

    • 1 个回答
  • Marko Smith

    如何制作自己的自动完成/自动更正?

    • 1 个回答
  • Marko Smith

    选择斐波那契数列

    • 2 个回答
  • Marko Smith

    所有 API 版本中的通用权限代码

    • 2 个回答
  • Martin Hope
    jfs *(星号)和 ** 双星号在 Python 中是什么意思? 2020-11-23 05:07:40 +0000 UTC
  • Martin Hope
    hwak 哪个孩子调用了父母的静态方法?还是不可能完成的任务? 2020-11-18 16:30:55 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    Arch ArrayList 与 LinkedList 的区别? 2020-09-20 02:42:49 +0000 UTC
  • Martin Hope
    iluxa1810 哪个更正确使用:if () 或 try-catch? 2020-08-23 18:56:13 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5