almost-lottery.vue 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. <template>
  2. <view class="almost-lottery" :style="{ width: canvasWidth + 40 + 'px', height: canvasHeight + 40 + 'px'}">
  3. <view class="almost-lottery__wrap" :style="{width: canvasWidth + canvasMarginTotal + 'px', height: canvasHeight + canvasMarginTotal + 'px'}">
  4. <!-- #ifdef MP-ALIPAY -->
  5. <canvas :class="className" :id="canvasId" :width="canvasWidth" :height="canvasHeight" :style="{
  6. width: canvasWidth + 'px',
  7. height: canvasHeight + 'px'
  8. }" />
  9. <!-- #endif -->
  10. <!-- #ifndef MP-ALIPAY -->
  11. <canvas :class="className" :canvas-id="canvasId" :width="canvasWidth" :height="canvasHeight" :style="{
  12. width: canvasWidth + 'px',
  13. height: canvasHeight + 'px'
  14. }" />
  15. <!-- #endif -->
  16. <image class="canvas-img" :src="lotteryImg" :style="{
  17. width: canvasWidth + canvasMarginTotal + 'px',
  18. height: canvasHeight + canvasMarginTotal + 'px',
  19. transform: `rotate(${canvasAngle + targetAngle}deg)`,
  20. transitionDuration: `${transitionDuration}s`
  21. }"
  22. v-if="lotteryImg"></image>
  23. <view class="almost-lottery__action" :style="{
  24. width: actionSize + 'px',
  25. height: actionSize + 'px'
  26. }" @click="handleActionStart"></view>
  27. <!-- 为了兼容 app 端 ctx.measureText 所需的标签 -->
  28. <text class="almost-lottery__measureText">{{ measureText }}</text>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. import { pathToBase64 } from '@/almost-utils/image-tools.js'
  34. import { getStore, setStore, clearStore, downloadFile } from '@/almost-utils/almost-utils.js'
  35. export default {
  36. name: 'AlmostLottery',
  37. props: {
  38. // canvas 宽度
  39. canvasWidth: {
  40. type: Number,
  41. default: 240
  42. },
  43. // canvas 高度
  44. canvasHeight: {
  45. type: Number,
  46. default: 240
  47. },
  48. // 奖品列表
  49. prizeList: {
  50. type: Array,
  51. required: true,
  52. validator: (value) => {
  53. return value.length > 1
  54. }
  55. },
  56. // 中奖奖品在列表中的下标
  57. prizeIndex: {
  58. type: Number,
  59. required: true
  60. },
  61. // 奖品区块对应背景颜色
  62. colors: {
  63. type: Array,
  64. default: () => [
  65. '#FFFFFF',
  66. '#FFE9AA'
  67. ]
  68. },
  69. // 旋转动画时间 单位s
  70. duration: {
  71. type: Number,
  72. default: 8
  73. },
  74. // 旋转的圈数
  75. ringCount: {
  76. type: Number,
  77. default: 8
  78. },
  79. // 指针位置
  80. pointerPosition: {
  81. type: String,
  82. default: 'edge',
  83. validator: (value) => {
  84. return value === 'edge' || value === 'middle'
  85. }
  86. },
  87. // 字体颜色
  88. fontColor: {
  89. type: String,
  90. default: '#C30B29'
  91. },
  92. // 文字的大小
  93. fontSize: {
  94. type: Number,
  95. default: 12
  96. },
  97. // 奖品文字多行情况下的行高
  98. lineHeight: {
  99. type: Number,
  100. default: 16
  101. },
  102. // 奖品名称所对应的 key 值
  103. strKey: {
  104. type: String,
  105. default: 'name'
  106. },
  107. // 奖品文字总长度限制
  108. strMaxLen: {
  109. type: Number,
  110. default: 12
  111. },
  112. // 奖品文字多行情况下第一行文字长度
  113. strLineLen: {
  114. type: Number,
  115. default: 6
  116. },
  117. // 奖品图片的宽
  118. imageWidth: {
  119. type: Number,
  120. default: 30
  121. },
  122. // 奖品图片的高
  123. imageHeight: {
  124. type: Number,
  125. default: 30
  126. },
  127. // 转盘绘制成功的提示
  128. successMsg: {
  129. type: String,
  130. default: '奖品准备就绪,快来参与抽奖吧'
  131. },
  132. // 转盘绘制失败的提示
  133. failMsg: {
  134. type: String,
  135. default: '奖品仍在准备中,请稍后再来...'
  136. },
  137. // 是否开启画板的缓存
  138. canvasCached: {
  139. type: Boolean,
  140. default: true
  141. },
  142. // 内圈与外圈的间距
  143. canvasMargin: {
  144. type: Number,
  145. default: 5
  146. }
  147. },
  148. data() {
  149. return {
  150. // 画板className
  151. className: 'almost-lottery__canvas',
  152. // 画板标识
  153. canvasId: 'almostLotteryCanvas',
  154. // 画板导出的图片
  155. lotteryImg: '',
  156. // 旋转到奖品目标需要的角度
  157. targetAngle: 0,
  158. // 旋转动画时间 单位 s
  159. transitionDuration: 0,
  160. // 是否正在旋转
  161. isRotate: false,
  162. // 当前停留在那个奖品的序号
  163. stayIndex: 0,
  164. // 当前中奖奖品的序号
  165. targetIndex: 0,
  166. // 是否存在可用的缓存转盘图
  167. isCacheImg: false,
  168. oldLotteryImg: '',
  169. // 解决 app 不支持 measureText 的问题
  170. // app 已在 2.9.3 的版本中提供了对 measureText 的支持,将在后续版本逐渐稳定后移除相关兼容代码
  171. measureText: ''
  172. }
  173. },
  174. computed: {
  175. // 根据奖品列表计算 canvas 旋转角度
  176. canvasAngle() {
  177. let prizeCount = this.prizeList.length
  178. let prizeClip = 360 / prizeCount
  179. let result = 0
  180. let diffNum = 90 / prizeClip
  181. if (this.pointerPosition === 'edge') {
  182. result = -(prizeClip * diffNum)
  183. } else {
  184. result = -(prizeClip * diffNum + prizeClip / 2)
  185. }
  186. return result
  187. },
  188. // 外圆的半径
  189. outsideRadius() {
  190. return this.canvasWidth / 2
  191. },
  192. // 内圆的半径
  193. insideRadius() {
  194. return 20
  195. },
  196. // 字体的半径
  197. textRadius() {
  198. return this.fontSize / 2
  199. },
  200. // 根据画板的宽度计算奖品文字与中心点的距离
  201. textDistance() {
  202. const textZeroY = Math.round(this.outsideRadius - (this.insideRadius / 2))
  203. return textZeroY - this.textRadius
  204. },
  205. // 设备像素密度
  206. pixelRatio() {
  207. return uni.getSystemInfoSync().pixelRatio
  208. },
  209. // 内圈与外圈的距离
  210. canvasMarginTotal () {
  211. let diffNum = 5
  212. let margin = this.canvasMargin * 2
  213. if (this.canvasWidth > 240) {
  214. return -(this.canvasWidth / 240 * 2) - margin
  215. } else if (this.canvasWidth < 240) {
  216. return diffNum + (this.canvasWidth / 240 * 2) - margin
  217. } else {
  218. return diffNum - margin
  219. }
  220. },
  221. // 抽奖按钮的宽高
  222. actionSize () {
  223. return this.canvasWidth / 2.4
  224. }
  225. },
  226. watch: {
  227. // 监听获奖序号的变动
  228. prizeIndex(newVal, oldVal) {
  229. if (newVal > -1) {
  230. this.targetIndex = newVal
  231. this.onRotateStart()
  232. } else {
  233. console.info('旋转结束,prizeIndex 已重置')
  234. }
  235. }
  236. },
  237. methods: {
  238. // 开始旋转
  239. onRotateStart() {
  240. // 奖品总数
  241. let prizeCount = this.prizeList.length
  242. let baseAngle = 360 / prizeCount
  243. let angles = 0
  244. if (this.targetAngle === 0) {
  245. // 第一次旋转
  246. // 因为第一个奖品是从0°开始的,即水平向右方向
  247. // 第一次旋转角度 = 270度 - (停留的序号-目标序号) * 每个奖品区间角度 - 每个奖品区间角度的一半 - canvas自身旋转的度数
  248. angles = (270 - (this.targetIndex - this.stayIndex) * baseAngle - baseAngle / 2) - this.canvasAngle
  249. } else {
  250. // 后续旋转
  251. // 后续继续旋转 就只需要计算停留的位置与目标位置的角度
  252. angles = -(this.targetIndex - this.stayIndex) * baseAngle
  253. }
  254. // 更新目前序号
  255. this.stayIndex = this.targetIndex
  256. // 转 8 圈,圈数越多,转的越快
  257. this.targetAngle += angles + 360 * this.ringCount
  258. // 计算转盘结束的时间,预加一些延迟确保转盘停止后触发结束事件
  259. let endTime = this.transitionDuration * 1000 + 100
  260. let endTimer = setTimeout(() => {
  261. clearTimeout(endTimer)
  262. endTimer = null
  263. this.isRotate = false
  264. this.$emit('draw-end')
  265. }, endTime)
  266. let resetPrizeTimer = setTimeout(() => {
  267. clearTimeout(resetPrizeTimer)
  268. resetPrizeTimer = null
  269. // 每次抽奖结束后都要重置父级附件的 prizeIndex
  270. this.$emit('reset-index')
  271. }, endTime + 50)
  272. },
  273. // 点击 开始抽奖 按钮
  274. handleActionStart() {
  275. if (this.isRotate) return
  276. this.isRotate = true
  277. this.$emit('draw-start')
  278. },
  279. // 渲染转盘
  280. async onCreateCanvas() {
  281. // 获取 canvas 画布
  282. const canvasId = this.canvasId
  283. const ctx = uni.createCanvasContext(canvasId, this)
  284. // canvas 的宽高
  285. let canvasW = this.canvasWidth
  286. let canvasH = this.canvasHeight
  287. // 根据奖品个数计算 角度
  288. let prizeCount = this.prizeList.length
  289. let baseAngle = Math.PI * 2 / prizeCount
  290. // 设置描边颜色
  291. ctx.setStrokeStyle('#FFBE04')
  292. // 设置字体和字号
  293. // #ifndef MP
  294. let fontFamily =
  295. '-apple-system, BlinkMacSystemFont, \'PingFang SC\', \'Helvetica Neue\', STHeiti, \'Microsoft Yahei\', Tahoma, Simsun, sans-serif'
  296. ctx.font = `${this.fontSize}px ${fontFamily}`
  297. // #endif
  298. // #ifdef MP
  299. ctx.setFontSize(this.fontSize)
  300. // #endif
  301. // 注意,开始画的位置是从0°角的位置开始画的。也就是水平向右的方向。
  302. // 画具体内容
  303. for (let i = 0; i < prizeCount; i++) {
  304. let prizeItem = this.prizeList[i]
  305. // 当前角度
  306. let angle = i * baseAngle
  307. // 保存当前画布的状态
  308. ctx.save()
  309. // 开始画内容
  310. ctx.beginPath()
  311. // 开始画圆弧
  312. // x => 圆弧对应的圆心横坐标 x
  313. // y => 圆弧对应的圆心横坐标 y
  314. // radius => 圆弧的半径大小
  315. // startAngle => 圆弧开始的角度,单位是弧度
  316. // endAngle => 圆弧结束的角度,单位是弧度
  317. // anticlockwise(可选) => 绘制方向,true 为逆时针,false 为顺时针
  318. ctx.arc(canvasW * 0.5, canvasH * 0.5, this.outsideRadius, angle, angle + baseAngle, false)
  319. ctx.arc(canvasW * 0.5, canvasH * 0.5, this.insideRadius, angle + baseAngle, angle, true)
  320. // 开始链接线条
  321. ctx.stroke()
  322. // 每个奖品区块背景填充颜色
  323. if (this.colors.length === 2) {
  324. ctx.setFillStyle(this.colors[i % 2])
  325. } else {
  326. ctx.setFillStyle(this.colors[i])
  327. }
  328. // 填充颜色
  329. ctx.fill()
  330. // 开始绘制奖品内容
  331. // 重新映射画布上的 (0,0) 位置
  332. let translateX = canvasW * 0.5 + Math.cos(angle + baseAngle / 2) * this.textDistance
  333. let translateY = canvasH * 0.5 + Math.sin(angle + baseAngle / 2) * this.textDistance
  334. ctx.translate(translateX, translateY)
  335. // 绘制奖品名称
  336. ctx.setFillStyle(this.fontColor)
  337. let rewardName = this.strLimit(prizeItem[this.strKey])
  338. // rotate方法旋转当前的绘图,因为文字是和当前扇形中心线垂直的
  339. ctx.rotate(angle + (baseAngle / 2) + (Math.PI / 2))
  340. // 设置文本位置并处理换行
  341. // 是否需要换行
  342. let isLineBreak = rewardName.length > this.strLineLen
  343. let textOffsetX = this.fontSize === 12 ? 0 : this.textRadius
  344. if (isLineBreak) {
  345. // 获得多行文本数组
  346. rewardName = rewardName.substring(0, this.strLineLen) + ',' + rewardName.substring(this.strLineLen)
  347. let rewardNames = rewardName.split(',')
  348. // 循环文本数组,计算每一行的文本宽度
  349. for (let j = 0; j < rewardNames.length; j++) {
  350. if (ctx.measureText && ctx.measureText(rewardNames[j]).width) {
  351. // 文本的宽度信息
  352. let tempStrSize = ctx.measureText(rewardNames[j])
  353. ctx.fillText(rewardNames[j], -(tempStrSize.width / 2 + textOffsetX), j * this.lineHeight)
  354. } else {
  355. this.measureText = rewardNames[j]
  356. // 等待页面重新渲染
  357. await this.$nextTick()
  358. let textWidth = await this.getTextWidth()
  359. ctx.fillText(rewardNames[j], -(textWidth / 2 + textOffsetX), j * this.lineHeight)
  360. // console.log(rewardNames[j], textWidth, i)
  361. }
  362. }
  363. } else {
  364. if (ctx.measureText && ctx.measureText(rewardName).width) {
  365. // 文本的宽度信息
  366. let tempStrSize = ctx.measureText(rewardName)
  367. ctx.fillText(rewardName, -(tempStrSize.width / 2 + textOffsetX), 0)
  368. } else {
  369. this.measureText = rewardName
  370. // 等待页面重新渲染
  371. await this.$nextTick()
  372. let textWidth = await this.getTextWidth()
  373. ctx.fillText(rewardName, -(textWidth / 2 + textOffsetX), 0)
  374. }
  375. }
  376. // 绘制奖品图片
  377. if (prizeItem.prizeImage) {
  378. // App-Android平台 系统 webview 更新到 Chrome84+ 后 canvas 组件绘制本地图像 uni.canvasToTempFilePath 会报错
  379. // 统一将图片处理成 base64
  380. // https://ask.dcloud.net.cn/question/103303
  381. let reg = /^(https|http)/g
  382. // 处理远程图片
  383. if (reg.test(prizeItem.prizeImage)) {
  384. console.warn('###当前数据列表中的奖品图片为网络图片,开始下载图片...###')
  385. let res = await downloadFile(prizeItem.prizeImage)
  386. console.log('处理远程图片', res)
  387. if (res.ok) {
  388. let tempFilePath = res.tempFilePath
  389. // #ifndef MP
  390. prizeItem.prizeImage = await pathToBase64(tempFilePath)
  391. // #endif
  392. // #ifdef MP
  393. prizeItem.prizeImage = tempFilePath
  394. // #endif
  395. }
  396. } else {
  397. // #ifndef MP
  398. prizeItem.prizeImage = await pathToBase64(prizeItem.prizeImage)
  399. // #endif
  400. }
  401. ctx.drawImage(prizeItem.prizeImage, -(this.imageWidth / 2), canvasW / 10, this.imageWidth, this.imageHeight)
  402. }
  403. ctx.restore()
  404. }
  405. // 保存绘图并导出图片
  406. ctx.draw(true, () => {
  407. let drawTimer = setTimeout(() => {
  408. clearTimeout(drawTimer)
  409. drawTimer = null
  410. // #ifdef MP-ALIPAY
  411. ctx.toTempFilePath({
  412. destWidth: this.canvasWidth * this.pixelRatio,
  413. destHeight: this.canvasHeight * this.pixelRatio,
  414. success: (res) => {
  415. // console.log(res.apFilePath)
  416. this.handlePrizeImg({
  417. ok: true,
  418. data: res.apFilePath,
  419. msg: '画布导出生成图片成功'
  420. })
  421. },
  422. fail: (err) => {
  423. this.handlePrizeImg({
  424. ok: false,
  425. data: err,
  426. msg: '画布导出生成图片失败'
  427. })
  428. }
  429. })
  430. // #endif
  431. // #ifndef MP-ALIPAY
  432. uni.canvasToTempFilePath({
  433. destWidth: this.canvasWidth * this.pixelRatio,
  434. destHeight: this.canvasHeight * this.pixelRatio,
  435. canvasId: this.canvasId,
  436. success: (res) => {
  437. // 在 H5 平台下,tempFilePath 为 base64
  438. // console.log(res.tempFilePath)
  439. this.handlePrizeImg({
  440. ok: true,
  441. data: res.tempFilePath,
  442. msg: '画布导出生成图片成功'
  443. })
  444. },
  445. fail: (err) => {
  446. this.handlePrizeImg({
  447. ok: false,
  448. data: err,
  449. msg: '画布导出生成图片失败'
  450. })
  451. }
  452. }, this)
  453. // #endif
  454. }, 500)
  455. })
  456. },
  457. // 处理导出的图片
  458. handlePrizeImg(res) {
  459. if (res.ok) {
  460. let data = res.data
  461. if (!this.canvasCached) {
  462. this.lotteryImg = data
  463. this.handlePrizeImgSuc(res)
  464. return
  465. }
  466. // #ifndef H5
  467. if (this.isCacheImg) {
  468. uni.getSavedFileList({
  469. success: (sucRes) => {
  470. let fileList = sucRes.fileList
  471. // console.log('getSavedFileList Cached', fileList)
  472. let cached = false
  473. for (let i = 0; i < fileList.length; i++) {
  474. let item = fileList[i]
  475. if (item.filePath === data) {
  476. cached = true
  477. this.lotteryImg = data
  478. console.info('经查,本地缓存中存在转盘图可用,本次将不再绘制转盘')
  479. this.handlePrizeImgSuc(res)
  480. break
  481. }
  482. }
  483. if (!cached) {
  484. console.info('经查,本地缓存中存在转盘图不可用,需要重新初始化转盘绘制')
  485. this.initCanvasDraw()
  486. }
  487. },
  488. fail: (err) => {
  489. this.initCanvasDraw()
  490. }
  491. })
  492. } else {
  493. uni.saveFile({
  494. tempFilePath: data,
  495. success: (sucRes) => {
  496. let filePath = sucRes.savedFilePath
  497. // console.log('saveFile', filePath)
  498. setStore('lotteryImg', filePath)
  499. this.lotteryImg = filePath
  500. this.handlePrizeImgSuc({
  501. ok: true,
  502. data: filePath,
  503. msg: '画布导出生成图片成功'
  504. })
  505. },
  506. fail: (err) => {
  507. this.handlePrizeImg({
  508. ok: false,
  509. data: err,
  510. msg: '画布导出生成图片失败'
  511. })
  512. }
  513. })
  514. }
  515. // #endif
  516. // #ifdef H5
  517. console.info('当前为 H5 端,直接使用导出的/缓存中的 base64 图')
  518. setStore('lotteryImg', data)
  519. this.lotteryImg = data
  520. this.handlePrizeImgSuc(res)
  521. // #endif
  522. } else {
  523. console.error('处理导出的图片失败', res)
  524. uni.hideLoading()
  525. // #ifdef H5
  526. console.error('###当前为 H5 端,下载网络图片需要后端配置允许跨域###')
  527. // #endif
  528. // #ifdef MP
  529. console.error('###当前为小程序端,下载网络图片需要配置域名白名单###')
  530. // #endif
  531. }
  532. },
  533. // 处理图片完成
  534. handlePrizeImgSuc (res) {
  535. uni.hideLoading()
  536. this.$emit('finish', {
  537. ok: res.ok,
  538. data: res.data,
  539. msg: res.ok ? this.successMsg : this.failMsg
  540. })
  541. },
  542. // 兼容 app 端不支持 ctx.measureText
  543. // 已知问题:初始绘制时,低端安卓机 平均耗时 2s
  544. getTextWidth() {
  545. return new Promise((resolve, reject) => {
  546. uni.createSelectorQuery().in(this).select('.almost-lottery__measureText').fields({
  547. size: true,
  548. }, (res) => {
  549. resolve(res.width)
  550. }).exec()
  551. })
  552. },
  553. // 处理文字溢出
  554. strLimit(value) {
  555. let maxLength = this.strMaxLen
  556. if (!value || !maxLength) return value
  557. return value.length > maxLength ? value.slice(0, maxLength - 1) + '...' : value
  558. },
  559. // 检查本地缓存中是否存在转盘图
  560. checkCacheImg () {
  561. console.log('检查本地缓存中是否存在转盘图')
  562. // 检查是否已有缓存的转盘图
  563. // 检查是否与本次奖品数据相同
  564. this.oldLotteryImg = getStore('lotteryImg')
  565. let oldPrizeList = getStore('prizeList')
  566. let newPrizeList = JSON.stringify(this.prizeList)
  567. if (this.oldLotteryImg) {
  568. if (oldPrizeList === newPrizeList) {
  569. console.log(`经查,本地缓存中存在转盘图 => ${this.oldLotteryImg}`)
  570. this.isCacheImg = true
  571. console.log('需要继续判断这张缓存图是否可用')
  572. this.handlePrizeImg({
  573. ok: true,
  574. data: this.oldLotteryImg,
  575. msg: '画布导出生成图片成功'
  576. })
  577. return
  578. }
  579. }
  580. console.log('经查,本地缓存中不存在转盘图')
  581. this.initCanvasDraw()
  582. },
  583. // 初始化绘制
  584. initCanvasDraw () {
  585. console.log('开始初始化转盘绘制')
  586. this.isCacheImg = false
  587. this.lotteryImg = ''
  588. clearStore('lotteryImg')
  589. setStore('prizeList', this.prizeList)
  590. this.onCreateCanvas()
  591. }
  592. },
  593. mounted() {
  594. this.$nextTick(() => {
  595. let stoTimer = setTimeout(() => {
  596. clearTimeout(stoTimer)
  597. stoTimer = null
  598. if (this.canvasCached) {
  599. this.checkCacheImg()
  600. } else {
  601. this.onCreateCanvas()
  602. }
  603. this.transitionDuration = this.duration
  604. }, 50)
  605. })
  606. }
  607. }
  608. </script>
  609. <style lang="scss" scoped>
  610. $lotteryBgUrl: '~static/almost-lottery/almost-lottery__bg';
  611. $actionBgUrl: '~static/almost-lottery/almost-lottery__action';
  612. .almost-lottery {
  613. display: flex;
  614. justify-content: center;
  615. align-items: center;
  616. margin: 0 auto;
  617. background-repeat: no-repeat;
  618. background-position: center center;
  619. background-size: contain;
  620. background-image: url($lotteryBgUrl + ".png");
  621. @media (-webkit-min-device-pixel-ratio: 2), (min-device-pixel-ratio: 2) {
  622. background-image: url($lotteryBgUrl + "2x.png");
  623. }
  624. @media (-webkit-min-device-pixel-ratio: 3), (min-device-pixel-ratio: 3) {
  625. background-image: url($lotteryBgUrl + "3x.png");
  626. }
  627. }
  628. .almost-lottery__wrap {
  629. position: relative;
  630. display: flex;
  631. justify-content: center;
  632. align-items: center;
  633. }
  634. .almost-lottery__canvas {
  635. position: absolute;
  636. left: -9999px;
  637. opacity: 0;
  638. display: flex;
  639. justify-content: center;
  640. align-items: center;
  641. }
  642. .almost-lottery__action {
  643. position: absolute;
  644. background-repeat: no-repeat;
  645. background-position: center center;
  646. background-size: contain;
  647. background-image: url($actionBgUrl + ".png");
  648. @media (-webkit-min-device-pixel-ratio: 2), (min-device-pixel-ratio: 2) {
  649. background-image: url($actionBgUrl + "2x.png");
  650. }
  651. @media (-webkit-min-device-pixel-ratio: 3), (min-device-pixel-ratio: 3) {
  652. background-image: url($actionBgUrl + "3x.png");
  653. }
  654. }
  655. .almost-lottery__measureText {
  656. position: absolute;
  657. left: 0;
  658. top: 0;
  659. white-space: nowrap;
  660. font-size: 12px;
  661. opacity: 0;
  662. }
  663. .canvas-img {
  664. display: block;
  665. transition: transform cubic-bezier(.34, .12, .05, .95);
  666. }
  667. </style>