1.0.1 • Published 12 months ago

red-envelope-rain v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
12 months ago

red-envelope-rain

基于 canvas、requestAnimationFrame 的高性能红包雨效果

如何使用

npm i red-envelope-rain -S
import RedEnvelopeWar from 'red-envelope-rain'

this.rains = [
  {
    image: require('./img/shixin.png'),
    width: 80,
    height: 80,
    ratio: 7,
    score: 2,

    clicked: {
      image: require('./img/xiaosan.png'),
      width: 80,
      height: 80,
      translate: [-2, -2],
    },
  },
  {
    id: 2,
    image: require('./img/kong.png'),
    width: 80,
    height: 80,
    ratio: 3,
    score: 3,

    clicked: {
      image: require('./img/kong.png'),
      width: 80,
      height: 80,
      translate: [-2, -2],
    },
  }
]

this.redEnvelopeRain = new RedEnvelopeRain({
  el: '#canvas',
  preload: true,
  interval: 300,
  speed: 6,
  horizontalMovement: false,
  // boundary: 20,
  rains: this.rains,
})

// 红包气泡传出来的点击事件
this.redEnvelopeRain.$on('strike', (rain) => {
  this.score += rain.score
  console.log(rain)
})

参数

参数说明类型默认值
el挂载的canvas元素string|HTMLCanvasElement-
proload是否预加载rains中的image图片booleantrue
interval生成红包的间隔时间number500
speed红包每帧移动的距离(px)number2
horizontalMovement是否支持横向移动booleanfalse
boundary点击红包边界距离,为number时表示四边的距离,为array时表示上右下左的距离number|array-
rains红包数组信息array-

rains参数说明

参数说明类型默认值
image红包图片地址string-
width宽度number-
height高度number-
ratio出现的权重number1
speed红包每帧移动的距离(px),优先级更高number-
clicked击中效果object-

clicked参数说明

参数说明类型默认值
image击中红包图片地址string-
width宽度number-
height高度number-
translate相对于红包的x、y轴位移array-
speed红包每帧移动的距离(px),优先级更高number-
duration被点击红包停留时长number200

ratio说明

以下面例子,图片1生成的概率为1/3,图片2生成的概率为2/3。

[
  {
    image: require('./img/1.png'),
    width: 140,
    height: 120,
    ratio: 1,
  },
  {
    image: require('./img/2.png'),
    width: 140,
    height: 120,
    ratio: 2,
  }
]

事件

// 红包被击中,被回传红包信息
redEnvelopeRain.$on('strike', (rain) => {
  console.log(rain)
})

方法

方法名说明参数
play游戏开始-
pause游戏暂停-
clear游戏停止-
add动态添加红包rain信息
remove若需移除红包,则传入的rain信息需包含id字段id
1.0.1

12 months ago

1.0.0

12 months ago