Node.js 通用 drawPolygon()函数
drawPolygon()函数是 GraphicsMagick 库中的一个内置函数,用于绘制具有指定坐标的多边形。该函数在成功时返回真值。
句法:
drawPolygon( [x0, y0], ... ,[xn, yn] )
参数:此函数接受 x 和 y 坐标数组。
返回值:此函数返回 GraphicsMagick 对象。
示例 1:
// Include gm library
var gm = require('gm');
// Import the image
gm(500, 200, 'white')
// Set the color for the stroke
.stroke("green", 3)
// Set font as 'Helvetica'
.font("Helvetica.ttf", 60)
// Use drawText() Function
.drawText(30, 160, "GeeksforGeeks!")
// Invoke drawPolygon function
// with array of points
.drawPolygon([12, 60], [45, 79],
[80, 120], [300, 32], [300, 98])
// Process and write the image
.write("drawPolygon1.png", function (err) {
if (!err) console.log('done');
});
输出:
示例 2:
// Include gm library
var gm = require('gm');
// Import the image
gm('https://media.geeksforgeeks.org/wp-content/uploads/20200227114541/1406-3.png')
// Set the color for the stroke
.stroke("#000000", 5)
// Invoke drawPolygon function with array of points
.drawPolygon([12, 60], [45, 79], [80, 120], [300, 32])
// Process and write the image
.write("drawPolygon1.png", function (err) {
if (!err) console.log('done');
});
输出:
参考:
- http://www.graphicsmagick.org/GraphicsMagick.html#details-draw
- https://www.npmjs.com/package/gm