react-native-marquee-animation 0.1.0

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.
Files changed (68) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +174 -0
  3. package/RNMarquee.podspec +27 -0
  4. package/THIRD_PARTY_NOTICES.md +8 -0
  5. package/android/build.gradle +58 -0
  6. package/android/settings.gradle +34 -0
  7. package/android/src/main/AndroidManifest.xml +1 -0
  8. package/android/src/main/java/com/marquee/MarqueeEvent.kt +44 -0
  9. package/android/src/main/java/com/marquee/MarqueeModels.kt +45 -0
  10. package/android/src/main/java/com/marquee/MarqueeMotion.kt +163 -0
  11. package/android/src/main/java/com/marquee/MarqueePackage.kt +16 -0
  12. package/android/src/main/java/com/marquee/MarqueeView.kt +376 -0
  13. package/android/src/main/java/com/marquee/MarqueeViewManager.kt +135 -0
  14. package/ios/MarqueeRenderer.swift +652 -0
  15. package/ios/RNMarqueeView.h +14 -0
  16. package/ios/RNMarqueeView.mm +118 -0
  17. package/lib/module/RNMarqueeViewNativeComponent.ts +32 -0
  18. package/lib/module/index.js +4 -0
  19. package/lib/module/index.js.map +1 -0
  20. package/lib/module/internal/events.js +10 -0
  21. package/lib/module/internal/events.js.map +1 -0
  22. package/lib/module/internal/geometry.js +10 -0
  23. package/lib/module/internal/geometry.js.map +1 -0
  24. package/lib/module/internal/options.js +29 -0
  25. package/lib/module/internal/options.js.map +1 -0
  26. package/lib/module/marquee.js +32 -0
  27. package/lib/module/marquee.js.map +1 -0
  28. package/lib/module/marquee.native.js +95 -0
  29. package/lib/module/marquee.native.js.map +1 -0
  30. package/lib/module/package.json +1 -0
  31. package/lib/module/types.js +4 -0
  32. package/lib/module/types.js.map +1 -0
  33. package/lib/module/use-reduced-motion.js +61 -0
  34. package/lib/module/use-reduced-motion.js.map +1 -0
  35. package/lib/module/view-props.js +5 -0
  36. package/lib/module/view-props.js.map +1 -0
  37. package/lib/typescript/package.json +1 -0
  38. package/lib/typescript/src/RNMarqueeViewNativeComponent.d.ts +28 -0
  39. package/lib/typescript/src/RNMarqueeViewNativeComponent.d.ts.map +1 -0
  40. package/lib/typescript/src/index.d.ts +3 -0
  41. package/lib/typescript/src/index.d.ts.map +1 -0
  42. package/lib/typescript/src/internal/events.d.ts +3 -0
  43. package/lib/typescript/src/internal/events.d.ts.map +1 -0
  44. package/lib/typescript/src/internal/geometry.d.ts +3 -0
  45. package/lib/typescript/src/internal/geometry.d.ts.map +1 -0
  46. package/lib/typescript/src/internal/options.d.ts +16 -0
  47. package/lib/typescript/src/internal/options.d.ts.map +1 -0
  48. package/lib/typescript/src/marquee.d.ts +469 -0
  49. package/lib/typescript/src/marquee.d.ts.map +1 -0
  50. package/lib/typescript/src/marquee.native.d.ts +470 -0
  51. package/lib/typescript/src/marquee.native.d.ts.map +1 -0
  52. package/lib/typescript/src/types.d.ts +46 -0
  53. package/lib/typescript/src/types.d.ts.map +1 -0
  54. package/lib/typescript/src/use-reduced-motion.d.ts +3 -0
  55. package/lib/typescript/src/use-reduced-motion.d.ts.map +1 -0
  56. package/lib/typescript/src/view-props.d.ts +4 -0
  57. package/lib/typescript/src/view-props.d.ts.map +1 -0
  58. package/package.json +166 -0
  59. package/src/RNMarqueeViewNativeComponent.ts +32 -0
  60. package/src/index.ts +12 -0
  61. package/src/internal/events.ts +12 -0
  62. package/src/internal/geometry.ts +14 -0
  63. package/src/internal/options.ts +48 -0
  64. package/src/marquee.native.tsx +144 -0
  65. package/src/marquee.tsx +35 -0
  66. package/src/types.ts +59 -0
  67. package/src/use-reduced-motion.ts +88 -0
  68. package/src/view-props.ts +24 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Vivek Indlebele Narasimha Prasad
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,174 @@
1
+ # React Native Marquee
2
+
3
+ A production-grade generic marquee for React Native's New Architecture. Render
4
+ ordinary React Native text, images, icons, badges, charts, and composed views
5
+ once; native iOS and Android code repeats the visual strip and owns all motion.
6
+
7
+ There is no per-frame JavaScript, duplicated React state, or list
8
+ virtualization.
9
+
10
+ ## Demo
11
+
12
+ https://github.com/user-attachments/assets/9f4e1655-0f2c-468f-b0e4-25af40de5631
13
+
14
+ ## Compatibility
15
+
16
+ | Requirement | Supported |
17
+ | ------------ | ---------------------------------------- |
18
+ | React Native | 0.85.x |
19
+ | React | 19.2.x |
20
+ | Expo | SDK 56 development and production builds |
21
+ | iOS | 17 and newer |
22
+ | Android | API 24 and newer |
23
+ | Architecture | New Architecture (Fabric) only |
24
+ | Expo Go | No |
25
+
26
+ ## Installation
27
+
28
+ ```sh
29
+ bun add react-native-marquee-animation
30
+ ```
31
+
32
+ Expo applications must regenerate and rebuild the native project:
33
+
34
+ ```sh
35
+ bunx expo prebuild --clean
36
+ bunx expo run:ios
37
+ # or: bunx expo run:android
38
+ ```
39
+
40
+ ## Quick start
41
+
42
+ ```tsx
43
+ import { Image, StyleSheet, Text, View } from 'react-native';
44
+ import { Marquee } from 'react-native-marquee-animation';
45
+
46
+ export const NewsStrip = () => (
47
+ <Marquee
48
+ accessibilityLabel="Latest headlines"
49
+ contentContainerStyle={styles.row}
50
+ spacing={32}
51
+ speed={25}
52
+ style={styles.marquee}
53
+ >
54
+ <View style={styles.card}>
55
+ <Image source={require('./logo.png')} style={styles.image} />
56
+ <Text>Images, text, badges, icons, and custom views</Text>
57
+ </View>
58
+ <View style={styles.card}>
59
+ <Text>React state is mounted exactly once</Text>
60
+ </View>
61
+ </Marquee>
62
+ );
63
+
64
+ const styles = StyleSheet.create({
65
+ card: { alignItems: 'center', flexDirection: 'row', gap: 8 },
66
+ image: { height: 24, width: 24 },
67
+ marquee: { height: 48 },
68
+ row: { alignItems: 'center', gap: 24 },
69
+ });
70
+ ```
71
+
72
+ The package wraps `children` in one non-collapsible horizontal content view,
73
+ measures that view only when layout changes, and passes one width update to
74
+ native code. Native motion remains uninterrupted when child pixels change
75
+ without changing width.
76
+
77
+ ## Gesture physics
78
+
79
+ Dragging, release velocity, decay, and the transition back to automatic motion
80
+ are native-owned:
81
+
82
+ ```tsx
83
+ <Marquee
84
+ accessibilityLabel="Live activity"
85
+ interaction={{
86
+ maxFlingVelocity: 3200,
87
+ deceleration: 0.9985,
88
+ pauseOnPress: true,
89
+ resumeDelay: 0,
90
+ }}
91
+ >
92
+ {content}
93
+ </Marquee>
94
+ ```
95
+
96
+ After a fling decelerates, both platforms blend velocity into the configured
97
+ automatic direction over 350 ms instead of stopping or snapping.
98
+
99
+ ## Content contract
100
+
101
+ Supported content includes normal layer/display-list-backed React Native views:
102
+
103
+ - `Text`, `View`, `Image`, and `expo-image`;
104
+ - SVG, icons, badges, gradients, and composed layouts;
105
+ - custom charts that render into the ordinary React Native view hierarchy;
106
+ - child state, asynchronous image loading, and dynamic value changes.
107
+
108
+ Marquee children are deliberately noninteractive. The host owns horizontal
109
+ pan, hold, and fling gestures, and repeated pixels are not additional React
110
+ views. Nested `Pressable`, `TextInput`, map, video, camera, `SurfaceView`,
111
+ `TextureView`, Metal, and other external rendering surfaces are outside the
112
+ contract. Use a separate typed interaction layer when individual repeated
113
+ items must be clickable.
114
+
115
+ ## Main props
116
+
117
+ | Prop | Default | Meaning |
118
+ | ----------------------- | -------- | -------------------------------------------- |
119
+ | `accessibilityLabel` | required | One semantic description of the strip |
120
+ | `speed` | `25` | Logical pixels per second |
121
+ | `direction` | `left` | Automatic motion direction |
122
+ | `spacing` | `0` | Empty distance between visual copies |
123
+ | `active` | `true` | Application-level pause control |
124
+ | `shortContentMode` | `static` | Static content or repeated short content |
125
+ | `contentAlignment` | `start` | Alignment while short content is stationary |
126
+ | `contentContainerStyle` | — | Style for the one horizontal content wrapper |
127
+ | `reduceMotion` | `system` | `system`, `always`, or `never` |
128
+
129
+ ## Native architecture
130
+
131
+ - iOS mounts one Fabric child container as the only subview of a
132
+ `CAReplicatorLayer`-backed Swift view. Core Animation translates it without a
133
+ display callback during automatic motion.
134
+ - Android mounts one Fabric child container in a custom `ViewGroup` and replays
135
+ the existing hardware display lists at repeated canvas offsets. A monotonic
136
+ `Choreographer` clock changes only the translation phase.
137
+ - Same-width visual updates do not restart motion. Width changes preserve the
138
+ physical offset and velocity while native code recomputes the loop period.
139
+ - Content that fits, inactive/offscreen/detached views, backgrounded apps, and
140
+ Reduced Motion schedule no continuous frame work.
141
+ - Only the host is accessible; visually repeated descendants are hidden from
142
+ VoiceOver and TalkBack.
143
+
144
+ See [architecture](docs/architecture.md), [accessibility](docs/accessibility.md),
145
+ [testing](docs/testing.md), and [performance](docs/performance.md).
146
+
147
+ ## Fabric and Codegen
148
+
149
+ `src/RNMarqueeViewNativeComponent.ts` is the source of truth. The native
150
+ component is `RNMarqueeView`, the Codegen library is `RNMarqueeViewSpec`, and
151
+ the iOS pod/module is `RNMarquee`. Generated bindings are consumer-owned and
152
+ are not published.
153
+
154
+ ## Development
155
+
156
+ The repository uses Bun, React Native Builder Bob, an Expo SDK 56 consumer,
157
+ Swift and Kotlin tests, Release builds, packed-tarball validation, and Maestro.
158
+ Start with [CONTRIBUTING.md](CONTRIBUTING.md).
159
+
160
+ ### Example experience
161
+
162
+ The Expo example follows the same showcase-first product system as the sibling
163
+ number-animation package. Release builds open a self-running green-and-cream
164
+ showcase with product, music, event, finance, image, badge, a Bhagavad Gita
165
+ verse in Kannada script, and gesture examples. Development builds expose an
166
+ `Open reliability lab` button; benchmark deep links open that lab directly in
167
+ either build.
168
+
169
+ Use `bun run capture:ios` or `bun run capture:android` from `example/` for clean
170
+ Release presentation captures. See [example/CAPTURE.md](example/CAPTURE.md).
171
+
172
+ ## License
173
+
174
+ [MIT](LICENSE)
@@ -0,0 +1,27 @@
1
+ require "json"
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, "package.json")))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = "RNMarquee"
7
+ s.version = package["version"]
8
+ s.summary = package["description"]
9
+ s.homepage = package["homepage"]
10
+ s.license = package["license"]
11
+ s.authors = package["author"]
12
+
13
+ s.platforms = { :ios => "17.0" }
14
+ s.source = { :git => "https://github.com/invivek26/react-native-marquee-animation.git", :tag => "#{s.version}" }
15
+
16
+ s.source_files = "ios/*.{h,m,mm,swift,cpp}"
17
+ s.private_header_files = "ios/*.h"
18
+ s.static_framework = true
19
+ s.swift_version = "5.9"
20
+ s.pod_target_xcconfig = {
21
+ "CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
22
+ "DEFINES_MODULE" => "YES",
23
+ "SWIFT_OBJC_INTERFACE_HEADER_NAME" => "RNMarquee-Swift.h"
24
+ }
25
+
26
+ install_modules_dependencies(s)
27
+ end
@@ -0,0 +1,8 @@
1
+ # Third-party notices
2
+
3
+ This package is an independent implementation. It does not currently bundle
4
+ third-party runtime source code.
5
+
6
+ Architecture research and documentation may reference platform documentation
7
+ and open-source projects. Any future copied or adapted implementation must be
8
+ reviewed for license compatibility and recorded here before release.
@@ -0,0 +1,58 @@
1
+ buildscript {
2
+ ext.Marquee = [
3
+ kotlinVersion: "2.0.21",
4
+ minSdkVersion: 24,
5
+ compileSdkVersion: 36
6
+ ]
7
+
8
+ ext.getExtOrDefault = { prop ->
9
+ if (rootProject.ext.has(prop) && rootProject.ext.get(prop) != null) {
10
+ return rootProject.ext.get(prop)
11
+ }
12
+ return Marquee[prop]
13
+ }
14
+
15
+ repositories {
16
+ google()
17
+ mavenCentral()
18
+ }
19
+
20
+ dependencies {
21
+ classpath "com.android.tools.build:gradle:8.7.2"
22
+ classpath "com.facebook.react:react-native-gradle-plugin"
23
+ // noinspection DifferentKotlinGradleVersion
24
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
25
+ }
26
+ }
27
+
28
+ apply plugin: "com.android.library"
29
+ apply plugin: "kotlin-android"
30
+ apply plugin: "com.facebook.react"
31
+
32
+ android {
33
+ namespace "com.marquee"
34
+ compileSdkVersion getExtOrDefault("compileSdkVersion")
35
+
36
+ defaultConfig {
37
+ minSdkVersion getExtOrDefault("minSdkVersion")
38
+ testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
39
+ }
40
+
41
+ compileOptions {
42
+ sourceCompatibility JavaVersion.VERSION_17
43
+ targetCompatibility JavaVersion.VERSION_17
44
+ }
45
+ }
46
+
47
+ def standaloneReactAndroid = file("../node_modules/react-native/package.json")
48
+ def reactAndroidDependency = standaloneReactAndroid.exists()
49
+ ? "com.facebook.react:react-android:${new groovy.json.JsonSlurper().parse(standaloneReactAndroid).version}"
50
+ : "com.facebook.react:react-android"
51
+
52
+ dependencies {
53
+ implementation reactAndroidDependency
54
+ testImplementation "junit:junit:4.13.2"
55
+ androidTestImplementation "androidx.test:core:1.6.1"
56
+ androidTestImplementation "androidx.test.ext:junit:1.2.1"
57
+ androidTestImplementation "androidx.test:runner:1.6.2"
58
+ }
@@ -0,0 +1,34 @@
1
+ pluginManagement {
2
+ def reactNativeGradlePlugin = new File(
3
+ providers.exec {
4
+ workingDir(rootDir)
5
+ commandLine(
6
+ "node",
7
+ "--print",
8
+ "require.resolve('@react-native/gradle-plugin/package.json', { paths: [require.resolve('react-native/package.json')] })"
9
+ )
10
+ }.standardOutput.asText.get().trim()
11
+ ).getParentFile().absolutePath
12
+ includeBuild(reactNativeGradlePlugin)
13
+ }
14
+
15
+ def reactNativeGradlePluginBuild = new File(
16
+ providers.exec {
17
+ workingDir(rootDir)
18
+ commandLine(
19
+ "node",
20
+ "--print",
21
+ "require.resolve('@react-native/gradle-plugin/package.json', { paths: [require.resolve('react-native/package.json')] })"
22
+ )
23
+ }.standardOutput.asText.get().trim()
24
+ ).getParentFile().absolutePath
25
+ includeBuild(reactNativeGradlePluginBuild)
26
+
27
+ dependencyResolutionManagement {
28
+ repositories {
29
+ google()
30
+ mavenCentral()
31
+ }
32
+ }
33
+
34
+ rootProject.name = "RNMarqueeView"
@@ -0,0 +1 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android" />
@@ -0,0 +1,44 @@
1
+ package com.marquee
2
+
3
+ import com.facebook.react.bridge.Arguments
4
+ import com.facebook.react.bridge.WritableMap
5
+ import com.facebook.react.uimanager.events.Event
6
+
7
+ internal class AnimationStateChangeEvent(
8
+ surfaceId: Int,
9
+ viewId: Int,
10
+ private val state: String,
11
+ ) : Event<AnimationStateChangeEvent>(surfaceId, viewId) {
12
+ override fun getEventName(): String = EVENT_NAME
13
+
14
+ override fun getEventData(): WritableMap = Arguments.createMap().apply {
15
+ putString("state", state)
16
+ }
17
+
18
+ companion object {
19
+ const val EVENT_NAME = "topAnimationStateChange"
20
+ }
21
+ }
22
+
23
+ internal class ContentLayoutEvent(
24
+ surfaceId: Int,
25
+ viewId: Int,
26
+ private val contentWidth: Float,
27
+ private val containerWidth: Float,
28
+ ) : Event<ContentLayoutEvent>(surfaceId, viewId) {
29
+ override fun getEventName(): String = EVENT_NAME
30
+
31
+ override fun getEventData(): WritableMap = Arguments.createMap().apply {
32
+ putDouble("contentWidth", contentWidth.toDouble())
33
+ putDouble("containerWidth", containerWidth.toDouble())
34
+ }
35
+
36
+ companion object {
37
+ const val EVENT_NAME = "topContentLayout"
38
+ }
39
+ }
40
+
41
+ internal interface MarqueeEventListener {
42
+ fun onAnimationStateChange(state: MotionState)
43
+ fun onContentLayout(contentWidth: Float, containerWidth: Float)
44
+ }
@@ -0,0 +1,45 @@
1
+ package com.marquee
2
+
3
+ internal enum class ShortContentMode {
4
+ STATIC,
5
+ REPEAT,
6
+ }
7
+
8
+ internal enum class ContentAlignment {
9
+ START,
10
+ CENTER,
11
+ END,
12
+ }
13
+
14
+ internal data class MarqueeProps(
15
+ var contentWidth: Float = 0f,
16
+ var spacing: Float = 0f,
17
+ var speed: Double = 25.0,
18
+ var direction: String = "left",
19
+ var active: Boolean = true,
20
+ var reduceMotion: Boolean = false,
21
+ var shortContentMode: ShortContentMode = ShortContentMode.STATIC,
22
+ var contentAlignment: ContentAlignment = ContentAlignment.START,
23
+ var maxFlingVelocity: Float = 3200f,
24
+ var deceleration: Float = 0.9985f,
25
+ var pauseOnPress: Boolean = true,
26
+ var resumeDelayMs: Double = 0.0,
27
+ )
28
+
29
+ internal enum class MotionState {
30
+ IDLE,
31
+ RUNNING,
32
+ PAUSED,
33
+ REDUCED_MOTION,
34
+ }
35
+
36
+ internal fun shortContentModeFrom(value: String?): ShortContentMode = when (value) {
37
+ "repeat" -> ShortContentMode.REPEAT
38
+ else -> ShortContentMode.STATIC
39
+ }
40
+
41
+ internal fun contentAlignmentFrom(value: String?): ContentAlignment = when (value) {
42
+ "center" -> ContentAlignment.CENTER
43
+ "end" -> ContentAlignment.END
44
+ else -> ContentAlignment.START
45
+ }
@@ -0,0 +1,163 @@
1
+ package com.marquee
2
+
3
+ import kotlin.math.abs
4
+ import kotlin.math.pow
5
+
6
+ internal class MarqueeMotion {
7
+ var phasePx: Double = 0.0
8
+ private set
9
+ var mode: Mode = Mode.PAUSED
10
+ private set
11
+ var crossedLoopBoundary: Boolean = false
12
+ private set
13
+
14
+ private var periodPx = 0.0
15
+ private var autoVelocityPxPerSecond = 0.0
16
+ private var flingVelocityPxPerSecond = 0.0
17
+ private var blendStartVelocityPxPerSecond = 0.0
18
+ private var blendElapsedSeconds = 0.0
19
+ private var lastFrameNanos = 0L
20
+ private var maxFlingVelocityPxPerSecond = 8_000.0
21
+ private var decelerationPerMillisecond = 0.9985
22
+
23
+ fun configure(periodPx: Double, speedPxPerSecond: Double, preservePhase: Boolean) {
24
+ val nextPeriodPx = periodPx.coerceAtLeast(0.0)
25
+ val timingChanged = this.periodPx != nextPeriodPx ||
26
+ autoVelocityPxPerSecond != speedPxPerSecond
27
+ this.periodPx = nextPeriodPx
28
+ autoVelocityPxPerSecond = speedPxPerSecond
29
+ phasePx = if (preservePhase) wrap(phasePx) else 0.0
30
+ if (!preservePhase || timingChanged) lastFrameNanos = 0L
31
+ }
32
+
33
+ fun startAuto() {
34
+ if (periodPx <= 0.0 || autoVelocityPxPerSecond == 0.0) {
35
+ mode = Mode.PAUSED
36
+ return
37
+ }
38
+ mode = Mode.AUTO
39
+ blendElapsedSeconds = 0.0
40
+ lastFrameNanos = 0L
41
+ }
42
+
43
+ fun configureInteraction(maxFlingVelocityPxPerSecond: Double, deceleration: Double) {
44
+ this.maxFlingVelocityPxPerSecond = maxFlingVelocityPxPerSecond.coerceAtLeast(0.0)
45
+ decelerationPerMillisecond = deceleration.takeIf { it > 0.0 && it < 1.0 } ?: DEFAULT_DECELERATION
46
+ }
47
+
48
+ fun pause() {
49
+ mode = Mode.PAUSED
50
+ blendElapsedSeconds = 0.0
51
+ lastFrameNanos = 0L
52
+ }
53
+
54
+ fun reduceMotion() {
55
+ mode = Mode.REDUCED_MOTION
56
+ phasePx = 0.0
57
+ lastFrameNanos = 0L
58
+ blendElapsedSeconds = 0.0
59
+ }
60
+
61
+ fun beginHold() {
62
+ if (mode == Mode.REDUCED_MOTION) return
63
+ mode = Mode.HOLD
64
+ lastFrameNanos = 0L
65
+ }
66
+
67
+ fun panBy(deltaXPx: Double) {
68
+ if (mode == Mode.REDUCED_MOTION || periodPx <= 0.0) return
69
+ mode = Mode.PAN
70
+ phasePx = wrap(phasePx - deltaXPx)
71
+ }
72
+
73
+ fun release(velocityXPxPerSecond: Double) {
74
+ if (mode == Mode.REDUCED_MOTION) return
75
+ flingVelocityPxPerSecond = -velocityXPxPerSecond.coerceIn(
76
+ -maxFlingVelocityPxPerSecond,
77
+ maxFlingVelocityPxPerSecond,
78
+ )
79
+ if (abs(flingVelocityPxPerSecond) >= MIN_FLING_VELOCITY) {
80
+ mode = Mode.FLING
81
+ } else {
82
+ beginAutoBlend(flingVelocityPxPerSecond)
83
+ }
84
+ lastFrameNanos = 0L
85
+ }
86
+
87
+ fun advance(frameTimeNanos: Long): Boolean {
88
+ crossedLoopBoundary = false
89
+ if (mode != Mode.AUTO && mode != Mode.FLING && mode != Mode.BLEND) {
90
+ lastFrameNanos = frameTimeNanos
91
+ return false
92
+ }
93
+ if (lastFrameNanos == 0L) {
94
+ lastFrameNanos = frameTimeNanos
95
+ return true
96
+ }
97
+
98
+ val deltaSeconds = ((frameTimeNanos - lastFrameNanos).coerceAtLeast(0L) / NANOS_PER_SECOND)
99
+ .coerceAtMost(MAX_FRAME_DELTA_SECONDS)
100
+ lastFrameNanos = frameTimeNanos
101
+ val velocity = when (mode) {
102
+ Mode.FLING -> flingVelocityPxPerSecond
103
+ Mode.BLEND -> {
104
+ blendElapsedSeconds = (blendElapsedSeconds + deltaSeconds).coerceAtMost(AUTO_BLEND_DURATION_SECONDS)
105
+ val progress = blendElapsedSeconds / AUTO_BLEND_DURATION_SECONDS
106
+ val eased = progress * progress * (3.0 - 2.0 * progress)
107
+ blendStartVelocityPxPerSecond +
108
+ (autoVelocityPxPerSecond - blendStartVelocityPxPerSecond) * eased
109
+ }
110
+ else -> autoVelocityPxPerSecond
111
+ }
112
+ val previous = phasePx
113
+ val unwrapped = previous + velocity * deltaSeconds
114
+ phasePx = wrap(unwrapped)
115
+ crossedLoopBoundary = periodPx > 0.0 && (unwrapped < 0.0 || unwrapped >= periodPx)
116
+
117
+ if (mode == Mode.FLING) {
118
+ flingVelocityPxPerSecond *= decelerationPerMillisecond.pow(deltaSeconds * MILLIS_PER_SECOND)
119
+ val transitionVelocity = maxOf(MIN_FLING_VELOCITY, abs(autoVelocityPxPerSecond) * 2.0)
120
+ if (abs(flingVelocityPxPerSecond) < transitionVelocity) {
121
+ beginAutoBlend(flingVelocityPxPerSecond)
122
+ }
123
+ }
124
+ if (mode == Mode.BLEND && blendElapsedSeconds >= AUTO_BLEND_DURATION_SECONDS) {
125
+ mode = Mode.AUTO
126
+ lastFrameNanos = 0L
127
+ }
128
+ return phasePx != previous || mode == Mode.AUTO || mode == Mode.FLING || mode == Mode.BLEND
129
+ }
130
+
131
+ fun isFrameDriven(): Boolean = mode == Mode.AUTO || mode == Mode.FLING || mode == Mode.BLEND
132
+
133
+ private fun beginAutoBlend(velocityPxPerSecond: Double) {
134
+ blendStartVelocityPxPerSecond = velocityPxPerSecond
135
+ blendElapsedSeconds = 0.0
136
+ mode = Mode.BLEND
137
+ }
138
+
139
+ private fun wrap(value: Double): Double {
140
+ if (periodPx <= 0.0) return 0.0
141
+ val remainder = value % periodPx
142
+ return if (remainder < 0.0) remainder + periodPx else remainder
143
+ }
144
+
145
+ internal enum class Mode {
146
+ AUTO,
147
+ HOLD,
148
+ PAN,
149
+ FLING,
150
+ BLEND,
151
+ PAUSED,
152
+ REDUCED_MOTION,
153
+ }
154
+
155
+ private companion object {
156
+ const val NANOS_PER_SECOND = 1_000_000_000.0
157
+ const val MAX_FRAME_DELTA_SECONDS = 0.05
158
+ const val MILLIS_PER_SECOND = 1_000.0
159
+ const val DEFAULT_DECELERATION = 0.9985
160
+ const val AUTO_BLEND_DURATION_SECONDS = 0.35
161
+ const val MIN_FLING_VELOCITY = 12.0
162
+ }
163
+ }
@@ -0,0 +1,16 @@
1
+ package com.marquee
2
+
3
+ import com.facebook.react.BaseReactPackage
4
+ import com.facebook.react.bridge.NativeModule
5
+ import com.facebook.react.bridge.ReactApplicationContext
6
+ import com.facebook.react.module.model.ReactModuleInfoProvider
7
+ import com.facebook.react.uimanager.ViewManager
8
+
9
+ class MarqueePackage : BaseReactPackage() {
10
+ override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> =
11
+ listOf(MarqueeViewManager())
12
+
13
+ override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? = null
14
+
15
+ override fun getReactModuleInfoProvider() = ReactModuleInfoProvider { emptyMap() }
16
+ }