@antv/g-svg 1.1.8 → 1.2.0

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.umd.js CHANGED
@@ -1336,7 +1336,12 @@
1336
1336
  function updateRectElementAttribute($el, parsedStyle) {
1337
1337
  var radius = parsedStyle.radius,
1338
1338
  width = parsedStyle.width,
1339
- height = parsedStyle.height;
1339
+ height = parsedStyle.height; // CSSKeyword: auto
1340
+
1341
+ if (!isFinite(width.value) || !isFinite(height.value)) {
1342
+ return;
1343
+ }
1344
+
1340
1345
  var hasRadius = radius && radius.some(function (r) {
1341
1346
  return r.value !== 0;
1342
1347
  });
@@ -1346,14 +1351,18 @@
1346
1351
  d = "M 0,0 l " + width.value + ",0 l 0," + height.value + " l" + -width.value + " 0 z";
1347
1352
  } else {
1348
1353
  var _radius$map = radius.map(function (r) {
1349
- return g.clamp(r.value, 0, Math.min(width.value / 2, height.value / 2));
1354
+ return g.clamp(r.value, 0, Math.min(Math.abs(width.value) / 2, Math.abs(height.value) / 2));
1350
1355
  }),
1351
1356
  tlr = _radius$map[0],
1352
1357
  trr = _radius$map[1],
1353
1358
  brr = _radius$map[2],
1354
1359
  blr = _radius$map[3];
1355
1360
 
1356
- d = [["M " + tlr + ",0"], ["l " + (width.value - tlr - trr) + ",0"], ["a " + trr + "," + trr + ",0,0,1," + trr + "," + trr], ["l 0," + (height.value - trr - brr)], ["a " + brr + "," + brr + ",0,0,1," + -brr + "," + brr], ["l " + (brr + blr - width.value) + ",0"], ["a " + blr + "," + blr + ",0,0,1," + -blr + "," + -blr], ["l 0," + (blr + tlr - height.value)], ["a " + tlr + "," + tlr + ",0,0,1," + tlr + "," + -tlr], ['z']].join(' ');
1361
+ var signX = width.value > 0 ? 1 : -1;
1362
+ var signY = height.value > 0 ? 1 : -1; // sweep-flag @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Tutorial/Paths#arcs
1363
+
1364
+ var sweepFlag = signX + signY !== 0 ? 1 : 0;
1365
+ d = [["M " + signX * tlr + ",0"], ["l " + (width.value - signX * (tlr + trr)) + ",0"], ["a " + trr + "," + trr + ",0,0," + sweepFlag + "," + signX * trr + "," + signY * trr], ["l 0," + (height.value - signY * (trr + brr))], ["a " + brr + "," + brr + ",0,0," + sweepFlag + "," + -signX * brr + "," + signY * brr], ["l " + (signX * (brr + blr) - width.value) + ",0"], ["a " + blr + "," + blr + ",0,0," + sweepFlag + "," + -signX * blr + "," + -signY * blr], ["l 0," + (signY * (blr + tlr) - height.value)], ["a " + tlr + "," + tlr + ",0,0," + sweepFlag + "," + signX * tlr + "," + -signY * tlr], ['z']].join(' ');
1357
1366
  }
1358
1367
 
1359
1368
  $el.setAttribute('d', d);
@@ -1533,1444 +1542,1669 @@
1533
1542
  DefaultCreateElementContribution = __decorate([g.singleton({
1534
1543
  token: CreateElementContribution
1535
1544
  })], DefaultCreateElementContribution);
1545
+ var FILTER_PREFIX = 'g-filter-';
1546
+ /**
1547
+ * use SVG filters, eg. blur, brightness, contrast...
1548
+ * @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Element/filter
1549
+ */
1536
1550
 
1537
- function _regeneratorRuntime$2() {
1538
- /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
1539
- _regeneratorRuntime$2 = function _regeneratorRuntime() {
1540
- return exports;
1541
- };
1542
-
1543
- var exports = {},
1544
- Op = Object.prototype,
1545
- hasOwn = Op.hasOwnProperty,
1546
- $Symbol = "function" == typeof Symbol ? Symbol : {},
1547
- iteratorSymbol = $Symbol.iterator || "@@iterator",
1548
- asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator",
1549
- toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
1550
-
1551
- function define(obj, key, value) {
1552
- return Object.defineProperty(obj, key, {
1553
- value: value,
1554
- enumerable: !0,
1555
- configurable: !0,
1556
- writable: !0
1557
- }), obj[key];
1558
- }
1551
+ function createOrUpdateFilter(document, $def, object, $el, filters) {
1552
+ // eg. filter="url(#f1) url(#f2)"
1553
+ var filterName = FILTER_PREFIX + object.entity;
1554
+ var $existedFilters = $def.querySelectorAll("[name=" + filterName + "]");
1559
1555
 
1560
- try {
1561
- define({}, "");
1562
- } catch (err) {
1563
- define = function define(obj, key, value) {
1564
- return obj[key] = value;
1565
- };
1556
+ if ($existedFilters.length) {
1557
+ $existedFilters.forEach(function ($filter) {
1558
+ $def.removeChild($filter);
1559
+ });
1566
1560
  }
1567
1561
 
1568
- function wrap(innerFn, outerFn, self, tryLocsList) {
1569
- var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator,
1570
- generator = Object.create(protoGenerator.prototype),
1571
- context = new Context(tryLocsList || []);
1572
- return generator._invoke = function (innerFn, self, context) {
1573
- var state = "suspendedStart";
1574
- return function (method, arg) {
1575
- if ("executing" === state) throw new Error("Generator is already running");
1576
-
1577
- if ("completed" === state) {
1578
- if ("throw" === method) throw arg;
1579
- return doneResult();
1580
- }
1581
-
1582
- for (context.method = method, context.arg = arg;;) {
1583
- var delegate = context.delegate;
1584
-
1585
- if (delegate) {
1586
- var delegateResult = maybeInvokeDelegate(delegate, context);
1587
-
1588
- if (delegateResult) {
1589
- if (delegateResult === ContinueSentinel) continue;
1590
- return delegateResult;
1591
- }
1592
- }
1562
+ if (filters.length === 0) {
1563
+ // 'none'
1564
+ $el === null || $el === void 0 ? void 0 : $el.removeAttribute('filter');
1565
+ } else {
1566
+ var filterIds = filters.map(function (_ref, i) {
1567
+ var name = _ref.name,
1568
+ params = _ref.params;
1569
+ var $filter = createSVGElement('filter', document); // @see https://github.com/antvis/g/issues/1025
1593
1570
 
1594
- if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) {
1595
- if ("suspendedStart" === state) throw state = "completed", context.arg;
1596
- context.dispatchException(context.arg);
1597
- } else "return" === context.method && context.abrupt("return", context.arg);
1598
- state = "executing";
1599
- var record = tryCatch(innerFn, self, context);
1571
+ $filter.setAttribute('filterUnits', 'userSpaceOnUse');
1600
1572
 
1601
- if ("normal" === record.type) {
1602
- if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue;
1603
- return {
1604
- value: record.arg,
1605
- done: context.done
1606
- };
1607
- }
1573
+ if (name === 'blur') {
1574
+ createBlur(document, $filter, params);
1575
+ } else if (name === 'brightness') {
1576
+ createBrightness(document, $filter, params);
1577
+ } else if (name === 'drop-shadow') {
1578
+ createDropShadow(document, $filter, params);
1579
+ } else if (name === 'contrast') {
1580
+ createContrast(document, $filter, params);
1581
+ } else if (name === 'grayscale') {
1582
+ createGrayscale(document, $filter, params);
1583
+ } else if (name === 'sepia') {
1584
+ createSepia(document, $filter, params);
1585
+ } else if (name === 'saturate') {
1586
+ createSaturate(document, $filter, params);
1587
+ } else if (name === 'hue-rotate') {
1588
+ createHueRotate(document, $filter, params);
1589
+ } else if (name === 'invert') {
1590
+ createInvert(document, $filter, params);
1591
+ }
1608
1592
 
1609
- "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg);
1610
- }
1611
- };
1612
- }(innerFn, self, context), generator;
1593
+ $filter.id = filterName + "-" + i;
1594
+ $filter.setAttribute('name', filterName);
1595
+ $def.appendChild($filter);
1596
+ return $filter.id;
1597
+ });
1598
+ $el === null || $el === void 0 ? void 0 : $el.setAttribute('filter', filterIds.map(function (filterId) {
1599
+ return "url(#" + filterId + ")";
1600
+ }).join(' '));
1613
1601
  }
1602
+ }
1614
1603
 
1615
- function tryCatch(fn, obj, arg) {
1616
- try {
1617
- return {
1618
- type: "normal",
1619
- arg: fn.call(obj, arg)
1620
- };
1621
- } catch (err) {
1622
- return {
1623
- type: "throw",
1624
- arg: err
1625
- };
1626
- }
1627
- }
1604
+ function convertToAbsoluteValue(param) {
1605
+ return param.unit === g.UnitType.kPercentage ? param.value / 100 : param.value;
1606
+ }
1607
+ /**
1608
+ * @see https://drafts.fxtf.org/filter-effects/#blurEquivalent
1609
+ */
1628
1610
 
1629
- exports.wrap = wrap;
1630
- var ContinueSentinel = {};
1631
1611
 
1632
- function Generator() {}
1612
+ function createBlur(document, $filter, params) {
1613
+ var $feGaussianBlur = createSVGElement('feGaussianBlur', document);
1614
+ $feGaussianBlur.setAttribute('in', 'SourceGraphic');
1615
+ $feGaussianBlur.setAttribute('stdDeviation', "" + params[0].value);
1616
+ $filter.appendChild($feGaussianBlur);
1617
+ }
1633
1618
 
1634
- function GeneratorFunction() {}
1619
+ function createFeComponentTransfer(document, $filter, _ref2) {
1620
+ var type = _ref2.type,
1621
+ slope = _ref2.slope,
1622
+ intercept = _ref2.intercept,
1623
+ tableValues = _ref2.tableValues;
1624
+ var $feComponentTransfer = createSVGElement('feComponentTransfer', document);
1625
+ [createSVGElement('feFuncR', document), createSVGElement('feFuncG', document), createSVGElement('feFuncB', document)].forEach(function ($feFunc) {
1626
+ $feFunc.setAttribute('type', type);
1635
1627
 
1636
- function GeneratorFunctionPrototype() {}
1628
+ if (type === 'table') {
1629
+ $feFunc.setAttribute('tableValues', "" + tableValues);
1630
+ } else {
1631
+ $feFunc.setAttribute('slope', "" + slope);
1632
+ $feFunc.setAttribute('intercept', "" + intercept);
1633
+ }
1637
1634
 
1638
- var IteratorPrototype = {};
1639
- define(IteratorPrototype, iteratorSymbol, function () {
1640
- return this;
1635
+ $feComponentTransfer.appendChild($feFunc);
1641
1636
  });
1642
- var getProto = Object.getPrototypeOf,
1643
- NativeIteratorPrototype = getProto && getProto(getProto(values([])));
1644
- NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype);
1645
- var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype);
1646
-
1647
- function defineIteratorMethods(prototype) {
1648
- ["next", "throw", "return"].forEach(function (method) {
1649
- define(prototype, method, function (arg) {
1650
- return this._invoke(method, arg);
1651
- });
1652
- });
1653
- }
1637
+ $filter.appendChild($feComponentTransfer);
1638
+ }
1654
1639
 
1655
- function AsyncIterator(generator, PromiseImpl) {
1656
- function invoke(method, arg, resolve, reject) {
1657
- var record = tryCatch(generator[method], generator, arg);
1640
+ function createContrast(document, $filter, params) {
1641
+ var slope = convertToAbsoluteValue(params[0]);
1642
+ createFeComponentTransfer(document, $filter, {
1643
+ type: 'linear',
1644
+ slope: slope,
1645
+ intercept: -(0.5 * slope) + 0.5
1646
+ });
1647
+ }
1658
1648
 
1659
- if ("throw" !== record.type) {
1660
- var result = record.arg,
1661
- value = result.value;
1662
- return value && "object" == typeof value && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) {
1663
- invoke("next", value, resolve, reject);
1664
- }, function (err) {
1665
- invoke("throw", err, resolve, reject);
1666
- }) : PromiseImpl.resolve(value).then(function (unwrapped) {
1667
- result.value = unwrapped, resolve(result);
1668
- }, function (error) {
1669
- return invoke("throw", error, resolve, reject);
1670
- });
1671
- }
1649
+ function createInvert(document, $filter, params) {
1650
+ var amount = convertToAbsoluteValue(params[0]);
1651
+ createFeComponentTransfer(document, $filter, {
1652
+ type: 'table',
1653
+ tableValues: amount + " " + (1 - amount)
1654
+ });
1655
+ }
1672
1656
 
1673
- reject(record.arg);
1674
- }
1657
+ function createBrightness(document, $filter, params) {
1658
+ var slope = convertToAbsoluteValue(params[0]);
1659
+ createFeComponentTransfer(document, $filter, {
1660
+ type: 'linear',
1661
+ slope: slope,
1662
+ intercept: 0
1663
+ });
1664
+ }
1675
1665
 
1676
- var previousPromise;
1666
+ function createSaturate(document, $filter, params) {
1667
+ var amount = convertToAbsoluteValue(params[0]);
1668
+ var $feColorMatrix = createSVGElement('feColorMatrix', document);
1669
+ $feColorMatrix.setAttribute('type', 'saturate');
1670
+ $feColorMatrix.setAttribute('values', "" + amount);
1671
+ $filter.appendChild($feColorMatrix);
1672
+ }
1677
1673
 
1678
- this._invoke = function (method, arg) {
1679
- function callInvokeWithMethodAndArg() {
1680
- return new PromiseImpl(function (resolve, reject) {
1681
- invoke(method, arg, resolve, reject);
1682
- });
1683
- }
1674
+ function createHueRotate(document, $filter, params) {
1675
+ var $feColorMatrix = createSVGElement('feColorMatrix', document);
1676
+ $feColorMatrix.setAttribute('type', 'hueRotate');
1677
+ $feColorMatrix.setAttribute('values', "" + params[0].to(g.UnitType.kDegrees).value);
1678
+ $filter.appendChild($feColorMatrix);
1679
+ }
1684
1680
 
1685
- return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
1686
- };
1687
- }
1681
+ function createDropShadow(document, $filter, params) {
1682
+ var shadowOffsetX = params[0].value;
1683
+ var shadowOffsetY = params[1].value;
1684
+ var shadowBlur = params[2].value; // @ts-ignore
1688
1685
 
1689
- function maybeInvokeDelegate(delegate, context) {
1690
- var method = delegate.iterator[context.method];
1686
+ var shadowColor = params[3].formatted;
1687
+ var $feGaussianBlur = createSVGElement('feGaussianBlur', document);
1688
+ $feGaussianBlur.setAttribute('in', 'SourceAlpha');
1689
+ $feGaussianBlur.setAttribute('stdDeviation', "" + shadowBlur);
1690
+ $filter.appendChild($feGaussianBlur);
1691
+ var $feOffset = createSVGElement('feOffset', document);
1692
+ $feOffset.setAttribute('dx', "" + shadowOffsetX);
1693
+ $feOffset.setAttribute('dy', "" + shadowOffsetY);
1694
+ $feOffset.setAttribute('result', 'offsetblur');
1695
+ $filter.appendChild($feOffset);
1696
+ var $feFlood = createSVGElement('feFlood', document);
1697
+ $feFlood.setAttribute('flood-color', shadowColor);
1698
+ $filter.appendChild($feFlood);
1699
+ var $feComposite = createSVGElement('feComposite', document);
1700
+ $feComposite.setAttribute('in2', 'offsetblur');
1701
+ $feComposite.setAttribute('operator', 'in');
1702
+ $filter.appendChild($feComposite);
1703
+ var $feMerge = createSVGElement('feMerge', document);
1704
+ $filter.appendChild($feMerge);
1705
+ var $feMergeNode1 = createSVGElement('feMergeNode', document);
1706
+ var $feMergeNode2 = createSVGElement('feMergeNode', document);
1707
+ $feMergeNode2.setAttribute('in', 'SourceGraphic');
1708
+ $feMerge.appendChild($feMergeNode1);
1709
+ $feMerge.appendChild($feMergeNode2);
1710
+ }
1691
1711
 
1692
- if (undefined === method) {
1693
- if (context.delegate = null, "throw" === context.method) {
1694
- if (delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel;
1695
- context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method");
1696
- }
1712
+ function createFeColorMatrix(document, $filter, matrix) {
1713
+ var $feColorMatrix = createSVGElement('feColorMatrix', document);
1714
+ $feColorMatrix.setAttribute('type', 'matrix');
1715
+ $feColorMatrix.setAttribute('values', matrix.join(' '));
1716
+ $filter.appendChild($feColorMatrix);
1717
+ }
1718
+ /**
1719
+ * @see https://drafts.fxtf.org/filter-effects/#grayscaleEquivalent
1720
+ */
1697
1721
 
1698
- return ContinueSentinel;
1699
- }
1700
1722
 
1701
- var record = tryCatch(method, delegate.iterator, context.arg);
1702
- if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel;
1703
- var info = record.arg;
1704
- return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel);
1705
- }
1723
+ function createGrayscale(document, $filter, params) {
1724
+ var amount = convertToAbsoluteValue(params[0]);
1725
+ createFeColorMatrix(document, $filter, [0.2126 + 0.7874 * (1 - amount), 0.7152 - 0.7152 * (1 - amount), 0.0722 - 0.0722 * (1 - amount), 0, 0, 0.2126 - 0.2126 * (1 - amount), 0.7152 + 0.2848 * (1 - amount), 0.0722 - 0.0722 * (1 - amount), 0, 0, 0.2126 - 0.2126 * (1 - amount), 0.7152 - 0.7152 * (1 - amount), 0.0722 + 0.9278 * (1 - amount), 0, 0, 0, 0, 0, 1, 0]);
1726
+ }
1727
+ /**
1728
+ * @see https://drafts.fxtf.org/filter-effects/#sepiaEquivalent
1729
+ */
1706
1730
 
