index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <template>
  2. <div>
  3. <canvas id="canvas"></canvas>
  4. </div>
  5. </template>
  6. <script>
  7. let speed = 20
  8. export default {
  9. props: {
  10. canvasWidth: {
  11. default: 1091,
  12. type: Number
  13. },
  14. canvasHeight: {
  15. default: 626,
  16. type: Number
  17. },
  18. start: {
  19. default: false
  20. }
  21. },
  22. data () {
  23. let devicePixelRatio = window.devicePixelRatio || 1
  24. let lineTxt = [
  25. {
  26. posi: [840, 290],
  27. val: '3.2米',
  28. guideLine: [{
  29. type: 'm',
  30. val: [790, 320]
  31. },
  32. {
  33. type: 'l',
  34. val: [800, 280]
  35. },
  36. {
  37. type: 'l',
  38. val: [815, 280]
  39. } ]
  40. },
  41. {
  42. posi: [690, 395],
  43. val: '5.4米',
  44. guideLine: [{
  45. type: 'm',
  46. val: [640, 425]
  47. },
  48. {
  49. type: 'l',
  50. val: [650, 385]
  51. },
  52. {
  53. type: 'l',
  54. val: [665, 385]
  55. } ]
  56. },
  57. {
  58. posi: [920, 470],
  59. val: '3.4米',
  60. guideLine: [{
  61. type: 'm',
  62. val: [870, 490]
  63. },
  64. {
  65. type: 'l',
  66. val: [880, 460]
  67. },
  68. {
  69. type: 'l',
  70. val: [895, 460]
  71. } ]
  72. }
  73. ]
  74. let lineArr2 = [
  75. [{
  76. type: 'm',
  77. val: [790, 430]
  78. },
  79. {
  80. type: 'l',
  81. val: [790, 85]
  82. } ],
  83. [{
  84. type: 'm',
  85. val: [790, 430]
  86. }, {
  87. type: 'l',
  88. val: [35, 400]
  89. }, {
  90. type: 'l',
  91. val: [0, 420]
  92. } ],
  93. [{
  94. type: 'm',
  95. val: [790, 430]
  96. }, {
  97. type: 'l',
  98. val: [1045, 626]
  99. }]
  100. ]
  101. let testArr2 = []
  102. let tempA = lineArr2.concat(lineTxt)
  103. tempA.forEach(() => {
  104. testArr2.push([])
  105. })
  106. // 这里存放数据
  107. return {
  108. realWidth: 1091,
  109. realHeigth: 626,
  110. devicePixelRatio,
  111. interval: null,
  112. index: 0,
  113. testArr: [],
  114. testArr2,
  115. lineArr2,
  116. lineTxt,
  117. arr: []
  118. }
  119. },
  120. computed: {
  121. imgWidth () {
  122. return this.canvasWidth
  123. },
  124. imgHeight () {
  125. return this.canvasHeight
  126. },
  127. fixCanvasWidth () {
  128. return this.canvasWidth * this.devicePixelRatio
  129. },
  130. fixCanvasHeight () {
  131. return this.canvasHeight * this.devicePixelRatio
  132. },
  133. img () {
  134. let img = new Image()
  135. img.src = require('@/assets/images/fjqxd.png')
  136. return img
  137. },
  138. ctx () {
  139. var mycanvas = document.getElementById('canvas')
  140. var ctx = mycanvas.getContext('2d')
  141. mycanvas.setAttribute('width', this.fixCanvasWidth)
  142. mycanvas.setAttribute('height', this.fixCanvasHeight)
  143. mycanvas.style.width = this.imgWidth + 'px'
  144. mycanvas.style.height = this.imgHeight + 'px'
  145. return ctx
  146. },
  147. lineArr () {
  148. return [
  149. {
  150. type: 'm',
  151. val: [790, 430]
  152. }, {
  153. type: 'l',
  154. val: [790, 90]
  155. }, {
  156. type: 'l',
  157. val: [890, 0]
  158. }, {
  159. type: 'm',
  160. val: [790, 90]
  161. }, {
  162. type: 'l',
  163. val: [35, 110]
  164. }, {
  165. type: 'l',
  166. val: [0, 100]
  167. },
  168. {
  169. type: 'm',
  170. val: [790, 430]
  171. }, {
  172. type: 'l',
  173. val: [35, 400]
  174. }, {
  175. type: 'l',
  176. val: [0, 420]
  177. }, {
  178. type: 'm',
  179. val: [35, 400]
  180. }, {
  181. type: 'l',
  182. val: [35, 110]
  183. },
  184. {
  185. type: 'm',
  186. val: [790, 430]
  187. }, {
  188. type: 'l',
  189. val: [1045, 626]
  190. },
  191. {
  192. type: 'm',
  193. val: [110, 150]
  194. },
  195. {
  196. type: 'l',
  197. val: [110, 350]
  198. },
  199. {
  200. type: 'l',
  201. val: [305, 350]
  202. },
  203. {
  204. type: 'l',
  205. val: [305, 150]
  206. },
  207. {
  208. type: 'l',
  209. val: [110, 150]
  210. },
  211. {
  212. type: 'm',
  213. val: [440, 150]
  214. },
  215. {
  216. type: 'l',
  217. val: [438, 360]
  218. },
  219. {
  220. type: 'l',
  221. val: [540, 362]
  222. },
  223. {
  224. type: 'l',
  225. val: [540, 150]
  226. },
  227. {
  228. type: 'l',
  229. val: [440, 150]
  230. },
  231. {
  232. type: 'm',
  233. val: [635, 150]
  234. },
  235. {
  236. type: 'l',
  237. val: [630, 423]
  238. },
  239. {
  240. type: 'l',
  241. val: [765, 428]
  242. },
  243. {
  244. type: 'l',
  245. val: [770, 150]
  246. },
  247. {
  248. type: 'l',
  249. val: [635, 150]
  250. }
  251. ]
  252. }
  253. },
  254. watch: {
  255. start (newVal) {
  256. if (newVal) {
  257. this.index = 0
  258. let t = this.lineArr2.concat(this.lineTxt)
  259. this.testArr2 = null
  260. this.testArr2 = t.map(item => {
  261. return []
  262. })
  263. this.animated()
  264. } else {
  265. this.init()
  266. }
  267. }
  268. },
  269. methods: {
  270. animated () {
  271. let drawTxt = () => {
  272. this.ctx.font = 'normal 16px Arial'
  273. this.ctx.textAlign = 'center'
  274. this.ctx.textBaseline = 'bottom'
  275. this.ctx.fillStyle = '#fff'
  276. this.ctx.strokeStyle = '#1fe4dc'
  277. this.ctx.lineWidth = 1
  278. this.lineTxt.forEach(item => {
  279. this.ctx.fillText(item.val, item.posi[0], item.posi[1])
  280. })
  281. }
  282. let drawLine = arr => {
  283. this.ctx.clearRect(0, 0, this.fixCanvasWidth, this.fixCanvasHeight)
  284. this.ctx.drawImage(this.img,
  285. 0,
  286. 0,
  287. this.realWidth,
  288. this.realHeigth,
  289. (this.fixCanvasWidth - this.imgWidth * this.devicePixelRatio) / 2,
  290. 0,
  291. this.imgWidth * this.devicePixelRatio,
  292. this.imgHeight * this.devicePixelRatio
  293. )
  294. this.ctx.strokeStyle = '#1fe4dc'
  295. this.ctx.lineWidth = 2
  296. this.ctx.beginPath()
  297. arr.forEach((item, index) => {
  298. item.forEach(i => {
  299. if (i.type === 'm') {
  300. this.ctx.moveTo(i.val[0], i.val[1])
  301. this.ctx.stroke()
  302. this.ctx.beginPath()
  303. if (index < myArr.length) {
  304. this.ctx.lineWidth = 2.5
  305. } else {
  306. this.ctx.lineWidth = 1
  307. }
  308. } else {
  309. this.ctx.lineTo(i.val[0], i.val[1])
  310. }
  311. })
  312. })
  313. this.ctx.stroke()
  314. }
  315. let getCutArr = () => {
  316. let arr = []
  317. this.lineArr2.forEach(() => {
  318. arr.push([])
  319. })
  320. let intervalX, intervalY
  321. this.lineArr2.forEach((item, index) => {
  322. item.forEach((sub, idx) => {
  323. if (sub.type === 'm') {
  324. arr[index].push(sub)
  325. }
  326. if (idx < this.lineArr2[index].length - 1) {
  327. if (this.lineArr2[index][idx + 1]['type'] !== 'm') {
  328. intervalX = (this.lineArr2[index][idx + 1]['val'][0] - this.lineArr2[index][idx]['val'][0]) / speed
  329. intervalY = (this.lineArr2[index][idx + 1]['val'][1] - this.lineArr2[index][idx]['val'][1]) / speed
  330. let i = 0
  331. while (i <= speed) {
  332. arr[index].push(Object.assign({}, sub, {
  333. val: [this.lineArr2[index][idx]['val'][0] + i * intervalX, this.lineArr2[index][idx]['val'][1] + i * intervalY],
  334. type: 'l'
  335. }))
  336. i++
  337. }
  338. }
  339. }
  340. })
  341. })
  342. return arr
  343. }
  344. let getGuideArr = () => {
  345. let arr = []
  346. this.lineTxt.forEach(() => {
  347. arr.push([])
  348. })
  349. let intervalX, intervalY
  350. this.lineTxt.forEach((item, index) => {
  351. item.guideLine.forEach((sub, idx) => {
  352. let lt = this.lineTxt[index]['guideLine']
  353. if (sub.type === 'm') {
  354. arr[index].push(sub)
  355. }
  356. if (idx < lt.length - 1) {
  357. if (lt[idx + 1]['type'] !== 'm') {
  358. intervalX = (lt[idx + 1]['val'][0] - lt[idx]['val'][0]) / speed
  359. intervalY = (lt[idx + 1]['val'][1] - lt[idx]['val'][1]) / speed
  360. let i = 0
  361. while (i <= speed) {
  362. arr[index].push(Object.assign({}, sub, {
  363. val: [lt[idx]['val'][0] + i * intervalX, lt[idx]['val'][1] + i * intervalY],
  364. type: 'l'
  365. }))
  366. i++
  367. }
  368. }
  369. }
  370. })
  371. })
  372. return arr
  373. }
  374. let guideArr = getGuideArr()
  375. let myArr = getCutArr()
  376. console.log(myArr)
  377. let maxLength = 0
  378. let guideLength = 0
  379. myArr.forEach(item => {
  380. maxLength = Math.max(item.length, maxLength)
  381. })
  382. guideArr.forEach(item => {
  383. guideLength = Math.max(item.length, guideLength)
  384. })
  385. let draw = () => {
  386. if (!this.start) {
  387. return
  388. }
  389. if (this.index === maxLength + guideLength) {
  390. setTimeout(() => {
  391. drawTxt()
  392. }, 300)
  393. return
  394. }
  395. myArr.forEach((item, index) => {
  396. item.forEach((sub, idx) => {
  397. item[this.index] && this.testArr2[index].push(item[this.index])
  398. })
  399. })
  400. if (this.index >= maxLength) {
  401. guideArr.forEach((item, index) => {
  402. item.forEach((sub, idx) => {
  403. item[this.index - maxLength] && this.testArr2[myArr.length + index].push(item[this.index - maxLength])
  404. })
  405. console.log(this.testArr2)
  406. })
  407. }
  408. this.index++
  409. drawLine(this.testArr2)
  410. requestAnimationFrame(draw)
  411. }
  412. draw()
  413. },
  414. init () {
  415. this.ctx.clearRect(0, 0, this.fixCanvasWidth, this.fixCanvasHeight)
  416. this.ctx.drawImage(this.img,
  417. 0,
  418. 0,
  419. this.realWidth,
  420. this.realHeigth,
  421. (this.fixCanvasWidth - this.imgWidth * this.devicePixelRatio) / 2,
  422. 0,
  423. this.imgWidth * this.devicePixelRatio,
  424. this.imgHeight * this.devicePixelRatio
  425. )
  426. }
  427. },
  428. mounted () {
  429. this.img.onload = () => {
  430. this.ctx.clearRect(0, 0, this.fixCanvasWidth, this.fixCanvasHeight)
  431. this.ctx.drawImage(this.img,
  432. 0,
  433. 0,
  434. this.realWidth,
  435. this.realHeigth,
  436. (this.fixCanvasWidth - this.imgWidth * this.devicePixelRatio) / 2,
  437. 0,
  438. this.imgWidth * this.devicePixelRatio,
  439. this.imgHeight * this.devicePixelRatio
  440. )
  441. // this.ctx.strokeStyle = '#1fe4dc'
  442. // this.ctx.lineWidth = 5
  443. // this.ctx.beginPath()
  444. // // arr1
  445. // this.ctx.moveTo(790, 430)
  446. // this.ctx.lineTo(790, 90)
  447. // this.ctx.lineTo(890, 0)
  448. // this.ctx.moveTo(790, 90)
  449. // this.ctx.lineTo(35, 110)
  450. // this.ctx.lineTo(0, 100)
  451. // // arr2
  452. // this.ctx.moveTo(790, 430)
  453. // this.ctx.lineTo(35, 400)
  454. // this.ctx.lineTo(0, 420)
  455. // this.ctx.moveTo(35, 400)
  456. // this.ctx.lineTo(35, 110)
  457. // // arr3
  458. // this.ctx.moveTo(790, 430)
  459. // this.ctx.lineTo(1045, 626)
  460. // this.ctx.stroke()
  461. }
  462. // this.animated()
  463. }
  464. }
  465. </script>
  466. <style scoped>
  467. </style>