我试图使用paper.js'优良path.simplify方法在用户完成拉丝后,以产生平滑的曲线,以简化手绘路径。由于这是一个非HTML输出(TV的远程图解)我试图创造的NodeJS采取点和输出产生的简化曲线的控制点的微服务。
我尝试使用纸质jsdom和方法工作,不抱怨,但总是输出在所有点零个COORDS单个段。我不知道是否我应该使用纸jsdom画布,而不是能得到相应的输出。
下面是我试图建立节点模块:
const Path = require('paper').Path
const Point = require('paper').Point
const Project = require('paper').Project
// the next line produces side effects without which
// the code will not run, but I'm not sure this is the way to go.
let p = new Project()
function simplify (points) {
let pts = []
for (let point in points) {
let pt = new Point(point.x, point.y)
pts.push(pt)
}
let path = new Path({
segments: pts,
strokeColor: 'black',
fullySelected: false
})
path.simplify(10)
let simplePath = []
for (const segment of path.segments) {
// only one segment reaches this point
// with all zeros in all the parameters
console.log(segment.path)
simplePath.push(
{
handleIn: { x: segment.handleIn.x, y: segment.handleIn.y },
handleOut: { x: segment.handleOut.x, y: segment.handleOut.y },
point: { x: segment.point.x, y: segment.point.y }
})
console.log(`
hi : (${segment.handleIn.x}, ${segment.handleIn.y})
ho : (${segment.handleOut.x}, ${segment.handleOut.y})
point: (${segment.point.x}, ${segment.point.y})`)
}
return simplePath
}
module.exports = simplify
我认为你的错误是在这里:
for (let point in points) {
let pt = new Point(point.x, point.y);
pts.push(pt);
}
如果,因为我认为,变量points
包含对象的数组,你应该使用:
for (let point of points) {
let pt = new Point(point.x, point.y);
pts.push(pt);
}
注意关键字of
在更换in
for循环。
你实际上是循环的钥匙,而不是价值。