avocado-old/packages/graphics/primitives.js

72 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-03-18 20:06:47 -05:00
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();
2019-03-18 20:06:47 -05:00
}
clear() {
this.primitives.clear();
2019-03-18 20:06:47 -05:00
}
drawCircle(position, radius, lineStyle, fillStyle) {
this._wrapStyle('drawCircle', '3rd', lineStyle, fillStyle, () => {
this.primitives.drawCircle(position[0], position[1], radius);
2019-03-18 20:06:47 -05:00
});
}
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]);
2019-03-18 20:06:47 -05:00
});
}
drawRectangle(rectangle, lineStyle, fillStyle) {
this._wrapStyle('drawLine', '2nd', lineStyle, fillStyle, () => {
this.primitives.drawRect(
2019-03-18 20:06:47 -05:00
rectangle[0],
rectangle[1],
rectangle[2],
rectangle[3]
);
});
}
get internal() {
return this.primitives;
}
2019-03-18 20:06:47 -05:00
_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)
2019-03-18 20:06:47 -05:00
}
const {color, thickness} = lineStyle;
this.primitives.lineStyle(thickness, color.toInteger(), color.alpha)
2019-03-18 20:06:47 -05:00
fn();
if (fillStyle) {
this.primitives.endFill();
2019-03-18 20:06:47 -05:00
}
}
}