5
$\begingroup$

This exchange about writing the infinity symbol using a calligraphic pen nib (https://mathematica.stackexchange.com/a/13376/43662) gave me an idea that is a bit steep for me to implement.

The idea is to create a large pane (a DynamicModule with a TouchPosition?) which on a machine with a touchscreen can be used to create a signature and export it to any graphics format for use in letters. The pane would track touching it (or clicking the mouse and moving it) and repeatedly so (i.e., raise the pen and write again). The result would be the signature but without the calligraphic pen effect.

Then, akin to the answer above, the user would assess the effect of changing the nib angle, width, and ink color. The user would save the shape for adjusting again in the future and export the calligraphic signature for importing into letters.

To Be Updated as We Work this Out: After updating according to N0va's first attempt and Michael E2's cautionary note I am trying to use MousePosition instead of TouchPosition until we get this code working but I am stuck. My goal is something akin to:

DynamicModule[{pt=Dynamic@MousePosition["Graphics"],tmp},(
tmp=If[pt!=None&&pt!=tmp[[-1]],AppendTo[tmp,pt],{0,0}];
Graphics[{...,Line@tmp])]

Accordingly, following None's example with LocatorPanel, I produced the following code, which does not produce a similar result:

DynamicModule[{pt = Dynamic@MousePosition["Graphics"], tmp = {}}, {
  Dynamic@Graphics[{
     Line@{{2.5, 2.5}, pt},
     , Line[AppendTo[tmp, pt]]}, Frame -> True, ImageSize -> Medium, 
    PlotRange -> {{0, 5}, {0, 5}}], pt, tmp}]

The last instance of tmp never appears different than {}. However, when I move the mouse fast inside the graph, a line of a few points corresponding to a tmp different than {} does appear:

enter image description here

Why does tmp get reset?

$\endgroup$
4
  • 3
    $\begingroup$ You really ought to ask a question, this is, after all, a Question and Answer sort of place. $\endgroup$ Jun 4, 2022 at 10:10
  • $\begingroup$ Probably need Dynamic[] in there somewhere. $\endgroup$
    – Michael E2
    Jun 4, 2022 at 13:37
  • $\begingroup$ @MichaelE2 Dynamic@gr does not do it. $\endgroup$
    – Nicholas G
    Jun 4, 2022 at 16:31
  • $\begingroup$ Is that how TouchPosition is supposed to work? The docs do not put Dynamic on the enclosing head. (I don't think gr can be the head of an enclosing object. Probably it should be Graphics.) See how N0va used Dynamic to update tmp. $\endgroup$
    – Michael E2
    Jun 4, 2022 at 17:09

1 Answer 1

5
$\begingroup$

I am not familiar with TouchPosition but I got something to work with LocatorPane

pts={};
inputField[]:=DynamicModule[{tmp={},pt={0,0}},
  Panel[
    LocatorPane[Dynamic@pt,Dynamic@ListLinePlot[If[pt=!={0,0},AppendTo[tmp,pt],{}],PlotRange->{{0,1},{0,1}},Axes->False]],
    Row[{Button["Save",pts=tmp],Button["Reset",tmp={};pt={0,0};]}]
  ]
]
inputField[]

which creates a canvas to draw on Input

which can be saved to the variable pts by pressing the Save button in the panel. The canvas gets reset (not the pts) when pressing the Reset button.

$\endgroup$
7
  • 1
    $\begingroup$ I'd probably use DynamicModule instead of Module. $\endgroup$
    – Michael E2
    Jun 4, 2022 at 17:09
  • $\begingroup$ I get a FrontEnd warning message "pt in Dynamic occurs where it is not going to be evaluated before going out of scope". No idea what that means. More importantly, this code only works for "writing" by using the mouse. Moving a finger moves the locator pane. Moving a finger on the mousepad produces excessive cursor movement. $\endgroup$
    – Nicholas G
    Jun 5, 2022 at 9:14
  • 2
    $\begingroup$ Let me put my comment more forcefully for others who might wish to use or adapt this code: "Module variables should never appear inside Dynamics or Manipulates internal to that Module." — John Fultz $\endgroup$
    – Michael E2
    Jun 5, 2022 at 19:39
  • 1
    $\begingroup$ I made the change to DynamicModule and it works fine for me: the change eliminated some syntax warnings related to Michael E2's comments and it still works as intended for me. $\endgroup$
    – N0va
    Jun 6, 2022 at 12:18
  • 1
    $\begingroup$ Thanks, looks good. If you want to see one problem, execute the original Module code for inputField[] and then execute Quit[]. The LocatorPane will stop working because the module variables are not reinitialized. With DynamicModule, they are, but the FE shows a pink box from an error message about ToBoxes, which lasts until the kernel finishes restarting and can evaluate ToBoxes[..] (examine the cell expression of the output cell to see where ToBoxes[] occurs). That seems unnecessary and irritating to me. $\endgroup$
    – Michael E2
    Jun 6, 2022 at 14:55

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.