Capture signature in SFDC Using Canvas

Capture the e-signature in SFDC using Canvas.


Code:
VF Page:

<script>var $j = jQuery.noConflict();</script>
<apex:stylesheet value="{!URLFOR($Resource.jquerymobile,'/jquerymobile/jquery.mobile-1.3.2.min.css')}"/>
<apex:includeScript value="{!URLFOR($Resource.Jquery)}"  />
<apex:includeScript value="{!URLFOR($Resource.jquerymobile,'/jquerymobile/jquery.mobile-1.3.2.min.js')}"/>

 <!-- <input id="saveSigButton" type="button" name="SigCap" onclick="saveSignature();"   value="Capture Signature"></input> -->
  <apex:form >
  <apex:actionStatus id="status" >
    <apex:facet name="start">
        <apex:outputPanel >
            Saving....
        </apex:outputPanel>
    </apex:facet>
    <apex:facet name="stop">
    <apex:outputPanel >
    <div align="center" >
    <apex:outputText value="Please Sign-In Below:"></apex:outputText><br></br>
<canvas id="signatureCanvas" height="300PX" width="600PX" style="border:1px solid #000000;text-align:center;"/><br></br>
        <apex:commandButton value="Save Signature"  onClick="this.disabled='disabled'; saveSignature();return false;" status="status"/>
        <apex:commandButton value="clear"  onclick="clearSignature();"  immediate="true"/>
        </div>
    </apex:outputPanel>
    </apex:facet>
 </apex:actionStatus>
 <!--  <apex:actionStatus id="status" >
  <apex:commandButton value="Save Signature" id="Save" onClick="saveSignature(); return false;"  status="status"/>
   <input id="clear" type="button" name="SigCap" onclick="clearSignature();"   value="Clear"></input>
   </apex:actionStatus> -->
  </apex:form>
 <script>
 var canvas;
    var context;
    var drawingUtil;
    var isDrawing = false;
    var accountId = '';

function DrawingUtil()
{
    isDrawing = false;
    canvas.addEventListener("touchstart",start,false);
    canvas.addEventListener("touchmove",draw,false);
    canvas.addEventListener("touchend",stop,false);
    context.strokeStyle = "#FFF"; 
}

//Start Event for Signature Captuare on HTML5 Canvas
function start(event)
{
    isDrawing = true;
    canvas = document.getElementById("signatureCanvas");
    context = canvas.getContext("2d");   
    context.strokeStyle = "rgba(155,0,0,0.5)";     
    context.beginPath();
     context.moveTo(event.touches[0].pageX - canvas.getBoundingClientRect().left,event.touches[0].pageY - canvas.getBoundingClientRect().top);
}

//Event while someone is drawing to caputre the path while they draw....
function draw(event) {
    event.preventDefault();
    if(isDrawing) {    
        context.lineTo(event.touches[0].pageX - canvas.getBoundingClientRect().left,event.touches[0].pageY - canvas.getBoundingClientRect().top);
        context.stroke();
    }
}


//Event when someone stops drawing their signature line
function stop(event) {
    if(isDrawing) {
        context.stroke();
        context.closePath();
        isDrawing = false;
    }
}

canvas = document.getElementById("signatureCanvas");
context = canvas.getContext("2d");
drawingUtil = new DrawingUtil(canvas);

function saveSignature()
{
    var strDataURI = canvas.toDataURL();
    var tempValue = '{!$CurrentPage.parameters.parentId}';
    strDataURI = strDataURI.replace(/^data:image\/(png|jpg);base64,/, "");
    VipTourListComponentController.saveSignature(strDataURI,tempValue,processResult);
}
function clearSignature()
{
    context.clearRect(0, 0, canvas.width, canvas.height);
}
function processResult(result)
{
alert('Your Signature has been captured!! Thank You!!');
context.clearRect(0, 0, canvas.width, canvas.height);
}
</script>
Controller:
 @RemoteAction
    global static String saveSignature(String signatureBody, String parentId)
    {
        try
        {
            //parentId = System.currentPageReference().getParameters().get('Id');
            system.debug('Record Id == ' + parentId);
            system.debug('Record IdTourId == ' + TourId);
            system.debug(signatureBody);
            Attachment a = new Attachment();
            a.ParentId = parentId;<
            a.Body = EncodingUtil.base64Decode(signatureBody);
            a.ContentType = 'image/png';
            a.Name = 'LeadGuest Signature.png';
            insert a;
            return '{success:true, attachId:' + a.Id + '}';
        }catch(Exception e)
        {
            return JSON.serialize(e);
        }
    return null;
    }

P.S: Comment Below for any Clarification or help!!
Happy working!! :) :) 

Comments

Popular posts from this blog

Embed Special Characters in VF Page

Error: Compile Error: Illegal conversion from String to System.PageReference

Some Common Errors in the Visual flow