2
$\begingroup$

I am trying to make a cookie cutter that will extract a small square from a much larger bitmap with a click of the mouse. The LocatorPane appears to be a useful way to move the cookie cutter around, but I'm not having much luck in drawing the small dynamic rectangle over the bitmap. I would like to change the appearance of the locator icon to a small rectangle, or just make the locator icon disappear. The LocatorPane option Appearance -> Graphics[Rectangle[]] didn't work. Beyond that there is the question of how to capture a point value with a mouse click. This is my first use of Dynamic. I suspect I am going about this all the wrong way and perhaps I should be doing something with mouse functions and cursor images. If so, I would appreciate it if someone would point me towards a pertinent page or two. I have not been able to find one.

This script draws the locator on a bitmap labeled "background":



Clear[background]
    
    
background = Image[Graphics[{LightGray, Rectangle[]}]];
DynamicModule[
    {pt = {180, 180}},
     LocatorPane[Dynamic[pt], background]
    ]

But when I add code for the dynamic rectangle, I find that it draws outside the background bitmap.


DynamicModule[
{pt = {180, 180}},
 
  {LocatorPane[Dynamic[pt], background],

   Graphics[

     {EdgeForm[{Thick, Red}],

      FaceForm[],
Rectangle[Dynamic[pt]]}

   ]}
 
 ]
$\endgroup$
  • $\begingroup$ Related: Szabolcs applied the crop rectangle to zoom into graphics objects here. $\endgroup$ – István Zachar Oct 3 '12 at 22:49
  • $\begingroup$ Also see image cropper here. $\endgroup$ – István Zachar Oct 3 '12 at 23:05
6
$\begingroup$

Combine the background and the Graphics with Show, so that you can supply both of them as the second argument to LocatorPane:

background = Image[Graphics[{LightGray, Rectangle[]}]];
DynamicModule[{pt1 = {180, 180}, pt2 = {20, 20}},
 LocatorPane[Dynamic[{pt1, pt2}],
  Show[
   background,
   Graphics[{EdgeForm[{Thick, Red}],
     FaceForm[], Rectangle[Dynamic[pt1], Dynamic[pt2]]}]]
  , Appearance -> Graphics[Rectangle[], ImageSize -> {10, 10}]]]

enter image description here

Update After further clarification of the question:

background = Image[Graphics[{LightGray, Rectangle[]}]];
DynamicModule[{pt1 = {180, 170}, pt2 = {190, 180}}, 
 LocatorPane[Dynamic[{pt1(*,pt2*)}], 
  Show[background, 
   Graphics[{EdgeForm[{Thick, Red}], FaceForm[], 
     Dynamic[Rectangle[pt1 + {5, 5}, pt1 - {5, 5}]]}]], Appearance -> None]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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