index.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <template>
  2. <div class="draw">
  3. <canvas
  4. id="CANVAS"
  5. class="canvas"
  6. />
  7. <n-select
  8. v-model:value="check"
  9. :options="options"
  10. :render-label="renderLabel"
  11. @update:value="changeOption"
  12. />
  13. <n-input-group>
  14. <n-input-group-label>旋转角度º</n-input-group-label>
  15. <n-input-number
  16. v-model:value="angle"
  17. type="number"
  18. style="width: 100%;"
  19. :min="0"
  20. :max="360"
  21. @update:value="changeOption(null, options[check])"
  22. />
  23. </n-input-group>
  24. <template
  25. v-for="(item, index) in btns"
  26. :key="index"
  27. >
  28. <n-button
  29. :type="item.type"
  30. block
  31. @click="item.fn"
  32. >
  33. {{ item.name }}
  34. </n-button>
  35. </template>
  36. </div>
  37. </template>
  38. <script setup lang='ts'>
  39. import {
  40. h, onMounted, onUnmounted, ref
  41. } from 'vue'
  42. import { DrawCanvas } from './assets/js/draw'
  43. import { useRoute } from 'vue-router'
  44. const drawCanvas = new DrawCanvas()
  45. const options = [
  46. {
  47. label: '边界线',
  48. icon: '0.png',
  49. value: 0,
  50. w: 5,
  51. h: 3
  52. },
  53. {
  54. label: '灰岩',
  55. icon: '1.png',
  56. value: 1,
  57. w: 10,
  58. h: 6
  59. },
  60. {
  61. label: '第四系全新统残坡积',
  62. icon: '2.png',
  63. value: 2,
  64. w: 10,
  65. h: 6
  66. },
  67. {
  68. label: '第四系全新统崩坡积',
  69. icon: '3.png',
  70. value: 3,
  71. w: 10,
  72. h: 6
  73. },
  74. {
  75. label: '震旦系上统灯影组',
  76. icon: '4.png',
  77. value: 4,
  78. w: 10,
  79. h: 6
  80. },
  81. {
  82. label: '志留系中统石门坎组',
  83. icon: '5.png',
  84. value: 5,
  85. w: 10,
  86. h: 6
  87. },
  88. {
  89. label: '寒武系中统西王庙组',
  90. icon: '6.png',
  91. value: 6,
  92. w: 10,
  93. h: 6
  94. },
  95. {
  96. label: '粉砂岩',
  97. icon: '7.png',
  98. value: 7,
  99. w: 10,
  100. h: 6
  101. },
  102. {
  103. label: '页岩',
  104. icon: '8.png',
  105. value: 8,
  106. w: 10,
  107. h: 6
  108. },
  109. {
  110. label: '粉砂质泥岩',
  111. icon: '9.png',
  112. value: 9,
  113. w: 10,
  114. h: 6
  115. },
  116. {
  117. label: '泥岩',
  118. icon: '10.png',
  119. value: 10,
  120. w: 10,
  121. h: 6
  122. },
  123. {
  124. label: '断层破碎带',
  125. icon: '11.png',
  126. value: 11,
  127. w: 10,
  128. h: 6
  129. },
  130. {
  131. label: '块石土',
  132. icon: '12.png',
  133. value: 12,
  134. w: 10,
  135. h: 6
  136. },
  137. {
  138. label: '白云质灰岩',
  139. icon: '13.png',
  140. value: 13,
  141. w: 10,
  142. h: 6
  143. },
  144. {
  145. label: '炭质页岩',
  146. icon: '14.png',
  147. value: 14,
  148. w: 10,
  149. h: 6
  150. },
  151. {
  152. label: '推测地层界线',
  153. icon: '15.png',
  154. value: 15,
  155. w: 10,
  156. h: 6
  157. },
  158. {
  159. label: '推测岩,土层界线',
  160. icon: '16.png',
  161. value: 16,
  162. w: 10,
  163. h: 6
  164. },
  165. {
  166. label: '细砂岩',
  167. icon: '17.png',
  168. value: 17,
  169. w: 10,
  170. h: 6
  171. },
  172. {
  173. label: '花岗闪长岩',
  174. icon: '18.png',
  175. value: 18,
  176. w: 10,
  177. h: 6
  178. },
  179. {
  180. label: '千枚岩',
  181. icon: '19.png',
  182. value: 19,
  183. w: 10,
  184. h: 6
  185. },
  186. {
  187. label: '钙质千枚岩',
  188. icon: '20.png',
  189. value: 20,
  190. w: 10,
  191. h: 6
  192. },
  193. {
  194. label: '碳质板岩',
  195. icon: '21.png',
  196. value: 21,
  197. w: 10,
  198. h: 6
  199. }
  200. ]
  201. const check = ref(0)
  202. const dicTypePath = {
  203. 2: 'M174.562965,0.513209692 C221.690071,-0.00711210434 258.74832,14.9447077 286.260378,35.6782336 C324.994127,64.8686054 344.820323,105.514125 347.165866,130.556406 C351.169981,173.306492 350.923005,204.505495 338.863366,216.40901 C332.465594,222.723955 293.34237,247.50004 156.505785,247.50004 C124.380762,247.273198 88.5993566,242.423806 60.1185245,235.625016 C31.7537688,228.853934 10.5049705,220.211836 7.77411598,212.201523 C0.968039201,195.148672 -3.37568328,153.116559 5.40190246,119.570011 C11.4412002,98.8625327 30.5685051,60.0564728 69.9004626,32.3721832 C95.541554,14.3243807 129.772401,1.00773417 174.562965,0.513209692 Z',
  204. 1: 'M69.4545878,34.6718448 C96.0319839,14.5464002 130.907219,-0.146079872 174.56443,0.522007913 C220.382312,1.22316053 255.607386,15.5063115 281.851378,35.1514114 C318.898628,62.8833568 338.052622,101.292047 343.850748,127.226896 C348.964999,150.102793 350.773132,170.630546 348.624737,186.798399 C346.628206,201.8234 341.223483,213.080679 331.813382,218.873688 C331.442887,219.101769 331.054704,219.344664 330.644655,219.601247 C319.902715,226.322867 294.07793,242.444674 177.407992,247.500112 C92.0383303,246.776789 19.7179823,226.018176 9.91797058,212.791135 C-0.632619328,198.551039 -1.84542038,163.783447 3.98419444,129.673937 C7.51590663,109.009626 28.7726098,65.4778309 69.4545878,34.6718448 Z',
  205. 0: 'M175.000036,0.5 C222.664535,0.5 265.865535,21.1909654 297.358963,54.7296453 C328.90593,88.3253418 348.705559,134.812441 349.476653,186.303442 L349.490723,188.280824 L349.5,247.5 L0.5,247.5 L0.5,188.283766 C0.787224943,136.359137 20.4197058,89.3998566 51.9881066,55.4283849 C83.5364037,21.4785471 127.005458,0.5 175.000036,0.5 Z'
  206. }
  207. const dicType = ref(0)
  208. const angle = ref(0)
  209. const btns = [
  210. { name: '撤销', fn: () => drawCanvas.pathBack(), type: 'tertiary' },
  211. { name: '清空', fn: () => drawCanvas.pathClear(), type: 'tertiary' },
  212. {
  213. name: '保存',
  214. fn: () => drawCanvas.pathSave((data) => {
  215. console.log('数据', data)
  216. // 返回=>小程序web-view bindmessage="getMessage" =>使用wx.getFileSystemManager() 写入临时文件拿到临时路径后进行上传操作
  217. wx.miniProgram.postMessage({ data })
  218. wx.miniProgram.navigateBack()
  219. }),
  220. type: 'info'
  221. }
  222. ]
  223. function renderLabel(option: { icon: string | URL; label: string; }) {
  224. return h('div', {
  225. style: {
  226. display: 'flex',
  227. alignItems: 'center'
  228. }
  229. }, [
  230. h('img', {
  231. src: new URL(`./assets/img/${option.icon}`, import.meta.url).href,
  232. style: {
  233. width: '20px',
  234. height: '20px',
  235. marginRight: '5px'
  236. }
  237. }),
  238. h('div', {
  239. style: {
  240. fontSize: '14px'
  241. }
  242. }, { default: () => option.label })
  243. ])
  244. }
  245. function changeOption(_: null, option: { label: string; icon: string; value: number; w: number; h: number; }) {
  246. drawCanvas.changeBrush({ ...option, url: new URL(`./assets/img/${option.icon}`, import.meta.url).href, angle: angle.value })
  247. }
  248. onMounted(() => {
  249. document.title = 'H5 canvas画图'
  250. const route = useRoute()
  251. const { query } = route
  252. if (query.type) {
  253. dicType.value = +query.type
  254. }
  255. const canvas = document.getElementById('CANVAS')
  256. drawCanvas._init({
  257. canvas,
  258. dicTypePath: dicTypePath[dicType.value],
  259. callBack: () => {
  260. changeOption(null, options[check.value])
  261. }
  262. })
  263. })
  264. onUnmounted(() => drawCanvas.destory())
  265. </script>
  266. <style lang="scss">
  267. .draw {
  268. max-width: 800px;
  269. min-width: 370px;
  270. margin: 0 auto;
  271. overflow: hidden;
  272. box-sizing: border-box;
  273. padding: 0 10px;
  274. .canvas {
  275. display: block;
  276. margin: 10px auto;
  277. width: 350px;
  278. height: 248px;
  279. }
  280. &>div {
  281. margin-bottom: 10px;
  282. }
  283. &>button:not(:last-child) {
  284. margin-bottom: 10px;
  285. }
  286. }
  287. </style>