index.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. function getRandomColor() {
  2. const rgb = []
  3. for (let i = 0; i < 3; ++i) {
  4. let color = Math.floor(Math.random() * 256).toString(16)
  5. color = color.length == 1 ? '0' + color : color
  6. rgb.push(color)
  7. }
  8. return '#' + rgb.join('')
  9. }
  10. Page({
  11. onReady(res) {
  12. this.videoContext = wx.createVideoContext('myVideo')
  13. },
  14. inputValue: '',
  15. data: {
  16. src: '',
  17. danmuList: [
  18. {
  19. text: '第 1s 出现的弹幕',
  20. color: '#ff0000',
  21. time: 1
  22. },
  23. {
  24. text: '第 3s 出现的弹幕',
  25. color: '#ff00ff',
  26. time: 3
  27. }]
  28. },
  29. bindInputBlur(e) {
  30. this.inputValue = e.detail.value
  31. },
  32. bindButtonTap() {
  33. const that = this
  34. wx.chooseVideo({
  35. sourceType: ['album', 'camera'],
  36. maxDuration: 60,
  37. camera: ['front', 'back'],
  38. success(res) {
  39. that.setData({
  40. src: res.tempFilePath
  41. })
  42. }
  43. })
  44. },
  45. bindSendDanmu() {
  46. this.videoContext.sendDanmu({
  47. text: this.inputValue,
  48. color: getRandomColor()
  49. })
  50. }
  51. })