Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to have the button follow the mouse while within the output text cell. The scaling does not exactly work as intended (fails to follow mouse correctly).

Dynamic[Overlay[{txtO, 
   Button[Style["Button", Blue, Italic, 15], Print[1]]}, All, 2, 
  Alignment -> 
   Rescale[MousePosition["CellContentsScaled", {0, 0}], {0, 1}, {-1, 
     1}]]]

Additionally, how would I make the button 1) Snap to 0,0 when the mouse is not over the output text cell anymore; and 2) disappear completely when not within the boundaries of the text cell.

The Rescale for {0,1} -> {-1,1} helped a little, but it seems I am not using CellContentsScaled correctly, as the documentation says it should range from 0 to 1 over the cell only (occasional negative numbers pop up, and values are present with the mouse outside the cell)

share|improve this question
    
I don't see much happening when I float my mouse in the neighborhood of the button. Perhaps the undefined txt0 has something to do with it? –  Sjoerd C. de Vries Aug 23 '12 at 19:18
    
Whoops sorry there, txt0 was a random paragraph of text –  Twoday Aug 25 '12 at 7:10
add comment

1 Answer

up vote 1 down vote accepted

The problem here seems to be that "CellContentsScaled" doesn't relate to the size of the cell's content. Instead it relates to the size of the cell content area, i.e., the space within the displayed cell that potentially could contain something, which may be bigger than the size of the actual content.

A brute-force work-around (there should be better ways to do this):

stuffSize = 
   Overlay[{"mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm", 
           Button[Style["Button", Blue, Italic, 15], Print[1]]}, All, 2, 
           Alignment -> {0, 0}
   ] // Rasterize // ImageDimensions;

 Dynamic[
    Overlay[{"mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm", 
              Button[Style["Button", Blue, Italic, 15], Print[1]]
            }, All, 2, 
            Alignment -> 
                (-1 + 2 MousePosition["CellContentsAbsolute", {0, 0}]/stuffSize), 
            Background -> LightBlue
   ]
 ]

Mathematica graphics

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.