Skip to content

Instantly share code, notes, and snippets.

@yay
Created August 8, 2018 13:48
Show Gist options
  • Save yay/86f23f1e09661c69b4ef6fe04813a874 to your computer and use it in GitHub Desktop.
Save yay/86f23f1e09661c69b4ef6fe04813a874 to your computer and use it in GitHub Desktop.
Renders a dashed crosshair on JavaFX canvas when mouse is dragged over it
fun GraphicsContext.group(op: GraphicsContext.() -> Unit) {
save()
op(this)
restore()
}
// ...
val ctx = canvas.graphicsContext2D
canvas.onMouseDragged = EventHandler { e ->
ctx.group {
setLineDashes(4.0, 4.0)
stroke = Color.RED
val offset = -.5 * lineWidth // for nice pixel grid alignment
beginPath()
moveTo(e.x + offset, 0.0)
lineTo(e.x + offset, ctx.canvas.height)
moveTo(0.0, e.y + offset)
lineTo(ctx.canvas.width, e.y + offset)
stroke()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment