123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491 |
- <template>
- <div>
- <canvas id="canvas"></canvas>
- </div>
- </template>
- <script>
- let speed = 20
- export default {
- props: {
- canvasWidth: {
- default: 1091,
- type: Number
- },
- canvasHeight: {
- default: 626,
- type: Number
- },
- start: {
- default: false
- }
- },
- data () {
- let devicePixelRatio = window.devicePixelRatio || 1
- let lineTxt = [
- {
- posi: [840, 290],
- val: '3.2米',
- guideLine: [{
- type: 'm',
- val: [790, 320]
- },
- {
- type: 'l',
- val: [800, 280]
- },
- {
- type: 'l',
- val: [815, 280]
- } ]
- },
- {
- posi: [690, 395],
- val: '5.4米',
- guideLine: [{
- type: 'm',
- val: [640, 425]
- },
- {
- type: 'l',
- val: [650, 385]
- },
- {
- type: 'l',
- val: [665, 385]
- } ]
- },
- {
- posi: [920, 470],
- val: '3.4米',
- guideLine: [{
- type: 'm',
- val: [870, 490]
- },
- {
- type: 'l',
- val: [880, 460]
- },
- {
- type: 'l',
- val: [895, 460]
- } ]
- }
- ]
- let lineArr2 = [
- [{
- type: 'm',
- val: [790, 430]
- },
- {
- type: 'l',
- val: [790, 85]
- } ],
- [{
- type: 'm',
- val: [790, 430]
- }, {
- type: 'l',
- val: [35, 400]
- }, {
- type: 'l',
- val: [0, 420]
- } ],
- [{
- type: 'm',
- val: [790, 430]
- }, {
- type: 'l',
- val: [1045, 626]
- }]
- ]
- let testArr2 = []
- let tempA = lineArr2.concat(lineTxt)
- tempA.forEach(() => {
- testArr2.push([])
- })
- // 这里存放数据
- return {
- realWidth: 1091,
- realHeigth: 626,
- devicePixelRatio,
- interval: null,
- index: 0,
- testArr: [],
- testArr2,
- lineArr2,
- lineTxt,
- arr: []
- }
- },
- computed: {
- imgWidth () {
- return this.canvasWidth
- },
- imgHeight () {
- return this.canvasHeight
- },
- fixCanvasWidth () {
- return this.canvasWidth * this.devicePixelRatio
- },
- fixCanvasHeight () {
- return this.canvasHeight * this.devicePixelRatio
- },
- img () {
- let img = new Image()
- img.src = require('@/assets/images/fjqxd.png')
- return img
- },
- ctx () {
- var mycanvas = document.getElementById('canvas')
- var ctx = mycanvas.getContext('2d')
- mycanvas.setAttribute('width', this.fixCanvasWidth)
- mycanvas.setAttribute('height', this.fixCanvasHeight)
- mycanvas.style.width = this.imgWidth + 'px'
- mycanvas.style.height = this.imgHeight + 'px'
- return ctx
- },
- lineArr () {
- return [
- {
- type: 'm',
- val: [790, 430]
- }, {
- type: 'l',
- val: [790, 90]
- }, {
- type: 'l',
- val: [890, 0]
- }, {
- type: 'm',
- val: [790, 90]
- }, {
- type: 'l',
- val: [35, 110]
- }, {
- type: 'l',
- val: [0, 100]
- },
- {
- type: 'm',
- val: [790, 430]
- }, {
- type: 'l',
- val: [35, 400]
- }, {
- type: 'l',
- val: [0, 420]
- }, {
- type: 'm',
- val: [35, 400]
- }, {
- type: 'l',
- val: [35, 110]
- },
- {
- type: 'm',
- val: [790, 430]
- }, {
- type: 'l',
- val: [1045, 626]
- },
- {
- type: 'm',
- val: [110, 150]
- },
- {
- type: 'l',
- val: [110, 350]
- },
- {
- type: 'l',
- val: [305, 350]
- },
- {
- type: 'l',
- val: [305, 150]
- },
- {
- type: 'l',
- val: [110, 150]
- },
- {
- type: 'm',
- val: [440, 150]
- },
- {
- type: 'l',
- val: [438, 360]
- },
- {
- type: 'l',
- val: [540, 362]
- },
- {
- type: 'l',
- val: [540, 150]
- },
- {
- type: 'l',
- val: [440, 150]
- },
- {
- type: 'm',
- val: [635, 150]
- },
- {
- type: 'l',
- val: [630, 423]
- },
- {
- type: 'l',
- val: [765, 428]
- },
- {
- type: 'l',
- val: [770, 150]
- },
- {
- type: 'l',
- val: [635, 150]
- }
- ]
- }
- },
- watch: {
- start (newVal) {
- if (newVal) {
- this.index = 0
- let t = this.lineArr2.concat(this.lineTxt)
- this.testArr2 = null
- this.testArr2 = t.map(item => {
- return []
- })
- this.animated()
- } else {
- this.init()
- }
- }
- },
- methods: {
- animated () {
- let drawTxt = () => {
- this.ctx.font = 'normal 16px Arial'
- this.ctx.textAlign = 'center'
- this.ctx.textBaseline = 'bottom'
- this.ctx.fillStyle = '#fff'
- this.ctx.strokeStyle = '#1fe4dc'
- this.ctx.lineWidth = 1
- this.lineTxt.forEach(item => {
- this.ctx.fillText(item.val, item.posi[0], item.posi[1])
- })
- }
- let drawLine = arr => {
- this.ctx.clearRect(0, 0, this.fixCanvasWidth, this.fixCanvasHeight)
- this.ctx.drawImage(this.img,
- 0,
- 0,
- this.realWidth,
- this.realHeigth,
- (this.fixCanvasWidth - this.imgWidth * this.devicePixelRatio) / 2,
- 0,
- this.imgWidth * this.devicePixelRatio,
- this.imgHeight * this.devicePixelRatio
- )
- this.ctx.strokeStyle = '#1fe4dc'
- this.ctx.lineWidth = 2
- this.ctx.beginPath()
- arr.forEach((item, index) => {
- item.forEach(i => {
- if (i.type === 'm') {
- this.ctx.moveTo(i.val[0], i.val[1])
- this.ctx.stroke()
- this.ctx.beginPath()
- if (index < myArr.length) {
- this.ctx.lineWidth = 2.5
- } else {
- this.ctx.lineWidth = 1
- }
- } else {
- this.ctx.lineTo(i.val[0], i.val[1])
- }
- })
- })
- this.ctx.stroke()
- }
- let getCutArr = () => {
- let arr = []
- this.lineArr2.forEach(() => {
- arr.push([])
- })
- let intervalX, intervalY
- this.lineArr2.forEach((item, index) => {
- item.forEach((sub, idx) => {
- if (sub.type === 'm') {
- arr[index].push(sub)
- }
- if (idx < this.lineArr2[index].length - 1) {
- if (this.lineArr2[index][idx + 1]['type'] !== 'm') {
- intervalX = (this.lineArr2[index][idx + 1]['val'][0] - this.lineArr2[index][idx]['val'][0]) / speed
- intervalY = (this.lineArr2[index][idx + 1]['val'][1] - this.lineArr2[index][idx]['val'][1]) / speed
- let i = 0
- while (i <= speed) {
- arr[index].push(Object.assign({}, sub, {
- val: [this.lineArr2[index][idx]['val'][0] + i * intervalX, this.lineArr2[index][idx]['val'][1] + i * intervalY],
- type: 'l'
- }))
- i++
- }
- }
- }
- })
- })
- return arr
- }
- let getGuideArr = () => {
- let arr = []
- this.lineTxt.forEach(() => {
- arr.push([])
- })
- let intervalX, intervalY
- this.lineTxt.forEach((item, index) => {
- item.guideLine.forEach((sub, idx) => {
- let lt = this.lineTxt[index]['guideLine']
- if (sub.type === 'm') {
- arr[index].push(sub)
- }
- if (idx < lt.length - 1) {
- if (lt[idx + 1]['type'] !== 'm') {
- intervalX = (lt[idx + 1]['val'][0] - lt[idx]['val'][0]) / speed
- intervalY = (lt[idx + 1]['val'][1] - lt[idx]['val'][1]) / speed
- let i = 0
- while (i <= speed) {
- arr[index].push(Object.assign({}, sub, {
- val: [lt[idx]['val'][0] + i * intervalX, lt[idx]['val'][1] + i * intervalY],
- type: 'l'
- }))
- i++
- }
- }
- }
- })
- })
- return arr
- }
- let guideArr = getGuideArr()
- let myArr = getCutArr()
- console.log(myArr)
- let maxLength = 0
- let guideLength = 0
- myArr.forEach(item => {
- maxLength = Math.max(item.length, maxLength)
- })
- guideArr.forEach(item => {
- guideLength = Math.max(item.length, guideLength)
- })
- let draw = () => {
- if (!this.start) {
- return
- }
- if (this.index === maxLength + guideLength) {
- setTimeout(() => {
- drawTxt()
- }, 300)
- return
- }
- myArr.forEach((item, index) => {
- item.forEach((sub, idx) => {
- item[this.index] && this.testArr2[index].push(item[this.index])
- })
- })
- if (this.index >= maxLength) {
- guideArr.forEach((item, index) => {
- item.forEach((sub, idx) => {
- item[this.index - maxLength] && this.testArr2[myArr.length + index].push(item[this.index - maxLength])
- })
- console.log(this.testArr2)
- })
- }
- this.index++
- drawLine(this.testArr2)
- requestAnimationFrame(draw)
- }
- draw()
- },
- init () {
- this.ctx.clearRect(0, 0, this.fixCanvasWidth, this.fixCanvasHeight)
- this.ctx.drawImage(this.img,
- 0,
- 0,
- this.realWidth,
- this.realHeigth,
- (this.fixCanvasWidth - this.imgWidth * this.devicePixelRatio) / 2,
- 0,
- this.imgWidth * this.devicePixelRatio,
- this.imgHeight * this.devicePixelRatio
- )
- }
- },
- mounted () {
- this.img.onload = () => {
- this.ctx.clearRect(0, 0, this.fixCanvasWidth, this.fixCanvasHeight)
- this.ctx.drawImage(this.img,
- 0,
- 0,
- this.realWidth,
- this.realHeigth,
- (this.fixCanvasWidth - this.imgWidth * this.devicePixelRatio) / 2,
- 0,
- this.imgWidth * this.devicePixelRatio,
- this.imgHeight * this.devicePixelRatio
- )
- // this.ctx.strokeStyle = '#1fe4dc'
- // this.ctx.lineWidth = 5
- // this.ctx.beginPath()
- // // arr1
- // this.ctx.moveTo(790, 430)
- // this.ctx.lineTo(790, 90)
- // this.ctx.lineTo(890, 0)
- // this.ctx.moveTo(790, 90)
- // this.ctx.lineTo(35, 110)
- // this.ctx.lineTo(0, 100)
- // // arr2
- // this.ctx.moveTo(790, 430)
- // this.ctx.lineTo(35, 400)
- // this.ctx.lineTo(0, 420)
- // this.ctx.moveTo(35, 400)
- // this.ctx.lineTo(35, 110)
- // // arr3
- // this.ctx.moveTo(790, 430)
- // this.ctx.lineTo(1045, 626)
- // this.ctx.stroke()
- }
- // this.animated()
- }
- }
- </script>
- <style scoped>
- </style>
|