tickAnimeItem.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. class TickAnime {
  2. constructor(imgLen, x, y, w, h, ctx,name,zIndex,imgName,imgBaseUrl) {
  3. this.imgLen = imgLen
  4. this.x = x
  5. this.y = y
  6. this.w = w
  7. this.h = h
  8. this.ctx = ctx
  9. this.name = name
  10. this.zIndex = zIndex
  11. this.imgName = imgName
  12. this.currentIndex = 0
  13. this.imgBaseUrl = imgBaseUrl
  14. this.loadingState = false
  15. this.clickState = false
  16. this.imgList = []
  17. this.getDrawImgUrl()
  18. }
  19. async getDrawImgUrl() {
  20. for (let i = 1; i < this.imgLen + 1; i++) {
  21. let imgHostUrl = await this.getImgInfo(i)
  22. this.imgList.push(imgHostUrl)
  23. }
  24. this.initDraw()
  25. }
  26. initDraw(){
  27. for(let i=0;i<this.imgList.length;i++){
  28. this.ctx.drawImage(this.imgList[i], this.x, this.y, this.w, this.h)
  29. }
  30. setTimeout(()=>{
  31. this.loadingState = true
  32. },1000)
  33. }
  34. getImgInfo(index) {
  35. return new Promise((resolev, reject) => {
  36. uni.getImageInfo({
  37. src: `${this.imgBaseUrl}/${this.imgName} (${index}).png`,
  38. success: (res) => {
  39. resolev(res.path)
  40. },
  41. fail(err) {
  42. reject(err)
  43. }
  44. })
  45. })
  46. }
  47. draw() {
  48. if (this.loadingState) {
  49. this.ctx.drawImage(this.imgList[this.currentIndex], this.x, this.y, this.w, this.h)
  50. }
  51. }
  52. update() {
  53. if (!this.clickState) {
  54. if (this.currentIndex < this.imgLen - 1) {
  55. this.currentIndex++
  56. } else {
  57. this.currentIndex = 1
  58. }
  59. }
  60. }
  61. }
  62. export default TickAnime