Skip to content

Instantly share code, notes, and snippets.

@quillaja
Last active February 19, 2018 07:51
Show Gist options
  • Save quillaja/5a8238fd7e41a897aa4ed7366c469799 to your computer and use it in GitHub Desktop.
Save quillaja/5a8238fd7e41a897aa4ed7366c469799 to your computer and use it in GitHub Desktop.
cameraOrigin := pixel.ZV.Add(win.Bounds().Center())
scale := 1.0
dragOrigin := pixel.V(0, 0)
second := time.Tick(time.Second)
viewMatrix := pixel.IM
frames := 0
for !win.Closed() {
if win.MouseScroll().Y != 0 {
factor := math.Pow(1.2, win.MouseScroll().Y)
zoomDeltaStart := viewMatrix.Unproject(win.MousePosition())
scale *= factor
cameraOrigin = zoomDeltaStart.Add(win.Bounds().Center().Sub(win.MousePosition().Scaled(1 / scale)))
}
if win.JustPressed(pixelgl.MouseButton1) {
dragOrigin = win.MousePosition().Scaled(1 / scale)
} else if win.Pressed(pixelgl.MouseButton1) {
newOrigin := win.MousePosition().Scaled(1 / scale)
cameraOrigin = cameraOrigin.Sub(newOrigin.Sub(dragOrigin))
dragOrigin = newOrigin
}
viewMatrix = pixel.IM.Moved(win.Bounds().Center().Sub(cameraOrigin)).Scaled(pixel.ZV, scale)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment