Files
rough/bin/generator-async.js

131 lines
4.7 KiB
JavaScript

import { RoughGenerator } from './generator';
export class RoughGeneratorAsync extends RoughGenerator {
// @ts-ignore
async line(x1, y1, x2, y2, options) {
const o = this._options(options);
return this._drawable('line', [await this.lib.line(x1, y1, x2, y2, o)], o);
}
// @ts-ignore
async rectangle(x, y, width, height, options) {
const o = this._options(options);
const paths = [];
if (o.fill) {
const points = [[x, y], [x + width, y], [x + width, y + height], [x, y + height]];
if (o.fillStyle === 'solid') {
paths.push(await this.lib.solidFillPolygon(points, o));
}
else {
paths.push(await this.lib.patternFillPolygon(points, o));
}
}
paths.push(await this.lib.rectangle(x, y, width, height, o));
return this._drawable('rectangle', paths, o);
}
// @ts-ignore
async ellipse(x, y, width, height, options) {
const o = this._options(options);
const paths = [];
if (o.fill) {
if (o.fillStyle === 'solid') {
const shape = await this.lib.ellipse(x, y, width, height, o);
shape.type = 'fillPath';
paths.push(shape);
}
else {
paths.push(await this.lib.patternFillEllipse(x, y, width, height, o));
}
}
paths.push(await this.lib.ellipse(x, y, width, height, o));
return this._drawable('ellipse', paths, o);
}
// @ts-ignore
async circle(x, y, diameter, options) {
const ret = await this.ellipse(x, y, diameter, diameter, options);
ret.shape = 'circle';
return ret;
}
// @ts-ignore
async linearPath(points, options) {
const o = this._options(options);
return this._drawable('linearPath', [await this.lib.linearPath(points, false, o)], o);
}
// @ts-ignore
async arc(x, y, width, height, start, stop, closed = false, options) {
const o = this._options(options);
const paths = [];
if (closed && o.fill) {
if (o.fillStyle === 'solid') {
const shape = await this.lib.arc(x, y, width, height, start, stop, true, false, o);
shape.type = 'fillPath';
paths.push(shape);
}
else {
paths.push(await this.lib.patternFillArc(x, y, width, height, start, stop, o));
}
}
paths.push(await this.lib.arc(x, y, width, height, start, stop, closed, true, o));
return this._drawable('arc', paths, o);
}
// @ts-ignore
async curve(points, options) {
const o = this._options(options);
return this._drawable('curve', [await this.lib.curve(points, o)], o);
}
// @ts-ignore
async polygon(points, options) {
const o = this._options(options);
const paths = [];
if (o.fill) {
if (o.fillStyle === 'solid') {
paths.push(await this.lib.solidFillPolygon(points, o));
}
else {
const size = this.computePolygonSize(points);
const fillPoints = [
[0, 0],
[size[0], 0],
[size[0], size[1]],
[0, size[1]]
];
const shape = await this.lib.patternFillPolygon(fillPoints, o);
shape.type = 'path2Dpattern';
shape.size = size;
shape.path = this.polygonPath(points);
paths.push(shape);
}
}
paths.push(await this.lib.linearPath(points, true, o));
return this._drawable('polygon', paths, o);
}
// @ts-ignore
async path(d, options) {
const o = this._options(options);
const paths = [];
if (!d) {
return this._drawable('path', paths, o);
}
if (o.fill) {
if (o.fillStyle === 'solid') {
const shape = { type: 'path2Dfill', path: d, ops: [] };
paths.push(shape);
}
else {
const size = this.computePathSize(d);
const points = [
[0, 0],
[size[0], 0],
[size[0], size[1]],
[0, size[1]]
];
const shape = await this.lib.patternFillPolygon(points, o);
shape.type = 'path2Dpattern';
shape.size = size;
shape.path = d;
paths.push(shape);
}
}
paths.push(await this.lib.svgPath(d, o));
return this._drawable('path', paths, o);
}
}