@linear_non/stellar-libs 1.0.11 → 1.0.13

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.13",
4
4
  "description": "Reusable JavaScript libraries for Non-Linear Studio projects.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -8,22 +8,32 @@ 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,
21
29
  useAlpha = false,
30
+ useWorker = true,
22
31
  }) {
23
32
  const rect = bounds(container)
24
33
  this.progress = gsap.utils.clamp(0, 1, progress)
25
34
  this.persistent = persistent
26
35
  this.isAlpha = !!useAlpha
36
+ this.useWorker = useWorker
27
37
 
28
38
  this.dom = { container, reference: container, images: [], alphaImages: [] }
29
39
  this.canvas = {
@@ -41,18 +51,31 @@ export default class SpritePlayer {
41
51
  total,
42
52
  file_extension: fileExtension,
43
53
  cache,
54
+ urls,
55
+ alpha_urls,
44
56
  alpha_path,
45
57
  fileAlpha,
46
58
  }
47
59
 
48
60
  this.state = { loaded: false }
49
61
  this.isVisible = true
62
+ this._loaders = []
50
63
  this.init()
51
64
  }
52
65
 
53
66
  async loadImages() {
54
- const { mobile_path, desktop_path, alpha_path, file_name, fileAlpha, total, file_extension, cache } =
55
- this.settings
67
+ const {
68
+ mobile_path,
69
+ desktop_path,
70
+ alpha_path,
71
+ file_name,
72
+ fileAlpha,
73
+ total,
74
+ file_extension,
75
+ cache,
76
+ urls,
77
+ alpha_urls,
78
+ } = this.settings
56
79
 
57
80
  const originBase = sniffer.isDesktop ? desktop_path : mobile_path || desktop_path
58
81
  const originAlpha = alpha_path
@@ -61,22 +84,28 @@ export default class SpritePlayer {
61
84
  : alpha_path.mobile || alpha_path.desktop
62
85
  : null
63
86
 
64
- const make = (origin, name) =>
65
- new ImageLoader({
87
+ const make = ({ origin, fileName, list }) => {
88
+ const loader = new ImageLoader({
66
89
  origin,
67
- fileName: name,
90
+ fileName,
68
91
  extension: file_extension,
69
92
  total,
93
+ urls: Array.isArray(list) ? list : [],
70
94
  cache,
71
- useWorker: true,
72
- }).load()
95
+ useWorker: this.useWorker,
96
+ })
97
+ this._loaders.push(loader)
98
+ return loader.load()
99
+ }
73
100
 
74
101
  const onProgress = e => emitter.emit("sprite:progress", { instance: this, ...e })
75
102
  emitter.on(ImageLoader.events.PROGRESS, onProgress)
76
103
 
77
104
  const [images, alphaImages] = await Promise.all([
78
- make(originBase, file_name),
79
- originAlpha && fileAlpha ? make(originAlpha, fileAlpha) : Promise.resolve([]),
105
+ make({ origin: originBase, fileName: file_name, list: urls }),
106
+ originAlpha && (fileAlpha || (alpha_urls && alpha_urls.length))
107
+ ? make({ origin: originAlpha, fileName: fileAlpha, list: alpha_urls })
108
+ : Promise.resolve([]),
80
109
  ])
81
110
 
82
111
  emitter.off?.(ImageLoader.events.PROGRESS, onProgress)
@@ -87,9 +116,12 @@ export default class SpritePlayer {
87
116
  if (this.isAlpha && this.dom.alphaImages.length) {
88
117
  this.dom.alphaImages = this.dom.alphaImages.map(img => this.toAlphaFromLuma(img))
89
118
  }
90
- if (this.isAlpha && !this.dom.alphaImages.length) this.isAlpha = false
119
+ if (this.isAlpha && this.dom.alphaImages.length !== this.dom.images.length) {
120
+ this.isAlpha = false
121
+ this.dom.alphaImages = []
122
+ }
91
123
 
92
- this.frameCount = images.length
124
+ this.frameCount = this.dom.images.length
93
125
  this.state.loaded = true
94
126
  emitter.emit("sprite:loaded", this)
95
127
  }
@@ -112,10 +144,9 @@ export default class SpritePlayer {
112
144
  Object.assign(this.canvas, { width: rect.width, height: rect.height, rect, area, offset })
113
145
  }
114
146
 
115
- // grayscale -> alpha (returns a canvas)
116
147
  toAlphaFromLuma(img) {
117
- const w = img.naturalWidth || img.width,
118
- h = img.naturalHeight || img.height
148
+ const w = img.naturalWidth || img.width
149
+ const h = img.naturalHeight || img.height
119
150
  const c = document.createElement("canvas")
120
151
  c.width = w
121
152
  c.height = h
@@ -210,12 +241,11 @@ export default class SpritePlayer {
210
241
  destroy() {
211
242
  this.off()
212
243
  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)
244
+ this._loaders.forEach(l => l.destroy?.())
216
245
  }
217
246
  this.dom.images = []
218
247
  this.dom.alphaImages = []
248
+ this._loaders = []
219
249
  }
220
250
 
221
251
  async init() {