# lightAnchor

> Reports this composable's position to [state], so that lights positioned with [LightPosition.Anchored] against the same state follow this composable.

## API Reference

### lightAnchor

> Source set: Common

```kotlin
public fun Modifier.lightAnchor(state: LightAnchorState): Modifier
```

Reports this composable's position to `state`, so that lights positioned with
[LightPosition.Anchored](https://composables.com/jetpack-compose/androidx.compose.ui/ui/classes/LightPosition.Anchored/api.md) against the same state follow this composable.

Create `state` with `remember` and pass the same instance to this modifier and to every
[LightPosition.Anchored](https://composables.com/jetpack-compose/androidx.compose.ui/ui/classes/LightPosition.Anchored/api.md) that should track it.

A given [LightAnchorState](https://composables.com/jetpack-compose/androidx.compose.ui/ui/classes/LightAnchorState/api.md) tracks one composable at a time. If this modifier is applied to more
than one composable simultaneously, the state reports whichever was placed most recently, which
is rarely what was intended. When the anchored composable leaves the composition the state is
cleared and the lights using it stop contributing.

#### Parameters

| | |
| --- | --- |
| state | the anchor to publish this composable's position to. |

## Code Examples

### LightAnchorSample

```kotlin
@Sampled
@Composable
fun LightAnchorSample() {
    // A single anchor, shared by the composable that defines the light's position and by the
    // light itself.
    val anchor = remember { LightAnchorState() }

    var moved by remember { mutableStateOf(false) }
    val lightX by animateDpAsState(if (moved) 200.dp else 24.dp, label = "lightX")

    // Since our light color is white, we set a darker background so that its visible.
    val surfaceColor = remember { Color.Black }
    val surfaceShape = remember { RoundedCornerShape(24.dp) }

    Box(Modifier.size(240.dp).clickable { moved = !moved }) {
        // The light using this anchor will now follow this composable
        Box(
            Modifier.offset { IntOffset(x = lightX.roundToPx(), y = 24.dp.roundToPx()) }
                .size(16.dp)
                .lightAnchor(anchor)
        )

        Box(
            Modifier.align(Alignment.Center)
                .size(160.dp)
                .background(surfaceColor)
                .illuminate(
                    lights =
                        listOf(
                            PointLight(
                                color = Color.White,
                                position = LightPosition.Anchored(anchor),
                                elevation = 48.dp,
                                falloffRadius = 320.dp,
                                intensity = 1f,
                            )
                        ),
                    surfaceMaterial =
                        SurfaceMaterial.diffuse(Color.White).specular(Color.White, shininess = 32f),
                    surfaceGeometrySpec =
                        SurfaceGeometrySpec.convex(
                            shape = surfaceShape,
                            thickness = 1.dp,
                            bevelWidth = 12.dp,
                        ),
                )
        )
    }
}
```
