圣诞节星星飘落的效果(微信小程序版)

圣诞节快到啦~🎄🎄🎄🎄咱们也试着做做小程序版本的星星✨飘落效果吧

在微信小程序内如何实现雪花飘落,星星飘落的效果呢~

先来个效果图:

1576672109245.gif

一步一步来:

页面内容wxml,先简单切个图吧。

复制代码
<view class="container">
  <image src="https://qiniu-image.qtshe.com/merry_001.jpg" mode="widthFix"/>
  <image src="https://qiniu-image.qtshe.com/merry_02.jpg" alt="" mode="widthFix"/>
  <image src="https://qiniu-image.qtshe.com/merry_03.jpg" alt="" mode="widthFix"/>
  <image src="https://qiniu-image.qtshe.com/merry_04.jpg" alt="" mode="widthFix"/>
  <image src="https://qiniu-image.qtshe.com/merry_05.jpg" alt="" mode="widthFix"/>
  <image src="https://qiniu-image.qtshe.com/merry_06.jpg" alt="" mode="widthFix"/>
  <image src="https://qiniu-image.qtshe.com/merry_07.jpg" alt="" mode="widthFix"/>
</view>
<canvas  canvas-id="myCanvas" />

页面样式wxss,因为切片用的不太熟练,图片之间有个2rpx的空隙。

复制代码
.container {
  height: 100%;
  box-sizing: border-box;
  min-height: 100vh;
}

image {
  width: 100%;
  display: block;
  margin-top: -2rpx;  //不会切图造的孽
}

canvas {
  width: 100%; 
  min-height:100vh; 
  position: fixed;
  top: 0;
  z-index: 888;
}

重点JS:

复制代码
//获取应用实例
const app = getApp()
// 存储所有的星星
const stars = []
// 下落的加速度
const G = 0.01
const stime = 60

// 速度上限,避免速度过快
const SPEED_LIMIT_X = 1
const SPEED_LIMIT_Y = 1

const W = wx.getSystemInfoSync().windowWidth
const H = wx.getSystemInfoSync().windowHeight
const starImage = 'https://qiniu-image.qtshe.com/WechatIMG470.png' //星星素材

Page({
  onLoad() {
    this.setAudioPlay()
  },
  onShow() {
    this.createStar()
  },
  createStar() {
    let starCount = 350 //星星总的数量
    let starNum = 0 //当前生成星星数
    let deltaTime = 0
    let ctx = wx.createCanvasContext('myCanvas')
    let requestAnimationFrame = (() => {
      return (callback) => {
        setTimeout(callback, 1000 / stime)
      }
    })()
    starLoop()

    function starLoop() {
      requestAnimationFrame(starLoop)
      ctx.clearRect(0, 0, W, H)
      deltaTime = 20 //每次增加的星星数量
      starNum += deltaTime
      if (starNum > starCount) {
        stars.push(
          new Star(Math.random() * W, 0, Math.random() * 5 + 5)
        );
        starNum %= starCount
      }
      stars.map((s, i) => { //重复绘制
        s.update()
        s.draw()
        if (s.y >= H) { //大于屏幕高度的就从数组里去掉
          stars.splice(i, 1)
        }
      })
      ctx.draw()
    }

    function Star(x, y, radius) {
      this.x = x
      this.y = y
      this.sx = 0
      this.sy = 0
      this.deg = 0
      this.radius = radius
      this.ax = Math.random() < 0.5 ? 0.005 : -0.005
    }

    Star.prototype.update = function() {
      const deltaDeg = Math.random() * 0.6 + 0.2

      this.sx += this.ax
      if (this.sx >= SPEED_LIMIT_X || this.sx <= -SPEED_LIMIT_X) {
        this.ax *= -1
      }

      if (this.sy < SPEED_LIMIT_Y) {
        this.sy += G
      }

      this.deg += deltaDeg
      this.x += this.sx
      this.y += this.sy
    }

    Star.prototype.draw = function() {
      const radius = this.radius
      ctx.save()
      ctx.translate(this.x, this.y)
      ctx.rotate(this.deg * Math.PI / 180)
      ctx.drawImage(starImage, -radius, -radius * 1.8, radius * 2, radius * 2)
      ctx.restore()
    }
  },
  setAudioPlay() {
    const adctx = wx.createInnerAudioContext()
    adctx.autoplay = true
    adctx.loop = true
    adctx.src = 'https://dn-qtshe.qbox.me/Jingle%20Bells.mp3'
    adctx.onPlay(() => {
      console.log('开始播放')
      adctx.play()
    })
  }
})

以上只是简单实现了一个星星飘落的效果,预览的时候需要开启不校验合法域名哦~

目前还有更优的h5版本,使用Three.js实现的,在小程序内使用Three.js对于我来说有点打脑壳,先把效果分享出来吧。圣诞快乐

如果有需要的可以留言,我可以整理下发源码给你。

作者:人类进化又没带我

链接:https://www.jianshu.com/p/30d2e487f393

来源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

相关推荐
低代码布道师7 小时前
互联网医院04科室管理
低代码·小程序
说私域11 小时前
分享经济应用:以“开源链动2+1模式AI智能名片S2B2C商城小程序”为例
人工智能·小程序·开源
一 乐15 小时前
智慧养老|基于springboot+小程序社区养老保障系统设计与实现(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端·小程序
小小王app小程序开发17 小时前
盈利 + 留存双驱动:分销商城小程序的增长法则与落地指南
小程序
不如摸鱼去17 小时前
uni-app 也能远程调试?使用 PageSpy 打开调试的新大门!
前端·小程序·uni-app
峰兄19830518 小时前
8266实现Modbus TCP协议转RTU串口通讯之旅
小程序
黑马源码库miui5208619 小时前
JAVA成人用品商城系统源码微信小程序+h5+安卓+ios
android·java·微信小程序
UI设计兰亭妙微19 小时前
从“功能堆砌“到“体验至上“的蜕变之路:兰亭妙微如何助力“臻选生活馆“实现小程序重生与业绩倍增
小程序·小程序开发
万岳软件开发小城19 小时前
开发一套私域直播 APP/Web/小程序需要哪些核心模块?完整技术清单来了
小程序·php·直播带货系统源码·直播带货软件开发·私域直播系统源码·私域直播平台搭建·私域直播软件开发
计算机毕设指导620 小时前
基于微信小程序的篮球场馆预订系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven