幾何圖形 API

Geometry API 可讓您建立互動式工具,例如橡皮擦和選取機制。

如要說明 Geometry API 的實際應用方式,請參閱以下資源: 橡皮擦實作範例

整筆線條橡皮擦

fun eraseWholeStrokes(
    eraserBox: ImmutableBox,
    finishedStrokesState: MutableState<Set<Stroke>>,
) {
    val threshold = 0.1f

    val strokesToErase = finishedStrokesState.value.filter { stroke ->
        stroke.shape.computeCoverageIsGreaterThan(
            box = eraserBox,
            coverageThreshold = threshold,
        )
    }
    if (strokesToErase.isNotEmpty()) {
        Snapshot.withMutableSnapshot {
            finishedStrokesState.value -= strokesToErase
        }
    }
}

如果是 Compose 實作,請務必觸發重組程序, 就能有效移除筆觸舉例來說,其中一種做法是 可組合項中的 rememberCoroutineScope,並將協同程式範圍傳遞至您的 觸控事件監聽器,可讓您修改範圍內的 finishedStrokesState 這都要歸功於 Compose