1
$\begingroup$

I am trying to write a nice little program to help me and colleagues draw outlines of objects in images

I came up with the following code which allowed me to click and hold down the primary mouse button to draw a shape:


imgs = {ConstantImage[0, {500, 500}], ConstantImage[0, {500, 500}]};

DynamicModule[{outlines = {{{}}, {{}}}, i = {1, 1}, n = 1, tmp, 
  labelimgs = imgs},
 EventHandler[
  Dynamic@
   Show[labelimgs[[n]], 
    Graphics[{Magenta, Opacity[.5], Polygon /@ outlines[[n]], White, 
      Line@outlines[[n, i[[n]]]]}], ImageSize -> Large],
  {
   {"MouseDragged", 
     1} :> (AppendTo[outlines[[n, i[[n]]]], 
       MousePosition["Graphics"]];),
   {"MouseUp", 1} :>
    If[Length@outlines[[n, i[[n]]]] > 1,
     AppendTo[outlines[[n, i[[n]]]], outlines[[n, i[[n]], 1]]];
     AppendTo[outlines[[n]], {}];
     i[[n]]++],
   "RightArrowKeyDown" :> If[n < Length@labelimgs, (n++;)],
   "LeftArrowKeyDown" :> If[n > 1, (n--;)]
   }]]

This works good enough in a notebook. Next I tried to evaluate the cell in a public cloud notebook (colleagues do not have Mathematica installed) which was unusable because every Dynamic step seems to be sent to the cloud and back.

Am I doing something wrong or is the cloud not intended to be used like this?

$\endgroup$
1
  • $\begingroup$ The cloud has some fundamental design challenges because Mathematica can’t be run directly in the browser. That means there are many cases where things have to be sent back to a kernel and this is one of them. A less elegant, but likely more workable fix is to do this with a number of Locators where you can Ctrl-click to make a new locator and you define the shape as the shortest tour through them. This won’t allow for perfect outlines, but can be made to be as good an approximation as you have the patience for. $\endgroup$
    – b3m2a1
    Mar 29, 2021 at 16:49

0

Your Answer

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

Browse other questions tagged or ask your own question.