-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathsetup-env.ts
More file actions
386 lines (351 loc) · 9.99 KB
/
setup-env.ts
File metadata and controls
386 lines (351 loc) · 9.99 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import '@testing-library/jest-dom'
import { vi } from 'vitest'
import { createCanvas, DOMMatrix } from '@napi-rs/canvas'
declare global {
interface Window {
ResizeObserver: any
SVGRectElement: typeof SVGRectElement
}
}
global.DOMMatrix = DOMMatrix as any
if (!(SVGSVGElement.prototype as any).createSVGMatrix) {
;(SVGSVGElement.prototype as any).createSVGMatrix = function () {
return new (global as any).DOMMatrix()
}
}
// 给 DOMMatrix 添加 rotate/translate/scale 方法(返回新矩阵)
if (!(DOMMatrix.prototype as any).rotate) {
DOMMatrix.prototype.rotate = function (angle: number) {
const m = this.clone()
m.rotateSelf(angle)
return m
}
}
if (!(DOMMatrix.prototype as any).translate) {
DOMMatrix.prototype.translate = function (x: number, y: number) {
const m = this.clone()
m.translateSelf(x, y)
return m
}
}
if (!(DOMMatrix.prototype as any).scale) {
DOMMatrix.prototype.scale = function (s: number) {
const m = this.clone()
m.scaleSelf(s)
return m
}
}
if (!(DOMMatrix.prototype as any).scaleNonUniform) {
// @ts-ignore
DOMMatrix.prototype.scaleNonUniform = function (sx: number, sy: number) {
return this.scale(sx, sy)
}
}
// ResizeObserver polyfill
class RO {
observe() {}
unobserve() {}
disconnect() {}
}
;(globalThis as any).ResizeObserver = RO
// PointerEvent polyfill(jsdom 没有原生)
class PE extends MouseEvent {
pointerId: number
constructor(type: string, params: any = {}) {
super(type, params)
this.pointerId = params.pointerId ?? 1
// 需要显式 defineProperty,因为 MouseEvent 的 clientX/clientY 是只读的
if (params.clientX !== undefined) {
Object.defineProperty(this, 'clientX', { value: params.clientX })
Object.defineProperty(this, 'pageX', { value: params.clientX })
}
if (params.clientY !== undefined) {
Object.defineProperty(this, 'clientY', { value: params.clientY })
Object.defineProperty(this, 'pageY', { value: params.clientY })
}
}
}
;(globalThis as any).PointerEvent = PE
export function createSVGMatrixMock(source?: Partial<DOMMatrix>) {
class SVGMatrixMock {
a = 1
b = 0
c = 0
d = 1
e = 0
f = 0
constructor(init?: Partial<DOMMatrix>) {
if (init) {
if (init.a != null) this.a = init.a
if (init.b != null) this.b = init.b
if (init.c != null) this.c = init.c
if (init.d != null) this.d = init.d
if (init.e != null) this.e = init.e
if (init.f != null) this.f = init.f
}
}
clone() {
return new SVGMatrixMock({
a: this.a,
b: this.b,
c: this.c,
d: this.d,
e: this.e,
f: this.f,
})
}
multiply(other: SVGMatrixMock) {
const m = new SVGMatrixMock()
m.a = this.a * other.a + this.c * other.b
m.b = this.b * other.a + this.d * other.b
m.c = this.a * other.c + this.c * other.d
m.d = this.b * other.c + this.d * other.d
m.e = this.a * other.e + this.c * other.f + this.e
m.f = this.b * other.e + this.d * other.f + this.f
return m
}
inverse() {
const det = this.a * this.d - this.b * this.c
if (det === 0) throw new Error('Matrix not invertible')
const m = new SVGMatrixMock()
m.a = this.d / det
m.b = -this.b / det
m.c = -this.c / det
m.d = this.a / det
m.e = (this.c * this.f - this.d * this.e) / det
m.f = (this.b * this.e - this.a * this.f) / det
return m
}
translate(tx: number, ty: number = 0) {
return this.multiply(
new SVGMatrixMock({ a: 1, b: 0, c: 0, d: 1, e: tx, f: ty }),
)
}
scale(sx: number, sy?: number) {
return this.multiply(
new SVGMatrixMock({ a: sx, b: 0, c: 0, d: sy ?? sx, e: 0, f: 0 }),
)
}
scaleNonUniform(sx: number, sy: number) {
return this.scale(sx, sy)
}
rotate(angle: number) {
const rad = (angle * Math.PI) / 180
const cos = Math.cos(rad)
const sin = Math.sin(rad)
return this.multiply(
new SVGMatrixMock({ a: cos, b: sin, c: -sin, d: cos, e: 0, f: 0 }),
)
}
rotateFromVector(x: number, y: number) {
const angle = (Math.atan2(y, x) * 180) / Math.PI
return this.rotate(angle)
}
skewX(angle: number) {
const rad = (angle * Math.PI) / 180
return this.multiply(
new SVGMatrixMock({ a: 1, b: 0, c: Math.tan(rad), d: 1, e: 0, f: 0 }),
)
}
skewY(angle: number) {
const rad = (angle * Math.PI) / 180
return this.multiply(
new SVGMatrixMock({ a: 1, b: Math.tan(rad), c: 0, d: 1, e: 0, f: 0 }),
)
}
}
return new SVGMatrixMock(source)
}
;(SVGElement as any).prototype.__tx = [0, 0]
;(SVGElement as any).prototype.translate = function (x: number, y: number) {
this.__tx = this.__tx || [0, 0]
this.__tx[0] += x // 仅更新自定义的__tx属性
this.__tx[1] += y
// 同步更新transform属性
this.setAttribute('transform', `translate(${this.__tx[0]},${this.__tx[1]})`)
}
;(SVGElement as any).prototype.getCTM = function () {
const transform = this.getAttribute('transform') || ''
let matrix = createSVGMatrixMock({ a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 })
const regex = /(\w+)\(([^)]+)\)/g
let match
while ((match = regex.exec(transform))) {
const fn = match[1]
const args = match[2].split(/[\s,]+/).map(Number)
switch (fn) {
case 'translate': {
const [tx, ty = 0] = args
matrix = matrix.multiply(
createSVGMatrixMock({ a: 1, b: 0, c: 0, d: 1, e: tx, f: ty }),
)
break
}
case 'scale': {
const [sx, sy = sx] = args
matrix = matrix.multiply(
createSVGMatrixMock({ a: sx, b: 0, c: 0, d: sy, e: 0, f: 0 }),
)
break
}
case 'rotate': {
const [angle, cx, cy] = args
const rad = (angle * Math.PI) / 180
const cos = Math.cos(rad)
const sin = Math.sin(rad)
if (cx != null && cy != null) {
// 平移到原点 -> 旋转 -> 平移回去
const translate1 = createSVGMatrixMock({
a: 1,
b: 0,
c: 0,
d: 1,
e: -cx,
f: -cy,
})
const rotateM = createSVGMatrixMock({
a: cos,
b: sin,
c: -sin,
d: cos,
e: 0,
f: 0,
})
const translate2 = createSVGMatrixMock({
a: 1,
b: 0,
c: 0,
d: 1,
e: cx,
f: cy,
})
matrix = matrix.multiply(
translate2.multiply(rotateM).multiply(translate1),
)
} else {
// 普通旋转
matrix = matrix.multiply(
createSVGMatrixMock({
a: cos,
b: sin,
c: -sin,
d: cos,
e: 0,
f: 0,
}),
)
}
break
}
case 'skewX': {
const [angle] = args
const rad = (angle * Math.PI) / 180
matrix = matrix.multiply(
createSVGMatrixMock({
a: 1,
b: 0,
c: Math.tan(rad),
d: 1,
e: 0,
f: 0,
}),
)
break
}
case 'skewY': {
const [angle] = args
const rad = (angle * Math.PI) / 180
matrix = matrix.multiply(
createSVGMatrixMock({
a: 1,
b: Math.tan(rad),
c: 0,
d: 1,
e: 0,
f: 0,
}),
)
break
}
case 'matrix': {
const [a, b, c, d, e, f] = args
matrix = matrix.multiply(createSVGMatrixMock({ a, b, c, d, e, f }))
break
}
}
}
return matrix
}
;(SVGElement as any).prototype.getScreenCTM = function () {
return this.getCTM()
}
;(SVGElement as any).prototype.createSVGMatrix = () => createSVGMatrixMock()
if (!SVGSVGElement.prototype.createSVGPoint) {
;(SVGSVGElement as any).prototype.createSVGPoint = () => ({
x: 0,
y: 0,
matrixTransform(m: DOMMatrix): { x: number; y: number } {
return {
x: this.x * m.a + this.y * m.c + m.e,
y: this.x * m.b + this.y * m.d + m.f,
}
},
})
}
// getBBox 根据属性返回
;(SVGElement as any).prototype.getBBox = function () {
const width = Number(this.getAttribute('width')) || 100
const height = Number(this.getAttribute('height')) || 100
const x = Number(this.getAttribute('x')) || 0
const y = Number(this.getAttribute('y')) || 0
return { x, y, width, height }
}
// requestAnimationFrame
if (!(globalThis as any).requestAnimationFrame) {
;(globalThis as any).requestAnimationFrame = (cb: FrameRequestCallback) =>
setTimeout(() => cb(Date.now()), 16)
;(globalThis as any).cancelAnimationFrame = (id: number) => clearTimeout(id)
}
const originalCreateElement = document.createElement
Object.defineProperty(document, 'createElement', {
value: (tagName: string) => {
if (tagName === 'canvas') {
return createCanvas(200, 200)
}
return originalCreateElement.call(document, tagName)
},
})
class SVGTransformMock {
static SVG_TRANSFORM_MATRIX = 1
type = SVGTransformMock.SVG_TRANSFORM_MATRIX
matrix: DOMMatrix
angle = 0
constructor(matrix: DOMMatrix) {
this.matrix = matrix
}
setMatrix(m: DOMMatrix) {
this.matrix = m
}
}
;(global as any).SVGTransform = SVGTransformMock
if (!(SVGSVGElement.prototype as any).createSVGTransformFromMatrix) {
;(SVGSVGElement.prototype as any).createSVGTransformFromMatrix = function (
matrix: DOMMatrix,
) {
return new (global as any).SVGTransform(matrix)
}
}
if (!(SVGSVGElement.prototype as any).createSVGTransform) {
;(SVGSVGElement.prototype as any).createSVGTransform = function (
matrix: DOMMatrix,
) {
return {
setMatrix: vi.fn(),
setTranslate: vi.fn(),
setScale: vi.fn(),
setRotate: vi.fn(),
setSkewX: vi.fn(),
setSkewY: vi.fn(),
matrix: matrix || createSVGMatrixMock(),
}
}
}