微信小程序中使用threejs-miniprogram-安装
目录
微信小程序中使用threejs-miniprogram - 【安装】
- 通过npm安装
在项目目录下,shift+鼠标右键,打开PowerShell,输入
npm i threejs-miniprogram
,执行安装;安装完成后,会出现
node_modules
目录,查看是否已经存在了threejs-miniprogram
文件,内容如下:
- 微信小程序开发者工具中构建npm
小程序工具栏,【工具】→ “构建npm”,即可完成npm构建,如下图:
- 导入小程序适配版本的Three.js
import {createScopedThreejs} from 'threejs-miniprogram'
Page({
onReady() {
wx.createSelectorQuery()
.select('#webgl')
.node()
.exec((res) => {
const canvas = res[0].node
// 创建一个与 canvas 绑定的 three.js
const THREE = createScopedThreejs(canvas)
// 传递并使用 THREE 变量
})
}
})