# CanvasStrokeRenderer

Artifact: [androidx.ink:ink-rendering](https://developer.android.com/jetpack/androidx/releases/ink) [View Source](https://cs.android.com/search?q=file:androidx/ink/rendering/android/canvas/CanvasStrokeRenderer.android.kt+class:androidx.ink.rendering.android.canvas.CanvasStrokeRenderer) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/ink#1.0.0)

*** ** * ** ***


```
public interface CanvasStrokeRenderer
```

<br />

*** ** * ** ***

Renders strokes to a `https://developer.android.com/reference/android/graphics/Canvas.html`.

Instead of calling the `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,androidx.ink.geometry.AffineTransform)` methods here directly, it may be simpler to pass an instance of `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer` to `https://developer.android.com/reference/androidx/ink/rendering/android/view/ViewStrokeRenderer` and use it to calculate transform matrix values.

An example of how to use `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,androidx.ink.geometry.AffineTransform)` directly:

```
class MyView {
  // Update these according to app business logic, and call `MyView.invalidate()`
  val worldToViewTransform = Matrix() // Call e.g. `setScale(2F)` to zoom in 2x
  val strokesWithTransforms = mutableMapOf<Stroke, Matrix>()

  private val strokeToViewTransform = Matrix() // reusable scratch object
  private val renderer = CanvasStrokeRenderer.create()

  fun onDraw(canvas: Canvas) {
    for ((stroke, strokeToWorldTransform) in strokesWithTransforms) {
      // Combine worldToViewTransform (drawing surface being panned/zoomed/rotated) with
      // strokeToWorldTransform (stroke itself being moved/scaled/rotated within the drawing
      // surface) to get the overall transform of this stroke.
      strokeToViewTransform.set(strokeToWorldTransform)
      strokeToViewTransform.postConcat(worldToViewTransform)

      canvas.withMatrix(strokeToViewTransform) {
        // If coordinates of MyView are scaled/rotated from screen coordinates, then those
        // scale/rotation values should be multiplied into the strokeToScreenTransform
        // argument to renderer.draw.
        renderer.draw(canvas, stroke, strokeToViewTransform)
      }
    }
  }
}
```

In almost all cases, a developer should use an implementation of this interface obtained from `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#create(androidx.ink.brush.TextureBitmapStore)`.

However, some developers may find it helpful to use their own implementation of this interface, possibly to draw other effects to the `https://developer.android.com/reference/android/graphics/Canvas.html`, typically delegating to a renderer from `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#create(androidx.ink.brush.TextureBitmapStore)` for part of the custom rendering behavior to have the additional effects add to or modify the standard stroke rendering behavior. Custom `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer` implementations are generally less efficient than effects that can be achieved with a custom `https://developer.android.com/reference/androidx/ink/brush/BrushFamily`. If a custom implementation draws to different screen locations than the standard implementation, for example surrounding a stroke with additional content, then that additional content will not be taken into account in geometry operations like `https://developer.android.com/reference/androidx/ink/geometry/Intersection` or `https://developer.android.com/reference/androidx/ink/geometry/PartitionedMesh#computeCoverage(androidx.ink.geometry.Triangle,androidx.ink.geometry.AffineTransform)`.

## Summary

| ### Public methods |
|---|---|
| `default static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer` | `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#create(androidx.ink.brush.TextureBitmapStore)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/ink/brush/TextureBitmapStore textureStore)` Create a `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer` that is appropriate to the device's API version. |
| `default void` | `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.InProgressStroke,androidx.ink.geometry.AffineTransform)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/Canvas.html canvas, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/ink/strokes/InProgressStroke inProgressStroke, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/ink/geometry/AffineTransform strokeToScreenTransform )` Render a single `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.InProgressStroke,androidx.ink.geometry.AffineTransform)` on the provided `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.InProgressStroke,androidx.ink.geometry.AffineTransform)`. |
| `default void` | `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.InProgressStroke,android.graphics.Matrix)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/Canvas.html canvas, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/ink/strokes/InProgressStroke inProgressStroke, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/Matrix.html strokeToScreenTransform )` Render a single `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.InProgressStroke,android.graphics.Matrix)` on the provided `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.InProgressStroke,android.graphics.Matrix)`. |
| `default void` | `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,androidx.ink.geometry.AffineTransform)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/Canvas.html canvas, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/ink/strokes/Stroke stroke, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/ink/geometry/AffineTransform strokeToScreenTransform )` Render a single `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,androidx.ink.geometry.AffineTransform)` on the provided `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,androidx.ink.geometry.AffineTransform)`. |
| `default void` | `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,android.graphics.Matrix)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/Canvas.html canvas, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/ink/strokes/Stroke stroke, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/Matrix.html strokeToScreenTransform )` Render a single `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,android.graphics.Matrix)` on the provided `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,android.graphics.Matrix)`. |

## Public methods

### create

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/ink#1.0.0)

```
default static final @NonNull CanvasStrokeRenderer create(@NonNull TextureBitmapStore textureStore)
```

Create a `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer` that is appropriate to the device's API version.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/ink/brush/TextureBitmapStore textureStore` | The `https://developer.android.com/reference/androidx/ink/brush/TextureBitmapStore` that will be called to retrieve image data for drawing textured strokes. |

### draw

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/ink#1.0.0)

```
default void draw(
    @NonNull Canvas canvas,
    @NonNull InProgressStroke inProgressStroke,
    @NonNull AffineTransform strokeToScreenTransform
)
```

Render a single `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.InProgressStroke,androidx.ink.geometry.AffineTransform)` on the provided `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.InProgressStroke,androidx.ink.geometry.AffineTransform)`.

The `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.InProgressStroke,androidx.ink.geometry.AffineTransform)` should represent the complete transformation from stroke coordinates to the canvas, modulo translation. This transform will not be applied to the `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.InProgressStroke,androidx.ink.geometry.AffineTransform)` in any way, as it may be made up of several individual transforms applied to the `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.InProgressStroke,androidx.ink.geometry.AffineTransform)` during an app's drawing logic. If this transform is inaccurate, strokes may appear blurry or aliased.

### draw

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/ink#1.0.0)

```
default void draw(
    @NonNull Canvas canvas,
    @NonNull InProgressStroke inProgressStroke,
    @NonNull Matrix strokeToScreenTransform
)
```

Render a single `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.InProgressStroke,android.graphics.Matrix)` on the provided `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.InProgressStroke,android.graphics.Matrix)`.

The `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.InProgressStroke,android.graphics.Matrix)` must be affine. It should represent the complete transformation from stroke coordinates to the canvas, modulo translation. This transform will not be applied to the `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.InProgressStroke,android.graphics.Matrix)` in any way, as it may be made up of several individual transforms applied to the `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.InProgressStroke,android.graphics.Matrix)` during an app's drawing logic. If this transform is inaccurate, strokes may appear blurry or aliased.

### draw

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/ink#1.0.0)

```
default void draw(
    @NonNull Canvas canvas,
    @NonNull Stroke stroke,
    @NonNull AffineTransform strokeToScreenTransform
)
```

Render a single `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,androidx.ink.geometry.AffineTransform)` on the provided `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,androidx.ink.geometry.AffineTransform)`.

To avoid needing to calculate and maintain `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,androidx.ink.geometry.AffineTransform)`, consider using `https://developer.android.com/reference/androidx/ink/rendering/android/view/ViewStrokeRenderer` instead.

The `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,androidx.ink.geometry.AffineTransform)` should represent the complete transformation from stroke coordinates to the screen, modulo translation. This transform will not be applied to the `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,androidx.ink.geometry.AffineTransform)` in any way, as it may be made up of several individual transforms applied to the `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,androidx.ink.geometry.AffineTransform)` during an app's drawing logic. If this transform is inaccurate, strokes may appear blurry or aliased.

### draw

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/ink#1.0.0)

```
default void draw(
    @NonNull Canvas canvas,
    @NonNull Stroke stroke,
    @NonNull Matrix strokeToScreenTransform
)
```

Render a single `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,android.graphics.Matrix)` on the provided `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,android.graphics.Matrix)`.

To avoid needing to calculate and maintain `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,android.graphics.Matrix)`, consider using `https://developer.android.com/reference/androidx/ink/rendering/android/view/ViewStrokeRenderer` instead.

The `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,android.graphics.Matrix)` must be affine. It should represent the complete transformation from stroke coordinates to the canvas, modulo translation. This transform will not be applied to the `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,android.graphics.Matrix)` in any way, as it may be made up of several individual transforms applied to the `https://developer.android.com/reference/androidx/ink/rendering/android/canvas/CanvasStrokeRenderer#draw(android.graphics.Canvas,androidx.ink.strokes.Stroke,android.graphics.Matrix)` during an app's drawing logic. If this transform is inaccurate, strokes may appear blurry or aliased.