Created
August 8, 2018 13:48
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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