sdk-tour.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. <style>
  8. [v-cloak] {
  9. display: none;
  10. }
  11. ul,
  12. li {
  13. padding: 0;
  14. margin: 0;
  15. list-style: none;
  16. }
  17. html,
  18. body {
  19. width: 100%;
  20. height: 100%;
  21. margin: 0;
  22. overflow: hidden;
  23. }
  24. .scene {
  25. width: 100%;
  26. height: 100%;
  27. }
  28. #app {
  29. position: absolute;
  30. pointer-events: none;
  31. left: 0;
  32. top: 0;
  33. width: 100%;
  34. height: 100%;
  35. z-index: 100;
  36. }
  37. .progress {
  38. display: none;
  39. position: absolute;
  40. left: 0;
  41. bottom: 0;
  42. width: 100%;
  43. height: 4px;
  44. }
  45. .progress div {
  46. height: 100%;
  47. width: 0;
  48. background-color: #f60;
  49. }
  50. #parts ul,
  51. #frames ul {
  52. display: flex;
  53. }
  54. #parts li {
  55. position: relative;
  56. color: #fff;
  57. padding: 5px;
  58. margin-right: 10px;
  59. cursor: pointer;
  60. }
  61. #parts li span {
  62. display: none;
  63. position: absolute;
  64. right: -10px;
  65. top: -10px;
  66. }
  67. #parts li:hover,
  68. #parts li.active {
  69. color: #f60;
  70. }
  71. #parts li.active .progress {
  72. display: block;
  73. }
  74. #parts li.active span {
  75. display: block;
  76. }
  77. #frames li {
  78. position: relative;
  79. margin-left: 5px;
  80. border: solid 1px transparent;
  81. cursor: pointer;
  82. }
  83. #frames li span {
  84. display: none;
  85. position: absolute;
  86. right: 0px;
  87. top: 0px;
  88. width: 16px;
  89. height: 16px;
  90. background-color: #fff;
  91. text-align: center;
  92. }
  93. #frames li.active {
  94. border: solid 1px #f60;
  95. }
  96. #frames li.active .progress {
  97. display: block;
  98. }
  99. #frames li.active span {
  100. display: block;
  101. }
  102. .toolbar {
  103. pointer-events: all;
  104. position: absolute;
  105. left: 50%;
  106. top: 0;
  107. transform: translateX(-50%);
  108. }
  109. .bottom {
  110. pointer-events: all;
  111. position: absolute;
  112. left: 0;
  113. bottom: 0;
  114. width: 100%;
  115. height: 200px;
  116. background-color: rgba(0, 0, 0, 0.5);
  117. }
  118. .bottom.disable {
  119. pointer-events: none;
  120. opacity: 0.5;
  121. }
  122. </style>
  123. </head>
  124. <body>
  125. <div id="scene" class="scene"></div>
  126. <div id="app" v-cloak>
  127. <div class="toolbar">
  128. <button :disabled="playing" @click="addFrame">添加画面</button>
  129. <button :disabled="playing" @click="addPart">添加片段</button>
  130. <button :disabled="playing" @click="clear">清空</button>
  131. <button @click="play">{{ playing?'暂停':'播放' }}</button>
  132. </div>
  133. <div class="bottom" :class="{disable:disable}">
  134. <div id="parts">
  135. <ul>
  136. <li v-for="(part,index) in tours" :class="{active:index == partId}" @click="selectPart(index)">
  137. {{part.name || '片段_'+index}}
  138. <span @click.stop="deletePart(index)">X</span>
  139. <div class="progress"><div :style="{width:progressPart+'%'}"></div></div>
  140. </li>
  141. </ul>
  142. </div>
  143. <div id="frames" :partid="partId" :frameid="frameId">
  144. <div v-for="(part,index) in tours" v-show="partId == index" :length="part.list.length">
  145. <ul>
  146. <li v-for="(frame,index) in part.list" :class="{active:index == frameId}" @click="selectFrame(index)">
  147. <img style="width: 200px; height: 140px" :src="frame.enter.cover" />
  148. <span @click.stop="deleteFrame(index)">X</span>
  149. <div class="progress"><div :style="{width:progress+'%'}"></div></div>
  150. </li>
  151. </ul>
  152. </div>
  153. </div>
  154. </div>
  155. </div>
  156. <script src="../dist/sdk/kankan-sdk-deps.js"></script>
  157. <script src="../dist/sdk/kankan-sdk.js"></script>
  158. <script src="./js/vue.min.js"></script>
  159. <script>
  160. var app = null
  161. new Vue({
  162. el: '#app',
  163. data() {
  164. return {
  165. tours: [],
  166. partId: 0,
  167. frameId: 0,
  168. progress: 0,
  169. progressPart: 0,
  170. disable: false,
  171. playing: false,
  172. }
  173. },
  174. mounted() {
  175. app = new KanKan({
  176. dom: '#scene',
  177. num: 'KJ-t-lChCHdnfPe',
  178. })
  179. app.use('TourRecorder').then(recorder => {
  180. recorder.on('change', e => {
  181. if (e.type == 'clear') {
  182. this.$forceUpdate()
  183. }
  184. else if (e.action == 'add') {
  185. this.partId = recorder.partId
  186. this.frameId = recorder.frameId
  187. } else if (e.action == 'delete') {
  188. this.partId = recorder.partId
  189. this.frameId = recorder.frameId
  190. }
  191. })
  192. })
  193. app.use('TourPlayer').then(player => {
  194. player.on('play', ({ partId, frameId }) => (this.playing = true))
  195. player.on('pause', ({ partId, frameId }) => (this.playing = false))
  196. player.on('end', () => {
  197. this.playing = false
  198. // 兼容最后一个画面没有进度的问题
  199. this.progressPart = 100
  200. })
  201. var currPartId
  202. var currFrames
  203. var currProgress
  204. player.on('progress', ({ partId, frameId, progress }) => {
  205. // 画面进度
  206. this.partId = partId
  207. this.frameId = frameId
  208. this.progress = Number(progress * 100).toFixed(5)
  209. // 片段进度
  210. if (this.tours.length == 1) {
  211. this.progressPart = this.progress
  212. } else {
  213. if (currPartId != partId) {
  214. currPartId = partId
  215. currFrames = this.tours[partId].list.length
  216. this.progressPart = 0
  217. }
  218. this.progressPart += progress / currFrames
  219. }
  220. })
  221. })
  222. // 需要双向绑定时,重新设置数据
  223. app.TourManager.on('loaded', tours => {
  224. this.tours = tours
  225. app.TourManager.load(this.tours)
  226. })
  227. app.render()
  228. },
  229. methods: {
  230. async selectPart(partId) {
  231. if (this.disable) {
  232. return
  233. }
  234. this.partId = partId
  235. this.frameId = 0
  236. this.disable = true
  237. this.progress = 0
  238. this.progressPart = 0
  239. var recorder = await app.TourManager.recorder
  240. await recorder.selectPart(partId)
  241. this.disable = false
  242. },
  243. async selectFrame(frameId) {
  244. if (this.disable) {
  245. return
  246. }
  247. this.frameId = frameId
  248. this.disable = true
  249. this.progress = 0
  250. this.progressPart = 0
  251. var recorder = await app.TourManager.recorder
  252. await recorder.selectFrame(frameId)
  253. this.disable = false
  254. },
  255. async play() {
  256. var player = await app.TourManager.player
  257. this.progress = 0
  258. if (this.playing) {
  259. player.pause()
  260. } else {
  261. player.play()
  262. }
  263. },
  264. async clear() {
  265. ;(await app.TourManager.recorder).clear()
  266. },
  267. async addPart() {
  268. ;(await app.TourManager.recorder).addPart()
  269. },
  270. async addFrame() {
  271. ;(await app.TourManager.recorder).addFrame()
  272. },
  273. async deletePart(partId) {
  274. this.partId = partId
  275. ;(await app.TourManager.recorder).deletePart(partId)
  276. },
  277. async deleteFrame(frameId) {
  278. this.frameId = frameId
  279. ;(await app.TourManager.recorder).deleteFrame(frameId)
  280. },
  281. },
  282. })
  283. </script>
  284. </body>
  285. </html>