如何使用 Node.js 绘制多边形并将其保存到图像

How to draw a polygon and save it to image using Node.js

我正在尝试创建一个图像,在其上绘制多边形形状并将其保存到 Node.js 中的文件中,但我很难找到可以实现该目的的库。
此外,我更喜欢不需要安装第三方程序的库。

我尝试创建的图像示例:

无需使用任何库,您只需编写 SVG 图像即可。

const fs = require('fs')

const result = `
    <svg
    width="541"
    height="271"
    >

        <rect
            width="541"
            height="271"
            x="0"
            y="0"
            fill="#3d5ea1"
        />

        <polygon
            fill="white"
            points="100,50 400,100 320,200 80,230"
        />

    </svg>
`

fs.writeFile("./file.svg", result, (err) => {
    if (err) {return console.err(err)}
    console.log("The file was saved!")
});

此代码将创建与您示例中的图像类似的图像。如果您需要 png 或 jpeg,您可以使用任何外部库来转换 svg。