Change color of drawing with jsignature

Viewed 633

I want to give the option to change the drawing color after clicking on a color using jsignature. I cannot figure out how to pass the color to the plugin. Behold how I try, please help know where I'm wrong.

    <table>
    <tr>
    <td style='background:green;width:40px;height:40px;' id="green" class='changeColor'></td>
    <td style='background:blue;width:40px;height:40px;' id="blue" class='changeColor'></td>
    <td style='background:red;width:40px;height:40px;' id="red" class='changeColor'></td>
    </tr>
    </table>

  <div id="signature" style="width:500;height:400"></div>

    $(document).ready(function() {
    //default color
    var pen ='green';

    $('.changeColor').on('click', function(event) {
    pen = $(this).attr('id');

    });

    var $sigdiv = $("#signature").jSignature({
    'UndoButton':true,
    'background-color': 'transparent',
    'decor-color': 'transparent',
    'color':pen,
    'width': 500,
    'height': 400
    });
1 Answers

You can use updateSetting option of jSignature plugin and inside this pass your settings name and new value i.e : $("#signature").jSignature('updateSetting', "color", pen);

Demo Code :

$(document).ready(function() { //default color
  var pen = 'green';
  var $sigdiv = $("#signature").jSignature({
    'UndoButton': true,
    'background-color': 'transparent',
    'decor-color': 'transparent',
    'color': pen,
    'width': 500,
    'height': 400
  });

  $('.changeColor').on('click', function(event) {
    pen = $(this).attr('id');
    //update settings 
    $("#signature").jSignature('updateSetting', "color", pen,true);
  });
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jSignature/2.1.3/jSignature.min.js" integrity="sha512-lZ7GJNAmaXw7L4bCR5ZgLFu12zSkuxHZGPJdIoAqP6lG+4eoSvwbmKvkyfauz8QyyzHGUGVHyoq/W+3gFHCLjA==" crossorigin="anonymous"></script>
<table>
  <tr>
    <td style='background:green;width:40px;height:40px;' id="green" class='changeColor'></td>
    <td style='background:blue;width:40px;height:40px;' id="blue" class='changeColor'></td>
    <td style='background:red;width:40px;height:40px;' id="red" class='changeColor'></td>
  </tr>
</table>

<div id="signature" style="width:500;height:400;border:1px solid black">
</div>

Related