1707
- function pushTryEntry(locs) {
1708
- var entry = {
1709
- tryLoc: locs[0]
1710
- };
1711
- 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry);
1712
- }
1713
1731
 
1714
- function resetTryEntry(entry) {
1715
- var record = entry.completion || {};
1716
- record.type = "normal", delete record.arg, entry.completion = record;
1732
+ function createSepia(document, $filter, params) {
1733
+ var amount = convertToAbsoluteValue(params[0]);
1734
+ createFeColorMatrix(document, $filter, [0.393 + 0.607 * (1 - amount), 0.769 - 0.769 * (1 - amount), 0.189 - 0.189 * (1 - amount), 0, 0, 0.349 - 0.349 * (1 - amount), 0.686 + 0.314 * (1 - amount), 0.168 - 0.168 * (1 - amount), 0, 0, 0.272 - 0.272 * (1 - amount), 0.534 - 0.534 * (1 - amount), 0.131 + 0.869 * (1 - amount), 0, 0, 0, 0, 0, 1, 0]);
1735
+ }
1736
+
1737
+ var PATTERN_PREFIX = 'g-pattern-';
1738
+ var cacheKey2IDMap = {};
1739
+ var counter = 0;
1740
+
1741
+ function createOrUpdateGradientAndPattern(document, $def, object, $el, parsedColor, name, createImage) {
1742
+ // eg. clipPath don't have fill/stroke
1743
+ if (!parsedColor) {
1744
+ return '';
1717
1745
  }
1718
1746
 
1719
- function Context(tryLocsList) {
1720
- this.tryEntries = [{
1721
- tryLoc: "root"
1722
- }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0);
1747
+ if (parsedColor instanceof g.CSSRGB) {
1748
+ // keep using currentColor @see https://github.com/d3/d3-axis/issues/49
1749
+ if (object.style[name] === 'currentColor') {
1750
+ $el === null || $el === void 0 ? void 0 : $el.setAttribute(name, 'currentColor');
1751
+ } else {
1752
+ // constant value, eg. '#fff'
1753
+ $el === null || $el === void 0 ? void 0 : $el.setAttribute(name, parsedColor.isNone ? 'none' : parsedColor.toString());
1754
+ }
1755
+ } else if (g.isPattern(parsedColor)) {
1756
+ var patternId = createOrUpdatePattern(document, $def, object, parsedColor, createImage); // use style instead of attribute when applying <pattern>
1757
+ // @see https://stackoverflow.com/a/7723115
1758
+
1759
+ $el.style[name] = "url(#" + patternId + ")";
1760
+ return patternId;
1761
+ } else {
1762
+ if (parsedColor.length === 1) {
1763
+ var gradientId = createOrUpdateGradient(document, object, $def, $el, parsedColor[0]);
1764
+ $el === null || $el === void 0 ? void 0 : $el.setAttribute(name, "url(#" + gradientId + ")");
1765
+ return gradientId;
1766
+ } else {
1767
+ // @see https://stackoverflow.com/questions/20671502/can-i-blend-gradients-in-svg
1768
+ var filterId = createOrUpdateMultiGradient(document, object, $def, $el, parsedColor);
1769
+ $el === null || $el === void 0 ? void 0 : $el.setAttribute('filter', "url(#" + filterId + ")");
1770
+ $el === null || $el === void 0 ? void 0 : $el.setAttribute('fill', 'black');
1771
+ return filterId;
1772
+ }
1723
1773
  }
1724
1774
 
1725
- function values(iterable) {
1726
- if (iterable) {
1727
- var iteratorMethod = iterable[iteratorSymbol];
1728
- if (iteratorMethod) return iteratorMethod.call(iterable);
1729
- if ("function" == typeof iterable.next) return iterable;
1775
+ return '';
1776
+ }
1730
1777
 
1731
- if (!isNaN(iterable.length)) {
1732
- var i = -1,
1733
- next = function next() {
1734
- for (; ++i < iterable.length;) {
1735
- if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next;
1736
- }
1778
+ function generateCacheKey(src) {
1779
+ var cacheKey = '';
1737
1780
 
1738
- return next.value = undefined, next.done = !0, next;
1739
- };
1781
+ if (src instanceof g.CSSGradientValue) {
1782
+ var type = src.type,
1783
+ value = src.value;
1740
1784
 
1741
- return next.next = next;
1742
- }
1785
+ if (type === g.GradientType.LinearGradient || type === g.GradientType.RadialGradient) {
1786
+ // @ts-ignore
1787
+ var _type = value.type,
1788
+ width = value.width,
1789
+ height = value.height,
1790
+ steps = value.steps,
1791
+ angle = value.angle,
1792
+ cx = value.cx,
1793
+ cy = value.cy;
1794
+ cacheKey = "" + _type + width + height + (angle || 0) + (cx || 0) + (cy || 0) + steps.map(function (step) {
1795
+ return step.join('');
1796
+ }).join('');
1797
+ }
1798
+ } else if (g.isPattern(src)) {
1799
+ if (g.isString(src.image)) {
1800
+ cacheKey = "pattern-" + src.image + "-" + src.repetition;
1801
+ } else {
1802
+ cacheKey = "pattern-" + counter;
1743
1803
  }
1744
-
1745
- return {
1746
- next: doneResult
1747
- };
1748
1804
  }
1749
1805
 
1750
- function doneResult() {
1751
- return {
1752
- value: undefined,
1753
- done: !0
1754
- };
1806
+ if (cacheKey) {
1807
+ if (!cacheKey2IDMap[cacheKey]) {
1808
+ cacheKey2IDMap[cacheKey] = PATTERN_PREFIX + ("" + counter++);
1809
+ }
1755
1810
  }
1756
1811
 
1757
- return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) {
1758
- var ctor = "function" == typeof genFun && genFun.constructor;
1759
- return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name));
1760
- }, exports.mark = function (genFun) {
1761
- return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun;
1762
- }, exports.awrap = function (arg) {
1763
- return {
1764
- __await: arg
1765
- };
1766
- }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
1767
- return this;
1768
- }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) {
1769
- void 0 === PromiseImpl && (PromiseImpl = Promise);
1770
- var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl);
1771
- return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) {
1772
- return result.done ? result.value : iter.next();
1773
- });
1774
- }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () {
1775
- return this;
1776
- }), define(Gp, "toString", function () {
1777
- return "[object Generator]";
1778
- }), exports.keys = function (object) {
1779
- var keys = [];
1812
+ return cacheKey2IDMap[cacheKey];
1813
+ }
1780
1814
 
1781
- for (var key in object) {
1782
- keys.push(key);
1783
- }
1815
+ function createOrUpdatePattern(document, $def, object, pattern, createImage) {
1816
+ var patternId = generateCacheKey(pattern);
1817
+ var $existed = $def.querySelector("#" + patternId);
1784
1818
 
1785
- return keys.reverse(), function next() {
1786
- for (; keys.length;) {
1787
- var key = keys.pop();
1788
- if (key in object) return next.value = key, next.done = !1, next;
1819
+ if (!$existed) {
1820
+ var image = pattern.image,
1821
+ repetition = pattern.repetition;
1822
+ var imageURL = '';
1823
+
1824
+ if (g.isString(image)) {
1825
+ imageURL = image;
1826
+ } else {
1827
+ if (g.isBrowser) {
1828
+ if (image instanceof HTMLImageElement) {
1829
+ imageURL = image.src;
1830
+ } else if (image instanceof HTMLCanvasElement) {
1831
+ imageURL = image.toDataURL();
1832
+ } else ;
1789
1833
  }
1834
+ }
1790
1835
 
1791
- return next.done = !0, next;
1792
- };
1793
- }, exports.values = values, Context.prototype = {
1794
- constructor: Context,
1795
- reset: function reset(skipTempReset) {
1796
- if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) {
1797
- "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined);
1836
+ if (imageURL) {
1837
+ // @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Element/pattern
1838
+ var $pattern = createSVGElement('pattern', document);
1839
+ $pattern.setAttribute('patternUnits', 'userSpaceOnUse');
1840
+ var $image = createSVGElement('image', document);
1841
+ $pattern.appendChild($image);
1842
+ $pattern.id = patternId;
1843
+ $def.appendChild($pattern); // use href instead of xlink:href
1844
+ // @see https://stackoverflow.com/a/13379007
1845
+
1846
+ $image.setAttribute('href', imageURL);
1847
+ var img;
1848
+
1849
+ if (createImage) {
1850
+ img = createImage(imageURL);
1851
+ } else if (g.isBrowser) {
1852
+ img = new window.Image();
1798
1853
  }
1799
- },
1800
- stop: function stop() {
1801
- this.done = !0;
1802
- var rootRecord = this.tryEntries[0].completion;
1803
- if ("throw" === rootRecord.type) throw rootRecord.arg;
1804
- return this.rval;
1805
- },
1806
- dispatchException: function dispatchException(exception) {
1807
- if (this.done) throw exception;
1808
- var context = this;
1809
1854
 
1810
- function handle(loc, caught) {
1811
- return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught;
1855
+ if (!imageURL.match(/^data:/i)) {
1856
+ img.crossOrigin = 'Anonymous';
1857
+ $image.setAttribute('crossorigin', 'anonymous');
1812
1858
  }
1813
1859
 
1814
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
1815
- var entry = this.tryEntries[i],
1816
- record = entry.completion;
1817
- if ("root" === entry.tryLoc) return handle("end");
1860
+ img.src = imageURL;
1818
1861
 
1819
- if (entry.tryLoc <= this.prev) {
1820
- var hasCatch = hasOwn.call(entry, "catchLoc"),
1821
- hasFinally = hasOwn.call(entry, "finallyLoc");
1862
+ var onload = function onload() {
1863
+ $pattern.setAttribute('x', '0');
1864
+ $pattern.setAttribute('y', '0');
1822
1865
 
1823
- if (hasCatch && hasFinally) {
1824
- if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
1825
- if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
1826
- } else if (hasCatch) {
1827
- if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
1828
- } else {
1829
- if (!hasFinally) throw new Error("try statement without catch or finally");
1830
- if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
1831
- }
1832
- }
1833
- }
1834
- },
1835
- abrupt: function abrupt(type, arg) {
1836
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
1837
- var entry = this.tryEntries[i];
1866
+ var _object$getGeometryBo = object.getGeometryBounds(),
1867
+ halfExtents = _object$getGeometryBo.halfExtents; // There is no equivalent to CSS no-repeat for SVG patterns
1868
+ // @see https://stackoverflow.com/a/33481956
1838
1869
 
1839
- if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) {
1840
- var finallyEntry = entry;
1841
- break;
1842
- }
1843
- }
1844
1870
 
1845
- finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null);
1846
- var record = finallyEntry ? finallyEntry.completion : {};
1847
- return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record);
1848
- },
1849
- complete: function complete(record, afterLoc) {
1850
- if ("throw" === record.type) throw record.arg;
1851
- return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel;
1852
- },
1853
- finish: function finish(finallyLoc) {
1854
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
1855
- var entry = this.tryEntries[i];
1856
- if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel;
1857
- }
1858
- },
1859
- catch: function _catch(tryLoc) {
1860
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
1861
- var entry = this.tryEntries[i];
1871
+ var patternWidth = img.width;
1872
+ var patternHeight = img.height;
1862
1873
 
1863
- if (entry.tryLoc === tryLoc) {
1864
- var record = entry.completion;
1874
+ if (repetition === 'repeat-x') {
1875
+ patternHeight = halfExtents[1] * 2;
1876
+ } else if (repetition === 'repeat-y') {
1877
+ patternWidth = halfExtents[0] * 2;
1878
+ } else if (repetition === 'no-repeat') {
1879
+ patternWidth = halfExtents[0] * 2;
1880
+ patternHeight = halfExtents[1] * 2;
1881
+ }
1865
1882
 
1866
- if ("throw" === record.type) {
1867
- var thrown = record.arg;
1868
- resetTryEntry(entry);
1869
- }
1883
+ $pattern.setAttribute('width', "" + patternWidth);
1884
+ $pattern.setAttribute('height', "" + patternHeight);
1885
+ $image.setAttribute('x', '0');
1886
+ $image.setAttribute('y', '0');
1887
+ $image.setAttribute('width', "" + img.width);
1888
+ $image.setAttribute('height', "" + img.height);
1889
+ };
1870
1890
 
1871
- return thrown;
1872
- }
1873
- }
1891
+ if (img.complete) {
1892
+ onload();
1893
+ } else {
1894
+ img.onload = onload; // Fix onload() bug in IE9
1874
1895
 
1875
- throw new Error("illegal catch attempt");
1876
- },
1877
- delegateYield: function delegateYield(iterable, resultName, nextLoc) {
1878
- return this.delegate = {
1879
- iterator: values(iterable),
1880
- resultName: resultName,
1881
- nextLoc: nextLoc
1882
- }, "next" === this.method && (this.arg = undefined), ContinueSentinel;
1896
+ img.src = img.src;
1897
+ }
1883
1898
  }
1884
- }, exports;
1885
- }
1886
-
1887
- function asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, key, arg) {
1888
- try {
1889
- var info = gen[key](arg);
1890
- var value = info.value;
1891
- } catch (error) {
1892
- reject(error);
1893
- return;
1894
1899
  }
1895
1900
 
1896
- if (info.done) {
1897
- resolve(value);
1898
- } else {
1899
- Promise.resolve(value).then(_next, _throw);
1900
- }
1901
+ return patternId;
1901
1902
  }
1902
1903
 
