Skip to content

Commit d9404e1

Browse files
committed
add migration guide for upgrading from Lightning 3 v2.x to v3.0
1 parent c3bb523 commit d9404e1

File tree

2 files changed

+228
-0
lines changed

2 files changed

+228
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,7 @@ await stage.loadFont('sdf', {
131131
```
132132

133133
For more information see [Font Loading](./docs/fontLoading.md)
134+
135+
## Migration Guide
136+
137+
Upgrading from Lightning 3 v2.x? See the [Migration Guide](./docs/migration-2x-to-3.0.md) for detailed information about breaking changes and how to update your code.

docs/migration-2x-to-3.0.md

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
# Migration Guide: Lightning 3 v2.x to v3.0
2+
3+
This guide helps you migrate from Lightning 3 version 2.x to the new 3.0 release. The 3.0 version introduces several breaking changes to improve performance, API consistency, and developer experience.
4+
5+
## Breaking Changes Overview
6+
7+
1. **New Font Loading API** - Explicit renderer type specification required
8+
2. **Property Deprecation** - `width`/`height` properties deprecated in favor of `w`/`h`
9+
3. **Texture Memory Management** - New `doNotExceedCriticalThreshold` behavior
10+
11+
## 1. Font Loading Changes
12+
13+
### What Changed
14+
15+
Lightning 3.0 introduces a new unified font loading API that requires explicit specification of the renderer type (`'canvas'` or `'sdf'`). This change improves type safety and makes font loading more predictable.
16+
17+
### v2.x Font Loading (Deprecated)
18+
19+
```typescript
20+
// v2.x - Implicit font loading
21+
await stage.fontManager.addFontFace({
22+
fontFamily: 'MyFont',
23+
fontUrl: '/fonts/my-font.ttf',
24+
});
25+
26+
await stage.fontManager.addSdfFont({
27+
fontFamily: 'MySdfFont',
28+
atlasUrl: '/fonts/my-font-atlas.png',
29+
atlasDataUrl: '/fonts/my-font-data.json',
30+
});
31+
```
32+
33+
### v3.0 Font Loading (New API)
34+
35+
```typescript
36+
// v3.0 - Explicit renderer type specification
37+
await stage.loadFont('canvas', {
38+
fontFamily: 'MyFont',
39+
fontUrl: '/fonts/my-font.ttf',
40+
metrics: {
41+
// Optional but recommended
42+
ascender: 800,
43+
descender: -200,
44+
lineGap: 0,
45+
unitsPerEm: 1000,
46+
},
47+
});
48+
49+
await stage.loadFont('sdf', {
50+
fontFamily: 'MySdfFont',
51+
atlasUrl: '/fonts/my-font-atlas.png',
52+
atlasDataUrl: '/fonts/my-font-data.json',
53+
metrics: {
54+
// Optional but recommended
55+
ascender: 800,
56+
descender: -200,
57+
lineGap: 0,
58+
unitsPerEm: 1000,
59+
},
60+
});
61+
```
62+
63+
### Migration Steps
64+
65+
1. **Replace `stage.fontManager.addFontFace()`** with `stage.loadFont('canvas', options)`
66+
2. **Replace `stage.fontManager.addSdfFont()`** with `stage.loadFont('sdf', options)`
67+
3. **Add explicit renderer type** as the first parameter
68+
4. **Consider adding font metrics** for better text rendering quality
69+
70+
### Benefits of the New API
71+
72+
- **Type Safety**: Explicit renderer type prevents runtime errors
73+
- **Consistency**: Single method for all font types
74+
- **Better Performance**: Improved text rendering and font metrics handling
75+
76+
## 2. Width/Height Property Changes
77+
78+
### What Changed
79+
80+
The `width` and `height` properties are being deprecated in favor of shorter `w` and `h` properties for better performance and consistency.
81+
82+
### Migration Example
83+
84+
```typescript
85+
// v2.x
86+
const node = renderer.createNode({
87+
x: 100,
88+
y: 100,
89+
width: 200, // ⚠️ No longer available
90+
height: 150, // ⚠️ No longer available
91+
color: 0xff0000ff,
92+
});
93+
94+
// v3.0
95+
const node = renderer.createNode({
96+
x: 100,
97+
y: 100,
98+
w: 200, // ✅ Available
99+
h: 150, // ✅ Available
100+
color: 0xff0000ff,
101+
});
102+
```
103+
104+
### Texture Loaded Event Changes
105+
106+
The texture loaded event now reports dimensions using `w` and `h` properties:
107+
108+
```typescript
109+
// v2.x - Dimensions with width/height
110+
node.on('loaded', (event) => {
111+
if (event.type === 'texture') {
112+
console.log('Texture loaded:', {
113+
width: event.dimensions.width, // ⚠️ No longer available
114+
height: event.dimensions.height, // ⚠️ No longer available
115+
});
116+
}
117+
});
118+
119+
// v3.0 - Dimensions with w/h
120+
node.on('loaded', (event) => {
121+
if (event.type === 'texture') {
122+
console.log('Texture loaded:', {
123+
w: event.dimensions.w, // ✅ Available
124+
h: event.dimensions.h, // ✅ Available
125+
});
126+
}
127+
});
128+
```
129+
130+
### Migration Steps
131+
132+
1. **Search and replace** `width:` with `w:` in node creation and texture loaded events
133+
2. **Search and replace** `height:` with `h:` in node creation and texture loaded events
134+
3. **Update property access**: `node.width``node.w`, `node.height``node.h`
135+
4. **Test thoroughly** to ensure no breaking changes
136+
137+
## 3. Texture Memory Management Changes
138+
139+
### What Changed
140+
141+
The `doNotExceedCriticalThreshold` setting now causes texture creation to fail when the memory threshold is exceeded, rather than just logging warnings. If you turn `doNotExceedCriticalThreshold` off you will mimic the behaviour of Lightning 2.
142+
143+
### v2.x Behavior
144+
145+
```typescript
146+
// v2.x - Would log warnings but allow texture creation
147+
const renderer = new RendererMain({
148+
textureMemory: {
149+
criticalThreshold: 100 * 1024 * 1024, // 100MB
150+
doNotExceedCriticalThreshold: true,
151+
},
152+
});
153+
// Textures would not be created but not return any feedback
154+
```
155+
156+
### v3.0 Behavior
157+
158+
```typescript
159+
// v3.0 - Texture creation fails when threshold exceeded
160+
const renderer = new RendererMain({
161+
textureMemory: {
162+
criticalThreshold: 100 * 1024 * 1024, // 100MB
163+
doNotExceedCriticalThreshold: true, // Now enforced strictly
164+
},
165+
});
166+
// Textures will fail to load when threshold is exceeded
167+
```
168+
169+
### Migration Steps
170+
171+
1. **Review your memory settings** and adjust `criticalThreshold` if needed
172+
2. **Test with realistic workloads** to ensure thresholds are appropriate
173+
3. **Implement error handling** for texture loading failures
174+
4. **Consider memory optimization** strategies for your application
175+
176+
### Error Handling Example
177+
178+
```typescript
179+
// Handle texture loading failures
180+
node.on('failed', (event) => {
181+
if (event.type === 'texture') {
182+
console.error('Texture failed to load:', event.error);
183+
// Implement fallback strategy
184+
}
185+
});
186+
187+
// Listen for critical cleanup events
188+
renderer.on('criticalCleanupFailed', (event) => {
189+
console.warn('Memory threshold exceeded:', event);
190+
// Implement memory management strategy
191+
});
192+
```
193+
194+
## Additional Changes and Improvements
195+
196+
### Enhanced Font Metrics
197+
198+
Lightning 3.0 provides better font metrics support:
199+
200+
```typescript
201+
// Recommended: Always provide font metrics for optimal rendering
202+
await stage.loadFont('canvas', {
203+
fontFamily: 'MyFont',
204+
fontUrl: '/fonts/my-font.ttf',
205+
metrics: {
206+
ascender: 800, // Font ascender in font units
207+
descender: -200, // Font descender in font units
208+
lineGap: 0, // Additional line spacing
209+
unitsPerEm: 1000, // Font units per EM
210+
},
211+
});
212+
```
213+
214+
### Performance Improvements
215+
216+
- Faster property access with `w`/`h` instead of `width`/`height`
217+
- Optimized font loading and rendering pipeline
218+
- Better texture memory management with strict thresholds and failed texture signalling
219+
220+
## Getting Help
221+
222+
- **Documentation**: See [Font Loading Guide](./fontLoading.md) for detailed font API documentation
223+
- **Examples**: Check the `examples/` directory for updated usage patterns
224+
- **Issues**: Report migration issues on the Renderer GitHub repository

0 commit comments

Comments
 (0)