index.umd.js 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@antv/g-lite')) :
  3. typeof define === 'function' && define.amd ? define(['exports', '@antv/g-lite'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.G = global.G || {}, global.G.CanvasPicker = {}), global.window.G));
  5. }(this, (function (exports, gLite) { 'use strict';
  6. function _regeneratorRuntime() {
  7. _regeneratorRuntime = function () {
  8. return exports;
  9. };
  10. var exports = {},
  11. Op = Object.prototype,
  12. hasOwn = Op.hasOwnProperty,
  13. defineProperty = Object.defineProperty || function (obj, key, desc) {
  14. obj[key] = desc.value;
  15. },
  16. $Symbol = "function" == typeof Symbol ? Symbol : {},
  17. iteratorSymbol = $Symbol.iterator || "@@iterator",
  18. asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator",
  19. toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
  20. function define(obj, key, value) {
  21. return Object.defineProperty(obj, key, {
  22. value: value,
  23. enumerable: !0,
  24. configurable: !0,
  25. writable: !0
  26. }), obj[key];
  27. }
  28. try {
  29. define({}, "");
  30. } catch (err) {
  31. define = function (obj, key, value) {
  32. return obj[key] = value;
  33. };
  34. }
  35. function wrap(innerFn, outerFn, self, tryLocsList) {
  36. var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator,
  37. generator = Object.create(protoGenerator.prototype),
  38. context = new Context(tryLocsList || []);
  39. return defineProperty(generator, "_invoke", {
  40. value: makeInvokeMethod(innerFn, self, context)
  41. }), generator;
  42. }
  43. function tryCatch(fn, obj, arg) {
  44. try {
  45. return {
  46. type: "normal",
  47. arg: fn.call(obj, arg)
  48. };
  49. } catch (err) {
  50. return {
  51. type: "throw",
  52. arg: err
  53. };
  54. }
  55. }
  56. exports.wrap = wrap;
  57. var ContinueSentinel = {};
  58. function Generator() {}
  59. function GeneratorFunction() {}
  60. function GeneratorFunctionPrototype() {}
  61. var IteratorPrototype = {};
  62. define(IteratorPrototype, iteratorSymbol, function () {
  63. return this;
  64. });
  65. var getProto = Object.getPrototypeOf,
  66. NativeIteratorPrototype = getProto && getProto(getProto(values([])));
  67. NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype);
  68. var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype);
  69. function defineIteratorMethods(prototype) {
  70. ["next", "throw", "return"].forEach(function (method) {
  71. define(prototype, method, function (arg) {
  72. return this._invoke(method, arg);
  73. });
  74. });
  75. }
  76. function AsyncIterator(generator, PromiseImpl) {
  77. function invoke(method, arg, resolve, reject) {
  78. var record = tryCatch(generator[method], generator, arg);
  79. if ("throw" !== record.type) {
  80. var result = record.arg,
  81. value = result.value;
  82. return value && "object" == typeof value && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) {
  83. invoke("next", value, resolve, reject);
  84. }, function (err) {
  85. invoke("throw", err, resolve, reject);
  86. }) : PromiseImpl.resolve(value).then(function (unwrapped) {
  87. result.value = unwrapped, resolve(result);
  88. }, function (error) {
  89. return invoke("throw", error, resolve, reject);
  90. });
  91. }
  92. reject(record.arg);
  93. }
  94. var previousPromise;
  95. defineProperty(this, "_invoke", {
  96. value: function (method, arg) {
  97. function callInvokeWithMethodAndArg() {
  98. return new PromiseImpl(function (resolve, reject) {
  99. invoke(method, arg, resolve, reject);
  100. });
  101. }
  102. return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
  103. }
  104. });
  105. }
  106. function makeInvokeMethod(innerFn, self, context) {
  107. var state = "suspendedStart";
  108. return function (method, arg) {
  109. if ("executing" === state) throw new Error("Generator is already running");
  110. if ("completed" === state) {
  111. if ("throw" === method) throw arg;
  112. return doneResult();
  113. }
  114. for (context.method = method, context.arg = arg;;) {
  115. var delegate = context.delegate;
  116. if (delegate) {
  117. var delegateResult = maybeInvokeDelegate(delegate, context);
  118. if (delegateResult) {
  119. if (delegateResult === ContinueSentinel) continue;
  120. return delegateResult;
  121. }
  122. }
  123. if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) {
  124. if ("suspendedStart" === state) throw state = "completed", context.arg;
  125. context.dispatchException(context.arg);
  126. } else "return" === context.method && context.abrupt("return", context.arg);
  127. state = "executing";
  128. var record = tryCatch(innerFn, self, context);
  129. if ("normal" === record.type) {
  130. if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue;
  131. return {
  132. value: record.arg,
  133. done: context.done
  134. };
  135. }
  136. "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg);
  137. }
  138. };
  139. }
  140. function maybeInvokeDelegate(delegate, context) {
  141. var methodName = context.method,
  142. method = delegate.iterator[methodName];
  143. if (undefined === method) return context.delegate = null, "throw" === methodName && delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method) || "return" !== methodName && (context.method = "throw", context.arg = new TypeError("The iterator does not provide a '" + methodName + "' method")), ContinueSentinel;
  144. var record = tryCatch(method, delegate.iterator, context.arg);
  145. if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel;
  146. var info = record.arg;
  147. 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);
  148. }
  149. function pushTryEntry(locs) {
  150. var entry = {
  151. tryLoc: locs[0]
  152. };
  153. 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry);
  154. }
  155. function resetTryEntry(entry) {
  156. var record = entry.completion || {};
  157. record.type = "normal", delete record.arg, entry.completion = record;
  158. }
  159. function Context(tryLocsList) {
  160. this.tryEntries = [{
  161. tryLoc: "root"
  162. }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0);
  163. }
  164. function values(iterable) {
  165. if (iterable) {
  166. var iteratorMethod = iterable[iteratorSymbol];
  167. if (iteratorMethod) return iteratorMethod.call(iterable);
  168. if ("function" == typeof iterable.next) return iterable;
  169. if (!isNaN(iterable.length)) {
  170. var i = -1,
  171. next = function next() {
  172. for (; ++i < iterable.length;) if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next;
  173. return next.value = undefined, next.done = !0, next;
  174. };
  175. return next.next = next;
  176. }
  177. }
  178. return {
  179. next: doneResult
  180. };
  181. }
  182. function doneResult() {
  183. return {
  184. value: undefined,
  185. done: !0
  186. };
  187. }
  188. return GeneratorFunction.prototype = GeneratorFunctionPrototype, defineProperty(Gp, "constructor", {
  189. value: GeneratorFunctionPrototype,
  190. configurable: !0
  191. }), defineProperty(GeneratorFunctionPrototype, "constructor", {
  192. value: GeneratorFunction,
  193. configurable: !0
  194. }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) {
  195. var ctor = "function" == typeof genFun && genFun.constructor;
  196. return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name));
  197. }, exports.mark = function (genFun) {
  198. return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun;
  199. }, exports.awrap = function (arg) {
  200. return {
  201. __await: arg
  202. };
  203. }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
  204. return this;
  205. }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) {
  206. void 0 === PromiseImpl && (PromiseImpl = Promise);
  207. var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl);
  208. return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) {
  209. return result.done ? result.value : iter.next();
  210. });
  211. }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () {
  212. return this;
  213. }), define(Gp, "toString", function () {
  214. return "[object Generator]";
  215. }), exports.keys = function (val) {
  216. var object = Object(val),
  217. keys = [];
  218. for (var key in object) keys.push(key);
  219. return keys.reverse(), function next() {
  220. for (; keys.length;) {
  221. var key = keys.pop();
  222. if (key in object) return next.value = key, next.done = !1, next;
  223. }
  224. return next.done = !0, next;
  225. };
  226. }, exports.values = values, Context.prototype = {
  227. constructor: Context,
  228. reset: function (skipTempReset) {
  229. 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) "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined);
  230. },
  231. stop: function () {
  232. this.done = !0;
  233. var rootRecord = this.tryEntries[0].completion;
  234. if ("throw" === rootRecord.type) throw rootRecord.arg;
  235. return this.rval;
  236. },
  237. dispatchException: function (exception) {
  238. if (this.done) throw exception;
  239. var context = this;
  240. function handle(loc, caught) {
  241. return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught;
  242. }
  243. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  244. var entry = this.tryEntries[i],
  245. record = entry.completion;
  246. if ("root" === entry.tryLoc) return handle("end");
  247. if (entry.tryLoc <= this.prev) {
  248. var hasCatch = hasOwn.call(entry, "catchLoc"),
  249. hasFinally = hasOwn.call(entry, "finallyLoc");
  250. if (hasCatch && hasFinally) {
  251. if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
  252. if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
  253. } else if (hasCatch) {
  254. if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
  255. } else {
  256. if (!hasFinally) throw new Error("try statement without catch or finally");
  257. if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
  258. }
  259. }
  260. }
  261. },
  262. abrupt: function (type, arg) {
  263. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  264. var entry = this.tryEntries[i];
  265. if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) {
  266. var finallyEntry = entry;
  267. break;
  268. }
  269. }
  270. finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null);
  271. var record = finallyEntry ? finallyEntry.completion : {};
  272. return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record);
  273. },
  274. complete: function (record, afterLoc) {
  275. if ("throw" === record.type) throw record.arg;
  276. 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;
  277. },
  278. finish: function (finallyLoc) {
  279. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  280. var entry = this.tryEntries[i];
  281. if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel;
  282. }
  283. },
  284. catch: function (tryLoc) {
  285. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  286. var entry = this.tryEntries[i];
  287. if (entry.tryLoc === tryLoc) {
  288. var record = entry.completion;
  289. if ("throw" === record.type) {
  290. var thrown = record.arg;
  291. resetTryEntry(entry);
  292. }
  293. return thrown;
  294. }
  295. }
  296. throw new Error("illegal catch attempt");
  297. },
  298. delegateYield: function (iterable, resultName, nextLoc) {
  299. return this.delegate = {
  300. iterator: values(iterable),
  301. resultName: resultName,
  302. nextLoc: nextLoc
  303. }, "next" === this.method && (this.arg = undefined), ContinueSentinel;
  304. }
  305. }, exports;
  306. }
  307. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
  308. try {
  309. var info = gen[key](arg);
  310. var value = info.value;
  311. } catch (error) {
  312. reject(error);
  313. return;
  314. }
  315. if (info.done) {
  316. resolve(value);
  317. } else {
  318. Promise.resolve(value).then(_next, _throw);
  319. }
  320. }
  321. function _asyncToGenerator(fn) {
  322. return function () {
  323. var self = this,
  324. args = arguments;
  325. return new Promise(function (resolve, reject) {
  326. var gen = fn.apply(self, args);
  327. function _next(value) {
  328. asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
  329. }
  330. function _throw(err) {
  331. asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
  332. }
  333. _next(undefined);
  334. });
  335. };
  336. }
  337. function _inheritsLoose(subClass, superClass) {
  338. subClass.prototype = Object.create(superClass.prototype);
  339. subClass.prototype.constructor = subClass;
  340. _setPrototypeOf(subClass, superClass);
  341. }
  342. function _setPrototypeOf(o, p) {
  343. _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
  344. o.__proto__ = p;
  345. return o;
  346. };
  347. return _setPrototypeOf(o, p);
  348. }
  349. function _unsupportedIterableToArray(o, minLen) {
  350. if (!o) return;
  351. if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  352. var n = Object.prototype.toString.call(o).slice(8, -1);
  353. if (n === "Object" && o.constructor) n = o.constructor.name;
  354. if (n === "Map" || n === "Set") return Array.from(o);
  355. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
  356. }
  357. function _arrayLikeToArray(arr, len) {
  358. if (len == null || len > arr.length) len = arr.length;
  359. for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  360. return arr2;
  361. }
  362. function _createForOfIteratorHelperLoose(o, allowArrayLike) {
  363. var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
  364. if (it) return (it = it.call(o)).next.bind(it);
  365. if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
  366. if (it) o = it;
  367. var i = 0;
  368. return function () {
  369. if (i >= o.length) return {
  370. done: true
  371. };
  372. return {
  373. done: false,
  374. value: o[i++]
  375. };
  376. };
  377. }
  378. throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  379. }
  380. /**
  381. * Common utilities
  382. * @module glMatrix
  383. */
  384. var ARRAY_TYPE = typeof Float32Array !== 'undefined' ? Float32Array : Array;
  385. if (!Math.hypot) Math.hypot = function () {
  386. var y = 0,
  387. i = arguments.length;
  388. while (i--) {
  389. y += arguments[i] * arguments[i];
  390. }
  391. return Math.sqrt(y);
  392. };
  393. /**
  394. * 4x4 Matrix<br>Format: column-major, when typed out it looks like row-major<br>The matrices are being post multiplied.
  395. * @module mat4
  396. */
  397. /**
  398. * Creates a new identity mat4
  399. *
  400. * @returns {mat4} a new 4x4 matrix
  401. */
  402. function create() {
  403. var out = new ARRAY_TYPE(16);
  404. if (ARRAY_TYPE != Float32Array) {
  405. out[1] = 0;
  406. out[2] = 0;
  407. out[3] = 0;
  408. out[4] = 0;
  409. out[6] = 0;
  410. out[7] = 0;
  411. out[8] = 0;
  412. out[9] = 0;
  413. out[11] = 0;
  414. out[12] = 0;
  415. out[13] = 0;
  416. out[14] = 0;
  417. }
  418. out[0] = 1;
  419. out[5] = 1;
  420. out[10] = 1;
  421. out[15] = 1;
  422. return out;
  423. }
  424. /**
  425. * Inverts a mat4
  426. *
  427. * @param {mat4} out the receiving matrix
  428. * @param {ReadonlyMat4} a the source matrix
  429. * @returns {mat4} out
  430. */
  431. function invert(out, a) {
  432. var a00 = a[0],
  433. a01 = a[1],
  434. a02 = a[2],
  435. a03 = a[3];
  436. var a10 = a[4],
  437. a11 = a[5],
  438. a12 = a[6],
  439. a13 = a[7];
  440. var a20 = a[8],
  441. a21 = a[9],
  442. a22 = a[10],
  443. a23 = a[11];
  444. var a30 = a[12],
  445. a31 = a[13],
  446. a32 = a[14],
  447. a33 = a[15];
  448. var b00 = a00 * a11 - a01 * a10;
  449. var b01 = a00 * a12 - a02 * a10;
  450. var b02 = a00 * a13 - a03 * a10;
  451. var b03 = a01 * a12 - a02 * a11;
  452. var b04 = a01 * a13 - a03 * a11;
  453. var b05 = a02 * a13 - a03 * a12;
  454. var b06 = a20 * a31 - a21 * a30;
  455. var b07 = a20 * a32 - a22 * a30;
  456. var b08 = a20 * a33 - a23 * a30;
  457. var b09 = a21 * a32 - a22 * a31;
  458. var b10 = a21 * a33 - a23 * a31;
  459. var b11 = a22 * a33 - a23 * a32; // Calculate the determinant
  460. var det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  461. if (!det) {
  462. return null;
  463. }
  464. det = 1.0 / det;
  465. out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
  466. out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
  467. out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
  468. out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
  469. out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
  470. out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
  471. out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
  472. out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
  473. out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
  474. out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
  475. out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
  476. out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
  477. out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
  478. out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
  479. out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
  480. out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
  481. return out;
  482. }
  483. /**
  484. * 3 Dimensional Vector
  485. * @module vec3
  486. */
  487. /**
  488. * Creates a new, empty vec3
  489. *
  490. * @returns {vec3} a new 3D vector
  491. */
  492. function create$1() {
  493. var out = new ARRAY_TYPE(3);
  494. if (ARRAY_TYPE != Float32Array) {
  495. out[0] = 0;
  496. out[1] = 0;
  497. out[2] = 0;
  498. }
  499. return out;
  500. }
  501. /**
  502. * Set the components of a vec3 to the given values
  503. *
  504. * @param {vec3} out the receiving vector
  505. * @param {Number} x X component
  506. * @param {Number} y Y component
  507. * @param {Number} z Z component
  508. * @returns {vec3} out
  509. */
  510. function set(out, x, y, z) {
  511. out[0] = x;
  512. out[1] = y;
  513. out[2] = z;
  514. return out;
  515. }
  516. /**
  517. * Transforms the vec3 with a mat4.
  518. * 4th vector component is implicitly '1'
  519. *
  520. * @param {vec3} out the receiving vector
  521. * @param {ReadonlyVec3} a the vector to transform
  522. * @param {ReadonlyMat4} m matrix to transform with
  523. * @returns {vec3} out
  524. */
  525. function transformMat4(out, a, m) {
  526. var x = a[0],
  527. y = a[1],
  528. z = a[2];
  529. var w = m[3] * x + m[7] * y + m[11] * z + m[15];
  530. w = w || 1.0;
  531. out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w;
  532. out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w;
  533. out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w;
  534. return out;
  535. }
  536. /**
  537. * Perform some operation over an array of vec3s.
  538. *
  539. * @param {Array} a the array of vectors to iterate over
  540. * @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed
  541. * @param {Number} offset Number of elements to skip at the beginning of the array
  542. * @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array
  543. * @param {Function} fn Function to call for each vector in the array
  544. * @param {Object} [arg] additional argument to pass to fn
  545. * @returns {Array} a
  546. * @function
  547. */
  548. var forEach = function () {
  549. var vec = create$1();
  550. return function (a, stride, offset, count, fn, arg) {
  551. var i, l;
  552. if (!stride) {
  553. stride = 3;
  554. }
  555. if (!offset) {
  556. offset = 0;
  557. }
  558. if (count) {
  559. l = Math.min(count * stride + offset, a.length);
  560. } else {
  561. l = a.length;
  562. }
  563. for (i = offset; i < l; i += stride) {
  564. vec[0] = a[i];
  565. vec[1] = a[i + 1];
  566. vec[2] = a[i + 2];
  567. fn(vec, vec, arg);
  568. a[i] = vec[0];
  569. a[i + 1] = vec[1];
  570. a[i + 2] = vec[2];
  571. }
  572. return a;
  573. };
  574. }();
  575. /**
  576. * 2 Dimensional Vector
  577. * @module vec2
  578. */
  579. /**
  580. * Creates a new, empty vec2
  581. *
  582. * @returns {vec2} a new 2D vector
  583. */
  584. function create$2() {
  585. var out = new ARRAY_TYPE(2);
  586. if (ARRAY_TYPE != Float32Array) {
  587. out[0] = 0;
  588. out[1] = 0;
  589. }
  590. return out;
  591. }
  592. /**
  593. * Normalize a vec2
  594. *
  595. * @param {vec2} out the receiving vector
  596. * @param {ReadonlyVec2} a vector to normalize
  597. * @returns {vec2} out
  598. */
  599. function normalize(out, a) {
  600. var x = a[0],
  601. y = a[1];
  602. var len = x * x + y * y;
  603. if (len > 0) {
  604. //TODO: evaluate use of glm_invsqrt here?
  605. len = 1 / Math.sqrt(len);
  606. }
  607. out[0] = a[0] * len;
  608. out[1] = a[1] * len;
  609. return out;
  610. }
  611. /**
  612. * Calculates the dot product of two vec2's
  613. *
  614. * @param {ReadonlyVec2} a the first operand
  615. * @param {ReadonlyVec2} b the second operand
  616. * @returns {Number} dot product of a and b
  617. */
  618. function dot(a, b) {
  619. return a[0] * b[0] + a[1] * b[1];
  620. }
  621. /**
  622. * Returns whether or not the vectors exactly have the same elements in the same position (when compared with ===)
  623. *
  624. * @param {ReadonlyVec2} a The first vector.
  625. * @param {ReadonlyVec2} b The second vector.
  626. * @returns {Boolean} True if the vectors are equal, false otherwise.
  627. */
  628. function exactEquals(a, b) {
  629. return a[0] === b[0] && a[1] === b[1];
  630. }
  631. /**
  632. * Perform some operation over an array of vec2s.
  633. *
  634. * @param {Array} a the array of vectors to iterate over
  635. * @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed
  636. * @param {Number} offset Number of elements to skip at the beginning of the array
  637. * @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array
  638. * @param {Function} fn Function to call for each vector in the array
  639. * @param {Object} [arg] additional argument to pass to fn
  640. * @returns {Array} a
  641. * @function
  642. */
  643. var forEach$1 = function () {
  644. var vec = create$2();
  645. return function (a, stride, offset, count, fn, arg) {
  646. var i, l;
  647. if (!stride) {
  648. stride = 2;
  649. }
  650. if (!offset) {
  651. offset = 0;
  652. }
  653. if (count) {
  654. l = Math.min(count * stride + offset, a.length);
  655. } else {
  656. l = a.length;
  657. }
  658. for (i = offset; i < l; i += stride) {
  659. vec[0] = a[i];
  660. vec[1] = a[i + 1];
  661. fn(vec, vec, arg);
  662. a[i] = vec[0];
  663. a[i + 1] = vec[1];
  664. }
  665. return a;
  666. };
  667. }();
  668. var tmpVec3a = create$1();
  669. var tmpVec3b = create$1();
  670. var tmpVec3c = create$1();
  671. var tmpMat4 = create();
  672. /**
  673. * pick shape(s) with Mouse/Touch event
  674. *
  675. * 1. find AABB with r-tree
  676. * 2. do math calculation with geometry in an accurate way
  677. */
  678. var CanvasPickerPlugin = /*#__PURE__*/function () {
  679. function CanvasPickerPlugin() {
  680. var _this = this;
  681. this.context = void 0;
  682. this.runtime = void 0;
  683. this.isHit = function (displayObject, position, worldTransform, isClipPath) {
  684. // use picker for current shape's type
  685. var pick = _this.context.pointInPathPickerFactory[displayObject.nodeName];
  686. if (pick) {
  687. // invert with world matrix
  688. var invertWorldMat = invert(tmpMat4, worldTransform);
  689. // transform client position to local space, do picking in local space
  690. var localPosition = transformMat4(tmpVec3b, set(tmpVec3c, position[0], position[1], 0), invertWorldMat);
  691. // account for anchor
  692. var _displayObject$getGeo = displayObject.getGeometryBounds(),
  693. halfExtents = _displayObject$getGeo.halfExtents;
  694. var anchor = displayObject.parsedStyle.anchor;
  695. localPosition[0] += (anchor && anchor[0] || 0) * halfExtents[0] * 2;
  696. localPosition[1] += (anchor && anchor[1] || 0) * halfExtents[1] * 2;
  697. if (pick(displayObject, new gLite.Point(localPosition[0], localPosition[1]), isClipPath, _this.isPointInPath, _this.context, _this.runtime)) {
  698. return true;
  699. }
  700. }
  701. return false;
  702. };
  703. /**
  704. * use native picking method
  705. * @see https://developer.mozilla.org/zh-CN/docs/Web/API/CanvasRenderingContext2D/isPointInPath
  706. */
  707. this.isPointInPath = function (displayObject, position) {
  708. var context = _this.runtime.offscreenCanvas.getOrCreateContext(_this.context.config.offscreenCanvas);
  709. var generatePath = _this.context.pathGeneratorFactory[displayObject.nodeName];
  710. if (generatePath) {
  711. context.beginPath();
  712. generatePath(context, displayObject.parsedStyle);
  713. context.closePath();
  714. }
  715. return context.isPointInPath(position.x, position.y);
  716. };
  717. }
  718. var _proto = CanvasPickerPlugin.prototype;
  719. _proto.apply = function apply(context, runtime) {
  720. var _renderingContext$roo,
  721. _this2 = this;
  722. var renderingService = context.renderingService,
  723. renderingContext = context.renderingContext;
  724. this.context = context;
  725. this.runtime = runtime;
  726. var document = (_renderingContext$roo = renderingContext.root) === null || _renderingContext$roo === void 0 ? void 0 : _renderingContext$roo.ownerDocument;
  727. renderingService.hooks.pick.tapPromise(CanvasPickerPlugin.tag, /*#__PURE__*/function () {
  728. var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(result) {
  729. return _regeneratorRuntime().wrap(function _callee$(_context) {
  730. while (1) switch (_context.prev = _context.next) {
  731. case 0:
  732. return _context.abrupt("return", _this2.pick(document, result));
  733. case 1:
  734. case "end":
  735. return _context.stop();
  736. }
  737. }, _callee);
  738. }));
  739. return function (_x) {
  740. return _ref.apply(this, arguments);
  741. };
  742. }());
  743. renderingService.hooks.pickSync.tap(CanvasPickerPlugin.tag, function (result) {
  744. return _this2.pick(document, result);
  745. });
  746. };
  747. _proto.pick = function pick(document, result) {
  748. var topmost = result.topmost,
  749. _result$position = result.position,
  750. x = _result$position.x,
  751. y = _result$position.y;
  752. // position in world space
  753. var position = set(tmpVec3a, x, y, 0);
  754. // query by AABB first with spatial index(r-tree)
  755. var hitTestList = document.elementsFromBBox(position[0], position[1], position[0], position[1]);
  756. // test with clip path & origin shape
  757. // @see https://github.com/antvis/g/issues/1064
  758. var pickedDisplayObjects = [];
  759. for (var _iterator = _createForOfIteratorHelperLoose(hitTestList), _step; !(_step = _iterator()).done;) {
  760. var displayObject = _step.value;
  761. var worldTransform = displayObject.getWorldTransform();
  762. var isHitOriginShape = this.isHit(displayObject, position, worldTransform, false);
  763. if (isHitOriginShape) {
  764. // should look up in the ancestor node
  765. var clipped = gLite.findClosestClipPathTarget(displayObject);
  766. if (clipped) {
  767. var clipPath = clipped.parsedStyle.clipPath;
  768. var isHitClipPath = this.isHit(clipPath, position, clipPath.getWorldTransform(), true);
  769. if (isHitClipPath) {
  770. if (topmost) {
  771. result.picked = [displayObject];
  772. return result;
  773. } else {
  774. pickedDisplayObjects.push(displayObject);
  775. }
  776. }
  777. } else {
  778. if (topmost) {
  779. result.picked = [displayObject];
  780. return result;
  781. } else {
  782. pickedDisplayObjects.push(displayObject);
  783. }
  784. }
  785. }
  786. }
  787. result.picked = pickedDisplayObjects;
  788. return result;
  789. };
  790. return CanvasPickerPlugin;
  791. }();
  792. CanvasPickerPlugin.tag = 'CanvasPicker';
  793. /**
  794. * 两点之间的距离
  795. * @param {number} x1 起始点 x
  796. * @param {number} y1 起始点 y
  797. * @param {number} x2 结束点 x
  798. * @param {number} y2 结束点 y
  799. * @return {number} 距离
  800. */
  801. function distance(x1, y1, x2, y2) {
  802. var dx = x1 - x2;
  803. var dy = y1 - y2;
  804. return Math.sqrt(dx * dx + dy * dy);
  805. }
  806. function isNumberEqual(v1, v2) {
  807. return Math.abs(v1 - v2) < 0.001;
  808. }
  809. function getBBoxByArray(xArr, yArr) {
  810. var minX = Math.min.apply(Math, xArr);
  811. var minY = Math.min.apply(Math, yArr);
  812. var maxX = Math.max.apply(Math, xArr);
  813. var maxY = Math.max.apply(Math, yArr);
  814. return {
  815. x: minX,
  816. y: minY,
  817. width: maxX - minX,
  818. height: maxY - minY
  819. };
  820. }
  821. function piMod(angle) {
  822. return (angle + Math.PI * 2) % (Math.PI * 2);
  823. }
  824. var line = {
  825. /**
  826. * 计算线段的包围盒
  827. * @param {number} x1 起始点 x
  828. * @param {number} y1 起始点 y
  829. * @param {number} x2 结束点 x
  830. * @param {number} y2 结束点 y
  831. * @return {object} 包围盒对象
  832. */
  833. box: function box(x1, y1, x2, y2) {
  834. return getBBoxByArray([x1, x2], [y1, y2]);
  835. },
  836. /**
  837. * 线段的长度
  838. * @param {number} x1 起始点 x
  839. * @param {number} y1 起始点 y
  840. * @param {number} x2 结束点 x
  841. * @param {number} y2 结束点 y
  842. * @return {number} 距离
  843. */
  844. length: function length(x1, y1, x2, y2) {
  845. return distance(x1, y1, x2, y2);
  846. },
  847. /**
  848. * 根据比例获取点
  849. * @param {number} x1 起始点 x
  850. * @param {number} y1 起始点 y
  851. * @param {number} x2 结束点 x
  852. * @param {number} y2 结束点 y
  853. * @param {number} t 指定比例
  854. * @return {object} 包含 x, y 的点
  855. */
  856. pointAt: function pointAt(x1, y1, x2, y2, t) {
  857. return {
  858. x: (1 - t) * x1 + t * x2,
  859. y: (1 - t) * y1 + t * y2
  860. };
  861. },
  862. /**
  863. * 点到线段的距离
  864. * @param {number} x1 起始点 x
  865. * @param {number} y1 起始点 y
  866. * @param {number} x2 结束点 x
  867. * @param {number} y2 结束点 y
  868. * @param {number} x 测试点 x
  869. * @param {number} y 测试点 y
  870. * @return {number} 距离
  871. */
  872. pointDistance: function pointDistance(x1, y1, x2, y2, x, y) {
  873. // 投影距离 x1, y1 的向量,假设 p, p1, p2 三个点,投影点为 a
  874. // p1a = p1p.p1p2/|p1p2| * (p1p 的单位向量)
  875. var cross = (x2 - x1) * (x - x1) + (y2 - y1) * (y - y1);
  876. if (cross < 0) {
  877. return distance(x1, y1, x, y);
  878. }
  879. var lengthSquare = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
  880. if (cross > lengthSquare) {
  881. return distance(x2, y2, x, y);
  882. }
  883. return this.pointToLine(x1, y1, x2, y2, x, y);
  884. },
  885. /**
  886. * 点到直线的距离,而不是点到线段的距离
  887. * @param {number} x1 起始点 x
  888. * @param {number} y1 起始点 y
  889. * @param {number} x2 结束点 x
  890. * @param {number} y2 结束点 y
  891. * @param {number} x 测试点 x
  892. * @param {number} y 测试点 y
  893. * @return {number} 距离
  894. */
  895. pointToLine: function pointToLine(x1, y1, x2, y2, x, y) {
  896. var d = [x2 - x1, y2 - y1];
  897. // 如果端点相等,则判定点到点的距离
  898. if (exactEquals(d, [0, 0])) {
  899. return Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
  900. }
  901. var u = [-d[1], d[0]];
  902. normalize(u, u);
  903. var a = [x - x1, y - y1];
  904. return Math.abs(dot(a, u));
  905. },
  906. /**
  907. * 线段的角度
  908. * @param {number} x1 起始点 x
  909. * @param {number} y1 起始点 y
  910. * @param {number} x2 结束点 x
  911. * @param {number} y2 结束点 y
  912. * @return {number} 导数
  913. */
  914. tangentAngle: function tangentAngle(x1, y1, x2, y2) {
  915. return Math.atan2(y2 - y1, x2 - x1);
  916. }
  917. };
  918. var EPSILON = 0.0001;
  919. /**
  920. * 使用牛顿切割法求最近的点
  921. * @param {number[]} xArr 点的 x 数组
  922. * @param {number[]} yArr 点的 y 数组
  923. * @param {number} x 指定的点 x
  924. * @param {number} y 指定的点 y
  925. * @param {Function} tCallback 差值函数
  926. */
  927. function nearestPoint(xArr, yArr, x, y, tCallback, length) {
  928. var t = -1;
  929. var d = Infinity;
  930. var v0 = [x, y];
  931. var segNum = 20;
  932. if (length && length > 200) {
  933. segNum = length / 10;
  934. }
  935. var increaseRate = 1 / segNum;
  936. var interval = increaseRate / 10;
  937. for (var i = 0; i <= segNum; i++) {
  938. var _t = i * increaseRate;
  939. var v1 = [tCallback.apply(void 0, xArr.concat([_t])), tCallback.apply(void 0, yArr.concat([_t]))];
  940. var d1 = distance(v0[0], v0[1], v1[0], v1[1]);
  941. if (d1 < d) {
  942. t = _t;
  943. d = d1;
  944. }
  945. }
  946. // 提前终止
  947. if (t === 0) {
  948. return {
  949. x: xArr[0],
  950. y: yArr[0]
  951. };
  952. }
  953. if (t === 1) {
  954. var count = xArr.length;
  955. return {
  956. x: xArr[count - 1],
  957. y: yArr[count - 1]
  958. };
  959. }
  960. d = Infinity;
  961. for (var _i = 0; _i < 32; _i++) {
  962. if (interval < EPSILON) {
  963. break;
  964. }
  965. var prev = t - interval;
  966. var next = t + interval;
  967. var _v = [tCallback.apply(void 0, xArr.concat([prev])), tCallback.apply(void 0, yArr.concat([prev]))];
  968. var _d = distance(v0[0], v0[1], _v[0], _v[1]);
  969. if (prev >= 0 && _d < d) {
  970. t = prev;
  971. d = _d;
  972. } else {
  973. var v2 = [tCallback.apply(void 0, xArr.concat([next])), tCallback.apply(void 0, yArr.concat([next]))];
  974. var d2 = distance(v0[0], v0[1], v2[0], v2[1]);
  975. if (next <= 1 && d2 < d) {
  976. t = next;
  977. d = d2;
  978. } else {
  979. interval *= 0.5;
  980. }
  981. }
  982. }
  983. return {
  984. x: tCallback.apply(void 0, xArr.concat([t])),
  985. y: tCallback.apply(void 0, yArr.concat([t]))
  986. };
  987. }
  988. // 近似求解 https://community.khronos.org/t/3d-cubic-bezier-segment-length/62363/2
  989. function snapLength(xArr, yArr) {
  990. var totalLength = 0;
  991. var count = xArr.length;
  992. for (var i = 0; i < count; i++) {
  993. var x = xArr[i];
  994. var y = yArr[i];
  995. var nextX = xArr[(i + 1) % count];
  996. var nextY = yArr[(i + 1) % count];
  997. totalLength += distance(x, y, nextX, nextY);
  998. }
  999. return totalLength / 2;
  1000. }
  1001. // 差值公式
  1002. function quadraticAt(p0, p1, p2, t) {
  1003. var onet = 1 - t;
  1004. return onet * onet * p0 + 2 * t * onet * p1 + t * t * p2;
  1005. }
  1006. // 求极值
  1007. function extrema(p0, p1, p2) {
  1008. var a = p0 + p2 - 2 * p1;
  1009. if (isNumberEqual(a, 0)) {
  1010. return [0.5];
  1011. }
  1012. var rst = (p0 - p1) / a;
  1013. if (rst <= 1 && rst >= 0) {
  1014. return [rst];
  1015. }
  1016. return [];
  1017. }
  1018. function derivativeAt(p0, p1, p2, t) {
  1019. return 2 * (1 - t) * (p1 - p0) + 2 * t * (p2 - p1);
  1020. }
  1021. // 分割贝塞尔曲线
  1022. function divideQuadratic(x1, y1, x2, y2, x3, y3, t) {
  1023. // 划分点
  1024. var xt = quadraticAt(x1, x2, x3, t);
  1025. var yt = quadraticAt(y1, y2, y3, t);
  1026. // 分割的第一条曲线的控制点
  1027. var controlPoint1 = line.pointAt(x1, y1, x2, y2, t);
  1028. // 分割的第二条曲线的控制点
  1029. var controlPoint2 = line.pointAt(x2, y2, x3, y3, t);
  1030. return [[x1, y1, controlPoint1.x, controlPoint1.y, xt, yt], [xt, yt, controlPoint2.x, controlPoint2.y, x3, y3]];
  1031. }
  1032. // 使用迭代法取贝塞尔曲线的长度
  1033. function quadraticLength(x1, y1, x2, y2, x3, y3, iterationCount) {
  1034. if (iterationCount === 0) {
  1035. return (distance(x1, y1, x2, y2) + distance(x2, y2, x3, y3) + distance(x1, y1, x3, y3)) / 2;
  1036. }
  1037. var quadratics = divideQuadratic(x1, y1, x2, y2, x3, y3, 0.5);
  1038. var left = quadratics[0];
  1039. var right = quadratics[1];
  1040. left.push(iterationCount - 1);
  1041. right.push(iterationCount - 1);
  1042. return quadraticLength.apply(void 0, left) + quadraticLength.apply(void 0, right);
  1043. }
  1044. var quadratic = {
  1045. box: function box(x1, y1, x2, y2, x3, y3) {
  1046. var xExtrema = extrema(x1, x2, x3)[0];
  1047. var yExtrema = extrema(y1, y2, y3)[0];
  1048. // 控制点不加入 box 的计算
  1049. var xArr = [x1, x3];
  1050. var yArr = [y1, y3];
  1051. if (xExtrema !== undefined) {
  1052. xArr.push(quadraticAt(x1, x2, x3, xExtrema));
  1053. }
  1054. if (yExtrema !== undefined) {
  1055. yArr.push(quadraticAt(y1, y2, y3, yExtrema));
  1056. }
  1057. return getBBoxByArray(xArr, yArr);
  1058. },
  1059. length: function length(x1, y1, x2, y2, x3, y3) {
  1060. return quadraticLength(x1, y1, x2, y2, x3, y3, 3);
  1061. },
  1062. nearestPoint: function nearestPoint$1(x1, y1, x2, y2, x3, y3, x0, y0) {
  1063. return nearestPoint([x1, x2, x3], [y1, y2, y3], x0, y0, quadraticAt);
  1064. },
  1065. pointDistance: function pointDistance(x1, y1, x2, y2, x3, y3, x0, y0) {
  1066. var point = this.nearestPoint(x1, y1, x2, y2, x3, y3, x0, y0);
  1067. return distance(point.x, point.y, x0, y0);
  1068. },
  1069. interpolationAt: quadraticAt,
  1070. pointAt: function pointAt(x1, y1, x2, y2, x3, y3, t) {
  1071. return {
  1072. x: quadraticAt(x1, x2, x3, t),
  1073. y: quadraticAt(y1, y2, y3, t)
  1074. };
  1075. },
  1076. divide: function divide(x1, y1, x2, y2, x3, y3, t) {
  1077. return divideQuadratic(x1, y1, x2, y2, x3, y3, t);
  1078. },
  1079. tangentAngle: function tangentAngle(x1, y1, x2, y2, x3, y3, t) {
  1080. var dx = derivativeAt(x1, x2, x3, t);
  1081. var dy = derivativeAt(y1, y2, y3, t);
  1082. var angle = Math.atan2(dy, dx);
  1083. return piMod(angle);
  1084. }
  1085. };
  1086. function cubicAt(p0, p1, p2, p3, t) {
  1087. var onet = 1 - t; // t * t * t 的性能大概是 Math.pow(t, 3) 的三倍
  1088. return onet * onet * onet * p0 + 3 * p1 * t * onet * onet + 3 * p2 * t * t * onet + p3 * t * t * t;
  1089. }
  1090. function derivativeAt$1(p0, p1, p2, p3, t) {
  1091. var onet = 1 - t;
  1092. return 3 * (onet * onet * (p1 - p0) + 2 * onet * t * (p2 - p1) + t * t * (p3 - p2));
  1093. }
  1094. function extrema$1(p0, p1, p2, p3) {
  1095. var a = -3 * p0 + 9 * p1 - 9 * p2 + 3 * p3;
  1096. var b = 6 * p0 - 12 * p1 + 6 * p2;
  1097. var c = 3 * p1 - 3 * p0;
  1098. var extremas = [];
  1099. var t1;
  1100. var t2;
  1101. var discSqrt;
  1102. if (isNumberEqual(a, 0)) {
  1103. if (!isNumberEqual(b, 0)) {
  1104. t1 = -c / b;
  1105. if (t1 >= 0 && t1 <= 1) {
  1106. extremas.push(t1);
  1107. }
  1108. }
  1109. } else {
  1110. var disc = b * b - 4 * a * c;
  1111. if (isNumberEqual(disc, 0)) {
  1112. extremas.push(-b / (2 * a));
  1113. } else if (disc > 0) {
  1114. discSqrt = Math.sqrt(disc);
  1115. t1 = (-b + discSqrt) / (2 * a);
  1116. t2 = (-b - discSqrt) / (2 * a);
  1117. if (t1 >= 0 && t1 <= 1) {
  1118. extremas.push(t1);
  1119. }
  1120. if (t2 >= 0 && t2 <= 1) {
  1121. extremas.push(t2);
  1122. }
  1123. }
  1124. }
  1125. return extremas;
  1126. }
  1127. // 分割贝塞尔曲线
  1128. function divideCubic(x1, y1, x2, y2, x3, y3, x4, y4, t) {
  1129. // 划分点
  1130. var xt = cubicAt(x1, x2, x3, x4, t);
  1131. var yt = cubicAt(y1, y2, y3, y4, t);
  1132. // 计算两点之间的差值点
  1133. var c1 = line.pointAt(x1, y1, x2, y2, t);
  1134. var c2 = line.pointAt(x2, y2, x3, y3, t);
  1135. var c3 = line.pointAt(x3, y3, x4, y4, t);
  1136. var c12 = line.pointAt(c1.x, c1.y, c2.x, c2.y, t);
  1137. var c23 = line.pointAt(c2.x, c2.y, c3.x, c3.y, t);
  1138. return [[x1, y1, c1.x, c1.y, c12.x, c12.y, xt, yt], [xt, yt, c23.x, c23.y, c3.x, c3.y, x4, y4]];
  1139. }
  1140. // 使用迭代法取贝塞尔曲线的长度,二阶和三阶分开写,更清晰和便于调试
  1141. function cubicLength(x1, y1, x2, y2, x3, y3, x4, y4, iterationCount) {
  1142. if (iterationCount === 0) {
  1143. return snapLength([x1, x2, x3, x4], [y1, y2, y3, y4]);
  1144. }
  1145. var cubics = divideCubic(x1, y1, x2, y2, x3, y3, x4, y4, 0.5);
  1146. var left = [].concat(cubics[0], [iterationCount - 1]);
  1147. var right = [].concat(cubics[1], [iterationCount - 1]);
  1148. return cubicLength.apply(void 0, left) + cubicLength.apply(void 0, right);
  1149. }
  1150. var cubic = {
  1151. extrema: extrema$1,
  1152. box: function box(x1, y1, x2, y2, x3, y3, x4, y4) {
  1153. var xArr = [x1, x4];
  1154. var yArr = [y1, y4];
  1155. var xExtrema = extrema$1(x1, x2, x3, x4);
  1156. var yExtrema = extrema$1(y1, y2, y3, y4);
  1157. for (var i = 0; i < xExtrema.length; i++) {
  1158. xArr.push(cubicAt(x1, x2, x3, x4, xExtrema[i]));
  1159. }
  1160. for (var _i = 0; _i < yExtrema.length; _i++) {
  1161. yArr.push(cubicAt(y1, y2, y3, y4, yExtrema[_i]));
  1162. }
  1163. return getBBoxByArray(xArr, yArr);
  1164. },
  1165. length: function length(x1, y1, x2, y2, x3, y3, x4, y4) {
  1166. // 迭代三次,划分成 8 段求长度
  1167. return cubicLength(x1, y1, x2, y2, x3, y3, x4, y4, 3);
  1168. },
  1169. nearestPoint: function nearestPoint$1(x1, y1, x2, y2, x3, y3, x4, y4, x0, y0, length) {
  1170. return nearestPoint([x1, x2, x3, x4], [y1, y2, y3, y4], x0, y0, cubicAt, length);
  1171. },
  1172. pointDistance: function pointDistance(x1, y1, x2, y2, x3, y3, x4, y4, x0, y0, length) {
  1173. var point = this.nearestPoint(x1, y1, x2, y2, x3, y3, x4, y4, x0, y0, length);
  1174. return distance(point.x, point.y, x0, y0);
  1175. },
  1176. interpolationAt: cubicAt,
  1177. pointAt: function pointAt(x1, y1, x2, y2, x3, y3, x4, y4, t) {
  1178. return {
  1179. x: cubicAt(x1, x2, x3, x4, t),
  1180. y: cubicAt(y1, y2, y3, y4, t)
  1181. };
  1182. },
  1183. divide: function divide(x1, y1, x2, y2, x3, y3, x4, y4, t) {
  1184. return divideCubic(x1, y1, x2, y2, x3, y3, x4, y4, t);
  1185. },
  1186. tangentAngle: function tangentAngle(x1, y1, x2, y2, x3, y3, x4, y4, t) {
  1187. var dx = derivativeAt$1(x1, x2, x3, x4, t);
  1188. var dy = derivativeAt$1(y1, y2, y3, y4, t);
  1189. return piMod(Math.atan2(dy, dx));
  1190. }
  1191. };
  1192. function distance$1(x1, y1, x2, y2) {
  1193. var dx = x1 - x2;
  1194. var dy = y1 - y2;
  1195. return Math.sqrt(dx * dx + dy * dy);
  1196. }
  1197. function inBox(minX, minY, width, height, x, y) {
  1198. return x >= minX && x <= minX + width && y >= minY && y <= minY + height;
  1199. }
  1200. function inRect(minX, minY, width, height, lineWidth, x, y) {
  1201. var halfWidth = lineWidth / 2;
  1202. // 将四个边看做矩形来检测,比边的检测算法要快
  1203. return inBox(minX - halfWidth, minY - halfWidth, width, lineWidth, x, y) ||
  1204. // 上边
  1205. inBox(minX + width - halfWidth, minY - halfWidth, lineWidth, height, x, y) ||
  1206. // 右边
  1207. inBox(minX + halfWidth, minY + height - halfWidth, width, lineWidth, x, y) ||
  1208. // 下边
  1209. inBox(minX - halfWidth, minY + halfWidth, lineWidth, height, x, y); // 左边
  1210. }
  1211. function inArc(cx, cy, r, startAngle, endAngle, lineWidth, x, y) {
  1212. var angle = (Math.atan2(y - cy, x - cx) + Math.PI * 2) % (Math.PI * 2); // 转换到 0 - 2 * Math.PI 之间
  1213. // if (angle < startAngle || angle > endAngle) {
  1214. // return false;
  1215. // }
  1216. var point = {
  1217. x: cx + r * Math.cos(angle),
  1218. y: cy + r * Math.sin(angle)
  1219. };
  1220. return distance$1(point.x, point.y, x, y) <= lineWidth / 2;
  1221. }
  1222. function inLine(x1, y1, x2, y2, lineWidth, x, y) {
  1223. var minX = Math.min(x1, x2);
  1224. var maxX = Math.max(x1, x2);
  1225. var minY = Math.min(y1, y2);
  1226. var maxY = Math.max(y1, y2);
  1227. var halfWidth = lineWidth / 2;
  1228. // 因为目前的方案是计算点到直线的距离,而有可能会在延长线上,所以要先判断是否在包围盒内
  1229. // 这种方案会在水平或者竖直的情况下载线的延长线上有半 lineWidth 的误差
  1230. if (!(x >= minX - halfWidth && x <= maxX + halfWidth && y >= minY - halfWidth && y <= maxY + halfWidth)) {
  1231. return false;
  1232. }
  1233. // 因为已经计算了包围盒,所以仅需要计算到直线的距离即可,可以显著提升性能
  1234. return line.pointToLine(x1, y1, x2, y2, x, y) <= lineWidth / 2;
  1235. }
  1236. function inPolyline(points, lineWidth, x, y, isClose) {
  1237. var count = points.length;
  1238. if (count < 2) {
  1239. return false;
  1240. }
  1241. for (var i = 0; i < count - 1; i++) {
  1242. var x1 = points[i][0];
  1243. var y1 = points[i][1];
  1244. var x2 = points[i + 1][0];
  1245. var y2 = points[i + 1][1];
  1246. if (inLine(x1, y1, x2, y2, lineWidth, x, y)) {
  1247. return true;
  1248. }
  1249. }
  1250. // 如果封闭,则计算起始点和结束点的边
  1251. if (isClose) {
  1252. var first = points[0];
  1253. var last = points[count - 1];
  1254. if (inLine(first[0], first[1], last[0], last[1], lineWidth, x, y)) {
  1255. return true;
  1256. }
  1257. }
  1258. return false;
  1259. }
  1260. // 多边形的射线检测,参考:https://blog.csdn.net/WilliamSun0122/article/details/77994526
  1261. var tolerance = 1e-6;
  1262. // 三态函数,判断两个double在eps精度下的大小关系
  1263. function dcmp(x) {
  1264. if (Math.abs(x) < tolerance) {
  1265. return 0;
  1266. }
  1267. return x < 0 ? -1 : 1;
  1268. }
  1269. // 判断点Q是否在p1和p2的线段上
  1270. function onSegment(p1, p2, q) {
  1271. if ((q[0] - p1[0]) * (p2[1] - p1[1]) === (p2[0] - p1[0]) * (q[1] - p1[1]) && Math.min(p1[0], p2[0]) <= q[0] && q[0] <= Math.max(p1[0], p2[0]) && Math.min(p1[1], p2[1]) <= q[1] && q[1] <= Math.max(p1[1], p2[1])) {
  1272. return true;
  1273. }
  1274. return false;
  1275. }
  1276. // 判断点P在多边形内-射线法
  1277. function inPolygon(points, x, y) {
  1278. var isHit = false;
  1279. var n = points.length;
  1280. if (n <= 2) {
  1281. // svg 中点小于 3 个时,不显示,也无法被拾取
  1282. return false;
  1283. }
  1284. for (var i = 0; i < n; i++) {
  1285. var p1 = points[i];
  1286. var p2 = points[(i + 1) % n];
  1287. if (onSegment(p1, p2, [x, y])) {
  1288. // 点在多边形一条边上
  1289. return true;
  1290. }
  1291. // 前一个判断min(p1[1],p2[1])<P.y<=max(p1[1],p2[1])
  1292. // 后一个判断被测点 在 射线与边交点 的左边
  1293. if (dcmp(p1[1] - y) > 0 !== dcmp(p2[1] - y) > 0 && dcmp(x - (y - p1[1]) * (p1[0] - p2[0]) / (p1[1] - p2[1]) - p1[0]) < 0) {
  1294. isHit = !isHit;
  1295. }
  1296. }
  1297. return isHit;
  1298. }
  1299. function inPolygons(polygons, x, y) {
  1300. var isHit = false;
  1301. for (var i = 0; i < polygons.length; i++) {
  1302. var points = polygons[i];
  1303. isHit = inPolygon(points, x, y);
  1304. if (isHit) {
  1305. break;
  1306. }
  1307. }
  1308. return isHit;
  1309. }
  1310. function isPointInPath(displayObject, position, isClipPath) {
  1311. var _displayObject$parsed = displayObject.parsedStyle,
  1312. r = _displayObject$parsed.r,
  1313. fill = _displayObject$parsed.fill,
  1314. stroke = _displayObject$parsed.stroke,
  1315. lineWidth = _displayObject$parsed.lineWidth,
  1316. increasedLineWidthForHitTesting = _displayObject$parsed.increasedLineWidthForHitTesting,
  1317. pointerEvents = _displayObject$parsed.pointerEvents;
  1318. var halfLineWidth = ((lineWidth || 0) + (increasedLineWidthForHitTesting || 0)) / 2;
  1319. var absDistance = distance$1(r, r, position.x, position.y);
  1320. var _isFillOrStrokeAffect = gLite.isFillOrStrokeAffected(pointerEvents, fill, stroke),
  1321. hasFill = _isFillOrStrokeAffect[0],
  1322. hasStroke = _isFillOrStrokeAffect[1];
  1323. if (hasFill && hasStroke || isClipPath) {
  1324. return absDistance <= r + halfLineWidth;
  1325. }
  1326. if (hasFill) {
  1327. return absDistance <= r;
  1328. }
  1329. if (hasStroke) {
  1330. return absDistance >= r - halfLineWidth && absDistance <= r + halfLineWidth;
  1331. }
  1332. return false;
  1333. }
  1334. function ellipseDistance(squareX, squareY, rx, ry) {
  1335. return squareX / (rx * rx) + squareY / (ry * ry);
  1336. }
  1337. function isPointInPath$1(displayObject, position, isClipPath) {
  1338. var _displayObject$parsed = displayObject.parsedStyle,
  1339. rx = _displayObject$parsed.rx,
  1340. ry = _displayObject$parsed.ry,
  1341. fill = _displayObject$parsed.fill,
  1342. stroke = _displayObject$parsed.stroke,
  1343. lineWidth = _displayObject$parsed.lineWidth,
  1344. increasedLineWidthForHitTesting = _displayObject$parsed.increasedLineWidthForHitTesting,
  1345. pointerEvents = _displayObject$parsed.pointerEvents;
  1346. var x = position.x,
  1347. y = position.y;
  1348. var _isFillOrStrokeAffect = gLite.isFillOrStrokeAffected(pointerEvents, fill, stroke),
  1349. hasFill = _isFillOrStrokeAffect[0],
  1350. hasStroke = _isFillOrStrokeAffect[1];
  1351. var halfLineWith = ((lineWidth || 0) + (increasedLineWidthForHitTesting || 0)) / 2;
  1352. var squareX = (x - rx) * (x - rx);
  1353. var squareY = (y - ry) * (y - ry);
  1354. // 使用椭圆的公式: x*x/rx*rx + y*y/ry*ry = 1;
  1355. if (hasFill && hasStroke || isClipPath) {
  1356. return ellipseDistance(squareX, squareY, rx + halfLineWith, ry + halfLineWith) <= 1;
  1357. }
  1358. if (hasFill) {
  1359. return ellipseDistance(squareX, squareY, rx, ry) <= 1;
  1360. }
  1361. if (hasStroke) {
  1362. return ellipseDistance(squareX, squareY, rx - halfLineWith, ry - halfLineWith) >= 1 && ellipseDistance(squareX, squareY, rx + halfLineWith, ry + halfLineWith) <= 1;
  1363. }
  1364. return false;
  1365. }
  1366. function isPointInPath$2(displayObject, position, isClipPath) {
  1367. var _displayObject$parsed = displayObject.parsedStyle,
  1368. x1 = _displayObject$parsed.x1,
  1369. y1 = _displayObject$parsed.y1,
  1370. x2 = _displayObject$parsed.x2,
  1371. y2 = _displayObject$parsed.y2,
  1372. lineWidth = _displayObject$parsed.lineWidth,
  1373. increasedLineWidthForHitTesting = _displayObject$parsed.increasedLineWidthForHitTesting,
  1374. _displayObject$parsed2 = _displayObject$parsed.defX,
  1375. x = _displayObject$parsed2 === void 0 ? 0 : _displayObject$parsed2,
  1376. _displayObject$parsed3 = _displayObject$parsed.defY,
  1377. y = _displayObject$parsed3 === void 0 ? 0 : _displayObject$parsed3,
  1378. pointerEvents = _displayObject$parsed.pointerEvents,
  1379. fill = _displayObject$parsed.fill,
  1380. stroke = _displayObject$parsed.stroke;
  1381. var _isFillOrStrokeAffect = gLite.isFillOrStrokeAffected(pointerEvents, fill, stroke),
  1382. hasStroke = _isFillOrStrokeAffect[1];
  1383. if (!hasStroke && !isClipPath || !lineWidth) {
  1384. return false;
  1385. }
  1386. return inLine(x1, y1, x2, y2, (lineWidth || 0) + (increasedLineWidthForHitTesting || 0), position.x + x, position.y + y);
  1387. }
  1388. function rotateVector(x, y, rad) {
  1389. var X = x * Math.cos(rad) - y * Math.sin(rad);
  1390. var Y = x * Math.sin(rad) + y * Math.cos(rad);
  1391. return { x: X, y: Y };
  1392. }
  1393. /**
  1394. * Converts A (arc-to) segments to C (cubic-bezier-to).
  1395. *
  1396. * For more information of where this math came from visit:
  1397. * http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
  1398. */
  1399. function arcToCubic(X1, Y1, RX, RY, angle, LAF, SF, X2, Y2, recursive) {
  1400. var x1 = X1;
  1401. var y1 = Y1;
  1402. var rx = RX;
  1403. var ry = RY;
  1404. var x2 = X2;
  1405. var y2 = Y2;
  1406. // for more information of where this Math came from visit:
  1407. // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
  1408. var d120 = (Math.PI * 120) / 180;
  1409. var rad = (Math.PI / 180) * (+angle || 0);
  1410. /** @type {number[]} */
  1411. var res = [];
  1412. var xy;
  1413. var f1;
  1414. var f2;
  1415. var cx;
  1416. var cy;
  1417. if (!recursive) {
  1418. xy = rotateVector(x1, y1, -rad);
  1419. x1 = xy.x;
  1420. y1 = xy.y;
  1421. xy = rotateVector(x2, y2, -rad);
  1422. x2 = xy.x;
  1423. y2 = xy.y;
  1424. var x = (x1 - x2) / 2;
  1425. var y = (y1 - y2) / 2;
  1426. var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
  1427. if (h > 1) {
  1428. h = Math.sqrt(h);
  1429. rx *= h;
  1430. ry *= h;
  1431. }
  1432. var rx2 = rx * rx;
  1433. var ry2 = ry * ry;
  1434. var k = (LAF === SF ? -1 : 1) *
  1435. Math.sqrt(Math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x)));
  1436. cx = (k * rx * y) / ry + (x1 + x2) / 2;
  1437. cy = (k * -ry * x) / rx + (y1 + y2) / 2;
  1438. // eslint-disable-next-line no-bitwise -- Impossible to satisfy no-bitwise
  1439. f1 = Math.asin(((((y1 - cy) / ry) * Math.pow(10, 9)) >> 0) / Math.pow(10, 9));
  1440. // eslint-disable-next-line no-bitwise -- Impossible to satisfy no-bitwise
  1441. f2 = Math.asin(((((y2 - cy) / ry) * Math.pow(10, 9)) >> 0) / Math.pow(10, 9));
  1442. f1 = x1 < cx ? Math.PI - f1 : f1;
  1443. f2 = x2 < cx ? Math.PI - f2 : f2;
  1444. if (f1 < 0)
  1445. f1 = Math.PI * 2 + f1;
  1446. if (f2 < 0)
  1447. f2 = Math.PI * 2 + f2;
  1448. if (SF && f1 > f2) {
  1449. f1 -= Math.PI * 2;
  1450. }
  1451. if (!SF && f2 > f1) {
  1452. f2 -= Math.PI * 2;
  1453. }
  1454. }
  1455. else {
  1456. f1 = recursive[0], f2 = recursive[1], cx = recursive[2], cy = recursive[3];
  1457. }
  1458. var df = f2 - f1;
  1459. if (Math.abs(df) > d120) {
  1460. var f2old = f2;
  1461. var x2old = x2;
  1462. var y2old = y2;
  1463. f2 = f1 + d120 * (SF && f2 > f1 ? 1 : -1);
  1464. x2 = cx + rx * Math.cos(f2);
  1465. y2 = cy + ry * Math.sin(f2);
  1466. res = arcToCubic(x2, y2, rx, ry, angle, 0, SF, x2old, y2old, [f2, f2old, cx, cy]);
  1467. }
  1468. df = f2 - f1;
  1469. var c1 = Math.cos(f1);
  1470. var s1 = Math.sin(f1);
  1471. var c2 = Math.cos(f2);
  1472. var s2 = Math.sin(f2);
  1473. var t = Math.tan(df / 4);
  1474. var hx = (4 / 3) * rx * t;
  1475. var hy = (4 / 3) * ry * t;
  1476. var m1 = [x1, y1];
  1477. var m2 = [x1 + hx * s1, y1 - hy * c1];
  1478. var m3 = [x2 + hx * s2, y2 - hy * c2];
  1479. var m4 = [x2, y2];
  1480. m2[0] = 2 * m1[0] - m2[0];
  1481. m2[1] = 2 * m1[1] - m2[1];
  1482. if (recursive) {
  1483. return m2.concat(m3, m4, res);
  1484. // return [...m2, ...m3, ...m4, ...res];
  1485. }
  1486. res = m2.concat(m3, m4, res);
  1487. // res = [...m2, ...m3, ...m4, ...res];
  1488. var newres = [];
  1489. for (var i = 0, ii = res.length; i < ii; i += 1) {
  1490. newres[i] = i % 2 ? rotateVector(res[i - 1], res[i], rad).y : rotateVector(res[i], res[i + 1], rad).x;
  1491. }
  1492. return newres;
  1493. }
  1494. // const TAU = Math.PI * 2;
  1495. // const mapToEllipse = (
  1496. // { x, y }: { x: number; y: number },
  1497. // rx: number,
  1498. // ry: number,
  1499. // cosphi: number,
  1500. // sinphi: number,
  1501. // centerx: number,
  1502. // centery: number,
  1503. // ) => {
  1504. // x *= rx;
  1505. // y *= ry;
  1506. // const xp = cosphi * x - sinphi * y;
  1507. // const yp = sinphi * x + cosphi * y;
  1508. // return {
  1509. // x: xp + centerx,
  1510. // y: yp + centery,
  1511. // };
  1512. // };
  1513. // const approxUnitArc = (ang1: number, ang2: number) => {
  1514. // // If 90 degree circular arc, use a constant
  1515. // // as derived from http://spencermortensen.com/articles/bezier-circle
  1516. // const a =
  1517. // ang2 === 1.5707963267948966
  1518. // ? 0.551915024494
  1519. // : ang2 === -1.5707963267948966
  1520. // ? -0.551915024494
  1521. // : (4 / 3) * Math.tan(ang2 / 4);
  1522. // const x1 = Math.cos(ang1);
  1523. // const y1 = Math.sin(ang1);
  1524. // const x2 = Math.cos(ang1 + ang2);
  1525. // const y2 = Math.sin(ang1 + ang2);
  1526. // return [
  1527. // {
  1528. // x: x1 - y1 * a,
  1529. // y: y1 + x1 * a,
  1530. // },
  1531. // {
  1532. // x: x2 + y2 * a,
  1533. // y: y2 - x2 * a,
  1534. // },
  1535. // {
  1536. // x: x2,
  1537. // y: y2,
  1538. // },
  1539. // ];
  1540. // };
  1541. // const vectorAngle = (ux: number, uy: number, vx: number, vy: number) => {
  1542. // const sign = ux * vy - uy * vx < 0 ? -1 : 1;
  1543. // let dot = ux * vx + uy * vy;
  1544. // if (dot > 1) {
  1545. // dot = 1;
  1546. // }
  1547. // if (dot < -1) {
  1548. // dot = -1;
  1549. // }
  1550. // return sign * Math.acos(dot);
  1551. // };
  1552. // const getArcCenter = (
  1553. // px: any,
  1554. // py: any,
  1555. // cx: any,
  1556. // cy: any,
  1557. // rx: number,
  1558. // ry: number,
  1559. // largeArcFlag: number,
  1560. // sweepFlag: number,
  1561. // sinphi: number,
  1562. // cosphi: number,
  1563. // pxp: number,
  1564. // pyp: number,
  1565. // ) => {
  1566. // const rxsq = Math.pow(rx, 2);
  1567. // const rysq = Math.pow(ry, 2);
  1568. // const pxpsq = Math.pow(pxp, 2);
  1569. // const pypsq = Math.pow(pyp, 2);
  1570. // let radicant = rxsq * rysq - rxsq * pypsq - rysq * pxpsq;
  1571. // if (radicant < 0) {
  1572. // radicant = 0;
  1573. // }
  1574. // radicant /= rxsq * pypsq + rysq * pxpsq;
  1575. // radicant = Math.sqrt(radicant) * (largeArcFlag === sweepFlag ? -1 : 1);
  1576. // const centerxp = ((radicant * rx) / ry) * pyp;
  1577. // const centeryp = ((radicant * -ry) / rx) * pxp;
  1578. // const centerx = cosphi * centerxp - sinphi * centeryp + (px + cx) / 2;
  1579. // const centery = sinphi * centerxp + cosphi * centeryp + (py + cy) / 2;
  1580. // const vx1 = (pxp - centerxp) / rx;
  1581. // const vy1 = (pyp - centeryp) / ry;
  1582. // const vx2 = (-pxp - centerxp) / rx;
  1583. // const vy2 = (-pyp - centeryp) / ry;
  1584. // const ang1 = vectorAngle(1, 0, vx1, vy1);
  1585. // let ang2 = vectorAngle(vx1, vy1, vx2, vy2);
  1586. // if (sweepFlag === 0 && ang2 > 0) {
  1587. // ang2 -= TAU;
  1588. // }
  1589. // if (sweepFlag === 1 && ang2 < 0) {
  1590. // ang2 += TAU;
  1591. // }
  1592. // return [centerx, centery, ang1, ang2];
  1593. // };
  1594. // const arcToBezier = ({ px, py, cx, cy, rx, ry, xAxisRotation = 0, largeArcFlag = 0, sweepFlag = 0 }) => {
  1595. // const curves = [];
  1596. // if (rx === 0 || ry === 0) {
  1597. // return [{ x1: 0, y1: 0, x2: 0, y2: 0, x: cx, y: cy }];
  1598. // }
  1599. // const sinphi = Math.sin((xAxisRotation * TAU) / 360);
  1600. // const cosphi = Math.cos((xAxisRotation * TAU) / 360);
  1601. // const pxp = (cosphi * (px - cx)) / 2 + (sinphi * (py - cy)) / 2;
  1602. // const pyp = (-sinphi * (px - cx)) / 2 + (cosphi * (py - cy)) / 2;
  1603. // if (pxp === 0 && pyp === 0) {
  1604. // return [{ x1: 0, y1: 0, x2: 0, y2: 0, x: cx, y: cy }];
  1605. // }
  1606. // rx = Math.abs(rx);
  1607. // ry = Math.abs(ry);
  1608. // const lambda = Math.pow(pxp, 2) / Math.pow(rx, 2) + Math.pow(pyp, 2) / Math.pow(ry, 2);
  1609. // if (lambda > 1) {
  1610. // rx *= Math.sqrt(lambda);
  1611. // ry *= Math.sqrt(lambda);
  1612. // }
  1613. // let [centerx, centery, ang1, ang2] = getArcCenter(
  1614. // px,
  1615. // py,
  1616. // cx,
  1617. // cy,
  1618. // rx,
  1619. // ry,
  1620. // largeArcFlag,
  1621. // sweepFlag,
  1622. // sinphi,
  1623. // cosphi,
  1624. // pxp,
  1625. // pyp,
  1626. // );
  1627. // // If 'ang2' == 90.0000000001, then `ratio` will evaluate to
  1628. // // 1.0000000001. This causes `segments` to be greater than one, which is an
  1629. // // unecessary split, and adds extra points to the bezier curve. To alleviate
  1630. // // this issue, we round to 1.0 when the ratio is close to 1.0.
  1631. // let ratio = Math.abs(ang2) / (TAU / 4);
  1632. // if (Math.abs(1.0 - ratio) < 0.0000001) {
  1633. // ratio = 1.0;
  1634. // }
  1635. // const segments = Math.max(Math.ceil(ratio), 1);
  1636. // ang2 /= segments;
  1637. // for (let i = 0; i < segments; i++) {
  1638. // curves.push(approxUnitArc(ang1, ang2));
  1639. // ang1 += ang2;
  1640. // }
  1641. // return curves.map((curve) => {
  1642. // const { x: x1, y: y1 } = mapToEllipse(curve[0], rx, ry, cosphi, sinphi, centerx, centery);
  1643. // const { x: x2, y: y2 } = mapToEllipse(curve[1], rx, ry, cosphi, sinphi, centerx, centery);
  1644. // const { x, y } = mapToEllipse(curve[2], rx, ry, cosphi, sinphi, centerx, centery);
  1645. // return { x1, y1, x2, y2, x, y };
  1646. // });
  1647. // };
  1648. // export function arcToCubic(
  1649. // x1: number,
  1650. // y1: number,
  1651. // rx: number,
  1652. // ry: number,
  1653. // angle: number,
  1654. // LAF: number,
  1655. // SF: number,
  1656. // x2: number,
  1657. // y2: number,
  1658. // ) {
  1659. // const curves = arcToBezier({
  1660. // px: x1,
  1661. // py: y1,
  1662. // cx: x2,
  1663. // cy: y2,
  1664. // rx,
  1665. // ry,
  1666. // xAxisRotation: angle,
  1667. // largeArcFlag: LAF,
  1668. // sweepFlag: SF,
  1669. // });
  1670. // return curves.reduce((prev, cur) => {
  1671. // const { x1, y1, x2, y2, x, y } = cur;
  1672. // prev.push(x1, y1, x2, y2, x, y);
  1673. // return prev;
  1674. // }, [] as number[]);
  1675. // }
  1676. var clamp = function (a, min, max) {
  1677. if (a < min) {
  1678. return min;
  1679. }
  1680. else if (a > max) {
  1681. return max;
  1682. }
  1683. return a;
  1684. };
  1685. // TODO: replace it with method in @antv/util
  1686. function isPointInStroke(segments, lineWidth, px, py, length) {
  1687. var isHit = false;
  1688. var halfWidth = lineWidth / 2;
  1689. for (var i = 0; i < segments.length; i++) {
  1690. var segment = segments[i];
  1691. var currentPoint = segment.currentPoint,
  1692. params = segment.params,
  1693. prePoint = segment.prePoint,
  1694. box = segment.box;
  1695. // 如果在前面已经生成过包围盒,直接按照包围盒计算
  1696. if (box && !inBox(box.x - halfWidth, box.y - halfWidth, box.width + lineWidth, box.height + lineWidth, px, py)) {
  1697. continue;
  1698. }
  1699. switch (segment.command) {
  1700. // L 和 Z 都是直线, M 不进行拾取
  1701. case 'L':
  1702. case 'Z':
  1703. isHit = inLine(prePoint[0], prePoint[1], currentPoint[0], currentPoint[1], lineWidth, px, py);
  1704. if (isHit) {
  1705. return true;
  1706. }
  1707. break;
  1708. case 'Q':
  1709. var qDistance = quadratic.pointDistance(prePoint[0], prePoint[1], params[1], params[2], params[3], params[4], px, py);
  1710. isHit = qDistance <= lineWidth / 2;
  1711. if (isHit) {
  1712. return true;
  1713. }
  1714. break;
  1715. case 'C':
  1716. var cDistance = cubic.pointDistance(prePoint[0],
  1717. // 上一段结束位置, 即 C 的起始点
  1718. prePoint[1], params[1],
  1719. // 'C' 的参数,1、2 为第一个控制点,3、4 为第二个控制点,5、6 为结束点
  1720. params[2], params[3], params[4], params[5], params[6], px, py, length);
  1721. isHit = cDistance <= lineWidth / 2;
  1722. if (isHit) {
  1723. return true;
  1724. }
  1725. break;
  1726. case 'A':
  1727. // cache conversion result
  1728. if (!segment.cubicParams) {
  1729. segment.cubicParams = arcToCubic(prePoint[0], prePoint[1], params[1], params[2], params[3], params[4], params[5], params[6], params[7], undefined);
  1730. }
  1731. var args = segment.cubicParams;
  1732. // fixArc
  1733. var prePointInCubic = prePoint;
  1734. for (var _i = 0; _i < args.length; _i += 6) {
  1735. var _cDistance = cubic.pointDistance(prePointInCubic[0],
  1736. // 上一段结束位置, 即 C 的起始点
  1737. prePointInCubic[1], args[_i], args[_i + 1], args[_i + 2], args[_i + 3], args[_i + 4], args[_i + 5], px, py, length);
  1738. prePointInCubic = [args[_i + 4], args[_i + 5]];
  1739. isHit = _cDistance <= lineWidth / 2;
  1740. if (isHit) {
  1741. return true;
  1742. }
  1743. }
  1744. break;
  1745. }
  1746. }
  1747. return isHit;
  1748. }
  1749. function isPointInPath$3(displayObject, position, isClipPath, isPointInPath, renderingPluginContext, runtime) {
  1750. var _displayObject$parsed = displayObject.parsedStyle,
  1751. lineWidth = _displayObject$parsed.lineWidth,
  1752. increasedLineWidthForHitTesting = _displayObject$parsed.increasedLineWidthForHitTesting,
  1753. stroke = _displayObject$parsed.stroke,
  1754. fill = _displayObject$parsed.fill,
  1755. _displayObject$parsed2 = _displayObject$parsed.defX,
  1756. x = _displayObject$parsed2 === void 0 ? 0 : _displayObject$parsed2,
  1757. _displayObject$parsed3 = _displayObject$parsed.defY,
  1758. y = _displayObject$parsed3 === void 0 ? 0 : _displayObject$parsed3,
  1759. path = _displayObject$parsed.path,
  1760. pointerEvents = _displayObject$parsed.pointerEvents;
  1761. var segments = path.segments,
  1762. hasArc = path.hasArc,
  1763. polylines = path.polylines,
  1764. polygons = path.polygons;
  1765. var _isFillOrStrokeAffect = gLite.isFillOrStrokeAffected(pointerEvents,
  1766. // Only a closed path can be filled.
  1767. (polygons === null || polygons === void 0 ? void 0 : polygons.length) && fill, stroke),
  1768. hasFill = _isFillOrStrokeAffect[0],
  1769. hasStroke = _isFillOrStrokeAffect[1];
  1770. var totalLength = gLite.getOrCalculatePathTotalLength(displayObject);
  1771. var isHit = false;
  1772. if (hasFill || isClipPath) {
  1773. if (hasArc) {
  1774. // 存在曲线时,暂时使用 canvas 的 api 计算,后续可以进行多边形切割
  1775. isHit = isPointInPath(displayObject, position);
  1776. } else {
  1777. // 提取出来的多边形包含闭合的和非闭合的,在这里统一按照多边形处理
  1778. isHit = inPolygons(polygons, position.x + x, position.y + y) || inPolygons(polylines, position.x + x, position.y + y);
  1779. }
  1780. return isHit;
  1781. } else if (hasStroke || isClipPath) {
  1782. isHit = isPointInStroke(segments, (lineWidth || 0) + (increasedLineWidthForHitTesting || 0), position.x + x, position.y + y, totalLength);
  1783. }
  1784. return isHit;
  1785. }
  1786. function isPointInPath$4(displayObject, position, isClipPath) {
  1787. var _displayObject$parsed = displayObject.parsedStyle,
  1788. stroke = _displayObject$parsed.stroke,
  1789. fill = _displayObject$parsed.fill,
  1790. lineWidth = _displayObject$parsed.lineWidth,
  1791. increasedLineWidthForHitTesting = _displayObject$parsed.increasedLineWidthForHitTesting,
  1792. points = _displayObject$parsed.points,
  1793. _displayObject$parsed2 = _displayObject$parsed.defX,
  1794. x = _displayObject$parsed2 === void 0 ? 0 : _displayObject$parsed2,
  1795. _displayObject$parsed3 = _displayObject$parsed.defY,
  1796. y = _displayObject$parsed3 === void 0 ? 0 : _displayObject$parsed3,
  1797. pointerEvents = _displayObject$parsed.pointerEvents;
  1798. var _isFillOrStrokeAffect = gLite.isFillOrStrokeAffected(pointerEvents, fill, stroke),
  1799. hasFill = _isFillOrStrokeAffect[0],
  1800. hasStroke = _isFillOrStrokeAffect[1];
  1801. var isHit = false;
  1802. if (hasStroke || isClipPath) {
  1803. isHit = inPolyline(points.points, (lineWidth || 0) + (increasedLineWidthForHitTesting || 0), position.x + x, position.y + y, true);
  1804. }
  1805. if (!isHit && (hasFill || isClipPath)) {
  1806. isHit = inPolygon(points.points, position.x + x, position.y + y);
  1807. }
  1808. return isHit;
  1809. }
  1810. function isPointInPath$5(displayObject, position, isClipPath) {
  1811. var _displayObject$parsed = displayObject.parsedStyle,
  1812. lineWidth = _displayObject$parsed.lineWidth,
  1813. increasedLineWidthForHitTesting = _displayObject$parsed.increasedLineWidthForHitTesting,
  1814. points = _displayObject$parsed.points,
  1815. _displayObject$parsed2 = _displayObject$parsed.defX,
  1816. x = _displayObject$parsed2 === void 0 ? 0 : _displayObject$parsed2,
  1817. _displayObject$parsed3 = _displayObject$parsed.defY,
  1818. y = _displayObject$parsed3 === void 0 ? 0 : _displayObject$parsed3,
  1819. pointerEvents = _displayObject$parsed.pointerEvents,
  1820. fill = _displayObject$parsed.fill,
  1821. stroke = _displayObject$parsed.stroke;
  1822. var _isFillOrStrokeAffect = gLite.isFillOrStrokeAffected(pointerEvents, fill, stroke),
  1823. hasStroke = _isFillOrStrokeAffect[1];
  1824. if (!hasStroke && !isClipPath || !lineWidth) {
  1825. return false;
  1826. }
  1827. return inPolyline(points.points, (lineWidth || 0) + (increasedLineWidthForHitTesting || 0), position.x + x, position.y + y, false);
  1828. }
  1829. function isPointInPath$6(displayObject, position, isClipPath, isPointInPath, runtime) {
  1830. var _displayObject$parsed = displayObject.parsedStyle,
  1831. radius = _displayObject$parsed.radius,
  1832. fill = _displayObject$parsed.fill,
  1833. stroke = _displayObject$parsed.stroke,
  1834. lineWidth = _displayObject$parsed.lineWidth,
  1835. increasedLineWidthForHitTesting = _displayObject$parsed.increasedLineWidthForHitTesting,
  1836. width = _displayObject$parsed.width,
  1837. height = _displayObject$parsed.height,
  1838. pointerEvents = _displayObject$parsed.pointerEvents;
  1839. var _isFillOrStrokeAffect = gLite.isFillOrStrokeAffected(pointerEvents, fill, stroke),
  1840. hasFill = _isFillOrStrokeAffect[0],
  1841. hasStroke = _isFillOrStrokeAffect[1];
  1842. var hasRadius = radius && radius.some(function (r) {
  1843. return r !== 0;
  1844. });
  1845. var lineWidthForHitTesting = (lineWidth || 0) + (increasedLineWidthForHitTesting || 0);
  1846. // 无圆角时的策略
  1847. if (!hasRadius) {
  1848. var halfWidth = lineWidthForHitTesting / 2;
  1849. // 同时填充和带有边框
  1850. if (hasFill && hasStroke || isClipPath) {
  1851. return inBox(0 - halfWidth, 0 - halfWidth, width + halfWidth, height + halfWidth, position.x, position.y);
  1852. }
  1853. // 仅填充
  1854. if (hasFill) {
  1855. return inBox(0, 0, width, height, position.x, position.y);
  1856. }
  1857. if (hasStroke) {
  1858. return inRect(0, 0, width, height, lineWidthForHitTesting, position.x, position.y);
  1859. }
  1860. } else {
  1861. var isHit = false;
  1862. if (hasStroke || isClipPath) {
  1863. isHit = inRectWithRadius(0, 0, width, height, radius.map(function (r) {
  1864. return clamp(r, 0, Math.min(Math.abs(width) / 2, Math.abs(height) / 2));
  1865. }), lineWidthForHitTesting, position.x, position.y);
  1866. }
  1867. // 仅填充时带有圆角的矩形直接通过图形拾取
  1868. // 以后可以改成纯数学的近似拾取,将圆弧切割成多边形
  1869. if (!isHit && (hasFill || isClipPath)) {
  1870. isHit = isPointInPath(displayObject, position);
  1871. }
  1872. return isHit;
  1873. }
  1874. return false;
  1875. }
  1876. function inRectWithRadius(minX, minY, width, height, radiusArray, lineWidth, x, y) {
  1877. var tlr = radiusArray[0],
  1878. trr = radiusArray[1],
  1879. brr = radiusArray[2],
  1880. blr = radiusArray[3];
  1881. return inLine(minX + tlr, minY, minX + width - trr, minY, lineWidth, x, y) || inLine(minX + width, minY + trr, minX + width, minY + height - brr, lineWidth, x, y) || inLine(minX + width - brr, minY + height, minX + blr, minY + height, lineWidth, x, y) || inLine(minX, minY + height - blr, minX, minY + tlr, lineWidth, x, y) || inArc(minX + width - trr, minY + trr, trr, 1.5 * Math.PI, 2 * Math.PI, lineWidth, x, y) || inArc(minX + width - brr, minY + height - brr, brr, 0, 0.5 * Math.PI, lineWidth, x, y) || inArc(minX + blr, minY + height - blr, blr, 0.5 * Math.PI, Math.PI, lineWidth, x, y) || inArc(minX + tlr, minY + tlr, tlr, Math.PI, 1.5 * Math.PI, lineWidth, x, y);
  1882. }
  1883. function isPointInPath$7(displayObject, position, isClipPath, isPointInPath, renderingPluginContext, runtime) {
  1884. var _displayObject$parsed = displayObject.parsedStyle,
  1885. pointerEvents = _displayObject$parsed.pointerEvents,
  1886. width = _displayObject$parsed.width,
  1887. height = _displayObject$parsed.height;
  1888. if (pointerEvents === 'non-transparent-pixel') {
  1889. var offscreenCanvas = renderingPluginContext.config.offscreenCanvas;
  1890. var canvas = runtime.offscreenCanvas.getOrCreateCanvas(offscreenCanvas);
  1891. var context = runtime.offscreenCanvas.getOrCreateContext(offscreenCanvas, {
  1892. willReadFrequently: true
  1893. });
  1894. canvas.width = width;
  1895. canvas.height = height;
  1896. renderingPluginContext.defaultStyleRendererFactory[gLite.Shape.IMAGE].render(context, displayObject.parsedStyle, displayObject, undefined, undefined, undefined);
  1897. var imagedata = context.getImageData(position.x, position.y, 1, 1).data;
  1898. return imagedata.every(function (component) {
  1899. return component !== 0;
  1900. });
  1901. }
  1902. return true;
  1903. }
  1904. var Plugin = /*#__PURE__*/function (_AbstractRendererPlug) {
  1905. _inheritsLoose(Plugin, _AbstractRendererPlug);
  1906. function Plugin() {
  1907. var _this;
  1908. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  1909. args[_key] = arguments[_key];
  1910. }
  1911. _this = _AbstractRendererPlug.call.apply(_AbstractRendererPlug, [this].concat(args)) || this;
  1912. _this.name = 'canvas-picker';
  1913. return _this;
  1914. }
  1915. var _proto = Plugin.prototype;
  1916. _proto.init = function init() {
  1917. var _pointInPathPickerFac;
  1918. var trueFunc = function trueFunc() {
  1919. return true;
  1920. };
  1921. var pointInPathPickerFactory = (_pointInPathPickerFac = {}, _pointInPathPickerFac[gLite.Shape.CIRCLE] = isPointInPath, _pointInPathPickerFac[gLite.Shape.ELLIPSE] = isPointInPath$1, _pointInPathPickerFac[gLite.Shape.RECT] = isPointInPath$6, _pointInPathPickerFac[gLite.Shape.LINE] = isPointInPath$2, _pointInPathPickerFac[gLite.Shape.POLYLINE] = isPointInPath$5, _pointInPathPickerFac[gLite.Shape.POLYGON] = isPointInPath$4, _pointInPathPickerFac[gLite.Shape.PATH] = isPointInPath$3, _pointInPathPickerFac[gLite.Shape.TEXT] = trueFunc, _pointInPathPickerFac[gLite.Shape.GROUP] = null, _pointInPathPickerFac[gLite.Shape.IMAGE] = isPointInPath$7, _pointInPathPickerFac[gLite.Shape.HTML] = null, _pointInPathPickerFac[gLite.Shape.MESH] = null, _pointInPathPickerFac);
  1922. // @ts-ignore
  1923. this.context.pointInPathPickerFactory = pointInPathPickerFactory;
  1924. this.addRenderingPlugin(new CanvasPickerPlugin());
  1925. };
  1926. _proto.destroy = function destroy() {
  1927. // @ts-ignore
  1928. delete this.context.pointInPathPickerFactory;
  1929. this.removeAllRenderingPlugins();
  1930. };
  1931. return Plugin;
  1932. }(gLite.AbstractRendererPlugin);
  1933. exports.Plugin = Plugin;
  1934. Object.defineProperty(exports, '__esModule', { value: true });
  1935. })));