{"id":18938248,"url":"https://github.com/wechat-miniprogram/miniprogram-barrage","last_synced_at":"2025-04-15T19:30:22.473Z","repository":{"id":53594336,"uuid":"213563226","full_name":"wechat-miniprogram/miniprogram-barrage","owner":"wechat-miniprogram","description":null,"archived":false,"fork":false,"pushed_at":"2024-04-18T10:40:58.000Z","size":69,"stargazers_count":27,"open_issues_count":7,"forks_count":9,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T01:06:07.456Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wechat-miniprogram.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-10-08T06:18:21.000Z","updated_at":"2025-01-02T07:26:13.000Z","dependencies_parsed_at":"2022-08-31T20:21:57.723Z","dependency_job_id":null,"html_url":"https://github.com/wechat-miniprogram/miniprogram-barrage","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wechat-miniprogram%2Fminiprogram-barrage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wechat-miniprogram%2Fminiprogram-barrage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wechat-miniprogram%2Fminiprogram-barrage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wechat-miniprogram%2Fminiprogram-barrage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wechat-miniprogram","download_url":"https://codeload.github.com/wechat-miniprogram/miniprogram-barrage/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249138495,"owners_count":21218897,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T12:13:53.484Z","updated_at":"2025-04-15T19:30:22.220Z","avatar_url":"https://github.com/wechat-miniprogram.png","language":"JavaScript","readme":"# Barrage for MiniProgram\n\n[![](https://img.shields.io/npm/v/miniprogram-barrage)](https://www.npmjs.com/package/miniprogram-barrage)\n[![](https://img.shields.io/npm/l/miniprogram-barrage)](https://github.com/wechat-miniprogram/miniprogram-barrage)\n\n小程序弹幕组件，覆盖在 原生组件上时，请确保组件已经同层化。[参考用例](https://developers.weixin.qq.com/s/FvXaI3mt7EgY)。弹幕组件的实现采用了 canvas \u0026 dom 两种方式，通过 rendering-mode 属性进行指定。dom 的方式兼容性高，当对小程序基础库低版本( v2.9.2 以下）有要求时，可以采用这种渲染方式。canvas 的方式通常性能更好，动画更为流畅，但仅在基础库 v2.9.2 版本及以上可以使用。\n\n注意事项：在开发者工具上，canvas 的渲染方式无法使用 `view` 等普通组件覆盖在弹幕上方，需采用 `cover-view`。真机上可以使用普通的 `view` 覆盖在弹幕上。\n\n## 属性列表\n\n| 属性           | 类型   | 默认值 | 必填 | 说明       |\n| -------------- | ------ | ------ | ---- | ---------- |\n| z-index        | number | 10     | 否   | 弹幕的层级 |\n| rendering-mode | string | canvas | 否   | 渲染模式   |\n\n\n## 使用方法\n1. npm 安装，参考 [小程序 npm 支持](https://developers.weixin.qq.com/miniprogram/dev/devtools/npm.html)\n\n```\nnpm install --save miniprogram-barrage\n```\n\n2. JSON 组件声明\n```json\n{\n  \"usingComponents\": {\n    \"barrage\": \"miniprogram-barrage\",\n  }\n}\n\n```\n\n3. wxml 引入弹幕组件\n```html\n\u003cvideo class=\"video\" src=\"{{src}}\"\u003e\n  \u003cbarrage class=\"barrage\" rendering-mode=\"canvas\" z-index=\"100\"\u003e\u003c/barrage\u003e\n\u003c/video\u003e\n```\n\n4. js 获取实例\n```js\n Page({\n  onReady() {\n    this.addBarrage()\n  },\n  addBarrage() {\n    const barrageComp = this.selectComponent('.barrage')\n    this.barrage = barrageComp.getBarrageInstance({\n      font: 'bold 16px sans-serif',\n      duration: this.data.duration,\n      lineHeight: 2,\n      mode: 'separate',\n      padding: [10, 0, 10, 0],\n      range: [0, 1]\n    })\n    this.barrage.open()\n    this.barrage.addData(data)\n  }\n })\n\n```\n\n## 配置\n### Barrage 默认配置\n```js\n{\n  duration: 15, // 弹幕动画时长 (移动 2000px 所需时长)\n  lineHeight: 1.2, // 弹幕行高\n  padding: [0, 0, 0, 0], // 弹幕区四周留白\n  alpha: 1, // 全局透明度\n  font: '10px sans-serif', // 字体大小\n  mode: 'separate', // 弹幕重叠 overlap  不重叠 separate\n  range: [0, 1], // 弹幕显示的垂直范围，支持两个值。[0,1]表示弹幕整个随机分布，\n  tunnelShow: false, // 显示轨道线\n  tunnelMaxNum: 30, // 隧道最大缓冲长度\n  maxLength: 30, // 弹幕最大字节长度，汉字算双字节\n  safeGap: 4, // 发送时的安全间隔\n}\n```\n### 弹幕数据配置\n```js\n{\n  color: '#000000', // 默认黑色\n  content: '', // 弹幕内容\n  image: {\n    head: {src, width, height, gap = 4}, // 弹幕头部添加图片\n    tail: {src, width, height, gap = 4}, // 弹幕尾部添加图片\n  }\n  \n}\n```\n\n## 接口\n```js\nbarrage.open() // 开启弹幕功能\nbarrage.close() // 关闭弹幕功能，清空弹幕\nbarrage.addData(data: array) // 添加弹幕数据\nbarrage.send(data: object) // 发送一条弹幕数据\nbarrage.setRange(range: array) // 设置垂直方向显示范围，默认 [0, 1]\nbarrage.setFont(font: string) // 设置全局字体，注 canvas 的渲染方式仅可设置大小，不支持字体设置\nbarrage.setAlpha(alpha: number) // 设置全局透明度, alpha 0 ~ 1, 值越小，越透明\nbarrage.showTunnel() // 显示弹幕轨道\nbarrage.hideTunnel() // 隐藏弹幕轨道\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwechat-miniprogram%2Fminiprogram-barrage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwechat-miniprogram%2Fminiprogram-barrage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwechat-miniprogram%2Fminiprogram-barrage/lists"}