1903
- function _asyncToGenerator$2(fn) {
1904
- return function () {
1905
- var self = this,
1906
- args = arguments;
1907
- return new Promise(function (resolve, reject) {
1908
- var gen = fn.apply(self, args);
1904
+ function createOrUpdateGradient(document, object, $def, $el, parsedColor) {
1905
+ var gradientId = generateCacheKey(parsedColor);
1906
+ var $existed = $def.querySelector("#" + gradientId);
1909
1907
 
1910
- function _next(value) {
1911
- asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, "next", value);
1912
- }
1908
+ if (!$existed) {
1909
+ // <linearGradient> <radialGradient>
1910
+ // @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Element/linearGradient
1911
+ // @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Element/radialGradient
1912
+ $existed = createSVGElement(parsedColor.type === g.GradientType.LinearGradient ? 'linearGradient' : 'radialGradient', document); // @see https://github.com/antvis/g/issues/1025
1913
1913
 
1914
- function _throw(err) {
1915
- asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, "throw", err);
1916
- }
1914
+ $existed.setAttribute('gradientUnits', 'userSpaceOnUse'); // add stops
1917
1915
 
1918
- _next(undefined);
1916
+ var innerHTML = '';
1917
+ parsedColor.value.steps.forEach(function (_ref) {
1918
+ var offset = _ref[0],
1919
+ color = _ref[1];
1920
+ innerHTML += "<stop offset=\"" + offset + "\" stop-color=\"" + color + "\"></stop>";
1919
1921
  });
1920
- };
1921
- }
1922
- /**
1923
- * Common utilities
1924
- * @module glMatrix
1925
- */
1926
- // Configuration Constants
1927
-
1922
+ $existed.innerHTML = innerHTML;
1923
+ $existed.id = gradientId;
1924
+ $def.appendChild($existed);
1925
+ }
1928
1926
 
1929
- var EPSILON = 0.000001;
1930
- var ARRAY_TYPE = typeof Float32Array !== 'undefined' ? Float32Array : Array;
1931
- if (!Math.hypot) Math.hypot = function () {
1932
- var y = 0,
1933
- i = arguments.length;
1927
+ var bounds = object.getGeometryBounds();
1928
+ var width = bounds && bounds.halfExtents[0] * 2 || 0;
1929
+ var height = bounds && bounds.halfExtents[1] * 2 || 0;
1934
1930
 
1935
- while (i--) {
1936
- y += arguments[i] * arguments[i];
1937
- }
1931
+ if (parsedColor.type === g.GradientType.LinearGradient) {
1932
+ var angle = parsedColor.value.angle;
1938
1933
 
1939
- return Math.sqrt(y);
1940
- };
1941
- /**
1942
- * 3x3 Matrix
1943
- * @module mat3
1944
- */
1934
+ var _computeLinearGradien = g.computeLinearGradient(width, height, angle),
1935
+ x1 = _computeLinearGradien.x1,
1936
+ y1 = _computeLinearGradien.y1,
1937
+ x2 = _computeLinearGradien.x2,
1938
+ y2 = _computeLinearGradien.y2;
1945
1939
 
1946
- /**
1947
- * Creates a new identity mat3
1948
- *
1949
- * @returns {mat3} a new 3x3 matrix
1950
- */
1940
+ $existed.setAttribute('x1', "" + x1);
1941
+ $existed.setAttribute('y1', "" + y1);
1942
+ $existed.setAttribute('x2', "" + x2);
1943
+ $existed.setAttribute('y2', "" + y2); // $existed.setAttribute('gradientTransform', `rotate(${angle})`);
1944
+ } else {
1945
+ var _parsedColor$value = parsedColor.value,
1946
+ cx = _parsedColor$value.cx,
1947
+ cy = _parsedColor$value.cy;
1951
1948
 
1952
- function create() {
1953
- var out = new ARRAY_TYPE(9);
1949
+ var _computeRadialGradien = g.computeRadialGradient(width, height, cx, cy),
1950
+ x = _computeRadialGradien.x,
1951
+ y = _computeRadialGradien.y,
1952
+ r = _computeRadialGradien.r;
1954
1953
 
1955
- if (ARRAY_TYPE != Float32Array) {
1956
- out[1] = 0;
1957
- out[2] = 0;
1958
- out[3] = 0;
1959
- out[5] = 0;
1960
- out[6] = 0;
1961
- out[7] = 0;
1954
+ $existed.setAttribute('cx', "" + x);
1955
+ $existed.setAttribute('cy', "" + y);
1956
+ $existed.setAttribute('r', "" + r);
1962
1957
  }
1963
1958
 
1964
- out[0] = 1;
1965
- out[4] = 1;
1966
- out[8] = 1;
1967
- return out;
1959
+ return gradientId;
1968
1960
  }
1969
- /**
1970
- * Returns the translation vector component of a transformation
1971
- * matrix. If a matrix is built with fromRotationTranslation,
1972
- * the returned vector will be the same as the translation vector
1973
- * originally supplied.
1974
- * @param {vec3} out Vector to receive translation component
1975
- * @param {ReadonlyMat4} mat Matrix to be decomposed (input)
1976
- * @return {vec3} out
1977
- */
1978
1961
 
1962
+ function createOrUpdateMultiGradient(document, object, $def, $el, gradients) {
1963
+ var filterId = FILTER_PREFIX + object.entity + '-gradient';
1964
+ var $existed = $def.querySelector("#" + filterId);
1979
1965
 
1980
- function getTranslation(out, mat) {
1981
- out[0] = mat[12];
1982
- out[1] = mat[13];
1983
- out[2] = mat[14];
1984
- return out;
1985
- }
1986
- /**
1987
- * Returns the scaling factor component of a transformation
1988
- * matrix. If a matrix is built with fromRotationTranslationScale
1989
- * with a normalized Quaternion paramter, the returned vector will be
1990
- * the same as the scaling vector
1991
- * originally supplied.
1992
- * @param {vec3} out Vector to receive scaling factor component
1993
- * @param {ReadonlyMat4} mat Matrix to be decomposed (input)
1994
- * @return {vec3} out
1995
- */
1966
+ if (!$existed) {
1967
+ $existed = createSVGElement('filter', document);
1968
+ $existed.setAttribute('filterUnits', 'userSpaceOnUse'); // @see https://github.com/antvis/g/issues/1025
1969
+
1970
+ $existed.setAttribute('x', '0%');
1971
+ $existed.setAttribute('y', '0%');
1972
+ $existed.setAttribute('width', '100%');
1973
+ $existed.setAttribute('height', '100%');
1974
+ $existed.id = filterId;
1975
+ $def.appendChild($existed);
1976
+ }
1977
+ /**
1978
+ * <rect id="wave-rect" x="0" y="0" width="100%" height="100%" fill="url(#wave)"></rect>
1979
+ * <filter id="blend-it" x="0%" y="0%" width="100%" height="100%">
1980
+ <feImage xlink:href="#wave-rect" result="myWave" x="100" y="100"/>
1981
+ <feImage xlink:href="#ry-rect" result="myRY" x="100" y="100"/>
1982
+ <feBlend in="myWave" in2="myRY" mode="multiply" result="blendedGrad"/>
1983
+ <feComposite in="blendedGrad" in2="SourceGraphic" operator="in"/>
1984
+ </filter>
1985
+ */
1996
1986
 
1997
1987
 
1998
- function getScaling(out, mat) {
1999
- var m11 = mat[0];
2000
- var m12 = mat[1];
2001
- var m13 = mat[2];
2002
- var m21 = mat[4];
2003
- var m22 = mat[5];
2004
- var m23 = mat[6];
2005
- var m31 = mat[8];
2006
- var m32 = mat[9];
2007
- var m33 = mat[10];
2008
- out[0] = Math.hypot(m11, m12, m13);
2009
- out[1] = Math.hypot(m21, m22, m23);
2010
- out[2] = Math.hypot(m31, m32, m33);
2011
- return out;
1988
+ var blended = 0;
1989
+ gradients.forEach(function (gradient, i) {
1990
+ var gradientId = createOrUpdateGradient(document, object, $def, $el, gradient);
1991
+ var rectId = gradientId + '_rect';
1992
+ var $rect = createSVGElement('rect', document);
1993
+ $rect.setAttribute('x', '0');
1994
+ $rect.setAttribute('y', '0');
1995
+ $rect.setAttribute('width', '100%');
1996
+ $rect.setAttribute('height', '100%');
1997
+ $rect.setAttribute('fill', "url(#" + gradientId + ")");
1998
+ $rect.id = rectId;
1999
+ $def.appendChild($rect);
2000
+ var $feImage = createSVGElement('feImage', document);
2001
+ $feImage.setAttribute('href', "#" + rectId);
2002
+ $feImage.setAttribute('result', filterId + "-" + i);
2003
+ $existed.appendChild($feImage);
2004
+
2005
+ if (i > 0) {
2006
+ var $feBlend = createSVGElement('feBlend', document);
2007
+ $feBlend.setAttribute('in', i === 1 ? filterId + "-" + (i - 1) : filterId + "-blended-" + (blended - 1));
2008
+ $feBlend.setAttribute('in2', filterId + "-" + i);
2009
+ $feBlend.setAttribute('result', filterId + "-blended-" + blended++); // @see https://developer.mozilla.org/zh-CN/docs/Web/CSS/blend-mode
2010
+
2011
+ $feBlend.setAttribute('mode', 'multiply');
2012
+ $existed.appendChild($feBlend);
2013
+ }
2014
+ });
2015
+ var $feComposite = createSVGElement('feComposite', document);
2016
+ $feComposite.setAttribute('in', filterId + "-blended-" + blended);
2017
+ $feComposite.setAttribute('in2', 'SourceGraphic');
2018
+ $feComposite.setAttribute('operator', 'in');
2019
+ $existed.appendChild($feComposite);
2020
+ return filterId;
2012
2021
  }
2013
- /**
2014
- * Returns a quaternion representing the rotational component
2015
- * of a transformation matrix. If a matrix is built with
2016
- * fromRotationTranslation, the returned quaternion will be the
2017
- * same as the quaternion originally supplied.
2018
- * @param {quat} out Quaternion to receive the rotation component
2019
- * @param {ReadonlyMat4} mat Matrix to be decomposed (input)
2020
- * @return {quat} out
2022
+
2023
+ var FILTER_DROPSHADOW_PREFIX = 'g-filter-dropshadow-';
2024
+ /**
2025
+ * use SVG filters
2026
+ * @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Element/filter
2021
2027
  */
2022
2028
 
2029
+ function createOrUpdateShadow(document, $def, object, $el, name) {
2030
+ var _object$parsedStyle$s;
2023
2031
 
2024
- function getRotation(out, mat) {
2025
- var scaling = new ARRAY_TYPE(3);
2026
- getScaling(scaling, mat);
2027
- var is1 = 1 / scaling[0];
2028
- var is2 = 1 / scaling[1];
2029
- var is3 = 1 / scaling[2];
2030
- var sm11 = mat[0] * is1;
2031
- var sm12 = mat[1] * is2;
2032
- var sm13 = mat[2] * is3;
2033
- var sm21 = mat[4] * is1;
2034
- var sm22 = mat[5] * is2;
2035
- var sm23 = mat[6] * is3;
2036
- var sm31 = mat[8] * is1;
2037
- var sm32 = mat[9] * is2;
2038
- var sm33 = mat[10] * is3;
2039
- var trace = sm11 + sm22 + sm33;
2040
- var S = 0;
2032
+ var shadowId = FILTER_DROPSHADOW_PREFIX + object.entity;
2033
+ var $existedFilter = $def.querySelector("#" + shadowId);
2041
2034
 
2042
- if (trace > 0) {
2043
- S = Math.sqrt(trace + 1.0) * 2;
2044
- out[3] = 0.25 * S;
2045
- out[0] = (sm23 - sm32) / S;
2046
- out[1] = (sm31 - sm13) / S;
2047
- out[2] = (sm12 - sm21) / S;
2048
- } else if (sm11 > sm22 && sm11 > sm33) {
2049
- S = Math.sqrt(1.0 + sm11 - sm22 - sm33) * 2;
2050
- out[3] = (sm23 - sm32) / S;
2051
- out[0] = 0.25 * S;
2052
- out[1] = (sm12 + sm21) / S;
2053
- out[2] = (sm31 + sm13) / S;
2054
- } else if (sm22 > sm33) {
2055
- S = Math.sqrt(1.0 + sm22 - sm11 - sm33) * 2;
2056
- out[3] = (sm31 - sm13) / S;
2057
- out[0] = (sm12 + sm21) / S;
2058
- out[1] = 0.25 * S;
2059
- out[2] = (sm23 + sm32) / S;
2060
- } else {
2061
- S = Math.sqrt(1.0 + sm33 - sm11 - sm22) * 2;
2062
- out[3] = (sm12 - sm21) / S;
2063
- out[0] = (sm31 + sm13) / S;
2064
- out[1] = (sm23 + sm32) / S;
2065
- out[2] = 0.25 * S;
2066
- }
2035
+ if (!$existedFilter) {
2036
+ $existedFilter = createSVGElement('filter', document);
2067
2037
 
2068
- return out;
2069
- }
2070
- /**
2071
- * 3 Dimensional Vector
2072
- * @module vec3
2073
- */
2038
+ var _$feDropShadow = createSVGElement('feDropShadow', document);
2074
2039
 
2075
- /**
2076
- * Creates a new, empty vec3
2077
- *
2078
- * @returns {vec3} a new 3D vector
2079
- */
2040
+ _$feDropShadow.setAttribute('dx', '0');
2080
2041
 
2042
+ _$feDropShadow.setAttribute('dy', '0');
2081
2043
 
2082
- function create$1() {
2083
- var out = new ARRAY_TYPE(3);
2044
+ $existedFilter.appendChild(_$feDropShadow);
2045
+ $existedFilter.id = shadowId; // @see https://github.com/antvis/g/issues/1025
2084
2046
 
2085
- if (ARRAY_TYPE != Float32Array) {
2086
- out[0] = 0;
2087
- out[1] = 0;
2088
- out[2] = 0;
2047
+ $existedFilter.setAttribute('filterUnits', 'userSpaceOnUse');
2048
+ $def.appendChild($existedFilter);
2089
2049
  }
2090
2050
 
2091
- return out;
2092
- }
2093
- /**
2094
- * Calculates the length of a vec3
2095
- *
2096
- * @param {ReadonlyVec3} a vector to calculate length of
2097
- * @returns {Number} length of a
2098
- */
2051
+ var $feDropShadow = $existedFilter.children[0];
2052
+
2053
+ if (name === 'shadowColor') {
2054
+ var parsedColor = object.parsedStyle[name];
2055
+ $feDropShadow.setAttribute('flood-color', parsedColor.toString());
2056
+ } else if (name === 'shadowBlur') {
2057
+ var shadowBlur = object.parsedStyle[name]; // half the blur radius
2058
+ // @see https://drafts.csswg.org/css-backgrounds/#shadow-blur
2059
+ // @see https://css-tricks.com/breaking-css-box-shadow-vs-drop-shadow/
2060
+
2061
+ $feDropShadow.setAttribute('stdDeviation', "" + ((shadowBlur === null || shadowBlur === void 0 ? void 0 : shadowBlur.value) || 0) / 4);
2062
+ } else if (name === 'shadowOffsetX') {
2063
+ var shadowOffsetX = object.parsedStyle[name];
2064
+ $feDropShadow.setAttribute('dx', "" + ((shadowOffsetX === null || shadowOffsetX === void 0 ? void 0 : shadowOffsetX.value) || 0) / 2);
2065
+ } else if (name === 'shadowOffsetY') {
2066
+ var shadowOffsetY = object.parsedStyle[name];
2067
+ $feDropShadow.setAttribute('dy', "" + ((shadowOffsetY === null || shadowOffsetY === void 0 ? void 0 : shadowOffsetY.value) || 0) / 2);
2068
+ } // only apply shadow when blur > 0
2099
2069
 
2100
2070
 
2101
- function length(a) {
2102
- var x = a[0];
2103
- var y = a[1];
2104
- var z = a[2];
2105
- return Math.hypot(x, y, z);
2071
+ if (!g.isNil(object.parsedStyle.shadowColor) && ((_object$parsedStyle$s = object.parsedStyle.shadowBlur) === null || _object$parsedStyle$s === void 0 ? void 0 : _object$parsedStyle$s.value) > 0) {
2072
+ // use filter <feDropShadow>
2073
+ // @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/feDropShadow
2074
+ $el === null || $el === void 0 ? void 0 : $el.setAttribute('filter', "url(#" + shadowId + ")");
2075
+ } else {
2076
+ $el === null || $el === void 0 ? void 0 : $el.removeAttribute('filter');
2077
+ }
2106
2078
  }
2107
- /**
2108
- * Creates a new vec3 initialized with the given values
2109
- *
2110
- * @param {Number} x X component
2111
- * @param {Number} y Y component
2112
- * @param {Number} z Z component
2113
- * @returns {vec3} a new 3D vector
2114
- */
2115
2079
 
2080
+ var urlRegexp = /url\(#(.*)\)/;
2116
2081
 
2117
- function fromValues(x, y, z) {
2118
- var out = new ARRAY_TYPE(3);
2119
- out[0] = x;
2120
- out[1] = y;
2121
- out[2] = z;
2122
- return out;
2123
- }
2124
- /**
2125
- * Normalize a vec3
2126
- *
2127
- * @param {vec3} out the receiving vector
2128
- * @param {ReadonlyVec3} a vector to normalize
2129
- * @returns {vec3} out
2130
- */
2082
+ var DefElementManager = /*#__PURE__*/function () {
2083
+ function DefElementManager() {
2084
+ this.canvasConfig = void 0;
2085
+ this.contextService = void 0;
2086
+ this.$def = void 0;
2087
+ this.gradientCache = {};
2088
+ }
2131
2089
 
2090
+ var _proto = DefElementManager.prototype;
2132
2091
 
2133
- function normalize(out, a) {
2134
- var x = a[0];
2135
- var y = a[1];
2136
- var z = a[2];
2137
- var len = x * x + y * y + z * z;
2092
+ _proto.getDefElement = function getDefElement() {
2093
+ return this.$def;
2094
+ };
2138
2095
 
2139
- if (len > 0) {
2140
- //TODO: evaluate use of glm_invsqrt here?
2141
- len = 1 / Math.sqrt(len);
2142
- }
2096
+ _proto.init = function init() {
2097
+ var document = this.canvasConfig.document;
2098
+ var $svg = this.contextService.getContext();
2099
+ this.$def = createSVGElement('defs', document);
2100
+ $svg.appendChild(this.$def);
2101
+ };
2143
2102
 
2144
- out[0] = a[0] * len;
2145
- out[1] = a[1] * len;
2146
- out[2] = a[2] * len;
2147
- return out;
2148
- }
2149
- /**
2150
- * Calculates the dot product of two vec3's
2151
- *
2152
- * @param {ReadonlyVec3} a the first operand
2153
- * @param {ReadonlyVec3} b the second operand
2154
- * @returns {Number} dot product of a and b
2155
- */
2103
+ _proto.clear = function clear(entity) {
2104
+ var _this = this;
2156
2105
 
2106
+ Object.keys(this.gradientCache).forEach(function (id) {
2107
+ _this.clearUnusedDefElement(_this.gradientCache, id, entity);
2108
+ });
2109
+ };
2157
2110
 
2158
- function dot(a, b) {
2159
- return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
2160
- }
2161
- /**
2162
- * Computes the cross product of two vec3's
2163
- *
2164
- * @param {vec3} out the receiving vector
2165
- * @param {ReadonlyVec3} a the first operand
2166
- * @param {ReadonlyVec3} b the second operand
2167
- * @returns {vec3} out
2168
- */
2111
+ _proto.clearUnusedDefElement = function clearUnusedDefElement(cache, id, entity) {
2112
+ if (cache[id].size === 1 && cache[id].has(entity)) {
2113
+ var targetElement = this.$def.querySelector("#" + id);
2169
2114
 
2115
+ if (targetElement) {
2116
+ this.$def.removeChild(targetElement);
2117
+ }
2118
+ }
2119
+ };
2170
2120
 
2171
- function cross(out, a, b) {
2172
- var ax = a[0],
2173
- ay = a[1],
2174
- az = a[2];
2175
- var bx = b[0],
2176
- by = b[1],
2177
- bz = b[2];
2178
- out[0] = ay * bz - az * by;
2179
- out[1] = az * bx - ax * bz;
2180
- out[2] = ax * by - ay * bx;
2181
- return out;
2182
- }
2183
- /**
2184
- * Alias for {@link vec3.length}
2185
- * @function
2186
- */
2121
+ _proto.createOrUpdateGradientAndPattern = function createOrUpdateGradientAndPattern$1(object, $el, parsedColor, name) {
2122
+ var _this$canvasConfig = this.canvasConfig,
2123
+ doc = _this$canvasConfig.document,
2124
+ createImage = _this$canvasConfig.createImage;
2187
2125
 
2126
+ if ($el) {
2127
+ var _$el$getAttribute; // `url(#${gradientId})`
2188
2128
 
2189
- var len = length;
2190
- /**
2191
- * Perform some operation over an array of vec3s.
2192
- *
2193
- * @param {Array} a the array of vectors to iterate over
2194
- * @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed
2195
- * @param {Number} offset Number of elements to skip at the beginning of the array
2196
- * @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array
2197
- * @param {Function} fn Function to call for each vector in the array
2198
- * @param {Object} [arg] additional argument to pass to fn
2199
- * @returns {Array} a
2200
- * @function
2201
- */
2202
2129
 
2203
- var forEach = function () {
2204
- var vec = create$1();
2205
- return function (a, stride, offset, count, fn, arg) {
2206
- var i, l;
2130
+ var matches = (_$el$getAttribute = $el.getAttribute(name)) === null || _$el$getAttribute === void 0 ? void 0 : _$el$getAttribute.match(urlRegexp);
2207
2131
 
2208
- if (!stride) {
2209
- stride = 3;
2210
- }
2132
+ if (matches && matches.length > 1) {
2133
+ this.clearUnusedDefElement(this.gradientCache, matches[1], object.entity);
2134
+ }
2211
2135
 
2212
- if (!offset) {
2213
- offset = 0;
2214
- }
2136
+ var newDefElementId = createOrUpdateGradientAndPattern(doc || document, this.$def, object, $el, parsedColor, name, createImage);
2215
2137
 
2216
- if (count) {
2217
- l = Math.min(count * stride + offset, a.length);
2218
- } else {
2219
- l = a.length;
2220
- }
2138
+ if (!this.gradientCache[newDefElementId]) {
2139
+ this.gradientCache[newDefElementId] = new Set();
2140
+ }
2221
2141
 
2222
- for (i = offset; i < l; i += stride) {
2223
- vec[0] = a[i];
2224
- vec[1] = a[i + 1];
2225
- vec[2] = a[i + 2];
2226
- fn(vec, vec, arg);
2227
- a[i] = vec[0];
2228
- a[i + 1] = vec[1];
2229
- a[i + 2] = vec[2];
2142
+ this.gradientCache[newDefElementId].add(object.entity);
2230
2143
  }
2144
+ };
2231
2145
 
2232
- return a;
2146
+ _proto.createOrUpdateShadow = function createOrUpdateShadow$1(object, $el, name) {
2147
+ var doc = this.canvasConfig.document;
2148
+ createOrUpdateShadow(doc || document, this.$def, object, $el, name);
2233
2149
  };
2234
- }();
2235
- /**
2236
- * 4 Dimensional Vector
2237
- * @module vec4
2238
- */
2239
2150
 
2240
- /**
2241
- * Creates a new, empty vec4
2242
- *
2243
- * @returns {vec4} a new 4D vector
2244
- */
2151
+ _proto.createOrUpdateFilter = function createOrUpdateFilter$1(object, $el, filters) {
2152
+ var doc = this.canvasConfig.document;
2153
+ createOrUpdateFilter(doc || document, this.$def, object, $el, filters);
2154
+ };
2245
2155
 
2156
+ return DefElementManager;
2157
+ }();
2246
2158
 
2247
- function create$2() {
2248
- var out = new ARRAY_TYPE(4);
2159
+ __decorate([g.inject(g.CanvasConfig), __metadata("design:type", Object)], DefElementManager.prototype, "canvasConfig", void 0);
2249
2160
 
2250
- if (ARRAY_TYPE != Float32Array) {
2251
- out[0] = 0;
2252
- out[1] = 0;
2253
- out[2] = 0;
2254
- out[3] = 0;
2255
- }
2161
+ __decorate([g.inject(g.ContextService), __metadata("design:type", Object)], DefElementManager.prototype, "contextService", void 0);
2256
2162
 
2257
- return out;
2258
- }
2259
- /**
2260
- * Normalize a vec4
2261
- *
2262
- * @param {vec4} out the receiving vector
2263
- * @param {ReadonlyVec4} a vector to normalize
2264
- * @returns {vec4} out
2265
- */
2163
+ DefElementManager = __decorate([g.singleton()], DefElementManager);
2266
2164
 
2165
+ function _regeneratorRuntime$2() {
2166
+ /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
2167
+ _regeneratorRuntime$2 = function _regeneratorRuntime() {
2168
+ return exports;
2169
+ };
2267
2170
 
2268
- function normalize$1(out, a) {
2269
- var x = a[0];
2270
- var y = a[1];
2271
- var z = a[2];
2272
- var w = a[3];
2273
- var len = x * x + y * y + z * z + w * w;
2171
+ var exports = {},
2172
+ Op = Object.prototype,
2173
+ hasOwn = Op.hasOwnProperty,
2174
+ $Symbol = "function" == typeof Symbol ? Symbol : {},
2175
+ iteratorSymbol = $Symbol.iterator || "@@iterator",
2176
+ asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator",
2177
+ toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
2274
2178
 
2275
- if (len > 0) {
2276
- len = 1 / Math.sqrt(len);
2179
+ function define(obj, key, value) {
2180
+ return Object.defineProperty(obj, key, {
2181
+ value: value,
2182
+ enumerable: !0,
2183
+ configurable: !0,
2184
+ writable: !0
2185
+ }), obj[key];
2277
2186
  }
2278
2187
 
2279
- out[0] = x * len;
2280
- out[1] = y * len;
2281
- out[2] = z * len;
2282
- out[3] = w * len;
2283
- return out;
2284
- }
2285
- /**
2286
- * Perform some operation over an array of vec4s.
2287
- *
2288
- * @param {Array} a the array of vectors to iterate over
2289
- * @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed
2290
- * @param {Number} offset Number of elements to skip at the beginning of the array
2291
- * @param {Number} count Number of vec4s to iterate over. If 0 iterates over entire array
2292
- * @param {Function} fn Function to call for each vector in the array
2293
- * @param {Object} [arg] additional argument to pass to fn
2294
- * @returns {Array} a
2295
- * @function
2296
- */
2188
+ try {
2189
+ define({}, "");
2190
+ } catch (err) {
2191
+ define = function define(obj, key, value) {
2192
+ return obj[key] = value;
2193
+ };
2194
+ }
2297
2195
 
2196
+ function wrap(innerFn, outerFn, self, tryLocsList) {
2197
+ var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator,
2198
+ generator = Object.create(protoGenerator.prototype),
2199
+ context = new Context(tryLocsList || []);
2200
+ return generator._invoke = function (innerFn, self, context) {
2201
+ var state = "suspendedStart";
2202
+ return function (method, arg) {
2203
+ if ("executing" === state) throw new Error("Generator is already running");
2298
2204
 
2299
- var forEach$1 = function () {
2300
- var vec = create$2();
2301
- return function (a, stride, offset, count, fn, arg) {
2302
- var i, l;
2205
+ if ("completed" === state) {
2206
+ if ("throw" === method) throw arg;
2207
+ return doneResult();
2208
+ }
2303
2209
 
2304
- if (!stride) {
2305
- stride = 4;
2306
- }
2210
+ for (context.method = method, context.arg = arg;;) {
2211
+ var delegate = context.delegate;
2307
2212
 
2308
- if (!offset) {
2309
- offset = 0;
2310
- }
2213
+ if (delegate) {
2214
+ var delegateResult = maybeInvokeDelegate(delegate, context);
2311
2215
 
2312
- if (count) {
2313
- l = Math.min(count * stride + offset, a.length);
2314
- } else {
2315
- l = a.length;
2316
- }
2216
+ if (delegateResult) {
2217
+ if (delegateResult === ContinueSentinel) continue;
2218
+ return delegateResult;
2219
+ }
2220
+ }
2317
2221
 
2318
- for (i = offset; i < l; i += stride) {
2319
- vec[0] = a[i];
2320
- vec[1] = a[i + 1];
2321
- vec[2] = a[i + 2];
2322
- vec[3] = a[i + 3];
2323
- fn(vec, vec, arg);
2324
- a[i] = vec[0];
2325
- a[i + 1] = vec[1];
2326
- a[i + 2] = vec[2];
2327
- a[i + 3] = vec[3];
2328
- }
2222
+ if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) {
2223
+ if ("suspendedStart" === state) throw state = "completed", context.arg;
2224
+ context.dispatchException(context.arg);
2225
+ } else "return" === context.method && context.abrupt("return", context.arg);
2226
+ state = "executing";
2227
+ var record = tryCatch(innerFn, self, context);
2329
2228
 
2330
- return a;
2331
- };
2332
- }();
2333
- /**
2334
- * Quaternion
2335
- * @module quat
2336
- */
2229
+ if ("normal" === record.type) {
2230
+ if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue;
2231
+ return {
2232
+ value: record.arg,
2233
+ done: context.done
2234
+ };
2235
+ }
2236
+
2237
+ "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg);
2238
+ }
2239
+ };
2240
+ }(innerFn, self, context), generator;
2241
+ }
2242
+
2243
+ function tryCatch(fn, obj, arg) {
2244
+ try {
2245
+ return {
2246
+ type: "normal",
2247
+ arg: fn.call(obj, arg)
2248
+ };
2249
+ } catch (err) {
2250
+ return {
2251
+ type: "throw",
2252
+ arg: err
2253
+ };
2254
+ }
2255
+ }
2256
+
2257
+ exports.wrap = wrap;
2258
+ var ContinueSentinel = {};
2259
+
2260
+ function Generator() {}
2261
+
2262
+ function GeneratorFunction() {}
2263
+
2264
+ function GeneratorFunctionPrototype() {}
2265
+
2266
+ var IteratorPrototype = {};
2267
+ define(IteratorPrototype, iteratorSymbol, function () {
2268
+ return this;
2269
+ });
2270
+ var getProto = Object.getPrototypeOf,
2271
+ NativeIteratorPrototype = getProto && getProto(getProto(values([])));
2272
+ NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype);
2273
+ var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype);
2274
+
2275
+ function defineIteratorMethods(prototype) {
2276
+ ["next", "throw", "return"].forEach(function (method) {
2277
+ define(prototype, method, function (arg) {
2278
+ return this._invoke(method, arg);
2279
+ });
2280
+ });
2281
+ }
2282
+
2283
+ function AsyncIterator(generator, PromiseImpl) {
2284
+ function invoke(method, arg, resolve, reject) {
2285
+ var record = tryCatch(generator[method], generator, arg);
2286
+
2287
+ if ("throw" !== record.type) {
2288
+ var result = record.arg,
2289
+ value = result.value;
2290
+ return value && "object" == typeof value && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) {
2291
+ invoke("next", value, resolve, reject);
2292
+ }, function (err) {
2293
+ invoke("throw", err, resolve, reject);
2294
+ }) : PromiseImpl.resolve(value).then(function (unwrapped) {
2295
+ result.value = unwrapped, resolve(result);
2296
+ }, function (error) {
2297
+ return invoke("throw", error, resolve, reject);
2298
+ });
2299
+ }
2300
+
2301
+ reject(record.arg);
2302
+ }
2303
+
2304
+ var previousPromise;
2305
+
2306
+ this._invoke = function (method, arg) {
2307
+ function callInvokeWithMethodAndArg() {
2308
+ return new PromiseImpl(function (resolve, reject) {
2309
+ invoke(method, arg, resolve, reject);
2310
+ });
2311
+ }
2312
+
2313
+ return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
2314
+ };
2315
+ }
2316
+
2317
+ function maybeInvokeDelegate(delegate, context) {
2318
+ var method = delegate.iterator[context.method];
2319
+
2320
+ if (undefined === method) {
2321
+ if (context.delegate = null, "throw" === context.method) {
2322
+ if (delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel;
2323
+ context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method");
2324
+ }
2325
+
2326
+ return ContinueSentinel;
2327
+ }
2328
+
2329
+ var record = tryCatch(method, delegate.iterator, context.arg);
2330
+ if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel;
2331
+ var info = record.arg;
2332
+ return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel);
2333
+ }
2334
+
2335
+ function pushTryEntry(locs) {
2336
+ var entry = {
2337
+ tryLoc: locs[0]
2338
+ };
2339
+ 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry);
2340
+ }
2341
+
2342
+ function resetTryEntry(entry) {
2343
+ var record = entry.completion || {};
2344
+ record.type = "normal", delete record.arg, entry.completion = record;
2345
+ }
2346
+
2347
+ function Context(tryLocsList) {
2348
+ this.tryEntries = [{
2349
+ tryLoc: "root"
2350
+ }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0);
2351
+ }
2352
+
2353
+ function values(iterable) {
2354
+ if (iterable) {
2355
+ var iteratorMethod = iterable[iteratorSymbol];
2356
+ if (iteratorMethod) return iteratorMethod.call(iterable);
2357
+ if ("function" == typeof iterable.next) return iterable;
2358
+
2359
+ if (!isNaN(iterable.length)) {
2360
+ var i = -1,
2361
+ next = function next() {
2362
+ for (; ++i < iterable.length;) {
2363
+ if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next;
2364
+ }
2365
+
2366
+ return next.value = undefined, next.done = !0, next;
2367
+ };
2368
+
2369
+ return next.next = next;
2370
+ }
2371
+ }
2372
+
2373
+ return {
2374
+ next: doneResult
2375
+ };
2376
+ }
2377
+
2378
+ function doneResult() {
2379
+ return {
2380
+ value: undefined,
2381
+ done: !0
2382
+ };
2383
+ }
2384
+
2385
+ return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) {
2386
+ var ctor = "function" == typeof genFun && genFun.constructor;
2387
+ return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name));
2388
+ }, exports.mark = function (genFun) {
2389
+ return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun;
2390
+ }, exports.awrap = function (arg) {
2391
+ return {
2392
+ __await: arg
2393
+ };
2394
+ }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
2395
+ return this;
2396
+ }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) {
2397
+ void 0 === PromiseImpl && (PromiseImpl = Promise);
2398
+ var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl);
2399
+ return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) {
2400
+ return result.done ? result.value : iter.next();
2401
+ });
2402
+ }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () {
2403
+ return this;
2404
+ }), define(Gp, "toString", function () {
2405
+ return "[object Generator]";
2406
+ }), exports.keys = function (object) {
2407
+ var keys = [];
2408
+
2409
+ for (var key in object) {
2410
+ keys.push(key);
2411
+ }
2412
+
2413
+ return keys.reverse(), function next() {
2414
+ for (; keys.length;) {
2415
+ var key = keys.pop();
2416
+ if (key in object) return next.value = key, next.done = !1, next;
2417
+ }
2418
+
2419
+ return next.done = !0, next;
2420
+ };
2421
+ }, exports.values = values, Context.prototype = {
2422
+ constructor: Context,
2423
+ reset: function reset(skipTempReset) {
2424
+ if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) {
2425
+ "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined);
2426
+ }
2427
+ },
2428
+ stop: function stop() {
2429
+ this.done = !0;
2430
+ var rootRecord = this.tryEntries[0].completion;
2431
+ if ("throw" === rootRecord.type) throw rootRecord.arg;
2432
+ return this.rval;
2433
+ },
2434
+ dispatchException: function dispatchException(exception) {
2435
+ if (this.done) throw exception;
2436
+ var context = this;
2437
+
2438
+ function handle(loc, caught) {
2439
+ return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught;
2440
+ }
2441
+
2442
+ for (var i = this.tryEntries.length - 1; i >= 0; --i) {
2443
+ var entry = this.tryEntries[i],
2444
+ record = entry.completion;
2445
+ if ("root" === entry.tryLoc) return handle("end");
2446
+
2447
+ if (entry.tryLoc <= this.prev) {
2448
+ var hasCatch = hasOwn.call(entry, "catchLoc"),
2449
+ hasFinally = hasOwn.call(entry, "finallyLoc");
2450
+
2451
+ if (hasCatch && hasFinally) {
2452
+ if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
2453
+ if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
2454
+ } else if (hasCatch) {
2455
+ if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
2456
+ } else {
2457
+ if (!hasFinally) throw new Error("try statement without catch or finally");
2458
+ if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
2459
+ }
2460
+ }
2461
+ }
2462
+ },
2463
+ abrupt: function abrupt(type, arg) {
2464
+ for (var i = this.tryEntries.length - 1; i >= 0; --i) {
2465
+ var entry = this.tryEntries[i];
2466
+
2467
+ if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) {
2468
+ var finallyEntry = entry;
2469
+ break;
2470
+ }
2471
+ }
2472
+
2473
+ finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null);
2474
+ var record = finallyEntry ? finallyEntry.completion : {};
2475
+ return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record);
2476
+ },
2477
+ complete: function complete(record, afterLoc) {
2478
+ if ("throw" === record.type) throw record.arg;
2479
+ return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel;
2480
+ },
2481
+ finish: function finish(finallyLoc) {
2482
+ for (var i = this.tryEntries.length - 1; i >= 0; --i) {
2483
+ var entry = this.tryEntries[i];
2484
+ if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel;
2485
+ }
2486
+ },
2487
+ catch: function _catch(tryLoc) {
2488
+ for (var i = this.tryEntries.length - 1; i >= 0; --i) {
2489
+ var entry = this.tryEntries[i];
2490
+
2491
+ if (entry.tryLoc === tryLoc) {
2492
+ var record = entry.completion;
2493
+
2494
+ if ("throw" === record.type) {
2495
+ var thrown = record.arg;
2496
+ resetTryEntry(entry);
2497
+ }
2498
+
2499
+ return thrown;
2500
+ }
2501
+ }
2502
+
2503
+ throw new Error("illegal catch attempt");
2504
+ },
2505
+ delegateYield: function delegateYield(iterable, resultName, nextLoc) {
2506
+ return this.delegate = {
2507
+ iterator: values(iterable),
2508
+ resultName: resultName,
2509
+ nextLoc: nextLoc
2510
+ }, "next" === this.method && (this.arg = undefined), ContinueSentinel;
2511
+ }
2512
+ }, exports;
2513
+ }
2514
+
2515
+ function asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, key, arg) {
2516
+ try {
2517
+ var info = gen[key](arg);
2518
+ var value = info.value;
2519
+ } catch (error) {
2520
+ reject(error);
2521
+ return;
2522
+ }
2523
+
2524
+ if (info.done) {
2525
+ resolve(value);
2526
+ } else {
2527
+ Promise.resolve(value).then(_next, _throw);
2528
+ }
2529
+ }
2530
+
2531
+ function _asyncToGenerator$2(fn) {
2532
+ return function () {
2533
+ var self = this,
2534
+ args = arguments;
2535
+ return new Promise(function (resolve, reject) {
2536
+ var gen = fn.apply(self, args);
2537
+
2538
+ function _next(value) {
2539
+ asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, "next", value);
2540
+ }
2337
2541
 
2542
+ function _throw(err) {
2543
+ asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, "throw", err);
2544
+ }
2545
+
2546
+ _next(undefined);
2547
+ });
2548
+ };
2549
+ }
2338
2550
  /**
2339
- * Creates a new identity quat
2340
- *
2341
- * @returns {quat} a new quaternion
2551
+ * Common utilities
2552
+ * @module glMatrix
2342
2553
  */
