-
Notifications
You must be signed in to change notification settings - Fork 80
/
index.js
156 lines (133 loc) · 3.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import URL from '../libs/URL';
import Blob from '../libs/Blob';
import atob from '../libs/atob';
import EventTarget, { Touch } from '../libs/EventTarget';
import XMLHttpRequest from './XMLHttpRequest';
import copyProperties from '../libs/copyProperties';
// import { DOMParser } from 'xmldom';
import { $DOMParser as DOMParser } from '../libs/DOMParser';
import { $TextDecoder as TextDecoder } from '../libs/TextDecoder';
function OffscreenCanvas() {
return wx.createOffscreenCanvas();
}
export class WechatPlatform {
constructor(canvas, width, height) {
const systemInfo = wx.getSystemInfoSync();
this.canvas = canvas;
this.canvasW = width === undefined ? canvas.width : width;
this.canvasH = height === undefined ? canvas.height : height;
this.document = {
createElementNS(_, type) {
if (type === 'canvas') return canvas;
if (type === 'img') return canvas.createImage();
},
};
this.window = {
innerWidth: systemInfo.windowWidth,
innerHeight: systemInfo.windowHeight,
devicePixelRatio: systemInfo.pixelRatio,
URL: new URL(),
AudioContext: function () {},
requestAnimationFrame: this.canvas.requestAnimationFrame,
cancelAnimationFrame: this.canvas.cancelAnimationFrame,
DeviceOrientationEvent: {
requestPermission() {
return Promise.resolve('granted');
},
},
DOMParser,
TextDecoder,
};
[this.canvas, this.document, this.window].forEach(i => {
copyProperties(i.constructor.prototype, EventTarget.prototype);
});
this.patchCanvas();
this.onDeviceMotionChange = e => {
e.type = 'deviceorientation';
e.alpha *= -1;
e.beta *= -1;
e.gamma *= -1;
this.window.dispatchEvent(e);
};
}
patchCanvas() {
const { canvasH, canvasW } = this;
Object.defineProperty(this.canvas, 'style', {
get() {
return {
width: this.width + 'px',
height: this.height + 'px',
};
},
});
Object.defineProperty(this.canvas, 'clientHeight', {
get() {
return canvasH || this.height;
},
});
Object.defineProperty(this.canvas, 'clientWidth', {
get() {
return canvasW || this.width;
},
});
this.canvas.ownerDocument = this.document;
}
getGlobals() {
return {
atob: atob,
Blob: Blob,
window: this.window,
document: this.document,
HTMLCanvasElement: undefined,
XMLHttpRequest: XMLHttpRequest,
OffscreenCanvas: OffscreenCanvas,
createImageBitmap: undefined,
};
}
enableDeviceOrientation(interval) {
return new Promise((resolve, reject) => {
wx.onDeviceMotionChange(this.onDeviceMotionChange);
wx.startDeviceMotionListening({
interval,
success: resolve,
fail: reject,
});
});
}
disableDeviceOrientation() {
return new Promise((resolve, reject) => {
wx.offDeviceMotionChange(this.onDeviceMotionChange);
wx.stopDeviceMotionListening({
success: resolve,
fail: reject,
});
});
}
dispatchTouchEvent(e = {}) {
const target = {
...this,
};
const event = {
changedTouches: e.changedTouches.map(touch => new Touch(touch)),
touches: e.touches.map(touch => new Touch(touch)),
targetTouches: Array.prototype.slice.call(
e.touches.map(touch => new Touch(touch)),
),
timeStamp: e.timeStamp,
target: target,
currentTarget: target,
type: e.type,
cancelBubble: false,
cancelable: false,
};
this.canvas.dispatchEvent(event);
}
dispose() {
this.disableDeviceOrientation();
if (this.canvas) this.canvas.ownerDocument = null;
this.onGyroscopeChange = null;
this.document = null;
this.window = null;
this.canvas = null;
}
}