Skip to content

Instantly share code, notes, and snippets.

@JD557
Last active November 3, 2024 19:39
Show Gist options
  • Save JD557/4855c0ea10a7b777d32a1dc46db4d4db to your computer and use it in GitHub Desktop.
Save JD557/4855c0ea10a7b777d32a1dc46db4d4db to your computer and use it in GitHub Desktop.
Scalakun
package eu.joaocosta.scalakun
import eu.joaocosta.minart.backend.defaults.given
import eu.joaocosta.minart.graphics._
import eu.joaocosta.minart.geometry._
import eu.joaocosta.minart.runtime._
object Main {
val canvasSettings = Canvas.Settings(width = 512, height = 512, scale = Some(1))
val red = Color(255, 85, 85)
val white = Color(250, 250, 250)
val black = Color(51, 51, 51)
val rectangle = Shape.rectangle(Point(-110, -40), Point(110, 40))
val spiral = List(
rectangle.translate(110, 40),
rectangle.rotate(-0.08726646).translate(110, 128),
rectangle.translate(110, 224),
)
def drawSpiral(surface: MutableSurface, x: Int, y: Int, scale: Double, rotation: Double): Unit =
spiral.foreach(shape =>
surface.rasterizeShape(shape.rotate(rotation).scale(1, scale), Some(red))(x, y)
)
def drawEye(surface: MutableSurface, x: Int, y: Int): Unit =
surface.rasterizeShape(Shape.circle(Point(x, y), 40), Some(white))(0, 0)
surface.rasterizeShape(Shape.circle(Point(x, y), 30), Some(black))(0, 0)
// Scala-kun https://github.com/windymelt/scala-kun
// Scala-kun © 2024 by Windymelt is licensed under CC BY-SA 4.0
def drawScalaKun(surface: MutableSurface, x: Int, y: Int, scale: Double, rotation: Double): Unit =
drawSpiral(surface, x + 0, y + 34, scale, rotation)
drawEye(surface, x + 64, (y + 40 * scale).toInt)
drawEye(surface, x + 160, (y + 70 * scale).toInt)
val background = Plane.fromFunction((x, y) => Color(0, (255 * y) / canvasSettings.height, 255))
val blurKernel = Kernel.averageBlur(5, 5)
def main(args: Array[String]): Unit = {
AppLoop
.statelessRenderLoop((canvas: Canvas) => {
val scaleFactor = 0.9 + 0.1 * Math.sin(System.currentTimeMillis / 200.0)
val rotation = Math.sin(System.currentTimeMillis / 400.0) * 0.05
canvas.blitPlane(
canvas.view
.zipWith(background, (foreground, background) => background * Color.grayscale(64) + foreground * Color.grayscale(191))
.precompute
.clamped
.coflatMap(blurKernel)
)(0, 0)
drawScalaKun(canvas, (canvas.width - 220) / 2, (canvas.height - 284) / 2, scaleFactor, rotation)
canvas.redraw()
})
.configure(canvasSettings, LoopFrequency.hz60)
.run()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment