Skip to content

Commit

Permalink
Update the surface view example to use a kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
JD557 committed Nov 1, 2024
1 parent d29cbe5 commit 2adcaca
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions examples/snapshot/09-surface-views.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,12 @@ Let's see an example with multiple effects, such as:
- Wobble
- Checkerboard invert

Before we start, let's precompute the convolution window that we will use for the blur
Before we start, let's precompute the convolution kernel that we will use for the blur.
We could use a simple function, but Minart already provides some optimized kernels
out of the box.

```scala
val convolutionWindow = for {
x <- (-1 to 1)
y <- (-1 to 1)
} yield (x, y)
val blurKernel = Kernel.averageBlur(3, 3)

```

Expand All @@ -79,14 +78,8 @@ def application(t: Double, canvas: Canvas): Unit = {

val image = updatedBitmap.view.repeating // Create an infinite Plane from our surface
.scale(zoom, zoom) // Scale
.coflatMap { img => // Average blur
convolutionWindow.iterator
.map { case (x, y) => img(x, y) }
.foldLeft(Color(0, 0, 0)) { case (Color(r, g, b), Color(rr, gg, bb)) =>
Color(r + rr / 9, g + gg / 9, b + bb / 9)
}
}
.rotate(t) // Rotate
.coflatMap(blurKernel) // Average blur
.rotate(t) // Rotate
.contramap((x, y) => (x + (5 * Math.sin(t + y / 10.0)).toInt, y)) // Wobbly effect
.flatMap(color =>
(x, y) => // Checkerboard effect
Expand Down

0 comments on commit 2adcaca

Please sign in to comment.