avocado-old/packages/graphics/primitives.js
2019-03-18 20:06:47 -05:00

68 lines
1.6 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.internal = new PIXI.Graphics();
}
clear() {
this.internal.clear();
}
drawCircle(position, radius, lineStyle, fillStyle) {
this._wrapStyle('drawCircle', '3rd', lineStyle, fillStyle, () => {
this.internal.drawCircle(position[0], position[1], radius);
});
}
drawLine(p1, p2, lineStyle, fillStyle) {
this._wrapStyle('drawLine', '3rd', lineStyle, fillStyle, () => {
this.internal.moveTo(p1[0], p1[1]);
this.internal.lineTo(p2[0], p2[1]);
});
}
drawRectangle(rectangle, lineStyle, fillStyle) {
this._wrapStyle('drawLine', '2nd', lineStyle, fillStyle, () => {
this.internal.drawRect(
rectangle[0],
rectangle[1],
rectangle[2],
rectangle[3]
);
});
}
_wrapStyle(method, where, lineStyle, fillStyle, fn) {
if (!lineStyle) {
throw new TypeError(
`Primitives::${method} expects lineStyle as ${where} parameter`
);
}
if (fillStyle) {
const {color} = fillStyle;
this.internal.beginFill(color.toInteger(), color.alpha)
}
const {color, thickness} = lineStyle;
this.internal.lineStyle(thickness, color.toInteger(), color.alpha)
fn();
if (fillStyle) {
this.internal.endFill();
}
}
}