@linear_non/stellar-libs 1.0.11 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@linear_non/stellar-libs",
3
- "version": "1.0.11",
3
+ "version": "1.0.12",
4
4
  "description": "Reusable JavaScript libraries for Non-Linear Studio projects.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -8,13 +8,21 @@ export default class SpritePlayer {
8
8
  constructor({
9
9
  canvas,
10
10
  container,
11
+
12
+ // path-based
11
13
  mobile_path,
12
14
  desktop_path,
13
15
  fileName,
14
16
  total = 0,
15
- progress = 0,
16
17
  fileExtension = "png",
18
+
19
+ // direct URLs
20
+ urls = null, // string[]
21
+ alpha_urls = null, // string[]
22
+
23
+ // misc
17
24
  cache = null,
25
+ progress = 0,
18
26
  persistent = false,
19
27
  alpha_path = null, // { mobile, desktop }
20
28
  fileAlpha = null,
@@ -41,18 +49,31 @@ export default class SpritePlayer {
41
49
  total,
42
50
  file_extension: fileExtension,
43
51
  cache,
52
+ urls,
53
+ alpha_urls,
44
54
  alpha_path,
45
55
  fileAlpha,
46
56
  }
47
57
 
48
58
  this.state = { loaded: false }
49
59
  this.isVisible = true
60
+ this._loaders = []
50
61
  this.init()
51
62
  }
52
63
 
53
64
  async loadImages() {
54
- const { mobile_path, desktop_path, alpha_path, file_name, fileAlpha, total, file_extension, cache } =
55
- this.settings
65
+ const {
66
+ mobile_path,
67
+ desktop_path,
68
+ alpha_path,
69
+ file_name,
70
+ fileAlpha,
71
+ total,
72
+ file_extension,
73
+ cache,
74
+ urls,
75
+ alpha_urls,
76
+ } = this.settings
56
77
 
57
78
  const originBase = sniffer.isDesktop ? desktop_path : mobile_path || desktop_path
58
79
  const originAlpha = alpha_path
@@ -61,22 +82,28 @@ export default class SpritePlayer {
61
82
  : alpha_path.mobile || alpha_path.desktop
62
83
  : null
63
84
 
64
- const make = (origin, name) =>
65
- new ImageLoader({
85
+ const make = ({ origin, fileName, list }) => {
86
+ const loader = new ImageLoader({
66
87
  origin,
67
- fileName: name,
88
+ fileName,
68
89
  extension: file_extension,
69
90
  total,
91
+ urls: Array.isArray(list) ? list : [],
70
92
  cache,
71
93
  useWorker: true,
72
- }).load()
94
+ })
95
+ this._loaders.push(loader)
96
+ return loader.load()
97
+ }
73
98
 
74
99
  const onProgress = e => emitter.emit("sprite:progress", { instance: this, ...e })
75
100
  emitter.on(ImageLoader.events.PROGRESS, onProgress)
76
101
 
77
102
  const [images, alphaImages] = await Promise.all([
78
- make(originBase, file_name),
79
- originAlpha && fileAlpha ? make(originAlpha, fileAlpha) : Promise.resolve([]),
103
+ make({ origin: originBase, fileName: file_name, list: urls }),
104
+ originAlpha && (fileAlpha || (alpha_urls && alpha_urls.length))
105
+ ? make({ origin: originAlpha, fileName: fileAlpha, list: alpha_urls })
106
+ : Promise.resolve([]),
80
107
  ])
81
108
 
82
109
  emitter.off?.(ImageLoader.events.PROGRESS, onProgress)
@@ -87,9 +114,12 @@ export default class SpritePlayer {
87
114
  if (this.isAlpha && this.dom.alphaImages.length) {
88
115
  this.dom.alphaImages = this.dom.alphaImages.map(img => this.toAlphaFromLuma(img))
89
116
  }
90
- if (this.isAlpha && !this.dom.alphaImages.length) this.isAlpha = false
117
+ if (this.isAlpha && this.dom.alphaImages.length !== this.dom.images.length) {
118
+ this.isAlpha = false
119
+ this.dom.alphaImages = []
120
+ }
91
121
 
92
- this.frameCount = images.length
122
+ this.frameCount = this.dom.images.length
93
123
  this.state.loaded = true
94
124
  emitter.emit("sprite:loaded", this)
95
125
  }
@@ -112,10 +142,9 @@ export default class SpritePlayer {
112
142
  Object.assign(this.canvas, { width: rect.width, height: rect.height, rect, area, offset })
113
143
  }
114
144
 
115
- // grayscale -> alpha (returns a canvas)
116
145
  toAlphaFromLuma(img) {
117
- const w = img.naturalWidth || img.width,
118
- h = img.naturalHeight || img.height
146
+ const w = img.naturalWidth || img.width
147
+ const h = img.naturalHeight || img.height
119
148
  const c = document.createElement("canvas")
120
149
  c.width = w
121
150
  c.height = h
@@ -210,12 +239,11 @@ export default class SpritePlayer {
210
239
  destroy() {
211
240
  this.off()
212
241
  if (!this.persistent) {
213
- const revoke = img => img?.src?.startsWith("blob:") && URL.revokeObjectURL(img.src)
214
- this.dom.images.forEach(revoke)
215
- this.dom.alphaImages.forEach(revoke)
242
+ this._loaders.forEach(l => l.destroy?.())
216
243
  }
217
244
  this.dom.images = []
218
245
  this.dom.alphaImages = []
246
+ this._loaders = []
219
247
  }
220
248
 
221
249
  async init() {