modern-canvas 0.4.34 → 0.4.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +569 -15
- package/dist/index.d.cts +51 -4
- package/dist/index.d.mts +51 -4
- package/dist/index.d.ts +51 -4
- package/dist/index.js +40 -40
- package/dist/index.mjs +567 -16
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -1112,6 +1112,24 @@ class Color {
|
|
|
1112
1112
|
constructor(value = 0) {
|
|
1113
1113
|
this.value = value;
|
|
1114
1114
|
}
|
|
1115
|
+
toHex() {
|
|
1116
|
+
return this._colord.toHex();
|
|
1117
|
+
}
|
|
1118
|
+
toRgb() {
|
|
1119
|
+
return this._colord.toRgb();
|
|
1120
|
+
}
|
|
1121
|
+
toRgbString() {
|
|
1122
|
+
return this._colord.toRgbString();
|
|
1123
|
+
}
|
|
1124
|
+
toHsl() {
|
|
1125
|
+
return this._colord.toHsl();
|
|
1126
|
+
}
|
|
1127
|
+
toHslString() {
|
|
1128
|
+
return this._colord.toHslString();
|
|
1129
|
+
}
|
|
1130
|
+
toHsv() {
|
|
1131
|
+
return this._colord.toHsv();
|
|
1132
|
+
}
|
|
1115
1133
|
toArgb(alpha = this.a, applyToRGB = true) {
|
|
1116
1134
|
if (alpha === 1) {
|
|
1117
1135
|
return (255 << 24) + this.rgb;
|
|
@@ -1129,9 +1147,6 @@ class Color {
|
|
|
1129
1147
|
}
|
|
1130
1148
|
return (alpha * 255 << 24) + (r << 16) + (g << 8) + b;
|
|
1131
1149
|
}
|
|
1132
|
-
toHex() {
|
|
1133
|
-
return this._colord.toHex();
|
|
1134
|
-
}
|
|
1135
1150
|
toArray() {
|
|
1136
1151
|
return [this.r, this.g, this.b, this.a];
|
|
1137
1152
|
}
|
|
@@ -1728,6 +1743,468 @@ class ColorMatrix extends Matrix {
|
|
|
1728
1743
|
}
|
|
1729
1744
|
}
|
|
1730
1745
|
|
|
1746
|
+
var GradientParser = GradientParser || {};
|
|
1747
|
+
GradientParser.parse = /* @__PURE__ */ function() {
|
|
1748
|
+
const tokens = {
|
|
1749
|
+
linearGradient: /^(-(webkit|o|ms|moz)-)?(linear-gradient)/i,
|
|
1750
|
+
repeatingLinearGradient: /^(-(webkit|o|ms|moz)-)?(repeating-linear-gradient)/i,
|
|
1751
|
+
radialGradient: /^(-(webkit|o|ms|moz)-)?(radial-gradient)/i,
|
|
1752
|
+
repeatingRadialGradient: /^(-(webkit|o|ms|moz)-)?(repeating-radial-gradient)/i,
|
|
1753
|
+
sideOrCorner: /^to (left (top|bottom)|right (top|bottom)|top (left|right)|bottom (left|right)|left|right|top|bottom)/i,
|
|
1754
|
+
extentKeywords: /^(closest-side|closest-corner|farthest-side|farthest-corner|contain|cover)/,
|
|
1755
|
+
positionKeywords: /^(left|center|right|top|bottom)/i,
|
|
1756
|
+
pixelValue: /^(-?((\d*\.\d+)|(\d+\.?)))px/,
|
|
1757
|
+
percentageValue: /^(-?((\d*\.\d+)|(\d+\.?)))%/,
|
|
1758
|
+
emValue: /^(-?((\d*\.\d+)|(\d+\.?)))em/,
|
|
1759
|
+
angleValue: /^(-?((\d*\.\d+)|(\d+\.?)))deg/,
|
|
1760
|
+
radianValue: /^(-?((\d*\.\d+)|(\d+\.?)))rad/,
|
|
1761
|
+
startCall: /^\(/,
|
|
1762
|
+
endCall: /^\)/,
|
|
1763
|
+
comma: /^,/,
|
|
1764
|
+
hexColor: /^#([0-9a-f]+)/i,
|
|
1765
|
+
literalColor: /^([a-z]+)/i,
|
|
1766
|
+
rgbColor: /^rgb/i,
|
|
1767
|
+
rgbaColor: /^rgba/i,
|
|
1768
|
+
varColor: /^var/i,
|
|
1769
|
+
calcValue: /^calc/i,
|
|
1770
|
+
variableName: /^(--[a-z0-9-,\s#]+)/i,
|
|
1771
|
+
number: /^((\d*\.\d+)|(\d+\.?))/,
|
|
1772
|
+
hslColor: /^hsl/i,
|
|
1773
|
+
hslaColor: /^hsla/i
|
|
1774
|
+
};
|
|
1775
|
+
let input = "";
|
|
1776
|
+
function error(msg) {
|
|
1777
|
+
const err = new Error(`${input}: ${msg}`);
|
|
1778
|
+
err.source = input;
|
|
1779
|
+
throw err;
|
|
1780
|
+
}
|
|
1781
|
+
function getAST() {
|
|
1782
|
+
const ast = matchListDefinitions();
|
|
1783
|
+
if (input.length > 0) {
|
|
1784
|
+
error("Invalid input not EOF");
|
|
1785
|
+
}
|
|
1786
|
+
return ast;
|
|
1787
|
+
}
|
|
1788
|
+
function matchListDefinitions() {
|
|
1789
|
+
return matchListing(matchDefinition);
|
|
1790
|
+
}
|
|
1791
|
+
function matchDefinition() {
|
|
1792
|
+
return matchGradient(
|
|
1793
|
+
"linear-gradient",
|
|
1794
|
+
tokens.linearGradient,
|
|
1795
|
+
matchLinearOrientation
|
|
1796
|
+
) || matchGradient(
|
|
1797
|
+
"repeating-linear-gradient",
|
|
1798
|
+
tokens.repeatingLinearGradient,
|
|
1799
|
+
matchLinearOrientation
|
|
1800
|
+
) || matchGradient(
|
|
1801
|
+
"radial-gradient",
|
|
1802
|
+
tokens.radialGradient,
|
|
1803
|
+
matchListRadialOrientations
|
|
1804
|
+
) || matchGradient(
|
|
1805
|
+
"repeating-radial-gradient",
|
|
1806
|
+
tokens.repeatingRadialGradient,
|
|
1807
|
+
matchListRadialOrientations
|
|
1808
|
+
);
|
|
1809
|
+
}
|
|
1810
|
+
function matchGradient(gradientType, pattern, orientationMatcher) {
|
|
1811
|
+
return matchCall(pattern, (captures) => {
|
|
1812
|
+
const orientation = orientationMatcher();
|
|
1813
|
+
if (orientation) {
|
|
1814
|
+
if (!scan(tokens.comma)) {
|
|
1815
|
+
error("Missing comma before color stops");
|
|
1816
|
+
}
|
|
1817
|
+
}
|
|
1818
|
+
return {
|
|
1819
|
+
type: gradientType,
|
|
1820
|
+
orientation,
|
|
1821
|
+
colorStops: matchListing(matchColorStop)
|
|
1822
|
+
};
|
|
1823
|
+
});
|
|
1824
|
+
}
|
|
1825
|
+
function matchCall(pattern, callback) {
|
|
1826
|
+
const captures = scan(pattern);
|
|
1827
|
+
if (captures) {
|
|
1828
|
+
if (!scan(tokens.startCall)) {
|
|
1829
|
+
error("Missing (");
|
|
1830
|
+
}
|
|
1831
|
+
const result = callback(captures);
|
|
1832
|
+
if (!scan(tokens.endCall)) {
|
|
1833
|
+
error("Missing )");
|
|
1834
|
+
}
|
|
1835
|
+
return result;
|
|
1836
|
+
}
|
|
1837
|
+
}
|
|
1838
|
+
function matchLinearOrientation() {
|
|
1839
|
+
const sideOrCorner = matchSideOrCorner();
|
|
1840
|
+
if (sideOrCorner) {
|
|
1841
|
+
return sideOrCorner;
|
|
1842
|
+
}
|
|
1843
|
+
const legacyDirection = match("position-keyword", tokens.positionKeywords, 1);
|
|
1844
|
+
if (legacyDirection) {
|
|
1845
|
+
return {
|
|
1846
|
+
type: "directional",
|
|
1847
|
+
value: legacyDirection.value
|
|
1848
|
+
};
|
|
1849
|
+
}
|
|
1850
|
+
return matchAngle();
|
|
1851
|
+
}
|
|
1852
|
+
function matchSideOrCorner() {
|
|
1853
|
+
return match("directional", tokens.sideOrCorner, 1);
|
|
1854
|
+
}
|
|
1855
|
+
function matchAngle() {
|
|
1856
|
+
return match("angular", tokens.angleValue, 1) || match("angular", tokens.radianValue, 1);
|
|
1857
|
+
}
|
|
1858
|
+
function matchListRadialOrientations() {
|
|
1859
|
+
let radialOrientations;
|
|
1860
|
+
let radialOrientation = matchRadialOrientation();
|
|
1861
|
+
let lookaheadCache;
|
|
1862
|
+
if (radialOrientation) {
|
|
1863
|
+
radialOrientations = [];
|
|
1864
|
+
radialOrientations.push(radialOrientation);
|
|
1865
|
+
lookaheadCache = input;
|
|
1866
|
+
if (scan(tokens.comma)) {
|
|
1867
|
+
radialOrientation = matchRadialOrientation();
|
|
1868
|
+
if (radialOrientation) {
|
|
1869
|
+
radialOrientations.push(radialOrientation);
|
|
1870
|
+
} else {
|
|
1871
|
+
input = lookaheadCache;
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
return radialOrientations;
|
|
1876
|
+
}
|
|
1877
|
+
function matchRadialOrientation() {
|
|
1878
|
+
let radialType = matchCircle() || matchEllipse();
|
|
1879
|
+
if (radialType) {
|
|
1880
|
+
radialType.at = matchAtPosition();
|
|
1881
|
+
} else {
|
|
1882
|
+
const extent = matchExtentKeyword();
|
|
1883
|
+
if (extent) {
|
|
1884
|
+
radialType = extent;
|
|
1885
|
+
const positionAt = matchAtPosition();
|
|
1886
|
+
if (positionAt) {
|
|
1887
|
+
radialType.at = positionAt;
|
|
1888
|
+
}
|
|
1889
|
+
} else {
|
|
1890
|
+
const atPosition = matchAtPosition();
|
|
1891
|
+
if (atPosition) {
|
|
1892
|
+
radialType = {
|
|
1893
|
+
type: "default-radial",
|
|
1894
|
+
at: atPosition
|
|
1895
|
+
};
|
|
1896
|
+
} else {
|
|
1897
|
+
const defaultPosition = matchPositioning();
|
|
1898
|
+
if (defaultPosition) {
|
|
1899
|
+
radialType = {
|
|
1900
|
+
type: "default-radial",
|
|
1901
|
+
at: defaultPosition
|
|
1902
|
+
};
|
|
1903
|
+
}
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
}
|
|
1907
|
+
return radialType;
|
|
1908
|
+
}
|
|
1909
|
+
function matchCircle() {
|
|
1910
|
+
const circle = match("shape", /^(circle)/i, 0);
|
|
1911
|
+
if (circle) {
|
|
1912
|
+
circle.style = matchLength() || matchExtentKeyword();
|
|
1913
|
+
}
|
|
1914
|
+
return circle;
|
|
1915
|
+
}
|
|
1916
|
+
function matchEllipse() {
|
|
1917
|
+
const ellipse = match("shape", /^(ellipse)/i, 0);
|
|
1918
|
+
if (ellipse) {
|
|
1919
|
+
ellipse.style = matchPositioning() || matchDistance() || matchExtentKeyword();
|
|
1920
|
+
}
|
|
1921
|
+
return ellipse;
|
|
1922
|
+
}
|
|
1923
|
+
function matchExtentKeyword() {
|
|
1924
|
+
return match("extent-keyword", tokens.extentKeywords, 1);
|
|
1925
|
+
}
|
|
1926
|
+
function matchAtPosition() {
|
|
1927
|
+
if (match("position", /^at/, 0)) {
|
|
1928
|
+
const positioning = matchPositioning();
|
|
1929
|
+
if (!positioning) {
|
|
1930
|
+
error("Missing positioning value");
|
|
1931
|
+
}
|
|
1932
|
+
return positioning;
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
function matchPositioning() {
|
|
1936
|
+
const location = matchCoordinates();
|
|
1937
|
+
if (location.x || location.y) {
|
|
1938
|
+
return {
|
|
1939
|
+
type: "position",
|
|
1940
|
+
value: location
|
|
1941
|
+
};
|
|
1942
|
+
}
|
|
1943
|
+
}
|
|
1944
|
+
function matchCoordinates() {
|
|
1945
|
+
return {
|
|
1946
|
+
x: matchDistance(),
|
|
1947
|
+
y: matchDistance()
|
|
1948
|
+
};
|
|
1949
|
+
}
|
|
1950
|
+
function matchListing(matcher) {
|
|
1951
|
+
let captures = matcher();
|
|
1952
|
+
const result = [];
|
|
1953
|
+
if (captures) {
|
|
1954
|
+
result.push(captures);
|
|
1955
|
+
while (scan(tokens.comma)) {
|
|
1956
|
+
captures = matcher();
|
|
1957
|
+
if (captures) {
|
|
1958
|
+
result.push(captures);
|
|
1959
|
+
} else {
|
|
1960
|
+
error("One extra comma");
|
|
1961
|
+
}
|
|
1962
|
+
}
|
|
1963
|
+
}
|
|
1964
|
+
return result;
|
|
1965
|
+
}
|
|
1966
|
+
function matchColorStop() {
|
|
1967
|
+
const color = matchColor();
|
|
1968
|
+
if (!color) {
|
|
1969
|
+
error("Expected color definition");
|
|
1970
|
+
}
|
|
1971
|
+
color.length = matchDistance();
|
|
1972
|
+
return color;
|
|
1973
|
+
}
|
|
1974
|
+
function matchColor() {
|
|
1975
|
+
return matchHexColor() || matchHSLAColor() || matchHSLColor() || matchRGBAColor() || matchRGBColor() || matchVarColor() || matchLiteralColor();
|
|
1976
|
+
}
|
|
1977
|
+
function matchLiteralColor() {
|
|
1978
|
+
return match("literal", tokens.literalColor, 0);
|
|
1979
|
+
}
|
|
1980
|
+
function matchHexColor() {
|
|
1981
|
+
return match("hex", tokens.hexColor, 1);
|
|
1982
|
+
}
|
|
1983
|
+
function matchRGBColor() {
|
|
1984
|
+
return matchCall(tokens.rgbColor, () => {
|
|
1985
|
+
return {
|
|
1986
|
+
type: "rgb",
|
|
1987
|
+
value: matchListing(matchNumber)
|
|
1988
|
+
};
|
|
1989
|
+
});
|
|
1990
|
+
}
|
|
1991
|
+
function matchRGBAColor() {
|
|
1992
|
+
return matchCall(tokens.rgbaColor, () => {
|
|
1993
|
+
return {
|
|
1994
|
+
type: "rgba",
|
|
1995
|
+
value: matchListing(matchNumber)
|
|
1996
|
+
};
|
|
1997
|
+
});
|
|
1998
|
+
}
|
|
1999
|
+
function matchVarColor() {
|
|
2000
|
+
return matchCall(tokens.varColor, () => {
|
|
2001
|
+
return {
|
|
2002
|
+
type: "var",
|
|
2003
|
+
value: matchVariableName()
|
|
2004
|
+
};
|
|
2005
|
+
});
|
|
2006
|
+
}
|
|
2007
|
+
function matchHSLColor() {
|
|
2008
|
+
return matchCall(tokens.hslColor, () => {
|
|
2009
|
+
const lookahead = scan(tokens.percentageValue);
|
|
2010
|
+
if (lookahead) {
|
|
2011
|
+
error("HSL hue value must be a number in degrees (0-360) or normalized (-360 to 360), not a percentage");
|
|
2012
|
+
}
|
|
2013
|
+
const hue = matchNumber();
|
|
2014
|
+
scan(tokens.comma);
|
|
2015
|
+
let captures = scan(tokens.percentageValue);
|
|
2016
|
+
const sat = captures ? captures[1] : null;
|
|
2017
|
+
scan(tokens.comma);
|
|
2018
|
+
captures = scan(tokens.percentageValue);
|
|
2019
|
+
const light = captures ? captures[1] : null;
|
|
2020
|
+
if (!sat || !light) {
|
|
2021
|
+
error("Expected percentage value for saturation and lightness in HSL");
|
|
2022
|
+
}
|
|
2023
|
+
return {
|
|
2024
|
+
type: "hsl",
|
|
2025
|
+
value: [hue, sat, light]
|
|
2026
|
+
};
|
|
2027
|
+
});
|
|
2028
|
+
}
|
|
2029
|
+
function matchHSLAColor() {
|
|
2030
|
+
return matchCall(tokens.hslaColor, () => {
|
|
2031
|
+
const hue = matchNumber();
|
|
2032
|
+
scan(tokens.comma);
|
|
2033
|
+
let captures = scan(tokens.percentageValue);
|
|
2034
|
+
const sat = captures ? captures[1] : null;
|
|
2035
|
+
scan(tokens.comma);
|
|
2036
|
+
captures = scan(tokens.percentageValue);
|
|
2037
|
+
const light = captures ? captures[1] : null;
|
|
2038
|
+
scan(tokens.comma);
|
|
2039
|
+
const alpha = matchNumber();
|
|
2040
|
+
if (!sat || !light) {
|
|
2041
|
+
error("Expected percentage value for saturation and lightness in HSLA");
|
|
2042
|
+
}
|
|
2043
|
+
return {
|
|
2044
|
+
type: "hsla",
|
|
2045
|
+
value: [hue, sat, light, alpha]
|
|
2046
|
+
};
|
|
2047
|
+
});
|
|
2048
|
+
}
|
|
2049
|
+
function matchVariableName() {
|
|
2050
|
+
return scan(tokens.variableName)[1];
|
|
2051
|
+
}
|
|
2052
|
+
function matchNumber() {
|
|
2053
|
+
return scan(tokens.number)[1];
|
|
2054
|
+
}
|
|
2055
|
+
function matchDistance() {
|
|
2056
|
+
return match("%", tokens.percentageValue, 1) || matchPositionKeyword() || matchCalc() || matchLength();
|
|
2057
|
+
}
|
|
2058
|
+
function matchPositionKeyword() {
|
|
2059
|
+
return match("position-keyword", tokens.positionKeywords, 1);
|
|
2060
|
+
}
|
|
2061
|
+
function matchCalc() {
|
|
2062
|
+
return matchCall(tokens.calcValue, () => {
|
|
2063
|
+
let openParenCount = 1;
|
|
2064
|
+
let i = 0;
|
|
2065
|
+
while (openParenCount > 0 && i < input.length) {
|
|
2066
|
+
const char = input.charAt(i);
|
|
2067
|
+
if (char === "(") {
|
|
2068
|
+
openParenCount++;
|
|
2069
|
+
} else if (char === ")") {
|
|
2070
|
+
openParenCount--;
|
|
2071
|
+
}
|
|
2072
|
+
i++;
|
|
2073
|
+
}
|
|
2074
|
+
if (openParenCount > 0) {
|
|
2075
|
+
error("Missing closing parenthesis in calc() expression");
|
|
2076
|
+
}
|
|
2077
|
+
const calcContent = input.substring(0, i - 1);
|
|
2078
|
+
consume(i - 1);
|
|
2079
|
+
return {
|
|
2080
|
+
type: "calc",
|
|
2081
|
+
value: calcContent
|
|
2082
|
+
};
|
|
2083
|
+
});
|
|
2084
|
+
}
|
|
2085
|
+
function matchLength() {
|
|
2086
|
+
return match("px", tokens.pixelValue, 1) || match("em", tokens.emValue, 1);
|
|
2087
|
+
}
|
|
2088
|
+
function match(type, pattern, captureIndex) {
|
|
2089
|
+
const captures = scan(pattern);
|
|
2090
|
+
if (captures) {
|
|
2091
|
+
return {
|
|
2092
|
+
type,
|
|
2093
|
+
value: captures[captureIndex]
|
|
2094
|
+
};
|
|
2095
|
+
}
|
|
2096
|
+
}
|
|
2097
|
+
function scan(regexp) {
|
|
2098
|
+
let captures, blankCaptures;
|
|
2099
|
+
blankCaptures = /^\s+/.exec(input);
|
|
2100
|
+
if (blankCaptures) {
|
|
2101
|
+
consume(blankCaptures[0].length);
|
|
2102
|
+
}
|
|
2103
|
+
captures = regexp.exec(input);
|
|
2104
|
+
if (captures) {
|
|
2105
|
+
consume(captures[0].length);
|
|
2106
|
+
}
|
|
2107
|
+
return captures;
|
|
2108
|
+
}
|
|
2109
|
+
function consume(size) {
|
|
2110
|
+
input = input.substr(size);
|
|
2111
|
+
}
|
|
2112
|
+
return function(code) {
|
|
2113
|
+
input = code.toString().trim();
|
|
2114
|
+
if (input.endsWith(";")) {
|
|
2115
|
+
input = input.slice(0, -1);
|
|
2116
|
+
}
|
|
2117
|
+
return getAST();
|
|
2118
|
+
};
|
|
2119
|
+
}();
|
|
2120
|
+
const parseGradient = GradientParser.parse.bind(GradientParser);
|
|
2121
|
+
|
|
2122
|
+
class LinearGradient {
|
|
2123
|
+
constructor(x0, y0, x1, y1, stops) {
|
|
2124
|
+
this.x0 = x0;
|
|
2125
|
+
this.y0 = y0;
|
|
2126
|
+
this.x1 = x1;
|
|
2127
|
+
this.y1 = y1;
|
|
2128
|
+
this.stops = stops;
|
|
2129
|
+
}
|
|
2130
|
+
static from(css) {
|
|
2131
|
+
let angleDeg = 0;
|
|
2132
|
+
const stops = [];
|
|
2133
|
+
const ast = parseGradient(css)[0];
|
|
2134
|
+
if (ast.type === "linear-gradient") {
|
|
2135
|
+
switch (ast?.orientation?.type) {
|
|
2136
|
+
case "angular":
|
|
2137
|
+
angleDeg = Number(ast.orientation.value);
|
|
2138
|
+
break;
|
|
2139
|
+
}
|
|
2140
|
+
ast.colorStops.forEach((stop) => {
|
|
2141
|
+
let offset = 0;
|
|
2142
|
+
let color = "rgba(0, 0, 0, 0)";
|
|
2143
|
+
switch (stop.type) {
|
|
2144
|
+
case "rgb":
|
|
2145
|
+
color = `rgb(${stop.value[0]}, ${stop.value[1]}, ${stop.value[2]})`;
|
|
2146
|
+
break;
|
|
2147
|
+
case "rgba":
|
|
2148
|
+
color = `rgba(${stop.value[0]}, ${stop.value[1]}, ${stop.value[2]}, ${stop.value[3]})`;
|
|
2149
|
+
break;
|
|
2150
|
+
case "literal":
|
|
2151
|
+
color = stop.value;
|
|
2152
|
+
break;
|
|
2153
|
+
case "hex":
|
|
2154
|
+
color = stop.value;
|
|
2155
|
+
break;
|
|
2156
|
+
}
|
|
2157
|
+
switch (stop.length?.type) {
|
|
2158
|
+
case "%":
|
|
2159
|
+
offset = Number(stop.length.value) / 100;
|
|
2160
|
+
break;
|
|
2161
|
+
}
|
|
2162
|
+
stops.push({ offset, color });
|
|
2163
|
+
});
|
|
2164
|
+
}
|
|
2165
|
+
const angleRad = (angleDeg + 90) * Math.PI / 180;
|
|
2166
|
+
const width = 1;
|
|
2167
|
+
const height = 1;
|
|
2168
|
+
const halfWidth = width / 2;
|
|
2169
|
+
const halfHeight = height / 2;
|
|
2170
|
+
const length = Math.sqrt(width * width + height * height) / 2;
|
|
2171
|
+
const x0 = halfWidth + length * Math.cos(angleRad + Math.PI);
|
|
2172
|
+
const y0 = halfHeight + length * Math.sin(angleRad + Math.PI);
|
|
2173
|
+
const x1 = halfWidth + length * Math.cos(angleRad);
|
|
2174
|
+
const y1 = halfHeight + length * Math.sin(angleRad);
|
|
2175
|
+
return new LinearGradient(x0, y0, x1, y1, stops);
|
|
2176
|
+
}
|
|
2177
|
+
parse(width, height) {
|
|
2178
|
+
return this._parseByCanvas(width, height);
|
|
2179
|
+
}
|
|
2180
|
+
_parseByCanvas(width, height) {
|
|
2181
|
+
const canvas = document.createElement("canvas");
|
|
2182
|
+
canvas.width = width;
|
|
2183
|
+
canvas.height = height;
|
|
2184
|
+
const ctx = canvas.getContext("2d");
|
|
2185
|
+
if (!ctx) {
|
|
2186
|
+
throw new Error("Failed to parse linear gradient, get canvas context is null.");
|
|
2187
|
+
}
|
|
2188
|
+
const gradient = ctx.createLinearGradient(
|
|
2189
|
+
this.x0 * width,
|
|
2190
|
+
this.y0 * height,
|
|
2191
|
+
this.x1 * width,
|
|
2192
|
+
this.y1 * height
|
|
2193
|
+
);
|
|
2194
|
+
this.stops.forEach((stop) => {
|
|
2195
|
+
gradient.addColorStop(stop.offset, stop.color);
|
|
2196
|
+
});
|
|
2197
|
+
ctx.fillStyle = gradient;
|
|
2198
|
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
2199
|
+
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
2200
|
+
return {
|
|
2201
|
+
width: imageData.width,
|
|
2202
|
+
height: imageData.height,
|
|
2203
|
+
pixels: new Uint8Array(imageData.data.buffer)
|
|
2204
|
+
};
|
|
2205
|
+
}
|
|
2206
|
+
}
|
|
2207
|
+
|
|
1731
2208
|
class Matrix2 extends Matrix {
|
|
1732
2209
|
constructor(array) {
|
|
1733
2210
|
super(2, 2, array);
|
|
@@ -1832,6 +2309,47 @@ class Projection2D extends Matrix3 {
|
|
|
1832
2309
|
}
|
|
1833
2310
|
}
|
|
1834
2311
|
|
|
2312
|
+
class RadialGradient {
|
|
2313
|
+
constructor(x0, y0, r0, x1, y1, r1, stops) {
|
|
2314
|
+
this.x0 = x0;
|
|
2315
|
+
this.y0 = y0;
|
|
2316
|
+
this.r0 = r0;
|
|
2317
|
+
this.x1 = x1;
|
|
2318
|
+
this.y1 = y1;
|
|
2319
|
+
this.r1 = r1;
|
|
2320
|
+
this.stops = stops;
|
|
2321
|
+
}
|
|
2322
|
+
parse(width, height) {
|
|
2323
|
+
return this._parseByCanvas(width, height);
|
|
2324
|
+
}
|
|
2325
|
+
_parseByCanvas(width, height) {
|
|
2326
|
+
const canvas = document.createElement("canvas");
|
|
2327
|
+
const ctx = canvas.getContext("2d");
|
|
2328
|
+
if (!ctx) {
|
|
2329
|
+
throw new Error("Failed to parse radial gradient, get canvas context is null.");
|
|
2330
|
+
}
|
|
2331
|
+
const gradient = ctx.createRadialGradient(
|
|
2332
|
+
this.x0 * width,
|
|
2333
|
+
this.y0 * height,
|
|
2334
|
+
this.r0,
|
|
2335
|
+
this.x1 * width,
|
|
2336
|
+
this.y1 * height,
|
|
2337
|
+
this.r1
|
|
2338
|
+
);
|
|
2339
|
+
this.stops.forEach((stop) => {
|
|
2340
|
+
gradient.addColorStop(stop.offset, stop.color);
|
|
2341
|
+
});
|
|
2342
|
+
ctx.fillStyle = gradient;
|
|
2343
|
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
2344
|
+
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
2345
|
+
return {
|
|
2346
|
+
width: imageData.width,
|
|
2347
|
+
height: imageData.height,
|
|
2348
|
+
pixels: new Uint8Array(imageData.data.buffer)
|
|
2349
|
+
};
|
|
2350
|
+
}
|
|
2351
|
+
}
|
|
2352
|
+
|
|
1835
2353
|
class Vector2 extends Vector {
|
|
1836
2354
|
get x() {
|
|
1837
2355
|
return this._array[0];
|
|
@@ -5136,6 +5654,19 @@ class ColorTexture extends Texture2D {
|
|
|
5136
5654
|
}
|
|
5137
5655
|
}
|
|
5138
5656
|
|
|
5657
|
+
class GradientTexture extends Texture2D {
|
|
5658
|
+
// |(radial-gradient)
|
|
5659
|
+
static regExp = /(linear-gradient)/;
|
|
5660
|
+
static test(value) {
|
|
5661
|
+
return this.regExp.test(value);
|
|
5662
|
+
}
|
|
5663
|
+
constructor(value, width, height) {
|
|
5664
|
+
super(
|
|
5665
|
+
LinearGradient.from(value).parse(width, height)
|
|
5666
|
+
);
|
|
5667
|
+
}
|
|
5668
|
+
}
|
|
5669
|
+
|
|
5139
5670
|
function resolveOptions$1(options) {
|
|
5140
5671
|
return {
|
|
5141
5672
|
autoLoad: Boolean(options?.autoLoad ?? true),
|
|
@@ -5578,6 +6109,21 @@ exports.Node = class Node extends CoreObject {
|
|
|
5578
6109
|
}).append(nodes);
|
|
5579
6110
|
this.on("treeEnter", this._onTreeEnter).on("treeExit", this._onTreeExit).on("parented", this._onParented).on("unparented", this._onUnparented).on("ready", this._onReady).on("process", this._onProcess);
|
|
5580
6111
|
}
|
|
6112
|
+
setProperties(properties) {
|
|
6113
|
+
if (properties) {
|
|
6114
|
+
const {
|
|
6115
|
+
meta,
|
|
6116
|
+
...restProperties
|
|
6117
|
+
} = properties;
|
|
6118
|
+
if (meta) {
|
|
6119
|
+
for (const key in meta) {
|
|
6120
|
+
this.setMeta(key, meta[key]);
|
|
6121
|
+
}
|
|
6122
|
+
}
|
|
6123
|
+
super.setProperties(restProperties);
|
|
6124
|
+
}
|
|
6125
|
+
return this;
|
|
6126
|
+
}
|
|
5581
6127
|
/** Name */
|
|
5582
6128
|
getName() {
|
|
5583
6129
|
return this.name;
|
|
@@ -7072,20 +7618,27 @@ class CanvasContext extends modernPath2d.Path2D {
|
|
|
7072
7618
|
miterLimit;
|
|
7073
7619
|
_defaultStyle = Texture2D.EMPTY;
|
|
7074
7620
|
_draws = [];
|
|
7621
|
+
_colorToTexture(color, width, height) {
|
|
7622
|
+
if (color instanceof Texture2D) {
|
|
7623
|
+
return color;
|
|
7624
|
+
} else if (typeof color === "string" && GradientTexture.test(color)) {
|
|
7625
|
+
return new GradientTexture(color, width, height);
|
|
7626
|
+
} else {
|
|
7627
|
+
return new ColorTexture(color);
|
|
7628
|
+
}
|
|
7629
|
+
}
|
|
7075
7630
|
stroke(options) {
|
|
7631
|
+
const path = new modernPath2d.Path2D(this);
|
|
7076
7632
|
let texture = this._defaultStyle;
|
|
7077
7633
|
if (this.strokeStyle) {
|
|
7078
|
-
|
|
7079
|
-
|
|
7080
|
-
} else {
|
|
7081
|
-
texture = new ColorTexture(this.strokeStyle);
|
|
7082
|
-
}
|
|
7634
|
+
const { width, height } = path.getBoundingBox();
|
|
7635
|
+
texture = this._colorToTexture(this.strokeStyle, width, height);
|
|
7083
7636
|
}
|
|
7084
7637
|
if (this.curves.length) {
|
|
7085
7638
|
this._draws.push({
|
|
7086
7639
|
...options,
|
|
7087
7640
|
type: "stroke",
|
|
7088
|
-
path
|
|
7641
|
+
path,
|
|
7089
7642
|
texture,
|
|
7090
7643
|
textureTransform: this.textureTransform,
|
|
7091
7644
|
style: {
|
|
@@ -7106,18 +7659,16 @@ class CanvasContext extends modernPath2d.Path2D {
|
|
|
7106
7659
|
this.rect(x, y, width, height).stroke();
|
|
7107
7660
|
}
|
|
7108
7661
|
fill(options) {
|
|
7662
|
+
const path = new modernPath2d.Path2D(this);
|
|
7109
7663
|
let texture = this._defaultStyle;
|
|
7110
7664
|
if (this.fillStyle) {
|
|
7111
|
-
|
|
7112
|
-
|
|
7113
|
-
} else {
|
|
7114
|
-
texture = new ColorTexture(this.fillStyle);
|
|
7115
|
-
}
|
|
7665
|
+
const { width, height } = path.getBoundingBox();
|
|
7666
|
+
texture = this._colorToTexture(this.fillStyle, width, height);
|
|
7116
7667
|
}
|
|
7117
7668
|
this._draws.push({
|
|
7118
7669
|
...options,
|
|
7119
7670
|
type: "fill",
|
|
7120
|
-
path
|
|
7671
|
+
path,
|
|
7121
7672
|
texture,
|
|
7122
7673
|
textureTransform: this.textureTransform
|
|
7123
7674
|
});
|
|
@@ -14056,6 +14607,7 @@ exports.FlexLayout = FlexLayout;
|
|
|
14056
14607
|
exports.FontLoader = FontLoader;
|
|
14057
14608
|
exports.GIFLoader = GIFLoader;
|
|
14058
14609
|
exports.Geometry = Geometry;
|
|
14610
|
+
exports.GradientTexture = GradientTexture;
|
|
14059
14611
|
exports.HTMLAudio = HTMLAudio;
|
|
14060
14612
|
exports.HTMLAudioContext = HTMLAudioContext;
|
|
14061
14613
|
exports.HTMLSound = HTMLSound;
|
|
@@ -14065,6 +14617,7 @@ exports.IndexBuffer = IndexBuffer;
|
|
|
14065
14617
|
exports.Input = Input;
|
|
14066
14618
|
exports.InputEvent = InputEvent;
|
|
14067
14619
|
exports.JSONLoader = JSONLoader;
|
|
14620
|
+
exports.LinearGradient = LinearGradient;
|
|
14068
14621
|
exports.Loader = Loader;
|
|
14069
14622
|
exports.LottieLoader = LottieLoader;
|
|
14070
14623
|
exports.MainLoop = MainLoop;
|
|
@@ -14082,6 +14635,7 @@ exports.Projection2D = Projection2D;
|
|
|
14082
14635
|
exports.QuadGeometry = QuadGeometry;
|
|
14083
14636
|
exports.QuadUvGeometry = QuadUvGeometry;
|
|
14084
14637
|
exports.RAD_TO_DEG = RAD_TO_DEG;
|
|
14638
|
+
exports.RadialGradient = RadialGradient;
|
|
14085
14639
|
exports.RawWeakMap = RawWeakMap;
|
|
14086
14640
|
exports.Rect2 = Rect2;
|
|
14087
14641
|
exports.RefCounted = RefCounted;
|