index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. import Wxml2Canvas from '../../src/index';
  2. Page({
  3. data: {
  4. },
  5. onLoad (res) {
  6. this.drawImage1();
  7. this.drawImage2();
  8. this.drawImage3();
  9. this.drawImage4();
  10. this.drawImage5();
  11. this.drawImage6();
  12. this.drawImage7();
  13. this.drawImage8();
  14. },
  15. drawImage1 () {
  16. this.drawImage1 = new Wxml2Canvas({
  17. width: 340,
  18. height: 210,
  19. element: 'canvas1',
  20. gradientBackground: {
  21. color: ['#27326b', '#540821'],
  22. line: [0, 0, 0, 210]
  23. },
  24. finish(url) {
  25. },
  26. error (res) {
  27. }
  28. });
  29. },
  30. drawImage2 () {
  31. this.drawImage2 = new Wxml2Canvas({
  32. width: 340,
  33. height: 210,
  34. element: 'canvas2',
  35. background: '#f0f0f0',
  36. finish(url) {
  37. },
  38. error (res) {
  39. }
  40. });
  41. let data2 = {
  42. list: [{
  43. type: 'rect',
  44. x: 10,
  45. y: 10,
  46. style: {
  47. width: 150,
  48. height: 80,
  49. fill: {
  50. color: ['#27326b', '#540821'],
  51. line: [0, 0, 0, 110]
  52. },
  53. border: '10px solid #aaaaaa',
  54. }
  55. },{
  56. type: 'rect',
  57. x: 170,
  58. y: 100,
  59. style: {
  60. width: 150,
  61. height: 80,
  62. fill: '#3762ab',
  63. border: '10px dashed #540821',
  64. boxShadow: '10 20 20 rgba(0, 0, 0, 0.4)',
  65. // dash: [10, 20, 10]
  66. }
  67. }]
  68. }
  69. this.drawImage2.draw(data2);
  70. },
  71. drawImage3 () {
  72. this.drawImage3 = new Wxml2Canvas({
  73. width: 340,
  74. height: 210,
  75. element: 'canvas3',
  76. background: '#f0f0f0',
  77. finish(url) {
  78. },
  79. error (res) {
  80. }
  81. });
  82. let text = '一行很长很长很长很长很长很长很长很长很长很长的文字';
  83. let data3 = {
  84. list: [{
  85. type: 'text',
  86. text,
  87. x: 0,
  88. y: 0,
  89. style: {
  90. textAlign: 'left',
  91. width: 130,
  92. height: 80,
  93. fontSize: 14,
  94. lineHeight: 16,
  95. fontWeight: 'bold',
  96. boxShadow: '5 5 20 rgba(255, 0, 0, 0.5)',
  97. }
  98. }, {
  99. type: 'text',
  100. text,
  101. x: 170,
  102. y: 0,
  103. style: {
  104. textAlign: 'center',
  105. width: 130,
  106. fontSize: 14,
  107. lineHeight: 16,
  108. color: '#888888',
  109. }
  110. }, {
  111. type: 'text',
  112. text,
  113. x: 0,
  114. y: 70,
  115. style: {
  116. textAlign: 'right',
  117. width: 130,
  118. fontSize: 14,
  119. lineHeight: 16,
  120. fontFamily: 'Serif'
  121. }
  122. }, {
  123. type: 'text',
  124. text,
  125. x: 170,
  126. y: 70,
  127. style: {
  128. textAlign: 'center',
  129. width: 130,
  130. lineClamp: 2,
  131. fontSize: 14,
  132. lineHeight: 16,
  133. }
  134. }, {
  135. type: 'text',
  136. text,
  137. x: 0,
  138. y: 140,
  139. style: {
  140. textAlign: 'left',
  141. width: 130,
  142. fontSize: 14,
  143. lineHeight: 16,
  144. height: 60,
  145. background: 'rgba(0, 111, 255, 0.5)',
  146. color: '#ffffff'
  147. }
  148. }, {
  149. type: 'text',
  150. text,
  151. x: 170,
  152. y: 140,
  153. style: {
  154. textAlign: 'left',
  155. width: 130,
  156. background: 'rgba(255, 0, 0, 0.3)',
  157. fontSize: 14,
  158. lineHeight: 16,
  159. height: 60,
  160. border: '2px solid #000000',
  161. padding: '5 10 0 10'
  162. }
  163. }]
  164. }
  165. this.drawImage3.draw(data3);
  166. },
  167. drawImage4 () {
  168. this.drawImage4 = new Wxml2Canvas({
  169. width: 340,
  170. height: 210,
  171. element: 'canvas4',
  172. background: '#f0f0f0',
  173. finish(url) {
  174. },
  175. error (res) {
  176. }
  177. });
  178. let data4 = {
  179. list: [{
  180. type: 'circle',
  181. x: 30,
  182. y: 30,
  183. style: {
  184. r: 40,
  185. fill: {
  186. color: ['#27326b', '#540821'],
  187. line: [0, 0, 0, 110]
  188. },
  189. border: '10px solid #aaaaaa',
  190. boxShadow: '10 10 20 rgba(0, 0, 0, 0.6)',
  191. }
  192. },{
  193. type: 'circle',
  194. x: 200,
  195. y: 100,
  196. style: {
  197. r: 40,
  198. fill: '#3762ab',
  199. border: '10px dashed #540821',
  200. // dash: [10, 20, 10]
  201. }
  202. }]
  203. }
  204. this.drawImage4.draw(data4);
  205. },
  206. drawImage5 () {
  207. this.drawImage5 = new Wxml2Canvas({
  208. width: 340,
  209. height: 210,
  210. element: 'canvas5',
  211. background: '#f0f0f0',
  212. finish(url) {
  213. },
  214. error (res) {
  215. }
  216. });
  217. let data5 = {
  218. list: [{
  219. type: 'line',
  220. x: 30,
  221. y: 30,
  222. x2: 200,
  223. y2: 30,
  224. style: {
  225. width: 10,
  226. // stroke: '#3762ab',
  227. stroke: {
  228. color: ['#27326b', '#540821'],
  229. line: [0, 0, 200, 10]
  230. },
  231. }
  232. },{
  233. type: 'line',
  234. x: 30,
  235. y: 100,
  236. x2: 200,
  237. y2: 100,
  238. style: {
  239. r: 40,
  240. width: 10,
  241. stroke: '#3762ab',
  242. dash: [10, 10, 0],
  243. boxShadow: '20 20 20 rgba(0, 0, 0, 0.3)',
  244. }
  245. }]
  246. }
  247. this.drawImage5.draw(data5);
  248. },
  249. drawImage6 () {
  250. this.drawImage6 = new Wxml2Canvas({
  251. width: 340,
  252. height: 210,
  253. element: 'canvas6',
  254. background: '#f0f0f0',
  255. finish(url) {
  256. },
  257. error (res) {
  258. }
  259. });
  260. let data6 = {
  261. list: [{
  262. type: 'image',
  263. x: 10,
  264. y: 10,
  265. url: 'https://mmocgame.qpic.cn/wechatgame/duc2TvpEgSQCEa0WicosqlEgJiacYLaTLEDFB8Kq9Hr7LjAVhM8wu8TT77KqNezQnH/0',
  266. style: {
  267. width: 150,
  268. height: 80,
  269. border: '10px solid #aaaaaa',
  270. boxShadow: '10 20 20 rgba(0, 0, 0, 0.4)',
  271. }
  272. },{
  273. type: 'image',
  274. x: 170,
  275. y: 100,
  276. url: './img/demo.png',
  277. style: {
  278. width: 150,
  279. height: 80,
  280. border: '10px dashed #540821',
  281. // dash: [10, 20, 10]
  282. }
  283. }]
  284. }
  285. this.drawImage6.draw(data6);
  286. },
  287. drawImage7 () {
  288. this.drawImage7 = new Wxml2Canvas({
  289. width: 340,
  290. height: 210,
  291. element: 'canvas7',
  292. background: '#f0f0f0',
  293. finish(url) {
  294. },
  295. error (res) {
  296. }
  297. });
  298. let data7 = {
  299. list: [{
  300. type: 'radius-image',
  301. x: 10,
  302. y: 10,
  303. url: './img/demo.png',
  304. style: {
  305. r: 40,
  306. border: '10px solid #aaaaaa',
  307. boxShadow: '10 10 20 rgba(0, 0, 0, 0.6)',
  308. }
  309. },{
  310. type: 'radius-image',
  311. x: 170,
  312. y: 100,
  313. url: './img/demo.png',
  314. style: {
  315. r: 40,
  316. border: '10px dashed #540821',
  317. // dash: [10, 20, 10]
  318. }
  319. }]
  320. }
  321. this.drawImage7.draw(data7);
  322. },
  323. drawImage8 () {
  324. this.drawImage8 = new Wxml2Canvas({
  325. width: 340,
  326. height: 400,
  327. element: 'canvas8',
  328. background: '#f0f0f0',
  329. finish(url) {
  330. },
  331. error (res) {
  332. }
  333. });
  334. let style = {
  335. text: {
  336. textAlign: 'left',
  337. width: 320,
  338. fontSize: 16,
  339. lineHeight: 25,
  340. }
  341. }
  342. let text = [
  343. 'Wxml2Cavnas库,是一个生成小程序分享图的通用方案,提供了两种绘制方式:',
  344. '封装基础图形的绘制接口,包括矩形、圆形、线条、图片、圆角图片、纯文本等,使用时只需要声明元素类型并提供关键数据即可,不需要再关注canvas的具体绘制过程;',
  345. 'wxml直接转换成canvas元素,使用时传入待绘制的wxml节点的class类名,并且声明绘制此节点的类型(图片、文字等),会自动读取此节点的computedStyle,利用这些数据完成元素的绘制。',
  346. ]
  347. let height1 = 20 + Math.ceil(this.drawImage8.measureWidth(text[0], '16px pingfang') / style.text.width) * style.text.lineHeight;
  348. let height2 = height1 + 10 + Math.ceil(this.drawImage8.measureWidth(text[1], '16px pingfang') / style.text.width) * style.text.lineHeight;
  349. let data8 = {
  350. list: [
  351. {
  352. type: 'text',
  353. text: text[0],
  354. x: 10,
  355. y: 10,
  356. style: style.text
  357. }, {
  358. type: 'text',
  359. text: text[1],
  360. x: 10,
  361. y: height1,
  362. style: style.text
  363. }, {
  364. type: 'image',
  365. x: 10,
  366. y: height2,
  367. url: './img/demo.png',
  368. style: {
  369. width: 100,
  370. height: 60,
  371. }
  372. }, {
  373. type: 'text',
  374. text: text[2],
  375. x: 15,
  376. y: height2 + 80,
  377. style: style.text
  378. }
  379. ]
  380. }
  381. this.drawImage8.draw(data8);
  382. }
  383. });