2554
+ // Configuration Constants
2343
2555
 
2344
2556
 
2345
- function create$3() {
2346
- var out = new ARRAY_TYPE(4);
2557
+ var EPSILON = 0.000001;
2558
+ var ARRAY_TYPE = typeof Float32Array !== 'undefined' ? Float32Array : Array;
2559
+ if (!Math.hypot) Math.hypot = function () {
2560
+ var y = 0,
2561
+ i = arguments.length;
2562
+
2563
+ while (i--) {
2564
+ y += arguments[i] * arguments[i];
2565
+ }
2566
+
2567
+ return Math.sqrt(y);
2568
+ };
2569
+ /**
2570
+ * 3x3 Matrix
2571
+ * @module mat3
2572
+ */
2573
+
2574
+ /**
2575
+ * Creates a new identity mat3
2576
+ *
2577
+ * @returns {mat3} a new 3x3 matrix
2578
+ */
2579
+
2580
+ function create() {
2581
+ var out = new ARRAY_TYPE(9);
2347
2582
 
2348
2583
  if (ARRAY_TYPE != Float32Array) {
2349
- out[0] = 0;
2350
2584
  out[1] = 0;
2351
2585
  out[2] = 0;
2586
+ out[3] = 0;
2587
+ out[5] = 0;
2588
+ out[6] = 0;
2589
+ out[7] = 0;
2352
2590
  }
2353
2591
 
2354
- out[3] = 1;
2592
+ out[0] = 1;
2593
+ out[4] = 1;
2594
+ out[8] = 1;
2355
2595
  return out;
2356
2596
  }
2357
2597
  /**
2358
- * Sets a quat from the given angle and rotation axis,
2359
- * then returns it.
2360
- *
2361
- * @param {quat} out the receiving quaternion
2362
- * @param {ReadonlyVec3} axis the axis around which to rotate
2363
- * @param {Number} rad the angle in radians
2364
- * @returns {quat} out
2365
- **/
2598
+ * Returns the translation vector component of a transformation
2599
+ * matrix. If a matrix is built with fromRotationTranslation,
2600
+ * the returned vector will be the same as the translation vector
2601
+ * originally supplied.
2602
+ * @param {vec3} out Vector to receive translation component
2603
+ * @param {ReadonlyMat4} mat Matrix to be decomposed (input)
2604
+ * @return {vec3} out
2605
+ */
2366
2606
 
2367
2607
 
2368
- function setAxisAngle(out, axis, rad) {
2369
- rad = rad * 0.5;
2370
- var s = Math.sin(rad);
2371
- out[0] = s * axis[0];
2372
- out[1] = s * axis[1];
2373
- out[2] = s * axis[2];
2374
- out[3] = Math.cos(rad);
2608
+ function getTranslation(out, mat) {
2609
+ out[0] = mat[12];
2610
+ out[1] = mat[13];
2611
+ out[2] = mat[14];
2375
2612
  return out;
2376
2613
  }
2377
2614
  /**
2378
- * Performs a spherical linear interpolation between two quat
2379
- *
2380
- * @param {quat} out the receiving quaternion
2381
- * @param {ReadonlyQuat} a the first operand
2382
- * @param {ReadonlyQuat} b the second operand
2383
- * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
2384
- * @returns {quat} out
2615
+ * Returns the scaling factor component of a transformation
2616
+ * matrix. If a matrix is built with fromRotationTranslationScale
2617
+ * with a normalized Quaternion paramter, the returned vector will be
2618
+ * the same as the scaling vector
2619
+ * originally supplied.
2620
+ * @param {vec3} out Vector to receive scaling factor component
2621
+ * @param {ReadonlyMat4} mat Matrix to be decomposed (input)
2622
+ * @return {vec3} out
2385
2623
  */
2386
2624
 
2387
2625
 
2388
- function slerp(out, a, b, t) {
2389
- // benchmarks:
2390
- // http://jsperf.com/quaternion-slerp-implementations
2391
- var ax = a[0],
2392
- ay = a[1],
2393
- az = a[2],
2394
- aw = a[3];
2395
- var bx = b[0],
2396
- by = b[1],
2397
- bz = b[2],
2398
- bw = b[3];
2399
- var omega, cosom, sinom, scale0, scale1; // calc cosine
2400
-
2401
- cosom = ax * bx + ay * by + az * bz + aw * bw; // adjust signs (if necessary)
2626
+ function getScaling(out, mat) {
2627
+ var m11 = mat[0];
2628
+ var m12 = mat[1];
2629
+ var m13 = mat[2];
2630
+ var m21 = mat[4];
2631
+ var m22 = mat[5];
2632
+ var m23 = mat[6];
2633
+ var m31 = mat[8];
2634
+ var m32 = mat[9];
2635
+ var m33 = mat[10];
2636
+ out[0] = Math.hypot(m11, m12, m13);
2637
+ out[1] = Math.hypot(m21, m22, m23);
2638
+ out[2] = Math.hypot(m31, m32, m33);
2639
+ return out;
2640
+ }
2641
+ /**
2642
+ * Returns a quaternion representing the rotational component
2643
+ * of a transformation matrix. If a matrix is built with
2644
+ * fromRotationTranslation, the returned quaternion will be the
2645
+ * same as the quaternion originally supplied.
2646
+ * @param {quat} out Quaternion to receive the rotation component
2647
+ * @param {ReadonlyMat4} mat Matrix to be decomposed (input)
2648
+ * @return {quat} out
2649
+ */
2402
2650
 
2403
- if (cosom < 0.0) {
2404
- cosom = -cosom;
2405
- bx = -bx;
2406
- by = -by;
2407
- bz = -bz;
2408
- bw = -bw;
2409
- } // calculate coefficients
2410
2651
 
2652
+ function getRotation(out, mat) {
2653
+ var scaling = new ARRAY_TYPE(3);
2654
+ getScaling(scaling, mat);
2655
+ var is1 = 1 / scaling[0];
2656
+ var is2 = 1 / scaling[1];
2657
+ var is3 = 1 / scaling[2];
2658
+ var sm11 = mat[0] * is1;
2659
+ var sm12 = mat[1] * is2;
2660
+ var sm13 = mat[2] * is3;
2661
+ var sm21 = mat[4] * is1;
2662
+ var sm22 = mat[5] * is2;
2663
+ var sm23 = mat[6] * is3;
2664
+ var sm31 = mat[8] * is1;
2665
+ var sm32 = mat[9] * is2;
2666
+ var sm33 = mat[10] * is3;
2667
+ var trace = sm11 + sm22 + sm33;
2668
+ var S = 0;
2411
2669
 
2412
- if (1.0 - cosom > EPSILON) {
2413
- // standard case (slerp)
2414
- omega = Math.acos(cosom);
2415
- sinom = Math.sin(omega);
2416
- scale0 = Math.sin((1.0 - t) * omega) / sinom;
2417
- scale1 = Math.sin(t * omega) / sinom;
2670
+ if (trace > 0) {
2671
+ S = Math.sqrt(trace + 1.0) * 2;
2672
+ out[3] = 0.25 * S;
2673
+ out[0] = (sm23 - sm32) / S;
2674
+ out[1] = (sm31 - sm13) / S;
2675
+ out[2] = (sm12 - sm21) / S;
2676
+ } else if (sm11 > sm22 && sm11 > sm33) {
2677
+ S = Math.sqrt(1.0 + sm11 - sm22 - sm33) * 2;
2678
+ out[3] = (sm23 - sm32) / S;
2679
+ out[0] = 0.25 * S;
2680
+ out[1] = (sm12 + sm21) / S;
2681
+ out[2] = (sm31 + sm13) / S;
2682
+ } else if (sm22 > sm33) {
2683
+ S = Math.sqrt(1.0 + sm22 - sm11 - sm33) * 2;
2684
+ out[3] = (sm31 - sm13) / S;
2685
+ out[0] = (sm12 + sm21) / S;
2686
+ out[1] = 0.25 * S;
2687
+ out[2] = (sm23 + sm32) / S;
2418
2688
  } else {
2419
- // "from" and "to" quaternions are very close
2420
- // ... so we can do a linear interpolation
2421
- scale0 = 1.0 - t;
2422
- scale1 = t;
2423
- } // calculate final values
2424
-
2689
+ S = Math.sqrt(1.0 + sm33 - sm11 - sm22) * 2;
2690
+ out[3] = (sm12 - sm21) / S;
2691
+ out[0] = (sm31 + sm13) / S;
2692
+ out[1] = (sm23 + sm32) / S;
2693
+ out[2] = 0.25 * S;
2694
+ }
2425
2695
 
2426
- out[0] = scale0 * ax + scale1 * bx;
2427
- out[1] = scale0 * ay + scale1 * by;
2428
- out[2] = scale0 * az + scale1 * bz;
2429
- out[3] = scale0 * aw + scale1 * bw;
2430
2696
  return out;
2431
2697
  }
2432
2698
  /**
2433
- * Creates a quaternion from the given 3x3 rotation matrix.
2434
- *
2435
- * NOTE: The resultant quaternion is not normalized, so you should be sure
2436
- * to renormalize the quaternion yourself where necessary.
2437
- *
2438
- * @param {quat} out the receiving quaternion
2439
- * @param {ReadonlyMat3} m rotation matrix
2440
- * @returns {quat} out
2441
- * @function
2699
+ * 3 Dimensional Vector
2700
+ * @module vec3
2442
2701
  */
2443
2702
 
2444
-
2445
- function fromMat3(out, m) {
2446
- // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
2447
- // article "Quaternion Calculus and Fast Animation".
2448
- var fTrace = m[0] + m[4] + m[8];
2449
- var fRoot;
2450
-
2451
- if (fTrace > 0.0) {
2452
- // |w| > 1/2, may as well choose w > 1/2
2453
- fRoot = Math.sqrt(fTrace + 1.0); // 2w
2454
-
2455
- out[3] = 0.5 * fRoot;
2456
- fRoot = 0.5 / fRoot; // 1/(4w)
2457
-
2458
- out[0] = (m[5] - m[7]) * fRoot;
2459
- out[1] = (m[6] - m[2]) * fRoot;
2460
- out[2] = (m[1] - m[3]) * fRoot;
2461
- } else {
2462
- // |w| <= 1/2
2463
- var i = 0;
2464
- if (m[4] > m[0]) i = 1;
2465
- if (m[8] > m[i * 3 + i]) i = 2;
2466
- var j = (i + 1) % 3;
2467
- var k = (i + 2) % 3;
2468
- fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1.0);
2469
- out[i] = 0.5 * fRoot;
2470
- fRoot = 0.5 / fRoot;
2471
- out[3] = (m[j * 3 + k] - m[k * 3 + j]) * fRoot;
2472
- out[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot;
2473
- out[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot;
2703
+ /**
2704
+ * Creates a new, empty vec3
2705
+ *
2706
+ * @returns {vec3} a new 3D vector
2707
+ */
2708
+
2709
+
2710
+ function create$1() {
2711
+ var out = new ARRAY_TYPE(3);
2712
+
2713
+ if (ARRAY_TYPE != Float32Array) {
2714
+ out[0] = 0;
2715
+ out[1] = 0;
2716
+ out[2] = 0;
2474
2717
  }
2475
2718
 
2476
2719
  return out;
2477
2720
  }
2478
2721
  /**
2479
- * Normalize a quat
2722
+ * Calculates the length of a vec3
2480
2723
  *
2481
- * @param {quat} out the receiving quaternion
2482
- * @param {ReadonlyQuat} a quaternion to normalize
2483
- * @returns {quat} out
2484
- * @function
2724
+ * @param {ReadonlyVec3} a vector to calculate length of
2725
+ * @returns {Number} length of a
2485
2726
  */
2486
2727
 
2487
2728
 
2488
- var normalize$2 = normalize$1;
2729
+ function length(a) {
2730
+ var x = a[0];
2731
+ var y = a[1];
2732
+ var z = a[2];
2733
+ return Math.hypot(x, y, z);
2734
+ }
2489
2735
  /**
2490
- * Sets a quaternion to represent the shortest rotation from one
2491
- * vector to another.
2492
- *
2493
- * Both vectors are assumed to be unit length.
2736
+ * Creates a new vec3 initialized with the given values
2494
2737
  *
2495
- * @param {quat} out the receiving quaternion.
2496
- * @param {ReadonlyVec3} a the initial vector
2497
- * @param {ReadonlyVec3} b the destination vector
2498
- * @returns {quat} out
2738
+ * @param {Number} x X component
2739
+ * @param {Number} y Y component
2740
+ * @param {Number} z Z component
2741
+ * @returns {vec3} a new 3D vector
2499
2742
  */
2500
2743
 
2501
- var rotationTo = function () {
2502
- var tmpvec3 = create$1();
2503
- var xUnitVec3 = fromValues(1, 0, 0);
2504
- var yUnitVec3 = fromValues(0, 1, 0);
2505
- return function (out, a, b) {
2506
- var dot$1 = dot(a, b);
2507
2744
 
2508
- if (dot$1 < -0.999999) {
2509
- cross(tmpvec3, xUnitVec3, a);
2510
- if (len(tmpvec3) < 0.000001) cross(tmpvec3, yUnitVec3, a);
2511
- normalize(tmpvec3, tmpvec3);
2512
- setAxisAngle(out, tmpvec3, Math.PI);
2513
- return out;
2514
- } else if (dot$1 > 0.999999) {
2515
- out[0] = 0;
2516
- out[1] = 0;
2517
- out[2] = 0;
2518
- out[3] = 1;
2519
- return out;
2520
- } else {
2521
- cross(tmpvec3, a, b);
2522
- out[0] = tmpvec3[0];
2523
- out[1] = tmpvec3[1];
2524
- out[2] = tmpvec3[2];
2525
- out[3] = 1 + dot$1;
2526
- return normalize$2(out, out);
2527
- }
2528
- };
2529
- }();
2745
+ function fromValues(x, y, z) {
2746
+ var out = new ARRAY_TYPE(3);
2747
+ out[0] = x;
2748
+ out[1] = y;
2749
+ out[2] = z;
2750
+ return out;
2751
+ }
2530
2752
  /**
2531
- * Performs a spherical linear interpolation with two control points
2753
+ * Normalize a vec3
2532
2754
  *
2533
- * @param {quat} out the receiving quaternion
2534
- * @param {ReadonlyQuat} a the first operand
2535
- * @param {ReadonlyQuat} b the second operand
2536
- * @param {ReadonlyQuat} c the third operand
2537
- * @param {ReadonlyQuat} d the fourth operand
2538
- * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
2539
- * @returns {quat} out
2755
+ * @param {vec3} out the receiving vector
2756
+ * @param {ReadonlyVec3} a vector to normalize
2757
+ * @returns {vec3} out
2540
2758
  */
2541
2759
 
2542
2760
 
2543
- var sqlerp = function () {
2544
- var temp1 = create$3();
2545
- var temp2 = create$3();
2546
- return function (out, a, b, c, d, t) {
2547
- slerp(temp1, a, d, t);
2548
- slerp(temp2, b, c, t);
2549
- slerp(out, temp1, temp2, 2 * t * (1 - t));
2550
- return out;
2551
- };
2552
- }();
2761
+ function normalize(out, a) {
2762
+ var x = a[0];
2763
+ var y = a[1];
2764
+ var z = a[2];
2765
+ var len = x * x + y * y + z * z;
2766
+
2767
+ if (len > 0) {
2768
+ //TODO: evaluate use of glm_invsqrt here?
2769
+ len = 1 / Math.sqrt(len);
2770
+ }
2771
+
2772
+ out[0] = a[0] * len;
2773
+ out[1] = a[1] * len;
2774
+ out[2] = a[2] * len;
2775
+ return out;
2776
+ }
2553
2777
  /**
2554
- * Sets the specified quaternion with values corresponding to the given
2555
- * axes. Each axis is a vec3 and is expected to be unit length and
2556
- * perpendicular to all other specified axes.
2778
+ * Calculates the dot product of two vec3's
2557
2779
  *
2558
- * @param {ReadonlyVec3} view the vector representing the viewing direction
2559
- * @param {ReadonlyVec3} right the vector representing the local "right" direction
2560
- * @param {ReadonlyVec3} up the vector representing the local "up" direction
2561
- * @returns {quat} out
2780
+ * @param {ReadonlyVec3} a the first operand
2781
+ * @param {ReadonlyVec3} b the second operand
2782
+ * @returns {Number} dot product of a and b
2562
2783
  */
2563
2784
 
2564
2785
 
2565
- var setAxes = function () {
2566
- var matr = create();
2567
- return function (out, view, right, up) {
2568
- matr[0] = right[0];
2569
- matr[3] = right[1];
2570
- matr[6] = right[2];
2571
- matr[1] = up[0];
2572
- matr[4] = up[1];
2573
- matr[7] = up[2];
2574
- matr[2] = -view[0];
2575
- matr[5] = -view[1];
2576
- matr[8] = -view[2];
2577
- return normalize$2(out, fromMat3(out, matr));
2578
- };
2579
- }();
2580
-
2581
- var FILTER_PREFIX = 'g-filter-';
2582
- /**
2583
- * use SVG filters, eg. blur, brightness, contrast...
2584
- * @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Element/filter
2786
+ function dot(a, b) {
2787
+ return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
2788
+ }
2789
+ /**
2790
+ * Computes the cross product of two vec3's
2791
+ *
2792
+ * @param {vec3} out the receiving vector
2793
+ * @param {ReadonlyVec3} a the first operand
2794
+ * @param {ReadonlyVec3} b the second operand
2795
+ * @returns {vec3} out
2585
2796
  */
2586
2797
 
2587
- function createOrUpdateFilter(document, $def, object, $el, filters) {
2588
- // eg. filter="url(#f1) url(#f2)"
2589
- var filterName = FILTER_PREFIX + object.entity;
2590
- var $existedFilters = $def.querySelectorAll("[name=" + filterName + "]");
2591
-
2592
- if ($existedFilters.length) {
2593
- $existedFilters.forEach(function ($filter) {
2594
- $def.removeChild($filter);
2595
- });
2596
- }
2597
-
2598
- if (filters.length === 0) {
2599
- // 'none'
2600
- $el === null || $el === void 0 ? void 0 : $el.removeAttribute('filter');
2601
- } else {
2602
- var filterIds = filters.map(function (_ref, i) {
2603
- var name = _ref.name,
2604
- params = _ref.params;
2605
- var $filter = createSVGElement('filter', document); // @see https://github.com/antvis/g/issues/1025
2606
-
2607
- $filter.setAttribute('filterUnits', 'userSpaceOnUse');
2608
-
2609
- if (name === 'blur') {
2610
- createBlur(document, $filter, params);
2611
- } else if (name === 'brightness') {
2612
- createBrightness(document, $filter, params);
2613
- } else if (name === 'drop-shadow') {
2614
- createDropShadow(document, $filter, params);
2615
- } else if (name === 'contrast') {
2616
- createContrast(document, $filter, params);
2617
- } else if (name === 'grayscale') {
2618
- createGrayscale(document, $filter, params);
2619
- } else if (name === 'sepia') {
2620
- createSepia(document, $filter, params);
2621
- } else if (name === 'saturate') {
2622
- createSaturate(document, $filter, params);
2623
- } else if (name === 'hue-rotate') {
2624
- createHueRotate(document, $filter, params);
2625
- } else if (name === 'invert') {
2626
- createInvert(document, $filter, params);
2627
- }
2628
-
2629
- $filter.id = filterName + "-" + i;
2630
- $filter.setAttribute('name', filterName);
2631
- $def.appendChild($filter);
2632
- return $filter.id;
2633
- });
2634
- $el === null || $el === void 0 ? void 0 : $el.setAttribute('filter', filterIds.map(function (filterId) {
2635
- return "url(#" + filterId + ")";
2636
- }).join(' '));
2637
- }
2638
- }
2639
2798
 
2640
- function convertToAbsoluteValue(param) {
2641
- return param.unit === g.UnitType.kPercentage ? param.value / 100 : param.value;
2799
+ function cross(out, a, b) {
2800
+ var ax = a[0],
2801
+ ay = a[1],
2802
+ az = a[2];
2803
+ var bx = b[0],
2804
+ by = b[1],
2805
+ bz = b[2];
2806
+ out[0] = ay * bz - az * by;
2807
+ out[1] = az * bx - ax * bz;
2808
+ out[2] = ax * by - ay * bx;
2809
+ return out;
2642
2810
  }
2643
- /**
2644
- * @see https://drafts.fxtf.org/filter-effects/#blurEquivalent
2811
+ /**
2812
+ * Alias for {@link vec3.length}
2813
+ * @function
2645
2814
  */
2646
2815
 
2647
2816
 
2648
- function createBlur(document, $filter, params) {
2649
- var $feGaussianBlur = createSVGElement('feGaussianBlur', document);
2650
- $feGaussianBlur.setAttribute('in', 'SourceGraphic');
2651
- $feGaussianBlur.setAttribute('stdDeviation', "" + params[0].value);
2652
- $filter.appendChild($feGaussianBlur);
2653
- }
2654
-
2655
- function createFeComponentTransfer(document, $filter, _ref2) {
2656
- var type = _ref2.type,
2657
- slope = _ref2.slope,
2658
- intercept = _ref2.intercept,
2659
- tableValues = _ref2.tableValues;
2660
- var $feComponentTransfer = createSVGElement('feComponentTransfer', document);
2661
- [createSVGElement('feFuncR', document), createSVGElement('feFuncG', document), createSVGElement('feFuncB', document)].forEach(function ($feFunc) {
2662
- $feFunc.setAttribute('type', type);
2817
+ var len = length;
2818
+ /**
2819
+ * Perform some operation over an array of vec3s.
2820
+ *
2821
+ * @param {Array} a the array of vectors to iterate over
2822
+ * @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed
2823
+ * @param {Number} offset Number of elements to skip at the beginning of the array
2824
+ * @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array
2825
+ * @param {Function} fn Function to call for each vector in the array
2826
+ * @param {Object} [arg] additional argument to pass to fn
2827
+ * @returns {Array} a
2828
+ * @function
2829
+ */
2663
2830
 
2664
- if (type === 'table') {
2665
- $feFunc.setAttribute('tableValues', "" + tableValues);
2666
- } else {
2667
- $feFunc.setAttribute('slope', "" + slope);
2668
- $feFunc.setAttribute('intercept', "" + intercept);
2831
+ var forEach = function () {
2832
+ var vec = create$1();
2833
+ return function (a, stride, offset, count, fn, arg) {
2834
+ var i, l;
2835
+
2836
+ if (!stride) {
2837
+ stride = 3;
2669
2838
  }
2670
2839
 
2671
- $feComponentTransfer.appendChild($feFunc);
2672
- });
2673
- $filter.appendChild($feComponentTransfer);
2674
- }
2840
+ if (!offset) {
2841
+ offset = 0;
2842
+ }
2675
2843
 
2676
- function createContrast(document, $filter, params) {
2677
- var slope = convertToAbsoluteValue(params[0]);
2678
- createFeComponentTransfer(document, $filter, {
2679
- type: 'linear',
2680
- slope: slope,
2681
- intercept: -(0.5 * slope) + 0.5
2682
- });
2683
- }
2844
+ if (count) {
2845
+ l = Math.min(count * stride + offset, a.length);
2846
+ } else {
2847
+ l = a.length;
2848
+ }
2684
2849
 
2685
- function createInvert(document, $filter, params) {
2686
- var amount = convertToAbsoluteValue(params[0]);
2687
- createFeComponentTransfer(document, $filter, {
2688
- type: 'table',
2689
- tableValues: amount + " " + (1 - amount)
2690
- });
2691
- }
2850
+ for (i = offset; i < l; i += stride) {
2851
+ vec[0] = a[i];
2852
+ vec[1] = a[i + 1];
2853
+ vec[2] = a[i + 2];
2854
+ fn(vec, vec, arg);
2855
+ a[i] = vec[0];
2856
+ a[i + 1] = vec[1];
2857
+ a[i + 2] = vec[2];
2858
+ }
2692
2859
 
2693
- function createBrightness(document, $filter, params) {
2694
- var slope = convertToAbsoluteValue(params[0]);
2695
- createFeComponentTransfer(document, $filter, {
2696
- type: 'linear',
2697
- slope: slope,
2698
- intercept: 0
2699
- });
2700
- }
2860
+ return a;
2861
+ };
2862
+ }();
2863
+ /**
2864
+ * 4 Dimensional Vector
2865
+ * @module vec4
2866
+ */
2701
2867
 
2702
- function createSaturate(document, $filter, params) {
2703
- var amount = convertToAbsoluteValue(params[0]);
2704
- var $feColorMatrix = createSVGElement('feColorMatrix', document);
2705
- $feColorMatrix.setAttribute('type', 'saturate');
2706
- $feColorMatrix.setAttribute('values', "" + amount);
2707
- $filter.appendChild($feColorMatrix);
2708
- }
2868
+ /**
2869
+ * Creates a new, empty vec4
2870
+ *
2871
+ * @returns {vec4} a new 4D vector
2872
+ */
2709
2873
 
2710
- function createHueRotate(document, $filter, params) {
2711
- var $feColorMatrix = createSVGElement('feColorMatrix', document);
2712
- $feColorMatrix.setAttribute('type', 'hueRotate');
2713
- $feColorMatrix.setAttribute('values', "" + params[0].to(g.UnitType.kDegrees).value);
2714
- $filter.appendChild($feColorMatrix);
2715
- }
2716
2874
 
2717
- function createDropShadow(document, $filter, params) {
2718
- var shadowOffsetX = params[0].value;
2719
- var shadowOffsetY = params[1].value;
2720
- var shadowBlur = params[2].value; // @ts-ignore
2875
+ function create$2() {
2876
+ var out = new ARRAY_TYPE(4);
2721
2877
 
2722
- var shadowColor = params[3].formatted;
2723
- var $feGaussianBlur = createSVGElement('feGaussianBlur', document);
2724
- $feGaussianBlur.setAttribute('in', 'SourceAlpha');
2725
- $feGaussianBlur.setAttribute('stdDeviation', "" + shadowBlur);
2726
- $filter.appendChild($feGaussianBlur);
2727
- var $feOffset = createSVGElement('feOffset', document);
2728
- $feOffset.setAttribute('dx', "" + shadowOffsetX);
2729
- $feOffset.setAttribute('dy', "" + shadowOffsetY);
2730
- $feOffset.setAttribute('result', 'offsetblur');
2731
- $filter.appendChild($feOffset);
2732
- var $feFlood = createSVGElement('feFlood', document);
2733
- $feFlood.setAttribute('flood-color', shadowColor);
2734
- $filter.appendChild($feFlood);
2735
- var $feComposite = createSVGElement('feComposite', document);
2736
- $feComposite.setAttribute('in2', 'offsetblur');
2737
- $feComposite.setAttribute('operator', 'in');
2738
- $filter.appendChild($feComposite);
2739
- var $feMerge = createSVGElement('feMerge', document);
2740
- $filter.appendChild($feMerge);
2741
- var $feMergeNode1 = createSVGElement('feMergeNode', document);
2742
- var $feMergeNode2 = createSVGElement('feMergeNode', document);
2743
- $feMergeNode2.setAttribute('in', 'SourceGraphic');
2744
- $feMerge.appendChild($feMergeNode1);
2745
- $feMerge.appendChild($feMergeNode2);
2746
- }
2878
+ if (ARRAY_TYPE != Float32Array) {
2879
+ out[0] = 0;
2880
+ out[1] = 0;
2881
+ out[2] = 0;
2882
+ out[3] = 0;
2883
+ }
2747
2884
 
2748
- function createFeColorMatrix(document, $filter, matrix) {
2749
- var $feColorMatrix = createSVGElement('feColorMatrix', document);
2750
- $feColorMatrix.setAttribute('type', 'matrix');
2751
- $feColorMatrix.setAttribute('values', matrix.join(' '));
2752
- $filter.appendChild($feColorMatrix);
2885
+ return out;
2753
2886
  }
2754
- /**
2755
- * @see https://drafts.fxtf.org/filter-effects/#grayscaleEquivalent
2887
+ /**
2888
+ * Normalize a vec4
2889
+ *
2890
+ * @param {vec4} out the receiving vector
2891
+ * @param {ReadonlyVec4} a vector to normalize
2892
+ * @returns {vec4} out
2756
2893
  */
2757
2894
 
2758
2895
 
2759
- function createGrayscale(document, $filter, params) {
2760
- var amount = convertToAbsoluteValue(params[0]);
2761
- createFeColorMatrix(document, $filter, [0.2126 + 0.7874 * (1 - amount), 0.7152 - 0.7152 * (1 - amount), 0.0722 - 0.0722 * (1 - amount), 0, 0, 0.2126 - 0.2126 * (1 - amount), 0.7152 + 0.2848 * (1 - amount), 0.0722 - 0.0722 * (1 - amount), 0, 0, 0.2126 - 0.2126 * (1 - amount), 0.7152 - 0.7152 * (1 - amount), 0.0722 + 0.9278 * (1 - amount), 0, 0, 0, 0, 0, 1, 0]);
2896
+ function normalize$1(out, a) {
2897
+ var x = a[0];
2898
+ var y = a[1];
2899
+ var z = a[2];
2900
+ var w = a[3];
2901
+ var len = x * x + y * y + z * z + w * w;
2902
+
2903
+ if (len > 0) {
2904
+ len = 1 / Math.sqrt(len);
2905
+ }
2906
+
2907
+ out[0] = x * len;
2908
+ out[1] = y * len;
2909
+ out[2] = z * len;
2910
+ out[3] = w * len;
2911
+ return out;
2762
2912
  }
2763
- /**
2764
- * @see https://drafts.fxtf.org/filter-effects/#sepiaEquivalent
2913
+ /**
2914
+ * Perform some operation over an array of vec4s.
2915
+ *
2916
+ * @param {Array} a the array of vectors to iterate over
2917
+ * @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed
2918
+ * @param {Number} offset Number of elements to skip at the beginning of the array
2919
+ * @param {Number} count Number of vec4s to iterate over. If 0 iterates over entire array
2920
+ * @param {Function} fn Function to call for each vector in the array
2921
+ * @param {Object} [arg] additional argument to pass to fn
2922
+ * @returns {Array} a
2923
+ * @function
2765
2924
  */
2766
2925
 
2767
2926
 
2768
- function createSepia(document, $filter, params) {
2769
- var amount = convertToAbsoluteValue(params[0]);
2770
- createFeColorMatrix(document, $filter, [0.393 + 0.607 * (1 - amount), 0.769 - 0.769 * (1 - amount), 0.189 - 0.189 * (1 - amount), 0, 0, 0.349 - 0.349 * (1 - amount), 0.686 + 0.314 * (1 - amount), 0.168 - 0.168 * (1 - amount), 0, 0, 0.272 - 0.272 * (1 - amount), 0.534 - 0.534 * (1 - amount), 0.131 + 0.869 * (1 - amount), 0, 0, 0, 0, 0, 1, 0]);
2771
- }
2927
+ var forEach$1 = function () {
2928
+ var vec = create$2();
2929
+ return function (a, stride, offset, count, fn, arg) {
2930
+ var i, l;
2772
2931
 
2773
- var cacheKey2IDMap = {};
2774
- var counter = 0;
2932
+ if (!stride) {
2933
+ stride = 4;
2934
+ }
2775
2935
 
2776
- function createOrUpdateGradientAndPattern(document, $def, object, $el, parsedColor, name) {
2777
- // eg. clipPath don't have fill/stroke
2778
- if (!parsedColor) {
2779
- return;
2780
- }
2936
+ if (!offset) {
2937
+ offset = 0;
2938
+ }
2781
2939
 
2782
- if (parsedColor instanceof g.CSSRGB) {
2783
- // keep using currentColor @see https://github.com/d3/d3-axis/issues/49
2784
- if (object.style[name] === 'currentColor') {
2785
- $el === null || $el === void 0 ? void 0 : $el.setAttribute(name, 'currentColor');
2940
+ if (count) {
2941
+ l = Math.min(count * stride + offset, a.length);
2786
2942
  } else {
2787
- // constant value, eg. '#fff'
2788
- $el === null || $el === void 0 ? void 0 : $el.setAttribute(name, parsedColor.isNone ? 'none' : parsedColor.toString());
2943
+ l = a.length;
2789
2944
  }
2790
- } else {
2791
- if (parsedColor.type === g.GradientPatternType.Pattern) {
2792
- createOrUpdatePattern(document, $def, $el, name, parsedColor);
2793
- } else {
2794
- createOrUpdateGradient(document, object, $def, $el, name, parsedColor);
2945
+
2946
+ for (i = offset; i < l; i += stride) {
2947
+ vec[0] = a[i];
2948
+ vec[1] = a[i + 1];
2949
+ vec[2] = a[i + 2];
2950
+ vec[3] = a[i + 3];
2951
+ fn(vec, vec, arg);
2952
+ a[i] = vec[0];
2953
+ a[i + 1] = vec[1];
2954
+ a[i + 2] = vec[2];
2955
+ a[i + 3] = vec[3];
2795
2956
  }
2957
+
2958
+ return a;
2959
+ };
2960
+ }();
2961
+ /**
2962
+ * Quaternion
2963
+ * @module quat
2964
+ */
2965
+
2966
+ /**
2967
+ * Creates a new identity quat
2968
+ *
2969
+ * @returns {quat} a new quaternion
2970
+ */
2971
+
2972
+
2973
+ function create$3() {
2974
+ var out = new ARRAY_TYPE(4);
2975
+
2976
+ if (ARRAY_TYPE != Float32Array) {
2977
+ out[0] = 0;
2978
+ out[1] = 0;
2979
+ out[2] = 0;
2796
2980
  }
2797
- }
2798
2981
 
2799
- function generateCacheKey(params) {
2800
- var cacheKey = '';
2982
+ out[3] = 1;
2983
+ return out;
2984
+ }
2985
+ /**
2986
+ * Sets a quat from the given angle and rotation axis,
2987
+ * then returns it.
2988
+ *
2989
+ * @param {quat} out the receiving quaternion
2990
+ * @param {ReadonlyVec3} axis the axis around which to rotate
2991
+ * @param {Number} rad the angle in radians
2992
+ * @returns {quat} out
2993
+ **/
2801
2994
 
2802
- if (params instanceof g.CSSGradientValue) {
2803
- var type = params.type;
2804
2995
 
2805
- if (type === g.GradientPatternType.Pattern) {
2806
- var src = params.value.src;
2807
- cacheKey = src;
2808
- } else if (type === g.GradientPatternType.LinearGradient || type === g.GradientPatternType.RadialGradient) {
2809
- // @ts-ignore
2810
- var _params$value = params.value,
2811
- x0 = _params$value.x0,
2812
- y0 = _params$value.y0,
2813
- x1 = _params$value.x1,
2814
- y1 = _params$value.y1,
2815
- r1 = _params$value.r1,
2816
- steps = _params$value.steps;
2817
- cacheKey = "" + type + x0 + y0 + x1 + y1 + (r1 || 0) + steps.map(function (step) {
2818
- return step.join('');
2819
- }).join('');
2820
- }
2996
+ function setAxisAngle(out, axis, rad) {
2997
+ rad = rad * 0.5;
2998
+ var s = Math.sin(rad);
2999
+ out[0] = s * axis[0];
3000
+ out[1] = s * axis[1];
3001
+ out[2] = s * axis[2];
3002
+ out[3] = Math.cos(rad);
3003
+ return out;
3004
+ }
3005
+ /**
3006
+ * Performs a spherical linear interpolation between two quat
3007
+ *
3008
+ * @param {quat} out the receiving quaternion
3009
+ * @param {ReadonlyQuat} a the first operand
3010
+ * @param {ReadonlyQuat} b the second operand
3011
+ * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
3012
+ * @returns {quat} out
3013
+ */
2821
3014
 
2822
- if (cacheKey) {
2823
- if (!cacheKey2IDMap[cacheKey]) {
2824
- cacheKey2IDMap[cacheKey] = "_pattern_" + type + "_" + counter++;
2825
- }
2826
- }
2827
- }
2828
3015
 
2829
- return cacheKey2IDMap[cacheKey];
2830
- }
3016
+ function slerp(out, a, b, t) {
3017
+ // benchmarks:
3018
+ // http://jsperf.com/quaternion-slerp-implementations
3019
+ var ax = a[0],
3020
+ ay = a[1],
3021
+ az = a[2],
3022
+ aw = a[3];
3023
+ var bx = b[0],
3024
+ by = b[1],
3025
+ bz = b[2],
3026
+ bw = b[3];
3027
+ var omega, cosom, sinom, scale0, scale1; // calc cosine
2831
3028
 
2832
- function createOrUpdatePattern(document, $def, $el, name, parsedColor) {
2833
- var patternId = generateCacheKey(parsedColor);
2834
- var $existed = $def.querySelector("#" + patternId);
3029
+ cosom = ax * bx + ay * by + az * bz + aw * bw; // adjust signs (if necessary)
2835
3030
 
2836
- if (!$existed) {
2837
- var src = parsedColor.value.src; // @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Element/pattern
2838
-
2839
- var $pattern = createSVGElement('pattern', document);
2840
- $pattern.setAttribute('patternUnits', 'userSpaceOnUse');
2841
- var $image = createSVGElement('image', document);
2842
- $pattern.appendChild($image);
2843
- $pattern.id = patternId;
2844
- $def.appendChild($pattern);
2845
- $el === null || $el === void 0 ? void 0 : $el.setAttribute(name, "url(#" + patternId + ")");
2846
- $image.setAttribute('href', src);
2847
- var img = new window.Image();
2848
-
2849
- if (!src.match(/^data:/i)) {
2850
- img.crossOrigin = 'Anonymous';
2851
- }
3031
+ if (cosom < 0.0) {
3032
+ cosom = -cosom;
3033
+ bx = -bx;
3034
+ by = -by;
3035
+ bz = -bz;
3036
+ bw = -bw;
3037
+ } // calculate coefficients
2852
3038
 
2853
- img.src = src;
2854
3039
 
2855
- var onload = function onload() {
2856
- $pattern.setAttribute('width', "" + img.width);
2857
- $pattern.setAttribute('height', "" + img.height);
2858
- };
3040
+ if (1.0 - cosom > EPSILON) {
3041
+ // standard case (slerp)
3042
+ omega = Math.acos(cosom);
3043
+ sinom = Math.sin(omega);
3044
+ scale0 = Math.sin((1.0 - t) * omega) / sinom;
3045
+ scale1 = Math.sin(t * omega) / sinom;
3046
+ } else {
3047
+ // "from" and "to" quaternions are very close
3048
+ // ... so we can do a linear interpolation
3049
+ scale0 = 1.0 - t;
3050
+ scale1 = t;
3051
+ } // calculate final values
2859
3052
 
2860
- if (img.complete) {
2861
- onload();
2862
- } else {
2863
- img.onload = onload; // Fix onload() bug in IE9
2864
3053
 
2865
- img.src = img.src;
2866
- }
2867
- }
3054
+ out[0] = scale0 * ax + scale1 * bx;
3055
+ out[1] = scale0 * ay + scale1 * by;
3056
+ out[2] = scale0 * az + scale1 * bz;
3057
+ out[3] = scale0 * aw + scale1 * bw;
3058
+ return out;
2868
3059
  }
3060
+ /**
3061
+ * Creates a quaternion from the given 3x3 rotation matrix.
3062
+ *
3063
+ * NOTE: The resultant quaternion is not normalized, so you should be sure
3064
+ * to renormalize the quaternion yourself where necessary.
3065
+ *
3066
+ * @param {quat} out the receiving quaternion
3067
+ * @param {ReadonlyMat3} m rotation matrix
3068
+ * @returns {quat} out
3069
+ * @function
3070
+ */
2869
3071
 
2870
- function createOrUpdateGradient(document, object, $def, $el, name, parsedColor) {
2871
- var gradientId = generateCacheKey(parsedColor);
2872
- var $existed = $def.querySelector("#" + gradientId);
2873
-
2874
- if (!$existed) {
2875
- // <linearGradient> <radialGradient>
2876
- // @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Element/linearGradient
2877
- // @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Element/radialGradient
2878
- $existed = createSVGElement(parsedColor.type === g.GradientPatternType.LinearGradient ? 'linearGradient' : 'radialGradient', document); // @see https://github.com/antvis/g/issues/1025
2879
3072
 
2880
- $existed.setAttribute('gradientUnits', 'userSpaceOnUse'); // add stops
3073
+ function fromMat3(out, m) {
3074
+ // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
3075
+ // article "Quaternion Calculus and Fast Animation".
3076
+ var fTrace = m[0] + m[4] + m[8];
3077
+ var fRoot;
2881
3078
 
2882
- var innerHTML = '';
2883
- parsedColor.value.steps.forEach(function (_ref) {
2884
- var offset = _ref[0],
2885
- color = _ref[1];
2886
- innerHTML += "<stop offset=\"" + offset + "\" stop-color=\"" + color + "\"></stop>";
2887
- });
2888
- $existed.innerHTML = innerHTML;
2889
- $existed.id = gradientId;
2890
- $def.appendChild($existed);
2891
- $el === null || $el === void 0 ? void 0 : $el.setAttribute(name, "url(#" + gradientId + ")");
2892
- }
3079
+ if (fTrace > 0.0) {
3080
+ // |w| > 1/2, may as well choose w > 1/2
3081
+ fRoot = Math.sqrt(fTrace + 1.0); // 2w
2893
3082
 
2894
- var bounds = object.getGeometryBounds();
2895
- var width = bounds && bounds.halfExtents[0] * 2 || 0;
2896
- var height = bounds && bounds.halfExtents[1] * 2 || 0;
3083
+ out[3] = 0.5 * fRoot;
3084
+ fRoot = 0.5 / fRoot; // 1/(4w)
2897
3085
 
2898
- if (parsedColor.type === g.GradientPatternType.LinearGradient) {
2899
- var _parsedColor$value = parsedColor.value,
2900
- x0 = _parsedColor$value.x0,
2901
- y0 = _parsedColor$value.y0,
2902
- x1 = _parsedColor$value.x1,
2903
- y1 = _parsedColor$value.y1;
2904
- $existed.setAttribute('x1', "" + x0 * width);
2905
- $existed.setAttribute('y1', "" + y0 * height);
2906
- $existed.setAttribute('x2', "" + x1 * width);
2907
- $existed.setAttribute('y2', "" + y1 * height);
3086
+ out[0] = (m[5] - m[7]) * fRoot;
3087
+ out[1] = (m[6] - m[2]) * fRoot;
3088
+ out[2] = (m[1] - m[3]) * fRoot;
2908
3089
  } else {
2909
- var r = Math.sqrt(width * width + height * height) / 2;
2910
- var _parsedColor$value2 = parsedColor.value,
2911
- _x = _parsedColor$value2.x0,
2912
- _y = _parsedColor$value2.y0,
2913
- r1 = _parsedColor$value2.r1;
2914
- $existed.setAttribute('cx', "" + _x * width);
2915
- $existed.setAttribute('cy', "" + _y * height);
2916
- $existed.setAttribute('r', "" + r1 * r);
3090
+ // |w| <= 1/2
3091
+ var i = 0;
3092
+ if (m[4] > m[0]) i = 1;
3093
+ if (m[8] > m[i * 3 + i]) i = 2;
3094
+ var j = (i + 1) % 3;
3095
+ var k = (i + 2) % 3;
3096
+ fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1.0);
3097
+ out[i] = 0.5 * fRoot;
3098
+ fRoot = 0.5 / fRoot;
3099
+ out[3] = (m[j * 3 + k] - m[k * 3 + j]) * fRoot;
3100
+ out[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot;
3101
+ out[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot;
2917
3102
  }
2918
- }
2919
3103
 
2920
- var FILTER_DROPSHADOW_PREFIX = 'filter-dropshadow-';
2921
- /**
2922
- * use SVG filters
2923
- * @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Element/filter
3104
+ return out;
3105
+ }
3106
+ /**
3107
+ * Normalize a quat
3108
+ *
3109
+ * @param {quat} out the receiving quaternion
3110
+ * @param {ReadonlyQuat} a quaternion to normalize
3111
+ * @returns {quat} out
3112
+ * @function
2924
3113
  */
2925
3114
 
2926
- function createOrUpdateShadow(document, $def, object, $el, name) {
2927
- var shadowId = FILTER_DROPSHADOW_PREFIX + object.entity;
2928
- var $existedFilter = $def.querySelector("#" + shadowId);
2929
-
2930
- if (!$existedFilter) {
2931
- $existedFilter = createSVGElement('filter', document);
2932
-
2933
- var _$feDropShadow = createSVGElement('feDropShadow', document);
2934
-
2935
- _$feDropShadow.setAttribute('dx', '0');
2936
-
2937
- _$feDropShadow.setAttribute('dy', '0');
2938
3115
 
2939
- $existedFilter.appendChild(_$feDropShadow);
2940
- $existedFilter.id = shadowId; // @see https://github.com/antvis/g/issues/1025
3116
+ var normalize$2 = normalize$1;
3117
+ /**
3118
+ * Sets a quaternion to represent the shortest rotation from one
3119
+ * vector to another.
3120
+ *
3121
+ * Both vectors are assumed to be unit length.
3122
+ *
3123
+ * @param {quat} out the receiving quaternion.
3124
+ * @param {ReadonlyVec3} a the initial vector
3125
+ * @param {ReadonlyVec3} b the destination vector
3126
+ * @returns {quat} out
3127
+ */
2941
3128
 
2942
- $existedFilter.setAttribute('filterUnits', 'userSpaceOnUse');
2943
- $def.appendChild($existedFilter);
2944
- }
3129
+ var rotationTo = function () {
3130
+ var tmpvec3 = create$1();
3131
+ var xUnitVec3 = fromValues(1, 0, 0);
3132
+ var yUnitVec3 = fromValues(0, 1, 0);
3133
+ return function (out, a, b) {
3134
+ var dot$1 = dot(a, b);
2945
3135
 
2946
- var $feDropShadow = $existedFilter.children[0];
3136
+ if (dot$1 < -0.999999) {
3137
+ cross(tmpvec3, xUnitVec3, a);
3138
+ if (len(tmpvec3) < 0.000001) cross(tmpvec3, yUnitVec3, a);
3139
+ normalize(tmpvec3, tmpvec3);
3140
+ setAxisAngle(out, tmpvec3, Math.PI);
3141
+ return out;
3142
+ } else if (dot$1 > 0.999999) {
3143
+ out[0] = 0;
3144
+ out[1] = 0;
3145
+ out[2] = 0;
3146
+ out[3] = 1;
3147
+ return out;
3148
+ } else {
3149
+ cross(tmpvec3, a, b);
3150
+ out[0] = tmpvec3[0];
3151
+ out[1] = tmpvec3[1];
3152
+ out[2] = tmpvec3[2];
3153
+ out[3] = 1 + dot$1;
3154
+ return normalize$2(out, out);
3155
+ }
3156
+ };
3157
+ }();
3158
+ /**
3159
+ * Performs a spherical linear interpolation with two control points
3160
+ *
3161
+ * @param {quat} out the receiving quaternion
3162
+ * @param {ReadonlyQuat} a the first operand
3163
+ * @param {ReadonlyQuat} b the second operand
3164
+ * @param {ReadonlyQuat} c the third operand
3165
+ * @param {ReadonlyQuat} d the fourth operand
3166
+ * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
3167
+ * @returns {quat} out
3168
+ */
2947
3169
 
2948
- if (name === 'shadowColor') {
2949
- var parsedColor = object.parsedStyle[name];
2950
- $feDropShadow.setAttribute('flood-color', parsedColor.toString());
2951
- } else if (name === 'shadowBlur') {
2952
- var shadowBlur = object.parsedStyle[name]; // half the blur radius
2953
- // @see https://drafts.csswg.org/css-backgrounds/#shadow-blur
2954
- // @see https://css-tricks.com/breaking-css-box-shadow-vs-drop-shadow/
2955
3170
 
2956
- $feDropShadow.setAttribute('stdDeviation', "" + ((shadowBlur === null || shadowBlur === void 0 ? void 0 : shadowBlur.value) || 0) / 4);
2957
- } else if (name === 'shadowOffsetX') {
2958
- var shadowOffsetX = object.parsedStyle[name];
2959
- $feDropShadow.setAttribute('dx', "" + ((shadowOffsetX === null || shadowOffsetX === void 0 ? void 0 : shadowOffsetX.value) || 0) / 2);
2960
- } else if (name === 'shadowOffsetY') {
2961
- var shadowOffsetY = object.parsedStyle[name];
2962
- $feDropShadow.setAttribute('dy', "" + ((shadowOffsetY === null || shadowOffsetY === void 0 ? void 0 : shadowOffsetY.value) || 0) / 2);
2963
- } // only apply shadow when blur > 0
3171
+ var sqlerp = function () {
3172
+ var temp1 = create$3();
3173
+ var temp2 = create$3();
3174
+ return function (out, a, b, c, d, t) {
3175
+ slerp(temp1, a, d, t);
3176
+ slerp(temp2, b, c, t);
3177
+ slerp(out, temp1, temp2, 2 * t * (1 - t));
3178
+ return out;
3179
+ };
3180
+ }();
3181
+ /**
3182
+ * Sets the specified quaternion with values corresponding to the given
3183
+ * axes. Each axis is a vec3 and is expected to be unit length and
3184
+ * perpendicular to all other specified axes.
3185
+ *
3186
+ * @param {ReadonlyVec3} view the vector representing the viewing direction
3187
+ * @param {ReadonlyVec3} right the vector representing the local "right" direction
3188
+ * @param {ReadonlyVec3} up the vector representing the local "up" direction
3189
+ * @returns {quat} out
3190
+ */
2964
3191
 
2965
3192
 
2966
- if (!g.isNil(object.parsedStyle.shadowColor)) {
2967
- // use filter <feDropShadow>
2968
- // @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/feDropShadow
2969
- $el === null || $el === void 0 ? void 0 : $el.setAttribute('filter', "url(#" + shadowId + ")");
2970
- } else {
2971
- $el === null || $el === void 0 ? void 0 : $el.removeAttribute('filter');
2972
- }
2973
- }
3193
+ var setAxes = function () {
3194
+ var matr = create();
3195
+ return function (out, view, right, up) {
3196
+ matr[0] = right[0];
3197
+ matr[3] = right[1];
3198
+ matr[6] = right[2];
3199
+ matr[1] = up[0];
3200
+ matr[4] = up[1];
3201
+ matr[7] = up[2];
3202
+ matr[2] = -view[0];
3203
+ matr[5] = -view[1];
3204
+ matr[8] = -view[2];
3205
+ return normalize$2(out, fromMat3(out, matr));
3206
+ };
3207
+ }();
2974
3208
 
2975
3209
  var _class$1;
2976
3210
 
@@ -3061,7 +3295,7 @@
3061
3295
  this.contextService = void 0;
3062
3296
  this.renderingContext = void 0;
3063
3297
  this.createElementContribution = void 0;
3064
- this.$def = void 0;
3298
+ this.defElementManager = void 0;
3065
3299
  this.$camera = void 0;
3066
3300
  this.renderQueue = [];
3067
3301
  this.pendingReorderQueue = new Set();
@@ -3083,6 +3317,8 @@
3083
3317
  var handleUnmounted = function handleUnmounted(e) {
3084
3318
  var object = e.target;
3085
3319
 
3320
+ _this.defElementManager.clear(object.entity);
3321
+
3086
3322
  _this.removeSVGDom(object);
3087
3323
  };
3088
3324
 
@@ -3120,7 +3356,6 @@
3120
3356
  var handleGeometryBoundsChanged = function handleGeometryBoundsChanged(e) {
3121
3357
  var _object$elementSVG;
3122
3358
 
3123
- var doc = _this.canvasConfig.document;
3124
3359
  var object = e.target; // @ts-ignore
3125
3360
 
3126
3361
  var $el = (_object$elementSVG = object.elementSVG) === null || _object$elementSVG === void 0 ? void 0 : _object$elementSVG.$el;
@@ -3129,11 +3364,11 @@
3129
3364
  stroke = _object$parsedStyle.stroke;
3130
3365
 
3131
3366
  if (fill && !(fill instanceof g.CSSRGB)) {
3132
- createOrUpdateGradientAndPattern(doc || document, _this.$def, object, $el, fill, 'fill');
3367
+ _this.defElementManager.createOrUpdateGradientAndPattern(object, $el, fill, 'fill');
3133
3368
  }
3134
3369
 
3135
3370
  if (stroke && !(stroke instanceof g.CSSRGB)) {
3136
- createOrUpdateGradientAndPattern(doc || document, _this.$def, object, $el, stroke, 'stroke');
3371
+ _this.defElementManager.createOrUpdateGradientAndPattern(object, $el, stroke, 'stroke');
3137
3372
  }
3138
3373
  };
3139
3374
 
@@ -3144,15 +3379,16 @@
3144
3379
  while (1) {
3145
3380
  switch (_context.prev = _context.next) {
3146
3381
  case 0:
3147
- _this$canvasConfig = _this.canvasConfig, background = _this$canvasConfig.background, document = _this$canvasConfig.document;
3148
- _this.$def = createSVGElement('defs', document);
3382
+ _this$canvasConfig = _this.canvasConfig, background = _this$canvasConfig.background, document = _this$canvasConfig.document; // <defs>
3383
+
3384
+ _this.defElementManager.init();
3385
+
3149
3386
  $svg = _this.contextService.getContext();
3150
3387
 
3151
3388
  if (background) {
3152
3389
  $svg.style.background = background;
3153
- }
3390
+ } // @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/color-interpolation-filters
3154
3391
 
3155
- $svg.appendChild(_this.$def); // @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/color-interpolation-filters
3156
3392
 
3157
3393
  $svg.setAttribute('color-interpolation-filters', 'sRGB');
3158
3394
  _this.$camera = createSVGElement('g', document);
@@ -3170,7 +3406,7 @@
3170
3406
 
3171
3407
  _this.renderingContext.root.addEventListener(g.ElementEvent.GEOMETRY_BOUNDS_CHANGED, handleGeometryBoundsChanged);
3172
3408
 
3173
- case 14:
3409
+ case 13:
3174
3410
  case "end":
3175
3411
  return _context.stop();
3176
3412
  }
@@ -3346,14 +3582,14 @@
3346
3582
  if (object.nodeName === g.Shape.HTML) {
3347
3583
  $el.style.background = usedValue.toString();
3348
3584
  } else {
3349
- createOrUpdateGradientAndPattern(document, _this2.$def, object, $el, usedValue, usedName);
3585
+ _this2.defElementManager.createOrUpdateGradientAndPattern(object, $el, usedValue, usedName);
3350
3586
  }
3351
3587
  } else if (name === 'stroke') {
3352
3588
  if (object.nodeName === g.Shape.HTML) {
3353
3589
  $el.style['border-color'] = usedValue.toString();
3354
3590
  $el.style['border-style'] = 'solid';
3355
3591
  } else {
3356
- createOrUpdateGradientAndPattern(document, _this2.$def, object, $el, usedValue, usedName);
3592
+ _this2.defElementManager.createOrUpdateGradientAndPattern(object, $el, usedValue, usedName);
3357
3593
  }
3358
3594
  } else if (name === 'visibility' && computedValue.value !== 'unset') {
3359
3595
  // use computed value
@@ -3362,9 +3598,9 @@
3362
3598
  } else if (name === 'clipPath') {
3363
3599
  _this2.createOrUpdateClipPath(document, usedValue, $groupEl);
3364
3600
  } else if (name === 'shadowColor' || name === 'shadowBlur' || name === 'shadowOffsetX' || name === 'shadowOffsetY') {
3365
- createOrUpdateShadow(document, _this2.$def, object, $el, name);
3601
+ _this2.defElementManager.createOrUpdateShadow(object, $el, name);
3366
3602
  } else if (name === 'filter') {
3367
- createOrUpdateFilter(document, _this2.$def, object, $el, usedValue);
3603
+ _this2.defElementManager.createOrUpdateFilter(object, $el, usedValue);
3368
3604
  } else if (name === 'innerHTML') {
3369
3605
  _this2.createOrUpdateInnerHTML(document, $el, usedValue);
3370
3606
  } else if (name === 'anchor') {
@@ -3508,13 +3744,14 @@
3508
3744
  var _clipPath$elementSVG;
3509
3745
 
3510
3746
  var clipPathId = CLIP_PATH_PREFIX + clipPath.entity;
3511
- var existed = this.$def.querySelector("#" + clipPathId);
3747
+ var $def = this.defElementManager.getDefElement();
3748
+ var existed = $def.querySelector("#" + clipPathId);
3512
3749
 
3513
3750
  if (!existed) {
3514
3751
  // create <clipPath> dom node, append it to <defs>
3515
3752
  var $clipPath = createSVGElement('clipPath', document);
3516
3753
  $clipPath.id = clipPathId;
3517
- this.$def.appendChild($clipPath); // <clipPath><circle /></clipPath>
3754
+ $def.appendChild($clipPath); // <clipPath><circle /></clipPath>
3518
3755
 
3519
3756
  this.createSVGDom(document, clipPath, $clipPath, true); // @ts-ignore
3520
3757
 
@@ -3557,10 +3794,12 @@
3557
3794
 
3558
3795
  [(_object$elementSVG7 = object.elementSVG) === null || _object$elementSVG7 === void 0 ? void 0 : _object$elementSVG7.$el, (_object$elementSVG8 = object.elementSVG) === null || _object$elementSVG8 === void 0 ? void 0 : _object$elementSVG8.$hitTestingEl].forEach(function ($el) {
3559
3796
  if ($el) {
3560
- // apply anchor to element's `transform` property
3561
- $el.setAttribute('transform', // can't use percent unit like translate(-50%, -50%)
3562
- // @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Attribute/transform#translate
3563
- "translate(-" + anchor[0].value * width + ",-" + anchor[1].value * height + ")");
3797
+ if (anchor[0].value !== 0 || anchor[1].value !== 0) {
3798
+ // apply anchor to element's `transform` property
3799
+ $el.setAttribute('transform', // can't use percent unit like translate(-50%, -50%)
3800
+ // @see https://developer.mozilla.org/zh-CN/docs/Web/SVG/Attribute/transform#translate
3801
+ "translate(-" + anchor[0].value * width + ",-" + anchor[1].value * height + ")");
3802
+ }
3564
3803
 
3565
3804
  if (object.nodeName === g.Shape.CIRCLE || object.nodeName === g.Shape.ELLIPSE) {
3566
3805
  $el.setAttribute('cx', "" + width / 2);
@@ -3583,10 +3822,13 @@
3583
3822
 
3584
3823
  __decorate([g.inject(CreateElementContribution), __metadata("design:type", Object)], SVGRendererPlugin.prototype, "createElementContribution", void 0);
3585
3824
 
3825
+ __decorate([g.inject(DefElementManager), __metadata("design:type", DefElementManager)], SVGRendererPlugin.prototype, "defElementManager", void 0);
3826
+
3586
3827
  SVGRendererPlugin = SVGRendererPlugin_1 = __decorate([g.singleton({
3587
3828
  contrib: g.RenderingPluginContribution
3588
3829
  })], SVGRendererPlugin);
3589
3830
  var containerModule$1 = g.Module(function (register) {
3831
+ register(DefElementManager);
3590
3832
  register(DefaultCreateElementContribution);
3591
3833
  register(SVGRendererPlugin);
3592
3834
  });