Learn R Programming

rgl (version 0.84)

rgl.setMouseCallbacks: User callbacks on mouse events

Description

This function sets user callbacks on mouse events.

Usage

rgl.setMouseCallbacks(button, begin = NULL, update = NULL, end = NULL)

Arguments

button
Which button?
begin
Called when mouse down event occurs
update
Called when mouse moves
end
Called when mouse is released

Value

  • This function is called for the side effect of setting the mouse event handlers.

Details

This function sets an event handler on mouse events that occur within the current rgl window. The begin and update events should be functions taking two arguments; these will be the mouse coordinates when the event occurs. The end event handler takes no arguments. Alternatively, the handlers may be set to NULL, the default value, in which case no action will occur.

See Also

par3d to set built-in handlers

Examples

Run this code
## Not quite right --- this doesn't play well with rescaling

 pan3d <- function(button) {
   start <- list()
   
   begin <- function(x, y) {
       start$userMatrix <<- par3d("userMatrix")
       start$viewport <<- par3d("viewport")
       start$scale <<- par3d("scale")
       start$projection <<- rgl.projection()
       start$pos <<- rgl.window2user( x/start$viewport[3], 1 - y/start$viewport[4], 0.5, 
                                      projection=start$projection)
   }
   
   update <- function(x, y) {
        xlat <- (rgl.window2user( x/start$viewport[3], 1 - y/start$viewport[4], 0.5,
                                 projection = start$projection) - start$pos)*start$scale
        mouseMatrix <- translationMatrix(xlat[1], xlat[2], xlat[3])
        par3d(userMatrix = start$userMatrix %*% t(mouseMatrix) )
   }
   rgl.setMouseCallbacks(button, begin, update)
   cat("Callbacks set on button", button, "of rgl device",rgl.cur(),"")
 }
 pan3d(3)

Run the code above in your browser using DataLab