Tech Tip: Using 4D SVG to capture signatures
PRODUCT: 4D | VERSION: 11.6 | PLATFORM: Mac & Win
Published On: June 8, 2010
You can use techniques described in other Tech Tips such as Dragging SVG Objects: Version 2 to respond to user interaction in a picture variable and modify your SVG. The following code shows how to use the same ideas to create an area where you can capture user signatures.
First in the Form method include the following code:
Case of : (Form event=On Load ) C_PICTURE(picvar) C_TEXT(svgref;$rect) svgref:=SVG_New (500;500;"signature") $rect:=SVG_New_rect (svgref;10;10;480;480;0;0;"black";"white";3) picvar:=SVG_Export_to_picture (svgref;1) : (Form event=On Unload ) SVG_CLEAR (svgref) End case |
The above code creates a new SVG document whose reference is stored in svgref. It then draws a rectangle which is used to encapsulate the signature area. Do not forget to clear your SVG reference when the form is unloaded.
Create a picture variable named picvar which is 500 by 500 pixels. For the purposes of creating a signature area with SVG use the following settings:
- No contextual menu
- No resizing
- Not focusable
- Not enterable
- No scrollbars
- Picture format Truncated (non-centered)
- Transparent and Transparent Border
- Not draggable, droppable, and no automatic drop
- All form events off except On Clicked and On Mouse Move
In the picture variable picvar's object method insert the following code:
Case of : (Form event=On Clicked ) C_LONGINT(old_myx;old_myy;myx;myy) C_BOOLEAN(testflag) If ((MouseX>10) & (MouseX<480) & (MouseY>10) & (MouseY<480)) old_myx:=MouseX old_myy:=MouseY testflag:=True End if : (Form event=On Mouse Move ) C_LONGINT($x;$y;$button) GET MOUSE($x;$y;$button) If ($button=1) If (testflag=True) If ((MouseX>10) & (MouseX<490) & (MouseY>10) & (MouseY<490)) C_TEXT($line) myx:=MouseX myy:=MouseY $line:=SVG_New_line (svgref;old_myx;old_myy;myx;myy;"blue";3) picvar:=SVG_Export_to_picture (svgref;1) old_myx:=myx old_myy:=myy Else testflag:=False End if End if Else testflag:=False End if End case |
The above code uses the old_myx and old_myy variables to store the old mouse position and then when the left mouse button is held down and dragged draws a line from the old position to the new one. Using this strategy as long as the left mouse button is held down the signature can be captured. Limits are placed on the position of the mouse (within 10 and 490 pixels on the X and Y axes) so that the user cannot attempt to draw outside the signature rectangle.