72 lines
1.7 KiB
JavaScript
72 lines
1.7 KiB
JavaScript
const PIXI = 'undefined' !== typeof window ? require('pixi.js') : undefined;
|
|
|
|
import {Color} from './color';
|
|
import {Renderable} from './renderable';
|
|
|
|
export class Primitives extends Renderable {
|
|
|
|
static fillStyle(color) {
|
|
return {color};
|
|
}
|
|
|
|
static lineStyle(color, thickness = 1) {
|
|
return {color, thickness};
|
|
}
|
|
|
|
constructor() {
|
|
super();
|
|
this.primitives = new PIXI.Graphics();
|
|
}
|
|
|
|
clear() {
|
|
this.primitives.clear();
|
|
}
|
|
|
|
drawCircle(position, radius, lineStyle, fillStyle) {
|
|
this._wrapStyle('drawCircle', '3rd', lineStyle, fillStyle, () => {
|
|
this.primitives.drawCircle(position[0], position[1], radius);
|
|
});
|
|
}
|
|
|
|
drawLine(p1, p2, lineStyle, fillStyle) {
|
|
this._wrapStyle('drawLine', '3rd', lineStyle, fillStyle, () => {
|
|
this.primitives.moveTo(p1[0], p1[1]);
|
|
this.primitives.lineTo(p2[0], p2[1]);
|
|
});
|
|
}
|
|
|
|
drawRectangle(rectangle, lineStyle, fillStyle) {
|
|
this._wrapStyle('drawLine', '2nd', lineStyle, fillStyle, () => {
|
|
this.primitives.drawRect(
|
|
rectangle[0],
|
|
rectangle[1],
|
|
rectangle[2],
|
|
rectangle[3]
|
|
);
|
|
});
|
|
}
|
|
|
|
get internal() {
|
|
return this.primitives;
|
|
}
|
|
|
|
_wrapStyle(method, where, lineStyle, fillStyle, fn) {
|
|
if (!lineStyle) {
|
|
throw new TypeError(
|
|
`Primitives::${method} expects lineStyle as ${where} parameter`
|
|
);
|
|
}
|
|
if (fillStyle) {
|
|
const {color} = fillStyle;
|
|
this.primitives.beginFill(color.toInteger(), color.alpha)
|
|
}
|
|
const {color, thickness} = lineStyle;
|
|
this.primitives.lineStyle(thickness, color.toInteger(), color.alpha)
|
|
fn();
|
|
if (fillStyle) {
|
|
this.primitives.endFill();
|
|
}
|
|
}
|
|
|
|
}
|