1
$\begingroup$

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)

$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$ Commented Aug 23, 2012 at 19:18
  • $\begingroup$ Whoops sorry there, txt0 was a random paragraph of text $\endgroup$
    – Twoday
    Commented Aug 25, 2012 at 7:10

1 Answer 1

1
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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