index.js 11 KB

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