@visactor/vrender-core
Version:
```typescript import { xxx } from '@visactor/vrender-core'; ```
62 lines (54 loc) • 2.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: !0
}), exports.RectSymbol = exports.rectSize = exports.rectSizeArray = void 0;
const vutils_1 = require("@visactor/vutils"), base_1 = require("./base");
function rectSizeArray(ctx, size, x, y) {
return ctx.rect(x - size[0] / 2, y - size[1] / 2, size[0], size[1]), !1;
}
function rectSize(ctx, size, x, y) {
const w = size, h = size / 2;
return ctx.rect(x - w / 2, y - h / 2, w, h), !1;
}
exports.rectSizeArray = rectSizeArray, exports.rectSize = rectSize;
class RectSymbol extends base_1.BaseSymbol {
constructor() {
super(...arguments), this.type = "rect", this.pathStr = "M -0.5,0.25 L 0.5,0.25 L 0.5,-0.25,L -0.5,-0.25 Z";
}
draw(ctx, size, x, y) {
return (0, vutils_1.isNumber)(size) ? rectSize(ctx, size, x, y) : rectSizeArray(ctx, size, x, y);
}
drawWithClipRange(ctx, size, x, y, clipRange, z, cb) {
(0, vutils_1.isNumber)(size) && (size = [ size, size / 2 ]);
const drawLength = 2 * (size[0] + size[1]) * clipRange, points = [ {
x: x + size[0] / 2,
y: y - size[1] / 2
}, {
x: x + size[0] / 2,
y: y + size[1] / 2
}, {
x: x - size[0] / 2,
y: y + size[1] / 2
}, {
x: x - size[0] / 2,
y: y - size[1] / 2
} ];
let currLength = 0, lastP = points[3];
ctx.moveTo(lastP.x, lastP.y);
for (let i = 0; i < points.length; i++) {
const p = points[i], len = Math.sqrt((p.x - lastP.x) * (p.x - lastP.x) + (p.y - lastP.y) * (p.y - lastP.y));
if (currLength + len > drawLength) {
const dx = (p.x - lastP.x) * (drawLength - currLength) / len, dy = (p.y - lastP.y) * (drawLength - currLength) / len;
ctx.lineTo(lastP.x + dx, lastP.y + dy);
break;
}
ctx.lineTo(p.x, p.y), lastP = p, currLength += len;
}
return !1;
}
drawOffset(ctx, size, x, y, offset) {
return (0, vutils_1.isNumber)(size) ? rectSize(ctx, size + 2 * offset, x, y) : rectSizeArray(ctx, [ size[0] + 2 * offset, size[1] + 2 * offset ], x, y);
}
}
exports.RectSymbol = RectSymbol, exports.default = new RectSymbol;
//# sourceMappingURL=rect.js.map