-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue-track-animejs.html
704 lines (681 loc) · 21.3 KB
/
vue-track-animejs.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<meta name="renderer" content="webkit">
<title>Vue 轨迹绘制及播放实验</title>
<meta name="description" content="首页描述"/>
<meta name="keywords" content="首页关键词"/>
<meta name="author" content="Web Layout:amu"/>
<meta name="format-detection" content="telephone=no,date=no,address=no,email=no,url=no"/>
<link rel="stylesheet" href="./src/style/base.css?v=1">
<style>
::-webkit-scrollbar {
margin-right: 3px;
width: 6px;
height: 6px;
background: rgba(255, 255, 255, 0);
}
::-webkit-scrollbar-thumb {
margin-right: 3px;
background: rgba(0, 0, 0, 0.1);
border-radius: 10px;
}
::-webkit-scrollbar-thumb:hover {background: rgba(0, 0, 0, 0.2);}
#app{
position: relative;
}
.paper{max-width: 2000px;}
h2{vertical-align: middle;font-size: 20px;}
.map{
position: relative;
overflow-x:auto;
height:380px;
padding-bottom: 30px;
}
ul{
margin:0;
padding:0;
display:flex;
height: 100%;
list-style: none;
white-space:nowrap;
justify-content: space-between;
width: auto;
min-width: 100%;
float: left;
}
ul li{height:100%;flex-shrink:0;margin-right: 16px;box-shadow: 0 2px 6px rgba(0,0,0,.1);}
ul li .img-layer{
height:100%;
position:relative;
}
.device-list{
position:absolute;
left:0;
top:0;
z-index: 999;
}
.device-list i{
position:absolute;font-weight:bold;font-style:normal;font-size:14px;color:red;line-height: 1;
width: 1em;
height: 1em;
display: block;
border-radius: 0.5em;
text-align: center;
text-indent: -2em;
background-color: rgba(23, 19, 0, 0.8);
border:1px solid #999;
box-sizing: border-box;
}
.device-list i em{
font-size: 12px;
color: #000;
font-style: normal;
position: absolute;
bottom: 14px;
left: -30px;
display: none;
background-color: #fff;
width: 150px;
border: 1px solid #ccc;
word-break: break-all;
word-wrap: break-word;
padding: 8px 0;
border-radius: 4px;
}
.device-list i.open em{
display: block;
box-shadow: 0 2px 7px rgba(0, 0, 0, 0.2);
}
.device-list i.open button{
padding:.2em .4em;
line-height: 1;
margin-top:8px
}
.device-list i em pre{
font-weight: normal;
padding: 0;
margin:0
}
ul li .img-layer img{
height:100%;
}
ul li h4{
margin: .2em 0;
font-size: 14px;
}
.svg-layer{
position: absolute;
z-index: 9;
left: 0;
top: 0;
height: 363px;
pointer-events: none;
}
.player{
position: relative;
text-align: center;
padding-top: 6px;
}
.player button{
background: blueviolet;
color: #fff;
}
.player button:hover{
background: rgb(107, 28, 180);
}
.pause i{
display: inline-block;
font-style: normal;
transform: rotate(90deg);
}
.run-circle{
box-shadow: 0 0 5px rgba(0,0,0,.5);
position: relative;
z-index: 9999;
}
#runer{
stroke-width: 2;
/* animation:heartbeat 1s infinite;
-webkit-animation:heartbeat 1s infinite; */
position: relative;
}
@keyframes heartbeat {
0% {
transform:rotate(45deg) scale(0.8,0.8);
opacity:1;
}
25% {
transform:rotate(45deg) scale(1,1);
opacity:0.8;
}
100% {
transform:rotate(45deg) scale(0.8,0.8);
opacity:1;
}
}
.device-img{
position: absolute;
right: 0;top: 42px;
height: 260px;
z-index: 999;
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.12);
border-radius: 4px;
overflow: hidden;
}
.device-img img{
height: 100%;
}
.readme ol{
display: block;
}
</style>
</head>
<body class="paper">
<div id="app">
<h2>轨迹绘制及播放实验:</h2>
<div class="device-img">
<img :src="deviceImg" alt="">
</div>
<div class="map" ref="mapOuter" @scroll="userScroll" @mousedown ="userMouseDown" @mouseup="userMouseUp" @mousemove="userMouseMove" @mouseleave="userMouseLeave">
<ul ref="mapLayers">
<li v-for="item in newData">
<div class="img-layer">
<img :src="item.url" alt="" draggable="false">
<div ref="deviceListLayer" v-if="item.devices && item.devices.length > 0" class="device-list" :style='"transform: scale("+item.scale+")"'>
<i v-for="d in item.devices" :style="d.deviceStyle" :title='JSON.stringify(d)' :key="d.index">
<em>id:{{d.deviceId}} -{{d.deviceName}}<br>
<button>查看大图</button>
<button @click="changeStart">此处开始</button>
</em>
</i>
</div>
</div>
<h4>{{item.floorName}}</h4>
</li>
</ul>
<div class="svg-layer">
<svg ref="svglayer" width="2277" height="100%" viewBox="0 0 2277 363" preserveAspectRatio="xMidYMid meet">
<!-- <path ref="pathObj" fill="none" stroke="#ff9966" strokeMiterLimit="10" strokeDashOffset="988.01" style="stroke-width: 1;"></path> -->
<g ref="svgGroup" fill="none" >
<!-- <path fill="none" stroke="#ff9966" strokeMiterLimit="10" strokeDashOffset="988.01" style="stroke-width: 1;"></path>
<path fill="none" stroke-dasharray="5,5" stroke="#ff9966" style="stroke-width: 1;"></path> -->
<polyline v-for="item in imgList" stroke="#ff9966" stroke-width="2" :points="item.deviceCoords"/>
<polyline v-for="item in imgList" stroke="#999999" :points="item.crossMapCoords" stroke-dasharray="5,5"/>
</g>
<!-- <circle id="runer" ref="runer" r="12" fill="#ff0000" stroke="#ffffff" :style="startPos"></circle> -->
<circle id="runer" ref="runer" r="12" fill="#ff0000" stroke="#ffffff" :style="startPos">
</circle>
</svg>
</div>
</div>
<div class="player">
<button @click="playControl">{{playText}}</button>
<button @click="pathRestart">Restart:↷</button>
<!-- <button @click="pathRun">pathRun:▶</button>
<button class="pause" @click="pathPause">pathPause:<i>〓</i></button>
<button @click="pathResume">pathResume:↷</button>
<button @click="pathRunTimeline">pathRunTimeline </button> -->
</div>
</div>
<div class="readme">
<ol>
<li>跨地图轨迹生成,地图大小是高度一致,宽度自适应</li>
<li>地图上设备点位坐标根据高度缩放比例自适应</li>
<li>地图轨迹用SVG 的path实现</li>
<li>轨迹播放从左到右,轨迹点移动到地图区域一半时,地图背景开始左移,保持轨迹点在屏幕中央</li>
<li>轨迹点移动到设备时,如果设备有抓拍图,需要等抓拍图播放完毕再向下一个点位移动</li>
<li>用户点击或拖动地图区域时,轨迹播放暂停</li>
<li>禁止图片拖动:draggable="false"</li>
</ol>
</div>
<script src="./lib/vue.2.6.11.js"></script>
<script src="./lib/anime.min.3.2.0.js"></script>
<script>
const reducer = (accumulator, currentValue) => accumulator + currentValue;
new Vue({
el: "#app",
data() {
return {
imgList: [
{
floorName: "隧道地图",
url: './assets/images/map01.jpg',
width:907,
height:390,
devices:[
{
deviceId: 1,
deviceName: "mock设备0",
points:[200, 96]
},
{
deviceId: 12,
deviceName: "mock设备1",
points:[408, 62],
},
{
deviceId: 16,
deviceName: "mock设备2",
points:[652, 210],
images:[
{id:1,url:"./assets/images/002.jpg"},
{id:1,url:"./assets/images/003.jpg"},
{id:1,url:"./assets/images/004.jpg"}
]
},
{
deviceId: 18,
deviceName: "mock设备3",
points:[812, 144],
}
]
},
{
floorName: "复兴岛公园",
url:'./assets/images/map02.png',
width:475,
height:784,
devices:[
{
deviceId: 1,
deviceName: "mock设备0",
points:[60, 387],
},
{
deviceId: 12,
deviceName: "mock设备1",
points:[326, 717],
images:[
{id:1,url:"./assets/images/004.jpg"}
]
}
]
},
{
floorName: "商场3层",
url: './assets/images/map03.png',
width:2144,
height:846,
devices:[
{
deviceId: 1,
deviceName: "mock设备0",
points:[342, 441],
images:[
{id:1,url:"./assets/images/002.jpg"},
{id:1,url:"./assets/images/003.jpg"}
]
},
{
deviceId: 12,
deviceName: "mock设备1",
points:[835, 616],
}
]
},
{
floorName: "上海师大徐汇校区",
url: './assets/images/map04.png',
width:727,
height:917,
devices:[
{
deviceId: 1,
deviceName: "mock设备0",
points:[46, 202],
},
{
deviceId: 12,
deviceName: "mock设备1",
points:[320, 217],
},
{
deviceId: 12,
deviceName: "mock设备1",
points:[357, 474],
}
]
}
],
newData:[],
pathLine:'',
snapObj:'',
pointArr:[],
mapHeight:'',
isLoad:true,
rectC:'',
mapInfo:{},
runTimer:'',
animation:'',
animationLine:'',
dirChanges:0,
startPos:"",
deviceMap:new Map(),
deviceImg:'',
playStatus:'0',
playText:"▶",
canAutoScroll:true,
isMouseDown :false,
mouseDownX : 0,
startX : 0
}
},
mounted(){
//window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
this.$nextTick(()=>{
this.mapHeight = this.$refs.mapLayers.getBoundingClientRect().height
this.mapInfo = this.$refs.mapOuter.getBoundingClientRect()
this.dataUpdate()
})
},
methods: {
setStartPos(index){
return `transform: translateX(${this.pointArr[index][0]}px) translateY(${this.pointArr[index][1]}px)`
},
changeStart(e){
console.log(e)
this.startPos()
},
showDevice(e){
let currentDevice = e.target;
currentDevice.classList.toggle('open')
//this.startPos = this.setStartPos(item.deviceIndex-1)
},
hideDevice(e){
console.log(this.$refs);
this.$refs.deviceListLayer.forEach(element => {
for(let i=0;i<element.children.length;i++){
let el = element.children.item(i).classList;
//console.log(el)
if(el.contains('open')){
el.remove('open')
}
}
});
//currentDevice.classList.add('open')
},
userScroll(e){
//console.log(e)
//if(!this.canAutoScroll){
//this.canAutoScroll = false
//}
//console.log(this.canAutoScroll)
},
userClick(){
// this.canAutoScroll = false;
// this.pathPause()
},
userMouseDown(ev){
console.log(ev)
let thisEl = ev.target;
if(thisEl.tagName ==='EM'){
return
}
if(thisEl.tagName ==='I'){
this.showDevice(ev)
}else{
this.hideDevice()
}
if(ev.button == 0){
this.mouseDownX = ev.clientX
this.isMouseDown = true
this.startX = (this.$refs.mapOuter.scrollLeft);
console.log('鼠标左键',this.mouseDownX,this.startX)
if(this.animationLine && this.animationLine.began){
this.animationLine.pause()
this.playerClass = "el-icon-video-play"
console.log(this.animationLine)
}
}
},
userMouseUp(e){
//console.log('弹起',this,e)
this.isMouseDown = false;
},
userMouseLeave(){
this.isMouseDown = false;
},
userMouseMove(ev){
ev.stopPropagation()
ev.preventDefault()
//按下并移动
if(this.isMouseDown){
//console.log(ev)
if(ev.movementX < 0){//左移
}
let scrollX = ev.clientX - this.mouseDownX;
this.$refs.mapOuter.scrollLeft = (this.startX - scrollX)
}
},
playControl(){
console.log(this.playStatus)
if(this.playStatus=="0"){
this.pathRunTimeline()
}else if(this.playStatus=="1"){
this.pathPause()
}else{
this.pathResume()
}
},
pathRunTimeline(){
let self = this
this.playText = "||"
this.playStatus="1"
let path = anime.path(this.$refs.pathObj);
console.log(path)
console.log(this.$refs.runer)
let halfW = this.mapInfo.width/2;
this.animationLine = anime.timeline({
targets: this.$refs.runer,
duration: 100,
easing: 'linear',
complete(){
console.log('播放完成')
self.playText = "▶"
self.playStatus="0"
}
})
this.pointArr.map((item,index)=>{
//console.log(item)
let endDelayTime = 10
if(self.deviceMap.get(index)){
endDelayTime = self.deviceMap.get(index).length*1000 + 10
}
this.animationLine.add({
translateX: item[0],
translateY: item[1],
duration: 1000,
endDelay:endDelayTime,
changeBegin: function(anim) {
console.log(self.$refs.mapOuter.scrollLeft)
},
change(e) {
//console.log(e)
let step = Number(e.animatables[0].transforms.list.get('translateX').split(".")[0])
let scrollX = step - halfW;
if(self.canAutoScroll && scrollX > 0){
self.$refs.mapOuter.scrollLeft = scrollX
}
},
changeComplete(e) {
if(self.deviceMap.get(index)){
self.deviceMap.get(index).map((item,index)=>{
setTimeout(()=>{
self.deviceImg = item.url;
},index*1000)
})
}
}
})
})
},
pathInit(){
//console.log(this.pointArr)
//this.pathLine = this.$refs.pathObj.setAttribute("d",this.pathStr(this.pointArr))
//this.$refs.runPath.setAttribute('path',this.pathStr(this.pointArr))
this.startPos = this.setStartPos(0)
},
dataUpdate(){
let self = this;
let offsetX = []
this.pointsArr = []
this.imgList.map((item,index)=>{
//console.log(item)
item.scale = this.mapHeight / item.height;
item.restoreScale = 1 /item.scale;
item.scaleW = (item.width * item.scale)+16;
offsetX.push(item.scaleW);
item.deviceCoords = [];
item.crossMapCoords = []
if(item.devices && item.devices.length > 0 ){
item.devices.map((d)=>{
d.deviceStyle = "left:"+d.points[0]+"px;top:"+d.points[1]+"px;transform: scale("+item.restoreScale+");margin-left:-.5em;margin-top:-.5em";
let offsetXW;
offsetXW = offsetX.reduce(reducer) - offsetX[offsetX.length-1];
d.points[0] = parseFloat((offsetXW + d.points[0] * item.scale).toFixed(2));
d.points[1] = parseFloat((d.points[1]*item.scale).toFixed(2));
this.pointArr.push(d.points)
item.deviceCoords.push(d.points)
this.deviceMap.set(this.pointArr.length-1,d.images)
item.crossMapCoords[0] = d.points
// if(index > 0){
// item.crossMapPiont[1] = item.piontArr[0]
// }
})
}
//item.crossMapCoords[1] = this.pointArr[this.pointArr.length]
})
setTimeout(()=>{
console.log(this.imgList)
self.newData = this.imgList;
let currentLength = 0
this.imgList.map((item,index)=>{
console.log(index , this.imgList.length-1)
currentLength += item.deviceCoords.length
item.crossMapCoords[1] = this.pointArr[currentLength]
if(index == this.imgList.length-1){
item.crossMapCoords = []
}
})
this.isLoad = false;
//console.log(this.deviceMap)
this.pathInit()
},200)
},
pathStr(arr){
//console.log(arr)
let strAll='',subStr='';
if(arr && arr.length > 0){
arr.map((item,index)=>{
//console.log(arr)
if(index === 0){
subStr = "M "+ item.join(" ")
}else{
subStr = " L "+ item.join(" ")
}
strAll += subStr
})
}
console.log(strAll)
return strAll
},
pathPause(){
//TODO:用户滑动滚动条时,播放状态切换为暂停
this.animationLine.pause()
this.playText = "继续▶"
this.playStatus="2"
},
pathResume(){
this.animationLine.play()
this.playText = "||"
this.playStatus="1"
this.canAutoScroll = true
},
pathRestart(){
this.animationLine.restart()
},
pathStop(){
},
pathRun(){
let self = this
let path = anime.path(this.$refs.pathObj);
let halfW = this.mapInfo.width/2
this.animation = anime({
targets: this.$refs.runer,
translateX: path('x'),
translateY: path('y'),
rotate: path('angle'),
easing: 'linear',
duration: 10000,
loop: false,
begin(e){
console.log('开始',e)
},
changeComplete(e){
// self.dirChanges ++
// console.log(self.dirChanges,e)
},
change(e) {
let step = Number(e.animatables[0].transforms.list.get('translateX').split(".")[0])
let scrollX = step - halfW;
if(scrollX > 0){
self.$refs.mapOuter.scrollLeft = scrollX
}
self.dirChanges ++;
//console.log(self.dirChanges)
},
loopComplete(){
console.log("第一段")
},
complete(e){
console.log('结束',e)
}
});
console.log(this.animation)
},
pathRunKeyframes(){
let self = this
let path = anime.path(this.$refs.pathObj);
let halfW = this.mapInfo.width/2;
this.animation = anime({
targets: this.$refs.runer,
translateX: [
{ value: this.pointArr[0][0] },
{ value: this.pointArr[1][0] },
{ value: this.pointArr[2][0] }, // 第三帧
{ value: this.pointArr[3][0] },
],
translateY: [
{ value: this.pointArr[0][1] },
{ value: this.pointArr[1][1] },
{ value: this.pointArr[2][1] }, // 第三帧
{ value: this.pointArr[3][1] },
],
easing: 'linear',
duration: 3000,
changeComplete(e) {
console.log(e)
}
})
},
// beforeunloadHandler (ev) {
// // Cancel the event as stated by the standard.
// ev.preventDefault();
// // Chrome requires returnValue to be set.
// ev.returnValue = '';
// },
// beforeDestroy(){
// alert("22")
// }
}
})
// window.addEventListener('unload', function(event) {
// console.log('I am the 3rd one.');
// });
</script>
</body>
</html>