简体   繁体   English

使用node.js渲染svg路径数据服务器端

[英]rendering svg path data serverside with node.js

I'm creating a svg path analyser and want to show the user the paths that the analyser picked up on. 我正在创建一个svg路径分析器,并希望向用户显示分析器选择的路径。 how do i render svg path data with svg transformation serverside 如何使用svg转换服务器端渲染svg路径数据

Front-end I am able to use Path2D to turn svg path data into a canvas, but serverside is a bit tricky. 前端我能够使用Path2D将svg路径数据转换为画布,但是服务器端有点棘手。 I've tried the packages canvas and canvas-5-polyfill, which works, but canvas-5-polyfill does not like absolute movements and I dont have the know how to fix that. 我已经尝试了包帆布和canvas-5-polyfill,它可以工作,但canvas-5-polyfill不喜欢绝对的动作,我不知道如何解决这个问题。

Have a look at: https://repl.it/repls/ThoroughPeacefulMenu 看看: https//repl.it/repls/ThoroughPeacefulMenu

I'd expect the D at the bottom of the image to display correctly, but it doesn't. 我希望图像底部的D能够正确显示,但事实并非如此。

Does anyone know how to address this proplem, or is there any other way I can render the image? 有谁知道如何解决这个问题,还是有其他方法可以渲染图像?

Thanks in advance! 提前致谢!

btw, this is my first post, so sorry if I'm missing anything :) 顺便说一句,这是我的第一篇文章,很抱歉,如果我错过了什么:)

For anyone struggling with the same problem as I did, This is what I did to solve it. 对于那些和我一样苦苦挣扎的人来说,这就是我为解决这个问题所做的。 I ended up using SVG.js and svgdom. 我最终使用了SVG.js和svgdom。

const window = require('svgdom')
const SVG = require('svg.js')(window)
const document = window.document

var fs = require('fs');
var data = {
    dims: {
        width: 755.90533,
        height: 755.90533
    },
    raster: {
        commands: ['M 339.624,237.408 H 56.159 v 283.464 h 283.465 z',
            'm 0,0 c 1.157,-0.21 2.84,-0.263 4.628,-0.263 9.784,0 15.096,5.47 15.096,15.043 0.052,8.363 -4.681,13.675 -14.359,13.675 -2.367,0 -4.155,-0.21 -5.365,-0.474 z m -4.576,31.348 c 2.788,0.421 6.101,0.737 9.731,0.737 6.574,0 11.255,-1.525 14.359,-4.419 3.155,-2.893 4.996,-6.995 4.996,-12.728 0,-5.786 -1.788,-10.52 -5.102,-13.781 -3.313,-3.313 -8.783,-5.101 -15.674,-5.101 -3.26,0 -5.996,0.157 -8.31,0.421 z'
        ],
        transform: [
            [1.3333333, 0, 0, -1.3333333, 0, 755.90533],
            [1.3333333, 0, 0, -1.3333333, 71.02479822438, 611.67693360571]
        ]
    }
};

const draw = SVG(document.documentElement).size(data.dims.width, data.dims.height)
draw.clear()
var raster = draw.group()

if (typeof(data.raster.commands) !== 'undefined') {
    for (var i = 0; i < data.raster.commands.length; i++) {
        var transform = data.raster.transform[i];
        var matrix = new SVG.Matrix(transform[0], transform[1], transform[2], transform[3], transform[4], transform[5])

        var path = draw.path(data.raster.commands[i]);
        path.fill('none')
        path.transform(matrix)
        path.stroke({
            color: '#000000'
        })
        raster.add(path)
    }
}
var visfile = 'test.svg';
fs.writeFile(visfile, draw.svg(), function(err) {
    console.log(err);
});

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

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