52 lines
1.3 KiB
JavaScript
52 lines
1.3 KiB
JavaScript
import { Graphics } from 'pixi.js';
|
|
import { assertBezierPoints, convertToBezierParams } from 'jl-graphic';
|
|
|
|
class SectionGraphic extends Graphics {
|
|
static Type = 'SectionGraphic';
|
|
_points = [];
|
|
get points() {
|
|
return this._points;
|
|
}
|
|
set points(value) {
|
|
if (!this.isCurve) {
|
|
if (value.length < 2) {
|
|
throw Error('Polyline must have at least 2 points');
|
|
}
|
|
}
|
|
else {
|
|
assertBezierPoints(value);
|
|
}
|
|
this._points = value;
|
|
}
|
|
_segmentsCount = 10;
|
|
get segmentsCount() {
|
|
return this._segmentsCount;
|
|
}
|
|
set segmentsCount(value) {
|
|
if (value < 1) {
|
|
throw Error('segmentsCount must be at least 1');
|
|
}
|
|
this._segmentsCount = value;
|
|
}
|
|
isCurve = false;
|
|
constructor() {
|
|
super();
|
|
}
|
|
paint() {
|
|
if (this.isCurve) {
|
|
const bps = convertToBezierParams(this.points);
|
|
bps.forEach((bp) => {
|
|
this.drawBezierCurve(bp.p1, bp.p2, bp.cp1, bp.cp2, this.segmentsCount);
|
|
});
|
|
}
|
|
else {
|
|
this.moveTo(this.points[0].x, this.points[0].y);
|
|
for (let i = 1; i < this.points.length; i++) {
|
|
this.lineTo(this.points[i].x, this.points[i].y);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export { SectionGraphic };
|