目录

微信小程序中使用threejs-miniprogram-安装

目录

微信小程序中使用threejs-miniprogram - 【安装】

  1. 通过npm安装
  • 在项目目录下,shift+鼠标右键,打开PowerShell,输入 npm i threejs-miniprogram ,执行安装;

  • 安装完成后,会出现 node_modules 目录,查看是否已经存在了 threejs-miniprogram 文件,内容如下:

    https://i-blog.csdnimg.cn/blog_migrate/16597b6200caf47dc667e52f0e81f090.png

  1. 微信小程序开发者工具中构建npm
  • 小程序工具栏,【工具】→ “构建npm”,即可完成npm构建,如下图:

    https://i-blog.csdnimg.cn/blog_migrate/4db3fc7fb9e81b46ea06c335248bedf3.png

  1. 导入小程序适配版本的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 变量
})
}
})