l-f2.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <template>
  2. <view class="l-f2" :style="customStyle" v-if="canvasId">
  3. <!-- #ifndef APP-NVUE -->
  4. <cover-view class="l-f2__mask" v-if="isMask"></cover-view>
  5. <canvas
  6. class="l-f2__canvas"
  7. v-if="use2dCanvas"
  8. type="2d"
  9. :id="canvasId"
  10. :style="'width:' + width + 'px;height:' + height + 'px'"
  11. :disable-scroll="isDisableScroll"
  12. @touchstart="touchStart"
  13. @touchmove="touchMove"
  14. @touchend="touchEnd"
  15. />
  16. <canvas
  17. class="l-f2__canvas"
  18. v-else
  19. :width="nodeWidth"
  20. :height="nodeHeight"
  21. :style="'width:' + width + 'px;height:' + height + 'px'"
  22. :canvas-id="canvasId"
  23. :id="canvasId"
  24. :disable-scroll="isDisableScroll"
  25. @touchstart="touchStart"
  26. @touchmove="touchMove"
  27. @touchend="touchEnd"
  28. />
  29. <view v-if="isCloud" style="width:2048px; height:2048px; position: fixed; left: 9999px;">
  30. <canvas v-if="use2dCanvas" type="2d" :canvas-id="canvasId + '_cloud'" :id="canvasId + '_cloud'" class="l-f2__canvas"></canvas>
  31. <canvas v-else :canvas-id="canvasId + '_cloud'" :id="canvasId + '_cloud'" class="l-f2__canvas"></canvas>
  32. </view>
  33. <!-- #endif -->
  34. <!-- #ifdef APP-NVUE -->
  35. <web-view
  36. class="l-f2__canvas"
  37. :id="canvasId"
  38. ref="webview"
  39. :webviewStyles="webviewStyles"
  40. src="http://liangei.gitee.io/limeui/hybrid/html/lime-ui/lime-f2/index.html?v=0.4.8"
  41. @pagefinish="isFinish = true"
  42. @onPostMessage="onMessage"
  43. ></web-view>
  44. <!-- #endif -->
  45. </view>
  46. </template>
  47. <script>
  48. // #ifndef APP-NVUE
  49. import extendContext from './canvas';
  50. import { compareVersion, wrapEvent, pixelRatio } from './utils';
  51. // #endif
  52. // #ifdef APP-NVUE
  53. import { base64ToPath } from './utils';
  54. // #endif
  55. export default {
  56. // version: '0.5.1'
  57. name: 'lime-f2',
  58. props: {
  59. // #ifdef MP-WEIXIN || MP-TOUTIAO
  60. type: {
  61. type: String,
  62. default: '2d'
  63. },
  64. // #endif
  65. // #ifdef APP-NVUE
  66. webviewStyles: Object,
  67. params: {
  68. type: Object,
  69. default: () => {}
  70. },
  71. // #endif
  72. customStyle: String,
  73. imageMask: String,
  74. source: {
  75. type: Array,
  76. default: () => []
  77. },
  78. isAutoPlay: Boolean,
  79. isDisableScroll: Boolean,
  80. isCloud: Boolean,
  81. onInit: {
  82. type: [Function, Object],
  83. default: () => {}
  84. }
  85. },
  86. data() {
  87. return {
  88. // #ifdef MP-WEIXIN || MP-TOUTIAO
  89. use2dCanvas: true,
  90. // #endif
  91. // #ifndef MP-WEIXIN || MP-TOUTIAO
  92. use2dCanvas: false,
  93. // #endif
  94. // #ifndef APP-NVUE
  95. width: null,
  96. height: null,
  97. nodeWidth: null,
  98. nodeHeight: null,
  99. isMask: false,
  100. isInited: false,
  101. imageData: null,
  102. // config: {},
  103. // #endif
  104. // #ifdef APP-NVUE
  105. isFinish: false,
  106. file: ''
  107. // #endif
  108. };
  109. },
  110. computed: {
  111. canvasId() {
  112. return `l-f2${this._uid}`;
  113. }
  114. },
  115. watch: {
  116. isAutoPlay(val) {
  117. if (val) {
  118. this.changeData(this.source);
  119. }
  120. },
  121. source: {
  122. handler: function(data) {
  123. if (this.isAutoPlay) {
  124. this.changeData(data);
  125. }
  126. },
  127. deep: true
  128. }
  129. },
  130. beforeDestroy() {
  131. this.clear();
  132. this.destroy();
  133. },
  134. created() {
  135. this.config = {}
  136. this.isMask = this.isCloud && this.imageMask;
  137. // #ifdef MP-WEIXIN || MP-TOUTIAO
  138. const { SDKVersion, version, platform, environment } = uni.getSystemInfoSync();
  139. // #endif
  140. // #ifdef MP-WEIXIN
  141. this.use2dCanvas = this.type === '2d' && compareVersion(SDKVersion, '2.9.2') >= 0 && !((/ios/i.test(platform) && /7.0.20/.test(version)) || /wxwork/i.test(environment)) && !/windows/i.test(platform);;
  142. // #endif
  143. // #ifdef MP-TOUTIAO
  144. this.use2dCanvas = this.type === '2d' && compareVersion(SDKVersion, '1.78.0') >= 0;
  145. // #endif
  146. },
  147. async mounted() {
  148. if (this.onInit) {
  149. this.init(this.onInit);
  150. }
  151. },
  152. methods: {
  153. // #ifdef APP-NVUE
  154. onMessage(e) {
  155. const res = e?.detail?.data[0] || null;
  156. if (res?.event) {
  157. this.$emit(res.event, JSON.parse(res.data));
  158. } else if (res?.file) {
  159. this.file = res.data;
  160. } else {
  161. console.error(res);
  162. }
  163. },
  164. // #endif
  165. async init(func, params = null) {
  166. // #ifdef APP-NVUE
  167. this.$watch(
  168. 'isFinish',
  169. (n, o) => {
  170. (n || o) && (params || this.params) && this.$refs.webview.evalJs(`init(${JSON.stringify(func.toString())}, ${JSON.stringify(params || this.params)})`);
  171. this.isInited = true;
  172. },
  173. {
  174. immediate: true
  175. }
  176. );
  177. // #endif
  178. // #ifndef APP-NVUE
  179. let config = await this.getContext(this.canvasId);
  180. if (this.isCloud) {
  181. let imageMask = null;
  182. if (this.imageMask) {
  183. this.isMask = true;
  184. imageMask = await this.getImageMask(config);
  185. this.imageData = imageMask;
  186. this.isMask = false;
  187. }
  188. let cloud = await this.getContext(this.canvasId + '_cloud');
  189. config = Object.assign({}, config, { cloud, imageMask });
  190. }
  191. const chart = await func(config);
  192. if (chart) {
  193. this.chart = chart;
  194. this.canvasEl = chart.get('el');
  195. this.isInited = true;
  196. }
  197. // #endif
  198. },
  199. changeData(data) {
  200. // #ifndef APP-NVUE
  201. if (this.chart) {
  202. this.chart.changeData(data || this.source);
  203. }
  204. // #endif
  205. // #ifdef APP-NVUE
  206. this.$refs.webview.evalJs(`changeData(${JSON.stringify(data || this.source)})`);
  207. // #endif
  208. },
  209. clear() {
  210. // #ifndef APP-NVUE
  211. if (this.chart) {
  212. this.chart.clear();
  213. }
  214. // #endif
  215. // #ifdef APP-NVUE
  216. this.$refs.webview.evalJs(`clear()`);
  217. // #endif
  218. },
  219. destroy() {
  220. // #ifndef APP-NVUE
  221. if (this.chart) {
  222. this.chart.destroy();
  223. }
  224. // #endif
  225. // #ifdef APP-NVUE
  226. this.$refs.webview.evalJs(`destroy()`);
  227. // #endif
  228. },
  229. repaint() {
  230. this.changeData(this.source);
  231. },
  232. reset(func, params = null) {
  233. // #ifndef APP-NVUE
  234. this.$watch(
  235. 'isInited',
  236. v => v && func(this.chart),
  237. {
  238. immediate: true
  239. }
  240. );
  241. // #endif
  242. // #ifdef APP-NVUE
  243. this.$refs.webview.evalJs(`reset(${JSON.stringify(func.toString())}, ${JSON.stringify(params || this.params)})`);
  244. // #endif
  245. },
  246. canvasToTempFilePath(args = {}) {
  247. // #ifndef APP-NVUE
  248. const { use2dCanvas, canvasId, config } = this;
  249. return new Promise((resolve, reject) => {
  250. const copyArgs = Object.assign(
  251. {
  252. canvasId,
  253. success: resolve,
  254. fail: reject
  255. },
  256. args
  257. );
  258. if (use2dCanvas) {
  259. let { canvas } = config[canvasId];
  260. delete copyArgs.canvasId;
  261. copyArgs.canvas = canvas;
  262. }
  263. uni.canvasToTempFilePath(copyArgs, this);
  264. });
  265. // #endif
  266. // #ifdef APP-NVUE
  267. this.file = '';
  268. this.$refs.webview.evalJs(`canvasToTempFilePath()`);
  269. return new Promise((resolve, reject) => {
  270. this.$watch('file', async file => {
  271. if (file) {
  272. const tempFilePath = await base64ToPath(file);
  273. resolve(args.success({ tempFilePath }));
  274. } else {
  275. reject(args.fail({ error: `` }));
  276. }
  277. });
  278. });
  279. // #endif
  280. },
  281. // #ifndef APP-NVUE
  282. getImageMask(config) {
  283. return new Promise(resolve => {
  284. uni.getImageInfo({
  285. src: this.imageMask,
  286. success: async res => {
  287. if (res.path) {
  288. // #ifdef MP-WEIXIN || MP-BAIDU || MP-QQ || MP-TOUTIAO
  289. const localReg = /^\.|^\/(?=[^\/])/;
  290. res.path = localReg.test(this.imageMask) ? `/${res.path}` : res.path;
  291. // #endif
  292. const { context, width, height, canvas } = config;
  293. if (this.use2dCanvas) {
  294. const imageMask = () => {
  295. const imageMask = canvas.createImage();
  296. imageMask.crossOrigin = '';
  297. imageMask.src = res.path;
  298. imageMask.onload = async () => {
  299. context.drawImage(imageMask, 0, 0, res.width, res.height, 0, 0, width, height);
  300. const imageData = context.getImageData(0, 0, width, height).data;
  301. context.clearRect(0, 0, width, height);
  302. resolve(imageData);
  303. };
  304. };
  305. imageMask();
  306. } else {
  307. // #ifndef MP-BAIDU
  308. context.drawImage(res.path, 0, 0, res.width, res.height, 0, 0, width, height);
  309. // #endif
  310. // #ifdef MP-BAIDU
  311. context.drawImage(res.path, 0, 0, width, height, 0, 0, res.width, res.height);
  312. // #endif
  313. await this.canvasDraw(context);
  314. const imageData = await context.getImageData(0, 0, width, height);
  315. context.clearRect(0, 0, width, height);
  316. await this.canvasDraw(context);
  317. resolve(imageData);
  318. }
  319. }
  320. },
  321. fail(err) {
  322. console.error(JSON.stringify(err));
  323. resolve(null);
  324. }
  325. });
  326. });
  327. },
  328. canvasDraw(ctx) {
  329. return new Promise(resolve => {
  330. ctx.draw(false, () => {
  331. setTimeout(() => {
  332. resolve(true);
  333. }, 100);
  334. });
  335. });
  336. },
  337. getContext(canvasId) {
  338. const { use2dCanvas, type = '2d', config } = this;
  339. if (config[canvasId]?.context) {
  340. return Promise.resolve(config[canvasId]);
  341. }
  342. if (use2dCanvas) {
  343. return new Promise(resolve => {
  344. uni.createSelectorQuery()
  345. .in(this)
  346. .select(`#${canvasId}`)
  347. .fields({
  348. node: true,
  349. size: true
  350. })
  351. .exec(res => {
  352. let { node, width, height } = res[0];
  353. width = width || 300;
  354. height = height || 300;
  355. const context = node.getContext(type);
  356. if (!canvasId.includes('_cloud')) {
  357. this.width = width;
  358. this.height = height;
  359. }
  360. node.width = width * pixelRatio;
  361. node.height = height * pixelRatio;
  362. this.config[canvasId] = { context, width, height, pixelRatio, canvas: node };
  363. resolve(this.config[canvasId]);
  364. });
  365. });
  366. }
  367. return new Promise(resolve => {
  368. uni.createSelectorQuery()
  369. .in(this)
  370. .select(`#${canvasId}`)
  371. .boundingClientRect()
  372. .exec(res => {
  373. if (res) {
  374. let { width, height } = res[0];
  375. width = width || 300;
  376. height = height || 300;
  377. const context = uni.createCanvasContext(canvasId, this);
  378. if (!canvasId.includes('_cloud')) {
  379. this.width = width;
  380. this.height = height;
  381. // #ifdef MP-ALIPAY
  382. this.nodeWidth = width * pixelRatio;
  383. this.nodeHeight = height * pixelRatio;
  384. // #endif
  385. }
  386. this.config[canvasId] = {
  387. context: extendContext(context),
  388. width,
  389. height,
  390. // #ifdef H5 || APP-PLUS
  391. pixelRatio: 1,
  392. oPixelRatio: pixelRatio,
  393. // #endif
  394. // #ifndef H5
  395. pixelRatio
  396. // #endif
  397. };
  398. resolve(this.config[canvasId]);
  399. }
  400. });
  401. });
  402. },
  403. touchStart(e) {
  404. if (this.canvasEl) {
  405. this.canvasEl.dispatchEvent('touchstart', wrapEvent(e));
  406. }
  407. },
  408. touchMove(e) {
  409. if (this.canvasEl) {
  410. this.canvasEl.dispatchEvent('touchmove', wrapEvent(e));
  411. }
  412. },
  413. touchEnd(e) {
  414. if (this.canvasEl) {
  415. this.canvasEl.dispatchEvent('touchend', wrapEvent(e));
  416. }
  417. }
  418. // #endif
  419. }
  420. };
  421. </script>
  422. <style scoped lang="stylus">
  423. full()
  424. // #ifndef APP-NVUE
  425. width 100%
  426. height 100%
  427. // #endif
  428. // #ifdef APP-NVUE
  429. flex 1
  430. // #endif
  431. .l-f2
  432. full()
  433. position relative
  434. &__mask
  435. position absolute
  436. left 0
  437. right 0
  438. bottom 0
  439. top 0
  440. background-color #fff
  441. z-index 1
  442. &__canvas
  443. full()
  444. </style>