【发布时间】:2017-03-16 04:45:00
【问题描述】:
我正在使用 Fabric.js,并且我有以下用于绘制六边形的代码:
makeObject(originPoint, newPoint, canvasObject, properties) {
let width = Math.abs(newPoint.x - originPoint.x);
let height = 0;
if(this.shouldKeepProportion){
height=width;
}else{
height=Math.abs(newPoint.y - originPoint.y);
}
width = (this.minWidth!=null && width<this.minWidth ? this.minWidth: width);
height = (this.minHeight!=null && height<this.minHeight ? this.minHeight : height);
let sweep=Math.PI*2/6;
let points=[];
//generate points for 6 angles
for(let i=0;i<6;i++){
let x=width*Math.cos(i*sweep);
let y=height*Math.sin(i*sweep);
points.push({x:x/2,y:y/1.75});
}
properties = {
objectType: 'octagon',
left: originPoint.x,
top: originPoint.y,
originX: 'left',
originY: 'top',
fill: 'rgba(0, 0, 0, 1.0)',
width: width,
height: height,
originX: originPoint.x > newPoint.x ? 'right' : 'left',
originY: originPoint.y > newPoint.y ? 'bottom' : 'top',
flipX: originPoint.x > newPoint.x,
stroke: 'rgba(0, 0, 0, 1.0)',
strokeWidth: 1,
strokeLineJoin: 'round',
...properties
};
return new fabric.fabric.Polygon(points, properties);
}
我想要的是一个正八边形,和六边形一样。如果我尝试更改角/角度的数量,我会得到以下类型的八角形:
请注意:我不需要它旋转或翻转或类似的东西,我需要像在图片上那样绘制它。
感谢您的帮助!
编辑:工作 JSFiddle:https://jsfiddle.net/42fb716n/
【问题讨论】:
-
请分享一个小提琴
-
jsfiddle.net/42fb716n 这里。谢谢
标签: javascript fabricjs