JigsawGame.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <template>
  2. <div
  3. class="jigsaw-game"
  4. >
  5. <div class="left-wrapper">
  6. <div class="object-wrapper">
  7. <object
  8. ref="svgContainer"
  9. :data="require(`@/assets/images/jigsaw-game-${$route.query.sceneL2Idx}/compound.svg`)"
  10. type=""
  11. />
  12. </div>
  13. <div
  14. ref="forDrop"
  15. class="for-drop"
  16. :style="{
  17. left: leftForDrop + 'px',
  18. top: topForDrop + 'px',
  19. width: widthForDrop + 'px',
  20. height: heightForDrop + 'px',
  21. }"
  22. @dragover.prevent
  23. @drop.prevent="onDrop"
  24. />
  25. </div>
  26. <div class="jigsaw-list">
  27. <h1>
  28. <img
  29. class=""
  30. :src="require(`@/assets/images/title-level2-${$route.query.sceneL2Idx}.png`)"
  31. alt=""
  32. draggable="false"
  33. >
  34. </h1>
  35. <li
  36. v-for="(jigsawItem, idx) in jigsawItemsFlatten"
  37. :key="idx"
  38. >
  39. <img
  40. class=""
  41. :style="{
  42. cursor: !jigsawProgressSceneL2.isJigsawDone && jigsawItem.hasGot && !jigsawItem.hasPut ? 'grab' : 'default',
  43. }"
  44. :src="require(`@/assets/images/jigsaw-game-${$route.query.sceneL2Idx}/jigsaw${jigsawItem.hasGot ? '' : '-shade'}/${jigsawItem.name}`)"
  45. alt=""
  46. :draggable="!jigsawProgressSceneL2.isJigsawDone && jigsawItem.hasGot && !jigsawItem.hasPut ? true : false"
  47. @dragstart="(e) => {
  48. onDragStart(e, jigsawItem.name, idx)
  49. }"
  50. @dragend="onDragEnd(jigsawItem.name)"
  51. >
  52. <div
  53. class="jigsaw-name"
  54. :title="jigsawItem.name.split('.')[1]"
  55. >
  56. {{ jigsawItem.name.split('.')[1] }}
  57. </div>
  58. </li>
  59. </div>
  60. </div>
  61. </template>
  62. <script>
  63. import useWindowSizeAdaptor from '@/useFunctions/useWindowSizeAdaptor.js'
  64. import { ElMessageBox } from 'element-plus'
  65. export default {
  66. name: 'JigsawGame',
  67. setup () {
  68. const { windowSizeWhenDesign, unit } = useWindowSizeAdaptor()
  69. return {
  70. windowSizeWhenDesign,
  71. unit
  72. }
  73. },
  74. data() {
  75. return {
  76. jigsawItemsFlatten: [],
  77. leftForDrop: 0,
  78. topForDrop: 0,
  79. widthForDrop: 0,
  80. heightForDrop: 0,
  81. imgFrameThicknessWhenDesign: '15',
  82. }
  83. },
  84. computed: {
  85. ...mapState([
  86. 'gameProgress',
  87. ]),
  88. jigsawProgressSceneL2() {
  89. return this.gameProgress.jigsawProgress[Number(this.$route.query.sceneL2Idx)]
  90. },
  91. },
  92. mounted() {
  93. for (const jigsawProgressSceneL3 of this.jigsawProgressSceneL2.children) {
  94. for (const jigsawItem of jigsawProgressSceneL3.jigsawList) {
  95. this.jigsawItemsFlatten.push({
  96. name: jigsawItem,
  97. hasGot: jigsawProgressSceneL3.hasGotJigsaw,
  98. hasPut: false,
  99. })
  100. }
  101. }
  102. setTimeout(() => {
  103. console.log('进入timeout!')
  104. if (!this.jigsawProgressSceneL2.isJigsawDone) {
  105. console.log('要hide!')
  106. const objectDocument = this.$refs.svgContainer.contentDocument
  107. const gList = objectDocument.getElementsByTagName('g')
  108. for (let index = 1; index < gList.length; index++) {
  109. const element = gList[index]
  110. element.setAttribute('visibility', 'hidden')
  111. }
  112. }
  113. }, 300)
  114. },
  115. unmounted() {
  116. },
  117. methods: {
  118. ...mapMutations([
  119. 'recordJigsawDone',
  120. ]),
  121. onDragStart(e, jigsawImgName, idx) {
  122. // 不知道为啥,在svg内部元素上无法触发drop事件,求助gpt也没用。只好用一个透明方块放在需要能drop的svg内部元素上面。
  123. // reset div for drop
  124. this.leftForDrop = 0
  125. this.topForDrop = 0
  126. this.widthForDrop = 0
  127. this.heightForDrop = 0
  128. // get id
  129. let temp = jigsawImgName.split('.')
  130. temp.pop()
  131. const id = `_${temp.join('.')}-剪影_图像`
  132. // get elements
  133. const objectDocument = this.$refs.svgContainer.contentDocument
  134. const svgEl = objectDocument.getElementsByTagName('svg')[0]
  135. const gTarget = objectDocument.getElementById(id)
  136. const imgTarget = gTarget.getElementsByTagName('image')[0]
  137. // get svg original size from viewBox attribute
  138. const svgWidth = Number(svgEl.getAttribute('viewBox').split(' ')[2])
  139. const svgHeight = Number(svgEl.getAttribute('viewBox').split(' ')[3])
  140. console.log('svg original size by viewBox attribute: ', svgWidth, svgHeight)
  141. // show g element
  142. gTarget.setAttribute('visibility', 'show')
  143. // save drag info
  144. e.dataTransfer.setData('text/plain', jigsawImgName)
  145. e.dataTransfer.setData('text/html', String(idx)) // 并非真的html类型,只是为了传输数据
  146. // get image's original size
  147. const imageWidthOriginal = Number(imgTarget.getAttribute('width'))
  148. const imageHeightOriginal = Number(imgTarget.getAttribute('height'))
  149. console.log("image's original size: ", imageWidthOriginal, imageHeightOriginal)
  150. // get image's original top left from transform attribute
  151. let imageLeftOriginal = 0
  152. let imageTopOriginal = 0
  153. const transformString = imgTarget.getAttribute('transform')
  154. if (transformString) {
  155. const regForTransform = /translate\((.+)\)/
  156. const matchRes = transformString.match(regForTransform)
  157. if (matchRes) {
  158. imageLeftOriginal = Number(matchRes[1].split(' ')[0])
  159. imageTopOriginal = Number(matchRes[1].split(' ')[1]) || 0
  160. } else {
  161. imageLeftOriginal = 0
  162. imageTopOriginal = 0
  163. }
  164. } else {
  165. imageLeftOriginal = 0
  166. imageTopOriginal = 0
  167. }
  168. console.log("image's original top left: ", imageLeftOriginal, imageTopOriginal)
  169. // 计算画框粗细
  170. let imageFrameThicknessActual = 0
  171. if (this.unit === '100vh') {
  172. imageFrameThicknessActual = Number(this.imgFrameThicknessWhenDesign) / 1080 * window.innerHeight
  173. } else {
  174. imageFrameThicknessActual = Number(this.imgFrameThicknessWhenDesign) / 1920 * window.innerWidth
  175. }
  176. console.log("image frame's actual thickness: ", imageFrameThicknessActual)
  177. // compute image's actual left top
  178. const topLeft = utils.mapPosFromDraftToWindow({
  179. x: imageLeftOriginal,
  180. y: imageTopOriginal,
  181. }, 'cover', svgWidth, svgHeight, this.$refs.svgContainer.clientWidth, this.$refs.svgContainer.clientHeight)
  182. console.log("image's actual left top: ", topLeft.x, topLeft.y)
  183. const rightBottom = utils.mapPosFromDraftToWindow({
  184. x: imageLeftOriginal + imageWidthOriginal,
  185. y: imageTopOriginal + imageHeightOriginal,
  186. }, 'cover', svgWidth, svgHeight, this.$refs.svgContainer.clientWidth, this.$refs.svgContainer.clientHeight)
  187. console.log("image's actual right top: ", rightBottom.x, rightBottom.y)
  188. this.leftForDrop = topLeft.x + imageFrameThicknessActual
  189. this.topForDrop = topLeft.y + imageFrameThicknessActual
  190. this.widthForDrop = rightBottom.x - topLeft.x
  191. this.heightForDrop = rightBottom.y - topLeft.y
  192. console.log("image's actual ltwh for render: ", this.leftForDrop, this.topForDrop, this.widthForDrop, this.heightForDrop)
  193. },
  194. onDragEnd(jigsawImgName) {
  195. let temp = jigsawImgName.split('.')
  196. temp.pop()
  197. const id = `_${temp.join('.')}-剪影_图像`
  198. const objectDocument = this.$refs.svgContainer.contentDocument
  199. objectDocument.getElementById(id).setAttribute('visibility', 'hidden')
  200. },
  201. onDrop(e) {
  202. e.preventDefault()
  203. let jigsawImgName = e.dataTransfer.getData('text/plain')
  204. let temp = jigsawImgName.split('.')
  205. temp.pop()
  206. const id = `_${temp.join('.')}_图像`
  207. const objectDocument = this.$refs.svgContainer.contentDocument
  208. objectDocument.getElementById(id).setAttribute('visibility', 'show')
  209. const idx = Number(e.dataTransfer.getData('text/html'))
  210. this.jigsawItemsFlatten[idx].hasPut = true
  211. if (!this.jigsawItemsFlatten.find((item) => {
  212. return !item.hasPut
  213. })) {
  214. ElMessageBox.alert('', '拼图完成!', {
  215. confirmButtonText: '确定',
  216. callback: (action) => {
  217. console.log(action)
  218. },
  219. })
  220. this.recordJigsawDone(Number(this.$route.query.sceneL2Idx))
  221. }
  222. },
  223. onDragOver(e) {
  224. e.preventDefault()
  225. },
  226. },
  227. }
  228. </script>
  229. <style lang="less" scoped>
  230. .jigsaw-game {
  231. position: absolute;
  232. left: 0;
  233. top: 0;
  234. width: 100%;
  235. height: 100%;
  236. background-image: url(@/assets/images/bg-game.jpg);
  237. background-size: cover;
  238. background-repeat: no-repeat;
  239. background-position: center center;
  240. display: flex;
  241. justify-content: center;
  242. align-items: center;
  243. >.left-wrapper {
  244. width: calc(950 / v-bind('windowSizeWhenDesign') * v-bind('unit'));
  245. height: calc(628 / v-bind('windowSizeWhenDesign') * v-bind('unit'));
  246. background-image: url(@/assets/images/bg-jigsaw-game-page-frame.jpg);
  247. background-size: cover;
  248. background-repeat: no-repeat;
  249. background-position: center center;
  250. margin-right: calc(83 / v-bind('windowSizeWhenDesign') * v-bind('unit'));
  251. padding: calc(v-bind('imgFrameThicknessWhenDesign') / v-bind('windowSizeWhenDesign') * v-bind('unit'));
  252. position: relative;
  253. >.object-wrapper {
  254. width: 100%;
  255. height: 100%;
  256. overflow: hidden;
  257. >object {
  258. user-select: none;
  259. // width: 100%;
  260. }
  261. }
  262. >.for-drop {
  263. position: absolute;
  264. // background-color: red;
  265. // opacity: 0.2;
  266. z-index: 1;
  267. }
  268. }
  269. >.jigsaw-list {
  270. position: relative;
  271. width: calc(580 / v-bind('windowSizeWhenDesign') * v-bind('unit'));
  272. height: calc(648 / v-bind('windowSizeWhenDesign') * v-bind('unit'));
  273. padding-top: calc(8 / v-bind('windowSizeWhenDesign') * v-bind('unit'));
  274. overflow: auto;
  275. >h1 {
  276. position: absolute;
  277. left: 0;
  278. top: calc(-101 / v-bind('windowSizeWhenDesign') * v-bind('unit'));
  279. height: calc(74 / v-bind('windowSizeWhenDesign') * v-bind('unit'));;
  280. font-size: 0;
  281. >img {
  282. height: 100%;
  283. }
  284. }
  285. >li {
  286. display: inline-block;
  287. margin-right: calc(28 / v-bind('windowSizeWhenDesign') * v-bind('unit'));
  288. margin-bottom: calc(17 / v-bind('windowSizeWhenDesign') * v-bind('unit'));
  289. text-align: center;
  290. >img {
  291. width: calc(157 / v-bind('windowSizeWhenDesign') * v-bind('unit'));
  292. height: calc(157 / v-bind('windowSizeWhenDesign') * v-bind('unit'));
  293. background: #fff;
  294. margin-bottom: calc(15 / v-bind('windowSizeWhenDesign') * v-bind('unit'));
  295. }
  296. >.jigsaw-name {
  297. font-size: calc(20 / v-bind('windowSizeWhenDesign') * v-bind('unit'));
  298. font-family: Source Han Sans CN-Bold, Source Han Sans CN;
  299. font-weight: bold;
  300. color: #C26827;
  301. overflow: hidden;
  302. white-space: pre;
  303. text-overflow: ellipsis;
  304. user-select: none;
  305. }
  306. }
  307. }
  308. }
  309. </style>