mixin.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import {
  2. getAssetUrl,
  3. getThumbnailUrl
  4. } from '@/api/asset'
  5. import {
  6. create,
  7. switchToNext
  8. } from './core/utils'
  9. import Volume from './components/Volume'
  10. import Card from './components/Card'
  11. export default {
  12. provide () {
  13. return {
  14. control: this
  15. }
  16. },
  17. components: {
  18. Volume,
  19. Card
  20. },
  21. props: {
  22. program: {
  23. type: Object,
  24. required: true
  25. }
  26. },
  27. data () {
  28. return {
  29. scale: 100,
  30. minScale: 100,
  31. maxScale: 100,
  32. padding: 0,
  33. selectedWidgetIndex: -1,
  34. node: null,
  35. bgm: '',
  36. muted: true
  37. }
  38. },
  39. computed: {
  40. transformStyles () {
  41. return {
  42. transform: `scale(${this.scale / 100})`,
  43. 'transform-origin': '0 0'
  44. }
  45. },
  46. styles () {
  47. if (this.node) {
  48. const { width, height, backgroundColor } = this.node
  49. return {
  50. width: `${width}px`,
  51. height: `${height}px`,
  52. color: backgroundColor
  53. }
  54. }
  55. return null
  56. },
  57. backgroundStyles () {
  58. if (this.node) {
  59. const { backgroundImage } = this.node
  60. return {
  61. 'background-image': backgroundImage[0] ? `url("${getThumbnailUrl(backgroundImage[0])}")` : 'none'
  62. }
  63. }
  64. return null
  65. },
  66. widgets () {
  67. return this.node?.widgets || []
  68. },
  69. widget () {
  70. return this.widgets[this.selectedWidgetIndex] || this.node
  71. },
  72. hasAudio () {
  73. if (this.node) {
  74. if (this.node.bgm.length) {
  75. return true
  76. }
  77. return this.node.widgets.some(widget => widget.mute === 0 && (widget.sources?.length || widget.url))
  78. }
  79. return false
  80. },
  81. bgmUrl () {
  82. return this.bgm ? getAssetUrl(this.bgm) : null
  83. },
  84. hasNext () {
  85. return !this.muted && this.node?.bgm.length > 1
  86. }
  87. },
  88. watch: {
  89. 'node.bgm' () {
  90. this.setBgm()
  91. }
  92. },
  93. mounted () {
  94. this.initCanvas(this.program.detail)
  95. this.checkRatio()
  96. window.addEventListener('resize', this.checkRatio)
  97. },
  98. methods: {
  99. onBack () {
  100. window.close()
  101. },
  102. toggleMute () {
  103. this.muted = !this.muted
  104. },
  105. setBgm () {
  106. this.$bgmIndex = -1
  107. this.switchBgm()
  108. },
  109. switchBgm () {
  110. const { bgm, toggleType } = this.node
  111. this.$bgmIndex = switchToNext(toggleType, bgm, this.$bgmIndex)
  112. this.bgm = bgm[this.$bgmIndex]?.keyName
  113. },
  114. onAudioEnded () {
  115. if (this.node.bgm.length > 1) {
  116. this.switchBgm()
  117. }
  118. this.$refs.audio.play()
  119. },
  120. onAudioError (e) {
  121. console.warn('audio error', e)
  122. if (this.node?.bgm.length > 1) {
  123. this.switchBgm()
  124. }
  125. },
  126. checkRatio () {
  127. const wrapper = this.$refs.wrapper
  128. if (wrapper && this.node) {
  129. const width = wrapper.offsetWidth - this.padding * 2
  130. const height = wrapper.offsetHeight - this.padding * 2
  131. const { width: cWidth, height: cHeight } = this.node
  132. const wScale = width / cWidth
  133. const hScale = height / cHeight
  134. if (cWidth > width || cHeight > height) {
  135. this.scale = Math.min(wScale, hScale) * 100 | 0
  136. this.maxScale = Math.max(1, wScale, hScale) * 100 | 0
  137. } else {
  138. this.scale = 100
  139. this.maxScale = Math.max(2, wScale, hScale) * 100 | 0
  140. }
  141. this.minScale = this.scale
  142. console.log('width: layout -> window', cWidth, '->', width, wScale)
  143. console.log('height: layout -> window', cHeight, '->', height, hScale)
  144. console.log(this.scale, this.maxScale)
  145. }
  146. },
  147. initCanvas (canvas) {
  148. this.node = create(canvas)
  149. console.log(this.node)
  150. }
  151. }
  152. }