stub-pixi.js
2.36 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
// stub-pixi.js — headless stub Pixi 命名空间(Sprite / Container / Graphics / Application / Assets)。
//
// 让 §8 的纯映射用例(SlotTransform→Sprite 写入、镜像、纵深)在无 GPU 的 Node 下可断言,
// 不依赖真实 pixi.js / WebGL。stub 只实现本包用到的最小 API 面。
class StubPoint {
constructor(x = 0, y = 0) {
this.x = x
this.y = y
}
set(x, y = x) {
this.x = x
this.y = y
}
}
class StubContainer {
constructor() {
this.children = []
this.position = new StubPoint()
this.scale = new StubPoint(1, 1)
this.pivot = new StubPoint()
this.zIndex = 0
this.sortableChildren = false
this.alpha = 1
this.visible = true
this.destroyed = false
}
addChild(...c) {
this.children.push(...c)
return c[0]
}
addChildAt(c, i) {
this.children.splice(i, 0, c)
return c
}
destroy() {
this.destroyed = true
}
}
class StubSprite extends StubContainer {
constructor(texture = null) {
super()
this.texture = texture
this.anchor = new StubPoint(0.5, 0.5)
this.width = 0
this.height = 0
this.mask = null
this.rotation = 0
}
}
class StubGraphics extends StubContainer {
constructor() {
super()
this.calls = []
}
poly(points) {
this.calls.push(['poly', points])
return this
}
fill(color) {
this.calls.push(['fill', color])
return this
}
rect(...a) {
this.calls.push(['rect', a])
return this
}
}
class StubApplication {
constructor() {
this.stage = new StubContainer()
this.screen = { width: 1280, height: 720 }
this.canvas = {}
}
async init() {}
destroy() {}
}
/** 创建一个可控的 stub Assets:load 返回可预设的纹理或受控 reject。 */
function makeStubAssets(behavior = {}) {
return {
loaded: [],
async load(url) {
this.loaded.push(url)
if (behavior.reject && behavior.reject(url)) throw new Error('load fail ' + url)
const t = behavior.texture ? behavior.texture(url) : { __tex: url }
return t
},
}
}
/** 组装一个 stub PIXI 命名空间。 */
export function makeStubPixi(behavior = {}) {
return {
Container: StubContainer,
Sprite: StubSprite,
Graphics: StubGraphics,
Application: StubApplication,
Assets: makeStubAssets(behavior),
}
}
export { StubContainer, StubSprite, StubGraphics, StubApplication }