简体   繁体   English

将画布或控制点转换为SVG

[英]Convert canvas or control points to SVG

I am developing a drawing app, and now I want to add a function which creates SVG from my canvas or control points. 我正在开发一个绘图应用程序,现在我想添加一个从我的画布或控制点创建SVG的函数。 (I save the mouse coordinates for each drawing step). (我为每个绘图步骤保存鼠标坐标)。

canvasElement.toDataURL("image/svg+xml"); // -- doesn't work

One condition - don't use external libs. 一个条件 - 不要使用外部库。

I understand, that it is possible to generate an SVG file in Javascript like: 我明白,可以在Javascript中生成一个SVG文件,如:

var mysvg = "<svg>"; for(something) { mysvg += "something"; } //etc

But I don't think that it is good way. 但我不认为这是好方法。

Can you advise anything? 你能告诉一下吗?

I solved the problem by generating SVG file. 我通过生成SVG文件解决了这个问题。 I translated all my canvas drawing functions into SVG drawing tags. 我将所有画布绘图功能翻译成SVG绘图标签。

Something like that: 像这样的东西:

function exportSVG() {
    var svg = '<?xml version="1.0" standalone="yes"?>';
svg += '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">';
svg += '<svg width="1065px" height="529px" xmlns="http://www.w3.org/2000/svg" version="1.1">';

for(var i=0; i<myPoints.length; i++) {
   svg += "M"+myPoints.x[i]+","+myPoints.y[i]+" ";
   svg += "L"+myPoints.x[i+1]+","+myPoints.y[i+1];
   svg += '" fill="none" stroke="rgb('+color+')" stroke-opacity="'+opacity+'" stroke-width="'+size+'px" stroke-linecap="round" stroke-linejoin="round" />';
}
svg += "</svg>";
}

So, in svg variable there will be SVG file generated from Canvas. 因此,在svg变量中,将存在从Canvas生成的SVG文件。 Thanks everybody! 谢谢大家!

也许在这个链接上: http//code.google.com/p/canvas-svg/你会找到你想要的东西。

You can generate an equivalent SVG in JavaScript If you want to try it go to http://www.irunmywebsite.com/raphael/SVGTOHTML_LIVE.php 您可以在JavaScript中生成等效的SVG如果您想尝试它,请访问http://www.irunmywebsite.com/raphael/SVGTOHTML_LIVE.php

You can generate 2 types of Raphael.js or one type in jQuery SVG All 3 options generate an HTML with the JavaScript inside 您可以在jQuery SVG中生成两种类型的Raphael.js或一种类型。所有3种选项都会生成一个带有JavaScript的HTML

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM