leafer-ui
Version:
一款革新、好用的 Canvas 引擎, 轻松实现专业图形编辑。适用于图形编辑、小游戏、互动应用、组态软件、生成图片与短视频等场景。
1,425 lines (1,409 loc) • 490 kB
JavaScript
var LeaferUI = (function (exports) {
'use strict';
const Platform = {
toURL(text, fileType) {
let url = encodeURIComponent(text);
if (fileType === 'text')
url = 'data:text/plain;charset=utf-8,' + url;
else if (fileType === 'svg')
url = 'data:image/svg+xml,' + url;
return url;
},
image: {
hitCanvasSize: 100,
maxCacheSize: 2560 * 1600,
maxPatternSize: 4096 * 2160,
crossOrigin: 'anonymous',
getRealURL(url) {
const { prefix, suffix } = Platform.image;
if (suffix && !url.startsWith('data:') && !url.startsWith('blob:'))
url += (url.includes("?") ? "&" : "?") + suffix;
if (prefix && url[0] === '/')
url = prefix + url;
return url;
}
}
};
const IncrementId = {
RUNTIME: 'runtime',
LEAF: 'leaf',
TASK: 'task',
CNAVAS: 'canvas',
IMAGE: 'image',
types: {},
create(typeName) {
const { types } = I$2;
if (types[typeName]) {
return types[typeName]++;
}
else {
types[typeName] = 1;
return 0;
}
}
};
const I$2 = IncrementId;
const { round: round$3, pow: pow$1, PI: PI$3 } = Math;
const MathHelper = {
within(value, min, max) {
if (typeof min === 'object')
max = min.max, min = min.min;
if (min !== undefined && value < min)
value = min;
if (max !== undefined && value > max)
value = max;
return value;
},
fourNumber(num, maxValue) {
let data;
if (num instanceof Array) {
switch (num.length) {
case 4:
data = maxValue === undefined ? num : [...num];
break;
case 2:
data = [num[0], num[1], num[0], num[1]];
break;
case 3:
data = [num[0], num[1], num[2], num[1]];
break;
case 1:
num = num[0];
break;
default:
num = 0;
}
}
if (!data)
data = [num, num, num, num];
if (maxValue)
for (let i = 0; i < 4; i++)
if (data[i] > maxValue)
data[i] = maxValue;
return data;
},
formatRotation(rotation, unsign) {
rotation %= 360;
if (unsign) {
if (rotation < 0)
rotation += 360;
}
else {
if (rotation > 180)
rotation -= 360;
if (rotation < -180)
rotation += 360;
}
return MathHelper.float(rotation);
},
getGapRotation(addRotation, gap, oldRotation = 0) {
let rotation = addRotation + oldRotation;
if (gap > 1) {
const r = Math.abs(rotation % gap);
if (r < 1 || r > gap - 1)
rotation = Math.round(rotation / gap) * gap;
}
return rotation - oldRotation;
},
float(num, maxLength) {
const a = maxLength !== undefined ? pow$1(10, maxLength) : 1000000000000;
num = round$3(num * a) / a;
return num === -0 ? 0 : num;
},
getScaleData(scale, size, originSize, scaleData) {
if (!scaleData)
scaleData = {};
if (size) {
const scaleX = (typeof size === 'number' ? size : size.width || 0) / originSize.width, scaleY = (typeof size === 'number' ? size : size.height || 0) / originSize.height;
scaleData.scaleX = scaleX || scaleY || 1;
scaleData.scaleY = scaleY || scaleX || 1;
}
else if (scale)
MathHelper.assignScale(scaleData, scale);
return scaleData;
},
assignScale(scaleData, scale) {
if (typeof scale === 'number') {
scaleData.scaleX = scaleData.scaleY = scale;
}
else {
scaleData.scaleX = scale.x;
scaleData.scaleY = scale.y;
}
},
randInt,
randColor(opacity) {
return `rgba(${randInt(255)},${randInt(255)},${randInt(255)},${opacity || 1})`;
}
};
function randInt(num) {
return Math.round(Math.random() * num);
}
const OneRadian = PI$3 / 180;
const PI2 = PI$3 * 2;
const PI_2 = PI$3 / 2;
function getPointData() { return { x: 0, y: 0 }; }
function getBoundsData() { return { x: 0, y: 0, width: 0, height: 0 }; }
function getMatrixData() { return { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 }; }
const { sin: sin$5, cos: cos$5, acos, sqrt: sqrt$3 } = Math;
const { float: float$1 } = MathHelper;
const tempPoint$3 = {};
function getWorld() {
return Object.assign(Object.assign(Object.assign({}, getMatrixData()), getBoundsData()), { scaleX: 1, scaleY: 1, rotation: 0, skewX: 0, skewY: 0 });
}
const MatrixHelper = {
defaultMatrix: getMatrixData(),
defaultWorld: getWorld(),
tempMatrix: {},
set(t, a = 1, b = 0, c = 0, d = 1, e = 0, f = 0) {
t.a = a;
t.b = b;
t.c = c;
t.d = d;
t.e = e;
t.f = f;
},
get: getMatrixData,
getWorld,
copy(t, matrix) {
t.a = matrix.a;
t.b = matrix.b;
t.c = matrix.c;
t.d = matrix.d;
t.e = matrix.e;
t.f = matrix.f;
},
translate(t, x, y) {
t.e += x;
t.f += y;
},
translateInner(t, x, y, hasOrigin) {
t.e += t.a * x + t.c * y;
t.f += t.b * x + t.d * y;
if (hasOrigin)
t.e -= x, t.f -= y;
},
scale(t, scaleX, scaleY = scaleX) {
t.a *= scaleX;
t.b *= scaleX;
t.c *= scaleY;
t.d *= scaleY;
},
scaleOfOuter(t, origin, scaleX, scaleY) {
M$6.toInnerPoint(t, origin, tempPoint$3);
M$6.scaleOfInner(t, tempPoint$3, scaleX, scaleY);
},
scaleOfInner(t, origin, scaleX, scaleY = scaleX) {
M$6.translateInner(t, origin.x, origin.y);
M$6.scale(t, scaleX, scaleY);
M$6.translateInner(t, -origin.x, -origin.y);
},
rotate(t, rotation) {
const { a, b, c, d } = t;
rotation *= OneRadian;
const cosR = cos$5(rotation);
const sinR = sin$5(rotation);
t.a = a * cosR - b * sinR;
t.b = a * sinR + b * cosR;
t.c = c * cosR - d * sinR;
t.d = c * sinR + d * cosR;
},
rotateOfOuter(t, origin, rotation) {
M$6.toInnerPoint(t, origin, tempPoint$3);
M$6.rotateOfInner(t, tempPoint$3, rotation);
},
rotateOfInner(t, origin, rotation) {
M$6.translateInner(t, origin.x, origin.y);
M$6.rotate(t, rotation);
M$6.translateInner(t, -origin.x, -origin.y);
},
skew(t, skewX, skewY) {
const { a, b, c, d } = t;
if (skewY) {
skewY *= OneRadian;
t.a = a + c * skewY;
t.b = b + d * skewY;
}
if (skewX) {
skewX *= OneRadian;
t.c = c + a * skewX;
t.d = d + b * skewX;
}
},
skewOfOuter(t, origin, skewX, skewY) {
M$6.toInnerPoint(t, origin, tempPoint$3);
M$6.skewOfInner(t, tempPoint$3, skewX, skewY);
},
skewOfInner(t, origin, skewX, skewY = 0) {
M$6.translateInner(t, origin.x, origin.y);
M$6.skew(t, skewX, skewY);
M$6.translateInner(t, -origin.x, -origin.y);
},
multiply(t, child) {
const { a, b, c, d, e, f } = t;
t.a = child.a * a + child.b * c;
t.b = child.a * b + child.b * d;
t.c = child.c * a + child.d * c;
t.d = child.c * b + child.d * d;
t.e = child.e * a + child.f * c + e;
t.f = child.e * b + child.f * d + f;
},
multiplyParent(t, parent, to, abcdChanged, childScaleData, scrollData) {
let { e, f } = t;
if (scrollData)
e += scrollData.scrollX, f += scrollData.scrollY;
to || (to = t);
if (abcdChanged === undefined)
abcdChanged = t.a !== 1 || t.b || t.c || t.d !== 1;
if (abcdChanged) {
const { a, b, c, d } = t;
to.a = a * parent.a + b * parent.c;
to.b = a * parent.b + b * parent.d;
to.c = c * parent.a + d * parent.c;
to.d = c * parent.b + d * parent.d;
if (childScaleData) {
to.scaleX = parent.scaleX * childScaleData.scaleX;
to.scaleY = parent.scaleY * childScaleData.scaleY;
}
}
else {
to.a = parent.a;
to.b = parent.b;
to.c = parent.c;
to.d = parent.d;
if (childScaleData) {
to.scaleX = parent.scaleX;
to.scaleY = parent.scaleY;
}
}
to.e = e * parent.a + f * parent.c + parent.e;
to.f = e * parent.b + f * parent.d + parent.f;
},
divide(t, child) {
M$6.multiply(t, M$6.tempInvert(child));
},
divideParent(t, parent) {
M$6.multiplyParent(t, M$6.tempInvert(parent));
},
tempInvert(t) {
const { tempMatrix } = M$6;
M$6.copy(tempMatrix, t);
M$6.invert(tempMatrix);
return tempMatrix;
},
invert(t) {
const { a, b, c, d, e, f } = t;
if (!b && !c) {
if (a === 1 && d === 1) {
t.e = -e;
t.f = -f;
}
else {
const s = 1 / (a * d);
t.a = d * s;
t.d = a * s;
t.e = -e * d * s;
t.f = -f * a * s;
}
}
else {
const s = 1 / (a * d - b * c);
t.a = d * s;
t.b = -b * s;
t.c = -c * s;
t.d = a * s;
t.e = -(e * d - f * c) * s;
t.f = -(f * a - e * b) * s;
}
},
toOuterPoint(t, inner, to, distance) {
const { x, y } = inner;
to || (to = inner);
to.x = x * t.a + y * t.c;
to.y = x * t.b + y * t.d;
if (!distance) {
to.x += t.e;
to.y += t.f;
}
},
toInnerPoint(t, outer, to, distance) {
const { a, b, c, d } = t;
const s = 1 / (a * d - b * c);
const { x, y } = outer;
to || (to = outer);
to.x = (x * d - y * c) * s;
to.y = (y * a - x * b) * s;
if (!distance) {
const { e, f } = t;
to.x -= (e * d - f * c) * s;
to.y -= (f * a - e * b) * s;
}
},
setLayout(t, layout, origin, around, bcChanged) {
const { x, y, scaleX, scaleY } = layout;
if (bcChanged === undefined)
bcChanged = layout.rotation || layout.skewX || layout.skewY;
if (bcChanged) {
const { rotation, skewX, skewY } = layout;
const r = rotation * OneRadian;
const cosR = cos$5(r);
const sinR = sin$5(r);
if (skewX || skewY) {
const sx = skewX * OneRadian;
const sy = skewY * OneRadian;
t.a = (cosR + sy * -sinR) * scaleX;
t.b = (sinR + sy * cosR) * scaleX;
t.c = (-sinR + sx * cosR) * scaleY;
t.d = (cosR + sx * sinR) * scaleY;
}
else {
t.a = cosR * scaleX;
t.b = sinR * scaleX;
t.c = -sinR * scaleY;
t.d = cosR * scaleY;
}
}
else {
t.a = scaleX;
t.b = 0;
t.c = 0;
t.d = scaleY;
}
t.e = x;
t.f = y;
if (origin = origin || around)
M$6.translateInner(t, -origin.x, -origin.y, !around);
},
getLayout(t, origin, around, firstSkewY) {
const { a, b, c, d, e, f } = t;
let x = e, y = f, scaleX, scaleY, rotation, skewX, skewY;
if (b || c) {
const s = a * d - b * c;
if (c && !firstSkewY) {
scaleX = sqrt$3(a * a + b * b);
scaleY = s / scaleX;
const cosR = a / scaleX;
rotation = b > 0 ? acos(cosR) : -acos(cosR);
}
else {
scaleY = sqrt$3(c * c + d * d);
scaleX = s / scaleY;
const cosR = c / scaleY;
rotation = PI_2 - (d > 0 ? acos(-cosR) : -acos(cosR));
}
const cosR = float$1(cos$5(rotation));
const sinR = sin$5(rotation);
scaleX = float$1(scaleX), scaleY = float$1(scaleY);
skewX = cosR ? float$1((c / scaleY + sinR) / cosR / OneRadian, 9) : 0;
skewY = cosR ? float$1((b / scaleX - sinR) / cosR / OneRadian, 9) : 0;
rotation = float$1(rotation / OneRadian);
}
else {
scaleX = a;
scaleY = d;
rotation = skewX = skewY = 0;
}
if (origin = around || origin) {
x += origin.x * a + origin.y * c;
y += origin.x * b + origin.y * d;
if (!around)
x -= origin.x, y -= origin.y;
}
return { x, y, scaleX, scaleY, rotation, skewX, skewY };
},
withScale(t, scaleX, scaleY = scaleX) {
const world = t;
if (!scaleX || !scaleY) {
const { a, b, c, d } = t;
if (b || c) {
scaleX = sqrt$3(a * a + b * b);
scaleY = (a * d - b * c) / scaleX;
}
else {
scaleX = a;
scaleY = d;
}
}
world.scaleX = scaleX;
world.scaleY = scaleY;
return world;
},
reset(t) {
M$6.set(t);
}
};
const M$6 = MatrixHelper;
const { toInnerPoint: toInnerPoint$2, toOuterPoint: toOuterPoint$3 } = MatrixHelper;
const { sin: sin$4, cos: cos$4, abs: abs$3, sqrt: sqrt$2, atan2: atan2$2, min: min$1, round: round$2 } = Math;
const PointHelper = {
defaultPoint: getPointData(),
tempPoint: {},
tempRadiusPoint: {},
set(t, x = 0, y = 0) {
t.x = x;
t.y = y;
},
setRadius(t, x, y) {
t.radiusX = x;
t.radiusY = y === undefined ? x : y;
},
copy(t, point) {
t.x = point.x;
t.y = point.y;
},
copyFrom(t, x, y) {
t.x = x;
t.y = y;
},
round(t, halfPixel) {
t.x = halfPixel ? round$2(t.x - 0.5) + 0.5 : round$2(t.x);
t.y = halfPixel ? round$2(t.y - 0.5) + 0.5 : round$2(t.y);
},
move(t, x, y) {
if (typeof x === 'object')
t.x += x.x, t.y += x.y;
else
t.x += x, t.y += y;
},
scale(t, scaleX, scaleY = scaleX) {
if (t.x)
t.x *= scaleX;
if (t.y)
t.y *= scaleY;
},
scaleOf(t, origin, scaleX, scaleY = scaleX) {
t.x += (t.x - origin.x) * (scaleX - 1);
t.y += (t.y - origin.y) * (scaleY - 1);
},
rotate(t, rotation, origin) {
if (!origin)
origin = P$5.defaultPoint;
rotation *= OneRadian;
const cosR = cos$4(rotation);
const sinR = sin$4(rotation);
const rx = t.x - origin.x;
const ry = t.y - origin.y;
t.x = origin.x + rx * cosR - ry * sinR;
t.y = origin.y + rx * sinR + ry * cosR;
},
tempToInnerOf(t, matrix) {
const { tempPoint: temp } = P$5;
copy$b(temp, t);
toInnerPoint$2(matrix, temp, temp);
return temp;
},
tempToOuterOf(t, matrix) {
const { tempPoint: temp } = P$5;
copy$b(temp, t);
toOuterPoint$3(matrix, temp, temp);
return temp;
},
tempToInnerRadiusPointOf(t, matrix) {
const { tempRadiusPoint: temp } = P$5;
copy$b(temp, t);
P$5.toInnerRadiusPointOf(t, matrix, temp);
return temp;
},
toInnerRadiusPointOf(t, matrix, to) {
to || (to = t);
toInnerPoint$2(matrix, t, to);
to.radiusX = Math.abs(t.radiusX / matrix.scaleX);
to.radiusY = Math.abs(t.radiusY / matrix.scaleY);
},
toInnerOf(t, matrix, to) {
toInnerPoint$2(matrix, t, to);
},
toOuterOf(t, matrix, to) {
toOuterPoint$3(matrix, t, to);
},
getCenter(t, to) {
return { x: t.x + (to.x - t.x) / 2, y: t.y + (to.y - t.y) / 2 };
},
getCenterX(x1, x2) {
return x1 + (x2 - x1) / 2;
},
getCenterY(y1, y2) {
return y1 + (y2 - y1) / 2;
},
getDistance(t, point) {
return getDistanceFrom(t.x, t.y, point.x, point.y);
},
getDistanceFrom(x1, y1, x2, y2) {
const x = abs$3(x2 - x1);
const y = abs$3(y2 - y1);
return sqrt$2(x * x + y * y);
},
getMinDistanceFrom(x1, y1, x2, y2, x3, y3) {
return min$1(getDistanceFrom(x1, y1, x2, y2), getDistanceFrom(x2, y2, x3, y3));
},
getAngle(t, to) {
return getAtan2(t, to) / OneRadian;
},
getRotation(t, origin, to, toOrigin) {
if (!toOrigin)
toOrigin = origin;
return P$5.getRadianFrom(t.x, t.y, origin.x, origin.y, to.x, to.y, toOrigin.x, toOrigin.y) / OneRadian;
},
getRadianFrom(fromX, fromY, originX, originY, toX, toY, toOriginX, toOriginY) {
if (toOriginX === undefined)
toOriginX = originX, toOriginY = originY;
const a = fromX - originX;
const b = fromY - originY;
const c = toX - toOriginX;
const d = toY - toOriginY;
return Math.atan2(a * d - b * c, a * c + b * d);
},
getAtan2(t, to) {
return atan2$2(to.y - t.y, to.x - t.x);
},
getDistancePoint(t, to, distance, changeTo) {
const r = getAtan2(t, to);
to = changeTo ? to : {};
to.x = t.x + cos$4(r) * distance;
to.y = t.y + sin$4(r) * distance;
return to;
},
toNumberPoints(originPoints) {
let points = originPoints;
if (typeof originPoints[0] === 'object')
points = [], originPoints.forEach(p => points.push(p.x, p.y));
return points;
},
reset(t) {
P$5.reset(t);
}
};
const P$5 = PointHelper;
const { getDistanceFrom, copy: copy$b, getAtan2 } = P$5;
class Point {
constructor(x, y) {
this.set(x, y);
}
set(x, y) {
typeof x === 'object' ? PointHelper.copy(this, x) : PointHelper.set(this, x, y);
return this;
}
get() {
const { x, y } = this;
return { x, y };
}
clone() {
return new Point(this);
}
move(x, y) {
PointHelper.move(this, x, y);
return this;
}
scale(scaleX, scaleY) {
PointHelper.scale(this, scaleX, scaleY);
return this;
}
scaleOf(origin, scaleX, scaleY) {
PointHelper.scaleOf(this, origin, scaleX, scaleY);
return this;
}
rotate(rotation, origin) {
PointHelper.rotate(this, rotation, origin);
return this;
}
rotateOf(origin, rotation) {
PointHelper.rotate(this, rotation, origin);
return this;
}
getRotation(origin, to, toOrigin) {
return PointHelper.getRotation(this, origin, to, toOrigin);
}
toInnerOf(matrix, to) {
PointHelper.toInnerOf(this, matrix, to);
return this;
}
toOuterOf(matrix, to) {
PointHelper.toOuterOf(this, matrix, to);
return this;
}
getCenter(to) {
return new Point(PointHelper.getCenter(this, to));
}
getDistance(to) {
return PointHelper.getDistance(this, to);
}
getDistancePoint(to, distance, changeTo) {
return new Point(PointHelper.getDistancePoint(this, to, distance, changeTo));
}
getAngle(to) {
return PointHelper.getAngle(this, to);
}
getAtan2(to) {
return PointHelper.getAtan2(this, to);
}
reset() {
PointHelper.reset(this);
return this;
}
}
const tempPoint$2 = new Point();
class Matrix {
constructor(a, b, c, d, e, f) {
this.set(a, b, c, d, e, f);
}
set(a, b, c, d, e, f) {
typeof a === 'object' ? MatrixHelper.copy(this, a) : MatrixHelper.set(this, a, b, c, d, e, f);
return this;
}
setWith(dataWithScale) {
MatrixHelper.copy(this, dataWithScale);
this.scaleX = dataWithScale.scaleX;
this.scaleY = dataWithScale.scaleY;
return this;
}
get() {
const { a, b, c, d, e, f } = this;
return { a, b, c, d, e, f };
}
clone() {
return new Matrix(this);
}
translate(x, y) {
MatrixHelper.translate(this, x, y);
return this;
}
translateInner(x, y) {
MatrixHelper.translateInner(this, x, y);
return this;
}
scale(x, y) {
MatrixHelper.scale(this, x, y);
return this;
}
scaleWith(x, y) {
MatrixHelper.scale(this, x, y);
this.scaleX *= x;
this.scaleY *= y || x;
return this;
}
scaleOfOuter(origin, x, y) {
MatrixHelper.scaleOfOuter(this, origin, x, y);
return this;
}
scaleOfInner(origin, x, y) {
MatrixHelper.scaleOfInner(this, origin, x, y);
return this;
}
rotate(angle) {
MatrixHelper.rotate(this, angle);
return this;
}
rotateOfOuter(origin, angle) {
MatrixHelper.rotateOfOuter(this, origin, angle);
return this;
}
rotateOfInner(origin, angle) {
MatrixHelper.rotateOfInner(this, origin, angle);
return this;
}
skew(x, y) {
MatrixHelper.skew(this, x, y);
return this;
}
skewOfOuter(origin, x, y) {
MatrixHelper.skewOfOuter(this, origin, x, y);
return this;
}
skewOfInner(origin, x, y) {
MatrixHelper.skewOfInner(this, origin, x, y);
return this;
}
multiply(child) {
MatrixHelper.multiply(this, child);
return this;
}
multiplyParent(parent) {
MatrixHelper.multiplyParent(this, parent);
return this;
}
divide(child) {
MatrixHelper.divide(this, child);
return this;
}
divideParent(parent) {
MatrixHelper.divideParent(this, parent);
return this;
}
invert() {
MatrixHelper.invert(this);
return this;
}
invertWith() {
MatrixHelper.invert(this);
this.scaleX = 1 / this.scaleX;
this.scaleY = 1 / this.scaleY;
return this;
}
toOuterPoint(inner, to, distance) {
MatrixHelper.toOuterPoint(this, inner, to, distance);
}
toInnerPoint(outer, to, distance) {
MatrixHelper.toInnerPoint(this, outer, to, distance);
}
setLayout(data, origin, around) {
MatrixHelper.setLayout(this, data, origin, around);
return this;
}
getLayout(origin, around, firstSkewY) {
return MatrixHelper.getLayout(this, origin, around, firstSkewY);
}
withScale(scaleX, scaleY) {
return MatrixHelper.withScale(this, scaleX, scaleY);
}
reset() {
MatrixHelper.reset(this);
}
}
const tempMatrix$1 = new Matrix();
const TwoPointBoundsHelper = {
tempPointBounds: {},
setPoint(t, minX, minY) {
t.minX = t.maxX = minX;
t.minY = t.maxY = minY;
},
addPoint(t, x, y) {
t.minX = x < t.minX ? x : t.minX;
t.minY = y < t.minY ? y : t.minY;
t.maxX = x > t.maxX ? x : t.maxX;
t.maxY = y > t.maxY ? y : t.maxY;
},
addBounds(t, x, y, width, height) {
addPoint$3(t, x, y);
addPoint$3(t, x + width, y + height);
},
copy(t, pb) {
t.minX = pb.minX;
t.minY = pb.minY;
t.maxX = pb.maxX;
t.maxY = pb.maxY;
},
addPointBounds(t, pb) {
t.minX = pb.minX < t.minX ? pb.minX : t.minX;
t.minY = pb.minY < t.minY ? pb.minY : t.minY;
t.maxX = pb.maxX > t.maxX ? pb.maxX : t.maxX;
t.maxY = pb.maxY > t.maxY ? pb.maxY : t.maxY;
},
toBounds(t, setBounds) {
setBounds.x = t.minX;
setBounds.y = t.minY;
setBounds.width = t.maxX - t.minX;
setBounds.height = t.maxY - t.minY;
}
};
const { addPoint: addPoint$3 } = TwoPointBoundsHelper;
exports.Direction4 = void 0;
(function (Direction4) {
Direction4[Direction4["top"] = 0] = "top";
Direction4[Direction4["right"] = 1] = "right";
Direction4[Direction4["bottom"] = 2] = "bottom";
Direction4[Direction4["left"] = 3] = "left";
})(exports.Direction4 || (exports.Direction4 = {}));
exports.Direction9 = void 0;
(function (Direction9) {
Direction9[Direction9["topLeft"] = 0] = "topLeft";
Direction9[Direction9["top"] = 1] = "top";
Direction9[Direction9["topRight"] = 2] = "topRight";
Direction9[Direction9["right"] = 3] = "right";
Direction9[Direction9["bottomRight"] = 4] = "bottomRight";
Direction9[Direction9["bottom"] = 5] = "bottom";
Direction9[Direction9["bottomLeft"] = 6] = "bottomLeft";
Direction9[Direction9["left"] = 7] = "left";
Direction9[Direction9["center"] = 8] = "center";
Direction9[Direction9["top-left"] = 0] = "top-left";
Direction9[Direction9["top-right"] = 2] = "top-right";
Direction9[Direction9["bottom-right"] = 4] = "bottom-right";
Direction9[Direction9["bottom-left"] = 6] = "bottom-left";
})(exports.Direction9 || (exports.Direction9 = {}));
const directionData = [
{ x: 0, y: 0 },
{ x: 0.5, y: 0 },
{ x: 1, y: 0 },
{ x: 1, y: 0.5 },
{ x: 1, y: 1 },
{ x: 0.5, y: 1 },
{ x: 0, y: 1 },
{ x: 0, y: 0.5 },
{ x: 0.5, y: 0.5 }
];
directionData.forEach(item => item.type = 'percent');
const AroundHelper = {
directionData,
tempPoint: {},
get: get$4,
toPoint(around, box, to, onlyBoxSize, content, onlyContentSize) {
const point = get$4(around);
to.x = point.x;
to.y = point.y;
if (point.type === 'percent') {
to.x *= box.width;
to.y *= box.height;
if (content) {
if (!onlyContentSize)
to.x -= content.x, to.y -= content.y;
if (point.x)
to.x -= (point.x === 1) ? content.width : (point.x === 0.5 ? point.x * content.width : 0);
if (point.y)
to.y -= (point.y === 1) ? content.height : (point.y === 0.5 ? point.y * content.height : 0);
}
}
if (!onlyBoxSize)
to.x += box.x, to.y += box.y;
},
getPoint(around, box, to) {
if (!to)
to = {};
AroundHelper.toPoint(around, box, to, true);
return to;
}
};
function get$4(around) {
return typeof around === 'string' ? directionData[exports.Direction9[around]] : around;
}
const { toPoint: toPoint$5 } = AroundHelper;
const AlignHelper = {
toPoint(align, content, box, to, onlyBoxSize, onlyContentSize) {
toPoint$5(align, box, to, onlyBoxSize, content, onlyContentSize);
}
};
const { tempPointBounds: tempPointBounds$1, setPoint: setPoint$2, addPoint: addPoint$2, toBounds: toBounds$3 } = TwoPointBoundsHelper;
const { toOuterPoint: toOuterPoint$2 } = MatrixHelper;
const { float, fourNumber } = MathHelper;
const { floor, ceil: ceil$2 } = Math;
let right$1, bottom$1, boundsRight, boundsBottom;
const point = {};
const toPoint$4 = {};
const tempBounds$2 = {};
const BoundsHelper = {
tempBounds: tempBounds$2,
set(t, x = 0, y = 0, width = 0, height = 0) {
t.x = x;
t.y = y;
t.width = width;
t.height = height;
},
copy(t, bounds) {
t.x = bounds.x;
t.y = bounds.y;
t.width = bounds.width;
t.height = bounds.height;
},
copyAndSpread(t, bounds, spread, isShrink, side) {
const { x, y, width, height } = bounds;
if (spread instanceof Array) {
const four = fourNumber(spread);
isShrink
? B.set(t, x + four[3], y + four[0], width - four[1] - four[3], height - four[2] - four[0])
: B.set(t, x - four[3], y - four[0], width + four[1] + four[3], height + four[2] + four[0]);
}
else {
if (isShrink)
spread = -spread;
B.set(t, x - spread, y - spread, width + spread * 2, height + spread * 2);
}
if (side) {
if (side === 'width')
t.y = y, t.height = height;
else
t.x = x, t.width = width;
}
},
minX(t) { return t.width > 0 ? t.x : t.x + t.width; },
minY(t) { return t.height > 0 ? t.y : t.y + t.height; },
maxX(t) { return t.width > 0 ? t.x + t.width : t.x; },
maxY(t) { return t.height > 0 ? t.y + t.height : t.y; },
move(t, x, y) {
t.x += x;
t.y += y;
},
getByMove(t, x, y) {
t = Object.assign({}, t);
B.move(t, x, y);
return t;
},
toOffsetOutBounds(t, to, parent) {
if (!to) {
to = t;
}
else {
copy$a(to, t);
}
if (parent) {
to.offsetX = -(B.maxX(parent) - t.x);
to.offsetY = -(B.maxY(parent) - t.y);
}
else {
to.offsetX = t.x + t.width;
to.offsetY = t.y + t.height;
}
B.move(to, -to.offsetX, -to.offsetY);
},
scale(t, scaleX, scaleY = scaleX, onlySize) {
onlySize || PointHelper.scale(t, scaleX, scaleY);
t.width *= scaleX;
t.height *= scaleY;
},
scaleOf(t, origin, scaleX, scaleY = scaleX) {
PointHelper.scaleOf(t, origin, scaleX, scaleY);
t.width *= scaleX;
t.height *= scaleY;
},
tempToOuterOf(t, matrix) {
B.copy(tempBounds$2, t);
B.toOuterOf(tempBounds$2, matrix);
return tempBounds$2;
},
getOuterOf(t, matrix) {
t = Object.assign({}, t);
B.toOuterOf(t, matrix);
return t;
},
toOuterOf(t, matrix, to) {
to || (to = t);
if (matrix.b === 0 && matrix.c === 0) {
const { a, d } = matrix;
if (a > 0) {
to.width = t.width * a;
to.x = matrix.e + t.x * a;
}
else {
to.width = t.width * -a;
to.x = matrix.e + t.x * a - to.width;
}
if (d > 0) {
to.height = t.height * d;
to.y = matrix.f + t.y * d;
}
else {
to.height = t.height * -d;
to.y = matrix.f + t.y * d - to.height;
}
}
else {
point.x = t.x;
point.y = t.y;
toOuterPoint$2(matrix, point, toPoint$4);
setPoint$2(tempPointBounds$1, toPoint$4.x, toPoint$4.y);
point.x = t.x + t.width;
toOuterPoint$2(matrix, point, toPoint$4);
addPoint$2(tempPointBounds$1, toPoint$4.x, toPoint$4.y);
point.y = t.y + t.height;
toOuterPoint$2(matrix, point, toPoint$4);
addPoint$2(tempPointBounds$1, toPoint$4.x, toPoint$4.y);
point.x = t.x;
toOuterPoint$2(matrix, point, toPoint$4);
addPoint$2(tempPointBounds$1, toPoint$4.x, toPoint$4.y);
toBounds$3(tempPointBounds$1, to);
}
},
toInnerOf(t, matrix, to) {
to || (to = t);
B.move(to, -matrix.e, -matrix.f);
B.scale(to, 1 / matrix.a, 1 / matrix.d);
},
getFitMatrix(t, put, baseScale = 1) {
const scale = Math.min(baseScale, B.getFitScale(t, put));
return new Matrix(scale, 0, 0, scale, -put.x * scale, -put.y * scale);
},
getFitScale(t, put, isCoverMode) {
const sw = t.width / put.width, sh = t.height / put.height;
return isCoverMode ? Math.max(sw, sh) : Math.min(sw, sh);
},
put(t, put, align = 'center', putScale = 1, changeSize = true, to) {
to || (to = put);
if (typeof putScale === 'string')
putScale = B.getFitScale(t, put, putScale === 'cover');
tempBounds$2.width = changeSize ? put.width *= putScale : put.width * putScale;
tempBounds$2.height = changeSize ? put.height *= putScale : put.height * putScale;
AlignHelper.toPoint(align, tempBounds$2, t, to, true, true);
},
getSpread(t, spread, side) {
const n = {};
B.copyAndSpread(n, t, spread, false, side);
return n;
},
spread(t, spread, side) {
B.copyAndSpread(t, t, spread, false, side);
},
shrink(t, shrink, side) {
B.copyAndSpread(t, t, shrink, true, side);
},
ceil(t) {
const { x, y } = t;
t.x = floor(t.x);
t.y = floor(t.y);
t.width = x > t.x ? ceil$2(t.width + x - t.x) : ceil$2(t.width);
t.height = y > t.y ? ceil$2(t.height + y - t.y) : ceil$2(t.height);
},
unsign(t) {
if (t.width < 0) {
t.x += t.width;
t.width = -t.width;
}
if (t.height < 0) {
t.y += t.height;
t.height = -t.height;
}
},
float(t, maxLength) {
t.x = float(t.x, maxLength);
t.y = float(t.y, maxLength);
t.width = float(t.width, maxLength);
t.height = float(t.height, maxLength);
},
add(t, bounds, isPoint) {
right$1 = t.x + t.width;
bottom$1 = t.y + t.height;
boundsRight = bounds.x;
boundsBottom = bounds.y;
if (!isPoint) {
boundsRight += bounds.width;
boundsBottom += bounds.height;
}
right$1 = right$1 > boundsRight ? right$1 : boundsRight;
bottom$1 = bottom$1 > boundsBottom ? bottom$1 : boundsBottom;
t.x = t.x < bounds.x ? t.x : bounds.x;
t.y = t.y < bounds.y ? t.y : bounds.y;
t.width = right$1 - t.x;
t.height = bottom$1 - t.y;
},
addList(t, list) {
B.setListWithFn(t, list, undefined, true);
},
setList(t, list, addMode = false) {
B.setListWithFn(t, list, undefined, addMode);
},
addListWithFn(t, list, boundsDataFn) {
B.setListWithFn(t, list, boundsDataFn, true);
},
setListWithFn(t, list, boundsDataFn, addMode = false) {
let bounds, first = true;
for (let i = 0, len = list.length; i < len; i++) {
bounds = boundsDataFn ? boundsDataFn(list[i]) : list[i];
if (bounds && (bounds.width || bounds.height)) {
if (first) {
first = false;
if (!addMode)
copy$a(t, bounds);
}
else {
add$1(t, bounds);
}
}
}
if (first)
B.reset(t);
},
setPoints(t, points) {
points.forEach((point, index) => index === 0 ? setPoint$2(tempPointBounds$1, point.x, point.y) : addPoint$2(tempPointBounds$1, point.x, point.y));
toBounds$3(tempPointBounds$1, t);
},
setPoint(t, point) {
B.set(t, point.x, point.y);
},
addPoint(t, point) {
add$1(t, point, true);
},
getPoints(t) {
const { x, y, width, height } = t;
return [
{ x, y },
{ x: x + width, y },
{ x: x + width, y: y + height },
{ x, y: y + height }
];
},
hitRadiusPoint(t, point, pointMatrix) {
if (pointMatrix)
point = PointHelper.tempToInnerRadiusPointOf(point, pointMatrix);
return (point.x >= t.x - point.radiusX && point.x <= t.x + t.width + point.radiusX) && (point.y >= t.y - point.radiusY && point.y <= t.y + t.height + point.radiusY);
},
hitPoint(t, point, pointMatrix) {
if (pointMatrix)
point = PointHelper.tempToInnerOf(point, pointMatrix);
return (point.x >= t.x && point.x <= t.x + t.width) && (point.y >= t.y && point.y <= t.y + t.height);
},
hit(t, other, otherMatrix) {
if (otherMatrix)
other = B.tempToOuterOf(other, otherMatrix);
return !((t.y + t.height < other.y) || (other.y + other.height < t.y) || (t.x + t.width < other.x) || (other.x + other.width < t.x));
},
includes(t, other, otherMatrix) {
if (otherMatrix)
other = B.tempToOuterOf(other, otherMatrix);
return (t.x <= other.x) && (t.y <= other.y) && (t.x + t.width >= other.x + other.width) && (t.y + t.height >= other.y + other.height);
},
getIntersectData(t, other, otherMatrix) {
if (otherMatrix)
other = B.tempToOuterOf(other, otherMatrix);
if (!B.hit(t, other))
return getBoundsData();
let { x, y, width, height } = other;
right$1 = x + width;
bottom$1 = y + height;
boundsRight = t.x + t.width;
boundsBottom = t.y + t.height;
x = x > t.x ? x : t.x;
y = y > t.y ? y : t.y;
right$1 = right$1 < boundsRight ? right$1 : boundsRight;
bottom$1 = bottom$1 < boundsBottom ? bottom$1 : boundsBottom;
width = right$1 - x;
height = bottom$1 - y;
return { x, y, width, height };
},
intersect(t, other, otherMatrix) {
B.copy(t, B.getIntersectData(t, other, otherMatrix));
},
isSame(t, bounds) {
return t.x === bounds.x && t.y === bounds.y && t.width === bounds.width && t.height === bounds.height;
},
isEmpty(t) {
return t.x === 0 && t.y === 0 && t.width === 0 && t.height === 0;
},
reset(t) {
B.set(t);
}
};
const B = BoundsHelper;
const { add: add$1, copy: copy$a } = B;
class Bounds {
get minX() { return BoundsHelper.minX(this); }
get minY() { return BoundsHelper.minY(this); }
get maxX() { return BoundsHelper.maxX(this); }
get maxY() { return BoundsHelper.maxY(this); }
constructor(x, y, width, height) {
this.set(x, y, width, height);
}
set(x, y, width, height) {
typeof x === 'object' ? BoundsHelper.copy(this, x) : BoundsHelper.set(this, x, y, width, height);
return this;
}
get() {
const { x, y, width, height } = this;
return { x, y, width, height };
}
clone() {
return new Bounds(this);
}
move(x, y) {
BoundsHelper.move(this, x, y);
return this;
}
scale(scaleX, scaleY, onlySize) {
BoundsHelper.scale(this, scaleX, scaleY, onlySize);
return this;
}
scaleOf(origin, scaleX, scaleY) {
BoundsHelper.scaleOf(this, origin, scaleX, scaleY);
return this;
}
toOuterOf(matrix, to) {
BoundsHelper.toOuterOf(this, matrix, to);
return this;
}
toInnerOf(matrix, to) {
BoundsHelper.toInnerOf(this, matrix, to);
return this;
}
getFitMatrix(put, baseScale) {
return BoundsHelper.getFitMatrix(this, put, baseScale);
}
put(put, align, putScale) {
BoundsHelper.put(this, put, align, putScale);
}
spread(fourNumber, side) {
BoundsHelper.spread(this, fourNumber, side);
return this;
}
shrink(fourNumber, side) {
BoundsHelper.shrink(this, fourNumber, side);
return this;
}
ceil() {
BoundsHelper.ceil(this);
return this;
}
unsign() {
BoundsHelper.unsign(this);
return this;
}
float(maxLength) {
BoundsHelper.float(this, maxLength);
return this;
}
add(bounds) {
BoundsHelper.add(this, bounds);
return this;
}
addList(boundsList) {
BoundsHelper.setList(this, boundsList, true);
return this;
}
setList(boundsList) {
BoundsHelper.setList(this, boundsList);
return this;
}
addListWithFn(list, boundsDataFn) {
BoundsHelper.setListWithFn(this, list, boundsDataFn, true);
return this;
}
setListWithFn(list, boundsDataFn) {
BoundsHelper.setListWithFn(this, list, boundsDataFn);
return this;
}
setPoint(point) {
BoundsHelper.setPoint(this, point);
return this;
}
setPoints(points) {
BoundsHelper.setPoints(this, points);
return this;
}
addPoint(point) {
BoundsHelper.addPoint(this, point);
return this;
}
getPoints() {
return BoundsHelper.getPoints(this);
}
hitPoint(point, pointMatrix) {
return BoundsHelper.hitPoint(this, point, pointMatrix);
}
hitRadiusPoint(point, pointMatrix) {
return BoundsHelper.hitRadiusPoint(this, point, pointMatrix);
}
hit(bounds, boundsMatrix) {
return BoundsHelper.hit(this, bounds, boundsMatrix);
}
includes(bounds, boundsMatrix) {
return BoundsHelper.includes(this, bounds, boundsMatrix);
}
intersect(bounds, boundsMatrix) {
BoundsHelper.intersect(this, bounds, boundsMatrix);
return this;
}
getIntersect(bounds, boundsMatrix) {
return new Bounds(BoundsHelper.getIntersectData(this, bounds, boundsMatrix));
}
isSame(bounds) {
return BoundsHelper.isSame(this, bounds);
}
isEmpty() {
return BoundsHelper.isEmpty(this);
}
reset() {
BoundsHelper.reset(this);
}
}
const tempBounds$1 = new Bounds();
class AutoBounds {
constructor(top, right, bottom, left, width, height) {
typeof top === 'object' ? this.copy(top) : this.set(top, right, bottom, left, width, height);
}
set(top = 0, right = 0, bottom = 0, left = 0, width = 0, height = 0) {
this.top = top;
this.right = right;
this.bottom = bottom;
this.left = left;
this.width = width;
this.height = height;
}
copy(autoSize) {
const { top, right, bottom, left, width, height } = autoSize;
this.set(top, right, bottom, left, width, height);
}
getBoundsFrom(parent) {
const { top, right, bottom, left, width, height } = this;
return new Bounds(left, top, width ? width : parent.width - left - right, height ? height : parent.height - top - bottom);
}
}
const StringNumberMap = {
'0': 1,
'1': 1,
'2': 1,
'3': 1,
'4': 1,
'5': 1,
'6': 1,
'7': 1,
'8': 1,
'9': 1,
'.': 1,
'e': 1,
'E': 1
};
const { randColor } = MathHelper;
class Debug {
constructor(name) {
this.repeatMap = {};
this.name = name;
}
static get(name) {
return new Debug(name);
}
static set filter(name) {
this.filterList = getNameList(name);
}
static set exclude(name) {
this.excludeList = getNameList(name);
}
static drawRepaint(canvas, bounds) {
const color = randColor();
canvas.fillWorld(bounds, color.replace('1)', '.1)'));
canvas.strokeWorld(bounds, color);
}
static drawBounds(leaf, canvas, _options) {
const showHit = Debug.showBounds === 'hit', w = leaf.__nowWorld, color = randColor();
if (showHit)
canvas.setWorld(w), leaf.__drawHitPath(canvas), canvas.fillStyle = color.replace('1)', '.2)'), canvas.fill();
canvas.resetTransform();
canvas.setStroke(color, 2);
showHit ? canvas.stroke() : canvas.strokeWorld(w, color);
}
log(...messages) {
if (D$4.enable) {
if (D$4.filterList.length && D$4.filterList.every(name => name !== this.name))
return;
if (D$4.excludeList.length && D$4.excludeList.some(name => name === this.name))
return;
console.log('%c' + this.name, 'color:#21ae62', ...messages);
}
}
tip(...messages) {
if (D$4.enable)
this.warn(...messages);
}
warn(...messages) {
if (D$4.showWarn)
console.warn(this.name, ...messages);
}
repeat(name, ...messages) {
if (!this.repeatMap[name]) {
this.warn('repeat:' + name, ...messages);
this.repeatMap[name] = true;
}
}
error(...messages) {
try {
throw new Error();
}
catch (e) {
console.error(this.name, ...messages, e);
}
}
}
Debug.filterList = [];
Debug.excludeList = [];
Debug.showWarn = true;
function getNameList(name) {
if (!name)
name = [];
else if (typeof name === 'string')
name = [name];
return name;
}
cons