index.umd.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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.ImageLoader = {}), global.window.G));
  5. }(this, (function (exports, gLite) { 'use strict';
  6. function _inheritsLoose(subClass, superClass) {
  7. subClass.prototype = Object.create(superClass.prototype);
  8. subClass.prototype.constructor = subClass;
  9. _setPrototypeOf(subClass, superClass);
  10. }
  11. function _setPrototypeOf(o, p) {
  12. _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
  13. o.__proto__ = p;
  14. return o;
  15. };
  16. return _setPrototypeOf(o, p);
  17. }
  18. /**
  19. * Common utilities
  20. * @module glMatrix
  21. */
  22. var ARRAY_TYPE = typeof Float32Array !== 'undefined' ? Float32Array : Array;
  23. if (!Math.hypot) Math.hypot = function () {
  24. var y = 0,
  25. i = arguments.length;
  26. while (i--) {
  27. y += arguments[i] * arguments[i];
  28. }
  29. return Math.sqrt(y);
  30. };
  31. /**
  32. * 4x4 Matrix<br>Format: column-major, when typed out it looks like row-major<br>The matrices are being post multiplied.
  33. * @module mat4
  34. */
  35. /**
  36. * Creates a new identity mat4
  37. *
  38. * @returns {mat4} a new 4x4 matrix
  39. */
  40. function create() {
  41. var out = new ARRAY_TYPE(16);
  42. if (ARRAY_TYPE != Float32Array) {
  43. out[1] = 0;
  44. out[2] = 0;
  45. out[3] = 0;
  46. out[4] = 0;
  47. out[6] = 0;
  48. out[7] = 0;
  49. out[8] = 0;
  50. out[9] = 0;
  51. out[11] = 0;
  52. out[12] = 0;
  53. out[13] = 0;
  54. out[14] = 0;
  55. }
  56. out[0] = 1;
  57. out[5] = 1;
  58. out[10] = 1;
  59. out[15] = 1;
  60. return out;
  61. }
  62. /**
  63. * Set a mat4 to the identity matrix
  64. *
  65. * @param {mat4} out the receiving matrix
  66. * @returns {mat4} out
  67. */
  68. function identity(out) {
  69. out[0] = 1;
  70. out[1] = 0;
  71. out[2] = 0;
  72. out[3] = 0;
  73. out[4] = 0;
  74. out[5] = 1;
  75. out[6] = 0;
  76. out[7] = 0;
  77. out[8] = 0;
  78. out[9] = 0;
  79. out[10] = 1;
  80. out[11] = 0;
  81. out[12] = 0;
  82. out[13] = 0;
  83. out[14] = 0;
  84. out[15] = 1;
  85. return out;
  86. }
  87. /**
  88. * Scales the mat4 by the dimensions in the given vec3 not using vectorization
  89. *
  90. * @param {mat4} out the receiving matrix
  91. * @param {ReadonlyMat4} a the matrix to scale
  92. * @param {ReadonlyVec3} v the vec3 to scale the matrix by
  93. * @returns {mat4} out
  94. **/
  95. function scale(out, a, v) {
  96. var x = v[0],
  97. y = v[1],
  98. z = v[2];
  99. out[0] = a[0] * x;
  100. out[1] = a[1] * x;
  101. out[2] = a[2] * x;
  102. out[3] = a[3] * x;
  103. out[4] = a[4] * y;
  104. out[5] = a[5] * y;
  105. out[6] = a[6] * y;
  106. out[7] = a[7] * y;
  107. out[8] = a[8] * z;
  108. out[9] = a[9] * z;
  109. out[10] = a[10] * z;
  110. out[11] = a[11] * z;
  111. out[12] = a[12];
  112. out[13] = a[13];
  113. out[14] = a[14];
  114. out[15] = a[15];
  115. return out;
  116. }
  117. var toString = {}.toString;
  118. var isType = function (value, type) { return toString.call(value) === '[object ' + type + ']'; };
  119. var isString = (function (str) {
  120. return isType(str, 'String');
  121. });
  122. var ImagePool = /*#__PURE__*/function () {
  123. function ImagePool(canvasConfig) {
  124. this.canvasConfig = void 0;
  125. this.imageCache = {};
  126. this.gradientCache = {};
  127. this.patternCache = {};
  128. this.canvasConfig = canvasConfig;
  129. }
  130. var _proto = ImagePool.prototype;
  131. _proto.getImageSync = function getImageSync(src, callback) {
  132. if (!this.imageCache[src]) {
  133. this.getOrCreateImage(src).then(function () {
  134. if (callback) {
  135. callback();
  136. }
  137. });
  138. } else {
  139. if (callback) {
  140. callback();
  141. }
  142. }
  143. return this.imageCache[src];
  144. };
  145. _proto.getOrCreateImage = function getOrCreateImage(src) {
  146. var _this = this;
  147. if (this.imageCache[src]) {
  148. return Promise.resolve(this.imageCache[src]);
  149. }
  150. // @see https://github.com/antvis/g/issues/938
  151. var createImage = this.canvasConfig.createImage;
  152. return new Promise(function (resolve, reject) {
  153. var image;
  154. if (createImage) {
  155. image = createImage(src);
  156. } else if (gLite.isBrowser) {
  157. image = new window.Image();
  158. }
  159. if (image) {
  160. image.onload = function () {
  161. _this.imageCache[src] = image;
  162. resolve(image);
  163. };
  164. image.onerror = function (ev) {
  165. reject(ev);
  166. };
  167. image.crossOrigin = 'Anonymous';
  168. image.src = src;
  169. }
  170. });
  171. };
  172. _proto.getOrCreatePatternSync = function getOrCreatePatternSync(pattern, context, $offscreenCanvas, dpr, callback) {
  173. var patternKey = this.generatePatternKey(pattern);
  174. if (patternKey && this.patternCache[patternKey]) {
  175. return this.patternCache[patternKey];
  176. }
  177. var image = pattern.image,
  178. repetition = pattern.repetition,
  179. transform = pattern.transform;
  180. var src;
  181. var needScaleWithDPR = false;
  182. // Image URL
  183. if (isString(image)) {
  184. src = this.getImageSync(image, callback);
  185. } else if ($offscreenCanvas) {
  186. src = $offscreenCanvas;
  187. needScaleWithDPR = true;
  188. } else {
  189. src = image;
  190. }
  191. // @see https://developer.mozilla.org/zh-CN/docs/Web/API/CanvasRenderingContext2D/createPattern
  192. var canvasPattern = src && context.createPattern(src, repetition);
  193. if (canvasPattern) {
  194. var mat;
  195. // @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasPattern/setTransform
  196. if (transform) {
  197. mat = gLite.parsedTransformToMat4(gLite.parseTransform(transform));
  198. } else {
  199. mat = identity(create());
  200. }
  201. if (needScaleWithDPR) {
  202. scale(mat, mat, [1 / dpr, 1 / dpr, 1]);
  203. }
  204. canvasPattern.setTransform({
  205. a: mat[0],
  206. b: mat[1],
  207. c: mat[4],
  208. d: mat[5],
  209. e: mat[12],
  210. f: mat[13]
  211. });
  212. }
  213. if (patternKey && canvasPattern) {
  214. this.patternCache[patternKey] = canvasPattern;
  215. }
  216. return canvasPattern;
  217. };
  218. _proto.getOrCreateGradient = function getOrCreateGradient(params, context) {
  219. var key = this.generateGradientKey(params);
  220. var type = params.type,
  221. steps = params.steps,
  222. width = params.width,
  223. height = params.height,
  224. angle = params.angle,
  225. cx = params.cx,
  226. cy = params.cy,
  227. size = params.size;
  228. if (this.gradientCache[key]) {
  229. return this.gradientCache[key];
  230. }
  231. var gradient = null;
  232. if (type === gLite.GradientType.LinearGradient) {
  233. var _computeLinearGradien = gLite.computeLinearGradient(width, height, angle),
  234. x1 = _computeLinearGradien.x1,
  235. y1 = _computeLinearGradien.y1,
  236. x2 = _computeLinearGradien.x2,
  237. y2 = _computeLinearGradien.y2;
  238. // @see https://developer.mozilla.org/zh-CN/docs/Web/API/CanvasRenderingContext2D/createLinearGradient
  239. gradient = context.createLinearGradient(x1, y1, x2, y2);
  240. } else if (type === gLite.GradientType.RadialGradient) {
  241. var _computeRadialGradien = gLite.computeRadialGradient(width, height, cx, cy, size),
  242. x = _computeRadialGradien.x,
  243. y = _computeRadialGradien.y,
  244. r = _computeRadialGradien.r;
  245. // @see https://developer.mozilla.org/zh-CN/docs/Web/API/CanvasRenderingContext2D/createRadialGradient
  246. gradient = context.createRadialGradient(x, y, 0, x, y, r);
  247. }
  248. if (gradient) {
  249. steps.forEach(function (_ref) {
  250. var offset = _ref.offset,
  251. color = _ref.color;
  252. if (offset.unit === gLite.UnitType.kPercentage) {
  253. var _gradient;
  254. (_gradient = gradient) === null || _gradient === void 0 ? void 0 : _gradient.addColorStop(offset.value / 100, color.toString());
  255. }
  256. });
  257. this.gradientCache[key] = gradient;
  258. }
  259. return this.gradientCache[key];
  260. };
  261. _proto.generateGradientKey = function generateGradientKey(params) {
  262. var type = params.type,
  263. width = params.width,
  264. height = params.height,
  265. steps = params.steps,
  266. angle = params.angle,
  267. cx = params.cx,
  268. cy = params.cy,
  269. size = params.size;
  270. return "gradient-" + type + "-" + ((angle === null || angle === void 0 ? void 0 : angle.toString()) || 0) + "-" + ((cx === null || cx === void 0 ? void 0 : cx.toString()) || 0) + "-" + ((cy === null || cy === void 0 ? void 0 : cy.toString()) || 0) + "-" + ((size === null || size === void 0 ? void 0 : size.toString()) || 0) + "-" + width + "-" + height + "-" + steps.map(function (_ref2) {
  271. var offset = _ref2.offset,
  272. color = _ref2.color;
  273. return "" + offset + color;
  274. }).join('-');
  275. };
  276. _proto.generatePatternKey = function generatePatternKey(pattern) {
  277. var image = pattern.image,
  278. repetition = pattern.repetition;
  279. // only generate cache for Image
  280. if (isString(image)) {
  281. return "pattern-" + image + "-" + repetition;
  282. } else if (image.nodeName === 'rect') {
  283. return "pattern-" + image.entity + "-" + repetition;
  284. }
  285. };
  286. return ImagePool;
  287. }();
  288. var LoadImagePlugin = /*#__PURE__*/function () {
  289. function LoadImagePlugin() {}
  290. var _proto = LoadImagePlugin.prototype;
  291. _proto.apply = function apply(context) {
  292. // @ts-ignore
  293. var renderingService = context.renderingService,
  294. renderingContext = context.renderingContext,
  295. imagePool = context.imagePool;
  296. var canvas = renderingContext.root.ownerDocument.defaultView;
  297. var handleMounted = function handleMounted(e) {
  298. var object = e.target;
  299. var nodeName = object.nodeName,
  300. attributes = object.attributes;
  301. if (nodeName === gLite.Shape.IMAGE) {
  302. var img = attributes.img;
  303. if (isString(img)) {
  304. imagePool.getImageSync(img, function () {
  305. // set dirty rectangle flag
  306. object.renderable.dirty = true;
  307. renderingService.dirtify();
  308. });
  309. }
  310. }
  311. };
  312. var handleAttributeChanged = function handleAttributeChanged(e) {
  313. var object = e.target;
  314. var attrName = e.attrName,
  315. newValue = e.newValue;
  316. if (object.nodeName === gLite.Shape.IMAGE) {
  317. if (attrName === 'img') {
  318. if (isString(newValue)) {
  319. imagePool.getOrCreateImage(newValue).then(function () {
  320. // set dirty rectangle flag
  321. object.renderable.dirty = true;
  322. renderingService.dirtify();
  323. });
  324. }
  325. }
  326. }
  327. };
  328. renderingService.hooks.init.tap(LoadImagePlugin.tag, function () {
  329. canvas.addEventListener(gLite.ElementEvent.MOUNTED, handleMounted);
  330. canvas.addEventListener(gLite.ElementEvent.ATTR_MODIFIED, handleAttributeChanged);
  331. });
  332. renderingService.hooks.destroy.tap(LoadImagePlugin.tag, function () {
  333. canvas.removeEventListener(gLite.ElementEvent.MOUNTED, handleMounted);
  334. canvas.removeEventListener(gLite.ElementEvent.ATTR_MODIFIED, handleAttributeChanged);
  335. });
  336. };
  337. return LoadImagePlugin;
  338. }();
  339. LoadImagePlugin.tag = 'LoadImage';
  340. var Plugin = /*#__PURE__*/function (_AbstractRendererPlug) {
  341. _inheritsLoose(Plugin, _AbstractRendererPlug);
  342. function Plugin() {
  343. var _this;
  344. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  345. args[_key] = arguments[_key];
  346. }
  347. _this = _AbstractRendererPlug.call.apply(_AbstractRendererPlug, [this].concat(args)) || this;
  348. _this.name = 'image-loader';
  349. return _this;
  350. }
  351. var _proto = Plugin.prototype;
  352. _proto.init = function init() {
  353. // @ts-ignore
  354. this.context.imagePool = new ImagePool(this.context.config);
  355. this.addRenderingPlugin(new LoadImagePlugin());
  356. };
  357. _proto.destroy = function destroy() {
  358. this.removeAllRenderingPlugins();
  359. };
  360. return Plugin;
  361. }(gLite.AbstractRendererPlugin);
  362. exports.ImagePool = ImagePool;
  363. exports.Plugin = Plugin;
  364. Object.defineProperty(exports, '__esModule', { value: true });
  365. })));