index.esm.js 11 KB

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