@rafaelaugusto/jolt 0.1.0-beta.3 → 0.1.0-beta.4
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/README.md +55 -13
- package/dist/animate.d.ts.map +1 -1
- package/dist/animate.js +24 -2
- package/dist/animate.js.map +1 -1
- package/dist/color.d.ts +11 -0
- package/dist/color.d.ts.map +1 -0
- package/dist/color.js +73 -0
- package/dist/color.js.map +1 -0
- package/dist/driver/android-color.d.ts +17 -0
- package/dist/driver/android-color.d.ts.map +1 -0
- package/dist/driver/android-color.js +134 -0
- package/dist/driver/android-color.js.map +1 -0
- package/dist/driver/android.d.ts.map +1 -1
- package/dist/driver/android.js +6 -0
- package/dist/driver/android.js.map +1 -1
- package/dist/driver/index.d.ts +1 -1
- package/dist/driver/index.d.ts.map +1 -1
- package/dist/driver/index.js.map +1 -1
- package/dist/driver/ios-color.d.ts +14 -0
- package/dist/driver/ios-color.d.ts.map +1 -0
- package/dist/driver/ios-color.js +381 -0
- package/dist/driver/ios-color.js.map +1 -0
- package/dist/driver/ios.d.ts.map +1 -1
- package/dist/driver/ios.js +9 -1
- package/dist/driver/ios.js.map +1 -1
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +9 -5
- package/dist/engine.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/liquid-grid.d.ts +3 -0
- package/dist/liquid-grid.d.ts.map +1 -0
- package/dist/liquid-grid.js +24 -0
- package/dist/liquid-grid.js.map +1 -0
- package/dist/liquid.d.ts +18 -0
- package/dist/liquid.d.ts.map +1 -0
- package/dist/liquid.js +229 -0
- package/dist/liquid.js.map +1 -0
- package/dist/press.d.ts.map +1 -1
- package/dist/press.js +6 -1
- package/dist/press.js.map +1 -1
- package/dist/properties.d.ts +4 -2
- package/dist/properties.d.ts.map +1 -1
- package/dist/properties.js +12 -1
- package/dist/properties.js.map +1 -1
- package/dist/text.d.ts.map +1 -1
- package/dist/text.js +3 -0
- package/dist/text.js.map +1 -1
- package/dist/track.d.ts +1 -1
- package/dist/track.d.ts.map +1 -1
- package/dist/track.js +38 -2
- package/dist/track.js.map +1 -1
- package/dist/transition.d.ts.map +1 -1
- package/dist/transition.js +8 -2
- package/dist/transition.js.map +1 -1
- package/dist/types.d.ts +5 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +11 -2
- package/platforms/android/include.gradle +7 -0
- package/platforms/android/java/org/rafaelaugusto/jolt/ColorTarget.java +102 -0
- package/platforms/android/proguard-rules.pro +6 -0
- package/src/animate.ts +30 -4
- package/src/color.ts +101 -0
- package/src/driver/android-color.ts +168 -0
- package/src/driver/android.ts +8 -4
- package/src/driver/index.ts +6 -1
- package/src/driver/ios-color.ts +432 -0
- package/src/driver/ios.ts +13 -3
- package/src/engine.ts +9 -5
- package/src/index.ts +3 -0
- package/src/liquid-grid.ts +28 -0
- package/src/liquid.ts +282 -0
- package/src/press.ts +8 -2
- package/src/properties.ts +15 -5
- package/src/text.ts +2 -0
- package/src/track.ts +63 -2
- package/src/transition.ts +10 -2
- package/src/types.ts +5 -0
package/src/liquid.ts
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { Application, Image, isIOS } from '@nativescript/core'
|
|
2
|
+
import { tintColorProperty } from '@nativescript/core/ui/image'
|
|
3
|
+
import { toView } from './animate.js'
|
|
4
|
+
import { liquidGrid } from './liquid-grid.js'
|
|
5
|
+
import type { Target } from './types.js'
|
|
6
|
+
|
|
7
|
+
export interface LiquidOptions {
|
|
8
|
+
/** Amount of internal deformation, from 0 to 1. Defaults to 0.7. */
|
|
9
|
+
intensity?: number
|
|
10
|
+
/** Milliseconds per seamless cycle. Defaults to 8000. */
|
|
11
|
+
duration?: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface LiquidEffect {
|
|
15
|
+
/** Freeze the current deformation. */
|
|
16
|
+
pause(): void
|
|
17
|
+
/** Continue from the paused deformation. */
|
|
18
|
+
resume(): void
|
|
19
|
+
/** Remove the effect and restore the image. Idempotent. */
|
|
20
|
+
stop(): void
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const running = new WeakMap<Image, LiquidEffect>()
|
|
24
|
+
const key = 'jolt:liquid'
|
|
25
|
+
const columns = 24
|
|
26
|
+
const rows = 24
|
|
27
|
+
const steps = 64
|
|
28
|
+
const noop = () => {}
|
|
29
|
+
|
|
30
|
+
/** Deforms a raster image with native iOS texture warps. Android leaves it unchanged. */
|
|
31
|
+
export function liquid(target: Target, options: LiquidOptions = {}): LiquidEffect {
|
|
32
|
+
const settings = {
|
|
33
|
+
intensity: options.intensity ?? 0.7,
|
|
34
|
+
duration: options.duration ?? 8000,
|
|
35
|
+
}
|
|
36
|
+
if (
|
|
37
|
+
!Number.isFinite(settings.intensity) ||
|
|
38
|
+
settings.intensity < 0 ||
|
|
39
|
+
settings.intensity > 1
|
|
40
|
+
)
|
|
41
|
+
throw new RangeError('Liquid intensity must be between zero and one.')
|
|
42
|
+
if (!Number.isFinite(settings.duration) || settings.duration <= 0)
|
|
43
|
+
throw new RangeError('Liquid duration must be a positive finite number.')
|
|
44
|
+
const image = toView(target)
|
|
45
|
+
if (!(image instanceof Image)) throw new TypeError('Expected a NativeScript Image.')
|
|
46
|
+
running.get(image)?.stop()
|
|
47
|
+
if (!isIOS || settings.intensity === 0) return { pause: noop, resume: noop, stop: noop }
|
|
48
|
+
return new Liquid(image, settings)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
class Liquid implements LiquidEffect {
|
|
52
|
+
private stopped = false
|
|
53
|
+
private queued = false
|
|
54
|
+
private pausedByUser = false
|
|
55
|
+
private active = UIApplication.sharedApplication.applicationState === 0 // UIApplicationStateActive
|
|
56
|
+
private native: UIImageView | undefined
|
|
57
|
+
private original: UIImage | undefined
|
|
58
|
+
private source: Image['imageSource'] | undefined
|
|
59
|
+
private renderer: SKView | undefined
|
|
60
|
+
private scene: SKScene | undefined
|
|
61
|
+
private sprite: SKSpriteNode | undefined
|
|
62
|
+
private observers: [
|
|
63
|
+
string,
|
|
64
|
+
ReturnType<typeof Application.ios.addNotificationObserver>,
|
|
65
|
+
][] = []
|
|
66
|
+
|
|
67
|
+
constructor(
|
|
68
|
+
private readonly image: Image,
|
|
69
|
+
private readonly settings: Required<LiquidOptions>,
|
|
70
|
+
) {
|
|
71
|
+
running.set(image, this)
|
|
72
|
+
for (const event of ['loaded', 'layoutChanged', 'imageSourceChange', 'stretchChange'])
|
|
73
|
+
image.on(event, this.schedule)
|
|
74
|
+
image.on('unloaded', this.unloaded)
|
|
75
|
+
image.style.on('visibilityChange', this.schedule)
|
|
76
|
+
image.style.on('tintColorChange', this.schedule)
|
|
77
|
+
this.observe(UIAccessibilityReduceMotionStatusDidChangeNotification, this.schedule)
|
|
78
|
+
this.observe(UIApplicationWillResignActiveNotification, this.suspend)
|
|
79
|
+
this.observe(UIApplicationDidBecomeActiveNotification, this.activate)
|
|
80
|
+
this.schedule()
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
pause = () => {
|
|
84
|
+
this.pausedByUser = true
|
|
85
|
+
if (this.renderer) this.renderer.paused = true
|
|
86
|
+
}
|
|
87
|
+
resume = () => {
|
|
88
|
+
if (this.stopped) return
|
|
89
|
+
this.pausedByUser = false
|
|
90
|
+
this.schedule()
|
|
91
|
+
}
|
|
92
|
+
stop = () => {
|
|
93
|
+
if (this.stopped) return
|
|
94
|
+
this.stopped = true
|
|
95
|
+
for (const event of ['loaded', 'layoutChanged', 'imageSourceChange', 'stretchChange'])
|
|
96
|
+
this.image.off(event, this.schedule)
|
|
97
|
+
this.image.off('unloaded', this.unloaded)
|
|
98
|
+
this.image.style.off('visibilityChange', this.schedule)
|
|
99
|
+
this.image.style.off('tintColorChange', this.schedule)
|
|
100
|
+
for (const [name, observer] of this.observers)
|
|
101
|
+
Application.ios.removeNotificationObserver(observer, name)
|
|
102
|
+
this.observers = []
|
|
103
|
+
if (running.get(this.image) === this) running.delete(this.image)
|
|
104
|
+
this.remove()
|
|
105
|
+
}
|
|
106
|
+
private observe(name: string, callback: () => void) {
|
|
107
|
+
this.observers.push([name, Application.ios.addNotificationObserver(name, callback)])
|
|
108
|
+
}
|
|
109
|
+
private suspend = () => {
|
|
110
|
+
this.active = false
|
|
111
|
+
if (this.renderer) this.renderer.paused = true
|
|
112
|
+
}
|
|
113
|
+
private activate = () => {
|
|
114
|
+
this.active = true
|
|
115
|
+
this.schedule()
|
|
116
|
+
}
|
|
117
|
+
private unloaded = () => this.remove()
|
|
118
|
+
private schedule = () => {
|
|
119
|
+
if (this.stopped || this.queued) return
|
|
120
|
+
this.queued = true
|
|
121
|
+
void Promise.resolve().then(() => {
|
|
122
|
+
this.queued = false
|
|
123
|
+
if (this.stopped) return
|
|
124
|
+
try {
|
|
125
|
+
this.update()
|
|
126
|
+
} catch (error) {
|
|
127
|
+
this.stop()
|
|
128
|
+
throw error
|
|
129
|
+
}
|
|
130
|
+
})
|
|
131
|
+
}
|
|
132
|
+
private update() {
|
|
133
|
+
if (
|
|
134
|
+
!this.image.isLoaded ||
|
|
135
|
+
this.image.visibility !== 'visible' ||
|
|
136
|
+
this.image.tintColor ||
|
|
137
|
+
UIAccessibilityIsReduceMotionEnabled()
|
|
138
|
+
)
|
|
139
|
+
return this.remove()
|
|
140
|
+
const native = this.image.nativeViewProtected as UIImageView | undefined
|
|
141
|
+
if (!(native instanceof UIImageView) || !native.window) return this.remove()
|
|
142
|
+
if (
|
|
143
|
+
this.native &&
|
|
144
|
+
(this.native !== native ||
|
|
145
|
+
native.image !== null ||
|
|
146
|
+
this.image.imageSource !== this.source)
|
|
147
|
+
)
|
|
148
|
+
this.remove()
|
|
149
|
+
const source = this.original ?? native.image
|
|
150
|
+
if (
|
|
151
|
+
!source?.CGImage ||
|
|
152
|
+
source.images?.count ||
|
|
153
|
+
source.renderingMode === 2 /* AlwaysTemplate */
|
|
154
|
+
)
|
|
155
|
+
return this.remove()
|
|
156
|
+
if (source.size.width * source.size.height * source.scale ** 2 > 16_777_216)
|
|
157
|
+
return this.remove()
|
|
158
|
+
const bounds = native.bounds
|
|
159
|
+
if (
|
|
160
|
+
bounds.size.width <= 0 ||
|
|
161
|
+
bounds.size.height <= 0 ||
|
|
162
|
+
source.size.width <= 0 ||
|
|
163
|
+
source.size.height <= 0
|
|
164
|
+
)
|
|
165
|
+
return this.remove()
|
|
166
|
+
if (!this.renderer) {
|
|
167
|
+
// A paused SKView cannot draw its first frame. Keep the original visible
|
|
168
|
+
// until rendering can start, including source changes while paused.
|
|
169
|
+
if (this.pausedByUser || !this.active) return
|
|
170
|
+
this.create(native, source)
|
|
171
|
+
}
|
|
172
|
+
const renderer = this.renderer!
|
|
173
|
+
renderer.frame = CGRectMake(
|
|
174
|
+
bounds.origin.x,
|
|
175
|
+
bounds.origin.y,
|
|
176
|
+
bounds.size.width,
|
|
177
|
+
bounds.size.height,
|
|
178
|
+
)
|
|
179
|
+
this.scene!.size = CGSizeMake(bounds.size.width, bounds.size.height)
|
|
180
|
+
const iw = source.size.width
|
|
181
|
+
const ih = source.size.height
|
|
182
|
+
const { width, height } = bounds.size
|
|
183
|
+
const stretch = this.image.stretch
|
|
184
|
+
const scale =
|
|
185
|
+
stretch === 'aspectFill'
|
|
186
|
+
? Math.max(width / iw, height / ih)
|
|
187
|
+
: Math.min(width / iw, height / ih)
|
|
188
|
+
this.sprite!.size = CGSizeMake(
|
|
189
|
+
stretch === 'fill' ? width : stretch === 'none' ? iw : iw * scale,
|
|
190
|
+
stretch === 'fill' ? height : stretch === 'none' ? ih : ih * scale,
|
|
191
|
+
)
|
|
192
|
+
this.sprite!.position = CGPointMake(width / 2, height / 2)
|
|
193
|
+
renderer.paused = this.pausedByUser || !this.active
|
|
194
|
+
}
|
|
195
|
+
private create(native: UIImageView, source: UIImage) {
|
|
196
|
+
const renderer = SKView.alloc().initWithFrame(native.bounds)
|
|
197
|
+
this.renderer = renderer
|
|
198
|
+
this.native = native
|
|
199
|
+
this.original = source
|
|
200
|
+
this.source = this.image.imageSource
|
|
201
|
+
renderer.opaque = false
|
|
202
|
+
renderer.backgroundColor = UIColor.clearColor
|
|
203
|
+
renderer.allowsTransparency = true
|
|
204
|
+
renderer.asynchronous = true
|
|
205
|
+
renderer.userInteractionEnabled = false
|
|
206
|
+
renderer.accessibilityElementsHidden = true
|
|
207
|
+
renderer.clipsToBounds = true
|
|
208
|
+
renderer.preferredFramesPerSecond = Math.min(
|
|
209
|
+
120,
|
|
210
|
+
native.window?.screen?.maximumFramesPerSecond ?? 60,
|
|
211
|
+
)
|
|
212
|
+
renderer.paused = true
|
|
213
|
+
const scene = SKScene.sceneWithSize(native.bounds.size)
|
|
214
|
+
this.scene = scene
|
|
215
|
+
scene.backgroundColor = UIColor.clearColor
|
|
216
|
+
scene.scaleMode = 3 // SKSceneScaleModeResizeFill
|
|
217
|
+
const texture = SKTexture.textureWithImage(source)
|
|
218
|
+
const sprite = SKSpriteNode.spriteNodeWithTexture(texture)
|
|
219
|
+
this.sprite = sprite
|
|
220
|
+
const warps: SKWarpGeometryGrid[] = []
|
|
221
|
+
const times: number[] = []
|
|
222
|
+
for (let step = 0; step <= steps; step++) {
|
|
223
|
+
const positions = liquidGrid(
|
|
224
|
+
columns,
|
|
225
|
+
rows,
|
|
226
|
+
step === steps ? 0 : step / steps,
|
|
227
|
+
this.settings.intensity,
|
|
228
|
+
)
|
|
229
|
+
warps.push(
|
|
230
|
+
SKWarpGeometryGrid.gridWithColumnsRowsSourcePositionsDestPositions(
|
|
231
|
+
columns,
|
|
232
|
+
rows,
|
|
233
|
+
null,
|
|
234
|
+
positions,
|
|
235
|
+
),
|
|
236
|
+
)
|
|
237
|
+
times.push(((step / steps) * this.settings.duration) / 1000)
|
|
238
|
+
}
|
|
239
|
+
const loop = SKAction.animateWithWarpsTimes(warps, times)
|
|
240
|
+
if (!loop) throw new Error('Unable to create the liquid warp action.')
|
|
241
|
+
sprite.warpGeometry = SKWarpGeometryGrid.gridWithColumnsRows(columns, rows)
|
|
242
|
+
const entrance = SKAction.warpToDuration(
|
|
243
|
+
warps[0]!,
|
|
244
|
+
Math.min(0.35, this.settings.duration / 10000),
|
|
245
|
+
)
|
|
246
|
+
if (!entrance) throw new Error('Unable to create the liquid entrance.')
|
|
247
|
+
sprite.runActionWithKey(
|
|
248
|
+
SKAction.sequence([entrance, SKAction.repeatActionForever(loop)]),
|
|
249
|
+
key,
|
|
250
|
+
)
|
|
251
|
+
scene.addChild(sprite)
|
|
252
|
+
renderer.presentScene(scene)
|
|
253
|
+
native.addSubview(renderer)
|
|
254
|
+
native.image = null
|
|
255
|
+
}
|
|
256
|
+
private remove() {
|
|
257
|
+
const { renderer, sprite, native, original } = this
|
|
258
|
+
if (renderer) {
|
|
259
|
+
renderer.paused = true
|
|
260
|
+
sprite?.removeAllActions()
|
|
261
|
+
sprite?.removeFromParent()
|
|
262
|
+
renderer.presentScene(null)
|
|
263
|
+
renderer.removeFromSuperview()
|
|
264
|
+
}
|
|
265
|
+
if (
|
|
266
|
+
native &&
|
|
267
|
+
native.image === null &&
|
|
268
|
+
original &&
|
|
269
|
+
this.image.imageSource === this.source
|
|
270
|
+
) {
|
|
271
|
+
native.image = original
|
|
272
|
+
// Core couldn't turn the suppressed UIImage into a template while it was
|
|
273
|
+
// hidden. Reapply its public native setter when a tint takes over.
|
|
274
|
+
if (this.image.tintColor)
|
|
275
|
+
(this.image as unknown as Record<symbol, (value: unknown) => void>)[
|
|
276
|
+
tintColorProperty.setNative
|
|
277
|
+
]!(this.image.tintColor)
|
|
278
|
+
}
|
|
279
|
+
this.renderer = this.scene = this.sprite = this.native = this.original = undefined
|
|
280
|
+
this.source = undefined
|
|
281
|
+
}
|
|
282
|
+
}
|
package/src/press.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { Color, type EventData, type TouchGestureEventData } from '@nativescript/core'
|
|
2
|
+
import { isColorProperty } from './color.js'
|
|
2
3
|
import { animate, toView } from './animate.js'
|
|
3
4
|
import { isProperty, readValue, type Property } from './properties.js'
|
|
4
5
|
import type { SpringName, Transition } from './transition.js'
|
|
@@ -30,7 +31,12 @@ export function press(
|
|
|
30
31
|
const onTouch = (event: EventData) => {
|
|
31
32
|
const { action } = event as TouchGestureEventData
|
|
32
33
|
if (action === 'down' && !rest && view.isEnabled !== false) {
|
|
33
|
-
rest = Object.fromEntries(
|
|
34
|
+
rest = Object.fromEntries(
|
|
35
|
+
properties.map((p) => [
|
|
36
|
+
p,
|
|
37
|
+
isColorProperty(p) ? new Color(readValue(view, p)) : readValue(view, p),
|
|
38
|
+
]),
|
|
39
|
+
)
|
|
34
40
|
animate(view, pressed, options ?? pressIn)
|
|
35
41
|
} else if ((action === 'up' || action === 'cancel') && rest) {
|
|
36
42
|
animate(
|
package/src/properties.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { Color, type View } from '@nativescript/core'
|
|
2
|
+
import { isColorProperty, readColor, type ColorProperty } from './color.js'
|
|
2
3
|
|
|
3
|
-
export type
|
|
4
|
+
export type NumericProperty =
|
|
4
5
|
'opacity' | 'x' | 'y' | 'scaleX' | 'scaleY' | 'rotate' | 'rotateX' | 'rotateY'
|
|
6
|
+
export type Property = NumericProperty | ColorProperty
|
|
5
7
|
|
|
6
8
|
const viewKeys = {
|
|
7
9
|
opacity: 'opacity',
|
|
@@ -12,9 +14,9 @@ const viewKeys = {
|
|
|
12
14
|
rotate: 'rotate',
|
|
13
15
|
rotateX: 'rotateX',
|
|
14
16
|
rotateY: 'rotateY',
|
|
15
|
-
} as const satisfies Record<
|
|
17
|
+
} as const satisfies Record<NumericProperty, keyof View>
|
|
16
18
|
|
|
17
|
-
const defaults: Record<
|
|
19
|
+
const defaults: Record<NumericProperty, number> = {
|
|
18
20
|
opacity: 1,
|
|
19
21
|
x: 0,
|
|
20
22
|
y: 0,
|
|
@@ -27,6 +29,9 @@ const defaults: Record<Property, number> = {
|
|
|
27
29
|
|
|
28
30
|
/** How close to its target a spring or decay has to be to count as settled. */
|
|
29
31
|
export const precision: Record<Property, number> = {
|
|
32
|
+
backgroundColor: 0.002,
|
|
33
|
+
borderColor: 0.002,
|
|
34
|
+
color: 0.002,
|
|
30
35
|
opacity: 0.002,
|
|
31
36
|
x: 0.25,
|
|
32
37
|
y: 0.25,
|
|
@@ -38,14 +43,19 @@ export const precision: Record<Property, number> = {
|
|
|
38
43
|
}
|
|
39
44
|
|
|
40
45
|
export function isProperty(key: string): key is Property {
|
|
41
|
-
return key in viewKeys
|
|
46
|
+
return key in viewKeys || isColorProperty(key)
|
|
42
47
|
}
|
|
43
48
|
|
|
44
49
|
export function readValue(view: View, property: Property): number {
|
|
50
|
+
if (isColorProperty(property)) return readColor(view, property)
|
|
45
51
|
const value = view[viewKeys[property]]
|
|
46
52
|
return typeof value === 'number' && !Number.isNaN(value) ? value : defaults[property]
|
|
47
53
|
}
|
|
48
54
|
|
|
49
55
|
export function writeValue(view: View, property: Property, value: number) {
|
|
56
|
+
if (isColorProperty(property)) {
|
|
57
|
+
view[property] = new Color(value)
|
|
58
|
+
return
|
|
59
|
+
}
|
|
50
60
|
view[viewKeys[property]] = value
|
|
51
61
|
}
|
package/src/text.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { stopIOSColorText } from './driver/ios-color.js'
|
|
1
2
|
import { Application, Label, isIOS } from '@nativescript/core'
|
|
2
3
|
import { textProperty, formattedTextProperty } from '@nativescript/core/ui/text-base'
|
|
3
4
|
import { toView } from './animate.js'
|
|
@@ -119,6 +120,7 @@ class TextAnimation implements Animation {
|
|
|
119
120
|
this.finished = new Promise((resolve) => {
|
|
120
121
|
this.resolve = resolve
|
|
121
122
|
})
|
|
123
|
+
if (isIOS) stopIOSColorText(view)
|
|
122
124
|
this.formatted = view.formattedText
|
|
123
125
|
running.set(view, this)
|
|
124
126
|
if (!isIOS) {
|
package/src/track.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { solveDecay } from './decay.js'
|
|
|
3
3
|
import { precision, type Property } from './properties.js'
|
|
4
4
|
import { solveSpring, springPhysics } from './spring.js'
|
|
5
5
|
import type { Curve, ResolvedTransition } from './transition.js'
|
|
6
|
+
import { colorMixer, isColorProperty, type ColorProperty } from './color.js'
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* One property animated over time. The curve is computed here and sampled,
|
|
@@ -44,9 +45,11 @@ const ARRIVAL_TOLERANCE = 0.05
|
|
|
44
45
|
export function createTrack(
|
|
45
46
|
property: Property,
|
|
46
47
|
keyframes: readonly number[],
|
|
47
|
-
|
|
48
|
+
transition: ResolvedTransition,
|
|
48
49
|
velocity: number,
|
|
49
50
|
): Track {
|
|
51
|
+
if (isColorProperty(property)) return colorTrack(property, keyframes, transition)
|
|
52
|
+
const { curve, delay, repeat, reverse } = transition
|
|
50
53
|
const motion =
|
|
51
54
|
curve.type === 'spring'
|
|
52
55
|
? spring(property, keyframes, curve, velocity)
|
|
@@ -93,6 +96,58 @@ export function createTrack(
|
|
|
93
96
|
}
|
|
94
97
|
}
|
|
95
98
|
|
|
99
|
+
function colorTrack(
|
|
100
|
+
property: ColorProperty,
|
|
101
|
+
colors: readonly number[],
|
|
102
|
+
transition: ResolvedTransition,
|
|
103
|
+
): Track {
|
|
104
|
+
const progress = createTrack(
|
|
105
|
+
'opacity',
|
|
106
|
+
colors.map((_, i) => i),
|
|
107
|
+
transition,
|
|
108
|
+
0,
|
|
109
|
+
)
|
|
110
|
+
const mixes = colors.slice(1).map((color, i) => colorMixer(colors[i]!, color))
|
|
111
|
+
const colorAt = (position: number) => {
|
|
112
|
+
const p = Math.max(0, Math.min(colors.length - 1, position))
|
|
113
|
+
const segment = Math.min(mixes.length - 1, Math.floor(p))
|
|
114
|
+
return mixes[segment]!(p - segment)
|
|
115
|
+
}
|
|
116
|
+
// Linear timing still needs intermediate colors: native RGB interpolation is
|
|
117
|
+
// only the short bridge between these perceptually spaced samples.
|
|
118
|
+
const steps = Math.max(
|
|
119
|
+
1,
|
|
120
|
+
Math.min(
|
|
121
|
+
MAX_DECAY_SAMPLES,
|
|
122
|
+
Math.ceil((progress.duration / 1000) * SAMPLES_PER_SECOND),
|
|
123
|
+
),
|
|
124
|
+
)
|
|
125
|
+
const times = [
|
|
126
|
+
...new Set([
|
|
127
|
+
...progress.times,
|
|
128
|
+
...Array.from({ length: steps + 1 }, (_, i) => i / steps),
|
|
129
|
+
]),
|
|
130
|
+
].sort((a, b) => a - b)
|
|
131
|
+
return {
|
|
132
|
+
...progress,
|
|
133
|
+
property,
|
|
134
|
+
times,
|
|
135
|
+
values: times.map((time) =>
|
|
136
|
+
colorAt(
|
|
137
|
+
time === 0
|
|
138
|
+
? progress.values[0]!
|
|
139
|
+
: time === 1
|
|
140
|
+
? progress.values.at(-1)!
|
|
141
|
+
: progress.valueAt(progress.delay + time * progress.duration),
|
|
142
|
+
),
|
|
143
|
+
),
|
|
144
|
+
to: colorAt(progress.to),
|
|
145
|
+
valueAt: (elapsed) => colorAt(progress.valueAt(elapsed)),
|
|
146
|
+
// Packed colors have no scalar velocity to carry into another spring.
|
|
147
|
+
velocityAt: () => 0,
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
96
151
|
function decay(
|
|
97
152
|
property: Property,
|
|
98
153
|
from: number,
|
|
@@ -185,7 +240,13 @@ function tween(
|
|
|
185
240
|
const steps =
|
|
186
241
|
easing === linear || from === to
|
|
187
242
|
? 1
|
|
188
|
-
: Math.max(
|
|
243
|
+
: Math.max(
|
|
244
|
+
1,
|
|
245
|
+
Math.min(
|
|
246
|
+
MAX_DECAY_SAMPLES,
|
|
247
|
+
Math.ceil(((span * duration) / 1000) * SAMPLES_PER_SECOND),
|
|
248
|
+
),
|
|
249
|
+
)
|
|
189
250
|
for (let step = 1; step <= steps; step++) {
|
|
190
251
|
times.push(start + (span * step) / steps)
|
|
191
252
|
values.push(step === steps ? to : from + (to - from) * easing(step / steps))
|
package/src/transition.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { resolveEasing, type Easing, type EasingFunction } from './easing.js'
|
|
2
2
|
import type { Property } from './properties.js'
|
|
3
|
+
import { isColorProperty } from './color.js'
|
|
3
4
|
import { springPhysics, type SpringPhysics } from './spring.js'
|
|
4
5
|
|
|
5
6
|
export interface Timing {
|
|
@@ -102,7 +103,10 @@ export function resolveTransition(
|
|
|
102
103
|
transition.type ??
|
|
103
104
|
(springKeys.some((key) => (transition as Spring)[key] !== undefined)
|
|
104
105
|
? 'spring'
|
|
105
|
-
: hasCurve(transition) ||
|
|
106
|
+
: hasCurve(transition) ||
|
|
107
|
+
property === 'opacity' ||
|
|
108
|
+
isColorProperty(property) ||
|
|
109
|
+
keyframes > 2
|
|
106
110
|
? 'tween'
|
|
107
111
|
: 'spring')
|
|
108
112
|
|
|
@@ -128,7 +132,11 @@ export function resolveTransition(
|
|
|
128
132
|
|
|
129
133
|
const tween = transition as Tween
|
|
130
134
|
const duration = tween.duration ?? DEFAULT_DURATION
|
|
131
|
-
if (
|
|
135
|
+
if (
|
|
136
|
+
duration < 0 ||
|
|
137
|
+
Number.isNaN(duration) ||
|
|
138
|
+
(isColorProperty(property) && !Number.isFinite(duration))
|
|
139
|
+
) {
|
|
132
140
|
throw new RangeError(`Invalid duration: ${duration}.`)
|
|
133
141
|
}
|
|
134
142
|
|
package/src/types.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import type { View } from '@nativescript/core'
|
|
2
2
|
import type { Haptic } from './haptics.js'
|
|
3
3
|
import type { SpringName, Transition } from './transition.js'
|
|
4
|
+
import type { ColorKeyframes } from './color.js'
|
|
4
5
|
|
|
5
6
|
/** A value to animate to, or keyframes. `null` stands for the value the view has when the animation starts. */
|
|
6
7
|
export type KeyframeValue = number | readonly (number | null)[]
|
|
7
8
|
|
|
8
9
|
export interface Keyframes {
|
|
10
|
+
backgroundColor?: ColorKeyframes
|
|
11
|
+
/** Uniform starting edge colors; widths and radii may differ. */
|
|
12
|
+
borderColor?: ColorKeyframes
|
|
13
|
+
color?: ColorKeyframes
|
|
9
14
|
opacity?: KeyframeValue
|
|
10
15
|
/** Same as `translateX`, in DIPs. */
|
|
11
16
|
x?: KeyframeValue
|