图形新方法 'drawPolygon'
作者: hsk-kr创建于 2021年1月22日更新于 2026年5月30日
标签enhancement
/**
* 绘制多边形
* @param {[x, y][]} posList 位置列表
*/
createjs.Graphics.prototype.drawPolygon = function (...posList) {
const copyPosList = [...posList];
if (copyPosList.length === 0) return this;
const firstPos = copyPosList.splice(0, 1)[0];
let chain = this.moveTo(firstPos[0], firstPos[1]);
for (const pos of copyPosList) chain = chain.lineTo(pos[0], pos[1]);
return chain.lineTo(firstPos[0], firstPos[1]);
};
这是我在项目中定义的一个方法。我搜索了如何使用 EaselJS 绘制多边形。没有相关方法。人们都有自己绘制多边形的方法。创建自己的方法并不难。但我认为 EaselJS 提供我们的方法会更好。内容来源: CreateJS/EaselJS