Skip to content

Commit b51d7b3

Browse files
committed
update
1 parent 53ed21e commit b51d7b3

File tree

9 files changed

+108
-49486
lines changed

9 files changed

+108
-49486
lines changed

.eslintrc.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module.exports = {
2+
root: true,
3+
parser: "babel-eslint",
4+
parserOptions: {
5+
ecmaVersion: 2018,
6+
sourceType: 'module'
7+
},
8+
env: {
9+
es6: true,
10+
node: true
11+
},
12+
extends: [
13+
"eslint:recommended"
14+
],
15+
globals: {
16+
'XMLHttpRequest': true,
17+
'window': true,
18+
'document': true,
19+
'navigator': true,
20+
'wx': true
21+
},
22+
rules: {
23+
'no-console': process.env.NODE_ENV !== 'production' ? 0 : 2,
24+
'no-useless-escape': 0,
25+
'no-empty': 0
26+
}
27+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 wechat-miniprogram
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,38 @@
1-
# weapp-adapter
1+
# threejs-miniprogram
2+
Three.js 小程序 WebGL 的适配版本。
3+
4+
## 使用
5+
6+
可参考该代码片段[]()或参照以下流程:
7+
8+
1. 通过 npm 安装
9+
10+
```
11+
npm install --save threejs-miniprogram
12+
```
13+
14+
2. 导入小程序适配版本的 Three.js
15+
16+
```javascript
17+
import {createScopedThreejs} from 'threejs-miniprogram'
18+
19+
Page({
20+
onReady() {
21+
wx.createSelectorQuery()
22+
.select('#webgl')
23+
.node()
24+
.exec((res) => {
25+
const canvas = res[0].node
26+
// 创建一个与 canvas 绑定的 three.js
27+
const THREE = createScopedThreejs(canvas)
28+
// 传递并使用 THREE 变量
29+
})
30+
}
31+
})
32+
```
33+
34+
## 说明
35+
36+
- 本项目当前使用的 Three.js 版本号为 0.108.0,如要更新 threejs 版本可发 PR 修改或 fork 后自行修改。
37+
- 该适配版本的 THREE 不在全局环境中,如使用 Three.js 的其他配套类库,需要自行传入 THREE 到类库中。
38+
- 如在使用过程中发现有适配问题,可通过 issue 反馈或发 PR 修复。

build/webpack.config.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ const fs = require('fs')
22
const path = require('path')
33
const StringReplacePlugin = require("string-replace-webpack-plugin");
44

5+
function resolveThreeModule() {
6+
const threePath = require.resolve('three')
7+
const code = fs.readFileSync(path.resolve(threePath), 'utf8')
8+
return code
9+
}
10+
511
module.exports = {
612
entry: path.join(__dirname, '../src/index'),
713
target: 'web',
@@ -30,7 +36,7 @@ module.exports = {
3036
{
3137
pattern: /__INJECT_THREE__/ig,
3238
replacement: () => {
33-
return fs.readFileSync(path.resolve(__dirname, '../vendor/three.js'), 'utf8')
39+
return resolveThreeModule()
3440
}
3541
}
3642
]

package.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
{
2-
"name": "miniprogram-threejs",
2+
"name": "threejs-miniprogram",
33
"version": "0.0.1",
4-
"description": "ThreeJS adapted for WeChat MiniProgram",
4+
"description": "ThreeJS adapter for WeChat MiniProgram",
55
"main": "dist/index.js",
6-
"directories": {
7-
"test": "test"
8-
},
96
"scripts": {
107
"dev": "webpack --config build/webpack.config.js --watch",
11-
"build": "webpack --hide-modules --config build/webpack.config.js"
8+
"build": "webpack --hide-modules --config build/webpack.config.js",
9+
"lint": "eslint \"src/**/*.js\""
1210
},
1311
"miniprogram": "dist/",
1412
"author": "wechat-miniprogram",
@@ -18,7 +16,7 @@
1816
"@babel/plugin-proposal-class-properties": "^7.5.5",
1917
"@babel/preset-env": "^7.5.5",
2018
"babel-loader": "^8.0.6",
21-
"eslint": "^3.4.0",
19+
"eslint": "^3.19.0",
2220
"eslint-friendly-formatter": "^2.0.6",
2321
"eslint-loader": "^1.5.0",
2422
"eslint-plugin-jest": "^20.0.3",
@@ -27,6 +25,7 @@
2725
"path": "^0.12.7",
2826
"string-replace-loader": "^2.2.0",
2927
"string-replace-webpack-plugin": "^0.1.3",
28+
"three": "^0.108.0",
3029
"webpack": "^4.39.1",
3130
"webpack-cli": "^3.3.6"
3231
}

src/EventTarget.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default class EventTarget {
2828
}
2929
}
3030

31-
removeEventListener(type, listener, options = {}) {
31+
removeEventListener(type, listener) {
3232
const events = _events.get(this)
3333

3434
if (events) {

src/XMLHttpRequest.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ export default class XMLHttpRequest extends EventTarget {
112112
if (typeof data !== 'string' && !(data instanceof ArrayBuffer)) {
113113
try {
114114
data = JSON.stringify(data)
115-
} catch (e) {
116-
data = data
117-
}
115+
} catch (e) {}
118116
}
119117

120118
this.status = statusCode

src/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
import _XMLHttpRequest from './XMLHttpRequest'
22

3-
export function getAdaptedThreejs(canvas) {
3+
export function createScopedThreejs(canvas) {
44
// adapt canvas
55
canvas.style = {width: canvas.width + 'px', height: canvas.height + 'px'}
66
canvas.addEventListener = function () {}
77
canvas.removeEventListener = function () {}
88

9+
// eslint-disable-next-line
910
const document = {
1011
createElementNS(_, type) {
1112
if (type === 'canvas') return canvas
1213
if (type === 'img') return canvas.createImage()
1314
}
1415
}
1516

17+
// eslint-disable-next-line
1618
const window = {
1719
AudioContext: function() {},
1820
addEventListener: function() {},
1921
removeEventListener: function() {}
2022
}
23+
// eslint-disable-next-line
2124
const XMLHttpRequest = _XMLHttpRequest
2225

23-
const innerExport = {};
26+
const exports = {};
2427

2528
// three.js source code will be injected here
29+
// eslint-disable-next-line
2630
__INJECT_THREE__
2731

28-
return innerExport
32+
return exports
2933
}
3034

0 commit comments

Comments
 (0)