@dcgw/excalibur-extended-label
Version:
Excalibur's Label class, but with extra features
148 lines • 7.77 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const excalibur_1 = require("excalibur");
const excalibur = require("excalibur");
const chain_1 = require("@softwareventures/chain");
const iterable_1 = require("@softwareventures/iterable");
const unknown_1 = require("unknown");
const offscreenCanvas = (() => {
let cache = null;
return (onscreenCanvas) => {
var _a, _b;
if (cache == null) {
cache = (_b = (_a = onscreenCanvas.ownerDocument) === null || _a === void 0 ? void 0 : _a.createElement("canvas")) !== null && _b !== void 0 ? _b : null;
}
if (cache != null) {
cache.width = onscreenCanvas.width;
cache.height = onscreenCanvas.height;
}
return cache;
};
})();
class Label extends excalibur_1.Actor {
constructor(options) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
super(options);
this.text = (_a = options === null || options === void 0 ? void 0 : options.text) !== null && _a !== void 0 ? _a : "";
this.bold = (_b = options === null || options === void 0 ? void 0 : options.bold) !== null && _b !== void 0 ? _b : false;
this.fontFamily = (_c = options === null || options === void 0 ? void 0 : options.fontFamily) !== null && _c !== void 0 ? _c : "sans-serif";
this.fontSize = (_d = options === null || options === void 0 ? void 0 : options.fontSize) !== null && _d !== void 0 ? _d : 10;
this.fontStyle = (_e = options === null || options === void 0 ? void 0 : options.fontStyle) !== null && _e !== void 0 ? _e : excalibur_1.FontStyle.Normal;
this.textAlign = (_f = options === null || options === void 0 ? void 0 : options.textAlign) !== null && _f !== void 0 ? _f : excalibur_1.TextAlign.Left;
this.baseAlign = (_g = options === null || options === void 0 ? void 0 : options.baseAlign) !== null && _g !== void 0 ? _g : excalibur_1.BaseAlign.Bottom;
this.lineHeight = options === null || options === void 0 ? void 0 : options.lineHeight;
this.wrapWidth = (_h = options === null || options === void 0 ? void 0 : options.wrapWidth) !== null && _h !== void 0 ? _h : Infinity;
this.outlineColor = (_j = options === null || options === void 0 ? void 0 : options.outlineColor) !== null && _j !== void 0 ? _j : excalibur_1.Color.Transparent;
this.outlineWidth = (_k = options === null || options === void 0 ? void 0 : options.outlineWidth) !== null && _k !== void 0 ? _k : 0;
this.shadowColor = (_l = options === null || options === void 0 ? void 0 : options.shadowColor) !== null && _l !== void 0 ? _l : excalibur_1.Color.Transparent;
this.shadowOffset = (_m = options === null || options === void 0 ? void 0 : options.shadowOffset) !== null && _m !== void 0 ? _m : excalibur_1.Vector.Zero;
this.shadowBlurRadius = (_o = options === null || options === void 0 ? void 0 : options.shadowBlurRadius) !== null && _o !== void 0 ? _o : 0;
this.alpha = (_q = (_p = options === null || options === void 0 ? void 0 : options.alpha) !== null && _p !== void 0 ? _p : options === null || options === void 0 ? void 0 : options.opacity) !== null && _q !== void 0 ? _q : 1;
if ((0, unknown_1.hasProperty)(excalibur, "Flags") &&
(0, unknown_1.hasProperty)(excalibur, "Legacy") &&
(0, unknown_1.hasProperty)(excalibur.Legacy, "LegacyDrawing") &&
!excalibur.Flags.isEnabled(excalibur.Legacy.LegacyDrawing)) {
throw new Error("excalibur-extended-label requires you to call Flags.useLegacyDrawing() before constructing Excalibur Engine");
}
}
draw(context, delta) {
var _a, _b;
const shadowVisible = this.shadowColor.a !== 0 &&
(this.shadowBlurRadius !== 0 || this.shadowOffset.x !== 0 || this.shadowOffset.y !== 0);
const canvas2 = shadowVisible ? offscreenCanvas(context.canvas) : null;
const context2 = (_a = canvas2 === null || canvas2 === void 0 ? void 0 : canvas2.getContext("2d")) !== null && _a !== void 0 ? _a : context;
context2.save();
if (context2 !== context) {
context2.setTransform(context.getTransform());
}
context2.translate(this.pos.x, this.pos.y);
context2.scale(this.scale.x, this.scale.y);
context2.rotate(this.rotation);
context2.textAlign = lookupTextAlign(this.textAlign);
context2.textBaseline = lookupBaseAlign(this.baseAlign);
context2.font = `${lookupFontStyle(this.fontStyle)} ${lookupFontWeight(this.bold)} ${this.fontSize}px ${this.fontFamily}`;
context2.lineWidth = this.outlineWidth * 2;
context2.strokeStyle =
this.outlineWidth === 0 ? "transparent" : this.outlineColor.toString();
context2.fillStyle = this.color.toString();
const lines = this.wrapLines(context2);
const lineHeight = (_b = this.lineHeight) !== null && _b !== void 0 ? _b : this.fontSize;
lines.forEach((line, i) => void context2.strokeText(line, 0, i * lineHeight));
lines.forEach((line, i) => void context2.fillText(line, 0, i * lineHeight));
context2.restore();
if (canvas2 != null) {
context.save();
context.resetTransform();
context.shadowBlur = this.shadowBlurRadius;
context.shadowColor = this.shadowColor.toString();
context.shadowOffsetX = this.shadowOffset.x;
context.shadowOffsetY = this.shadowOffset.y;
context.globalAlpha = this.alpha;
context.drawImage(canvas2, 0, 0);
context.restore();
}
}
wrapLines(context) {
const lines = this.text.split("\n");
if (isFinite(this.wrapWidth)) {
return (0, chain_1.default)(lines)
.map((0, iterable_1.mapFn)(line => line.split(/\s+/u)))
.map((0, iterable_1.mapFn)((0, iterable_1.foldFn)(([line, ...lines], word) => line == null
? [word]
: context.measureText(`${line} ${word}`).width < this.wrapWidth
? [`${line} ${word}`, ...lines]
: [word, line, ...lines], [])))
.map(iterable_1.concat)
.map(iterable_1.toArray)
.value.reverse();
}
else {
return lines;
}
}
}
exports.default = Label;
function lookupTextAlign(textAlign) {
switch (textAlign) {
case excalibur_1.TextAlign.Left:
return "left";
case excalibur_1.TextAlign.Right:
return "right";
case excalibur_1.TextAlign.Center:
return "center";
case excalibur_1.TextAlign.End:
return "end";
case excalibur_1.TextAlign.Start:
return "start";
}
}
function lookupBaseAlign(baseAlign) {
switch (baseAlign) {
case excalibur_1.BaseAlign.Alphabetic:
return "alphabetic";
case excalibur_1.BaseAlign.Bottom:
return "bottom";
case excalibur_1.BaseAlign.Hanging:
return "hanging";
case excalibur_1.BaseAlign.Ideographic:
return "ideographic";
case excalibur_1.BaseAlign.Middle:
return "middle";
case excalibur_1.BaseAlign.Top:
return "top";
}
}
function lookupFontStyle(fontStyle) {
switch (fontStyle) {
case excalibur_1.FontStyle.Italic:
return "italic";
case excalibur_1.FontStyle.Normal:
return "normal";
case excalibur_1.FontStyle.Oblique:
return "oblique";
}
}
function lookupFontWeight(bold) {
return bold ? "bold" : "normal";
}
//# sourceMappingURL=index.js.map