JSFiddle - React, Tailwind, and code Playground

HTML

<input type="button" id="clear" value="clear" />
<canvas id="canvas" width="200" height="100" style="border:1px solid #000000;">

JavaScript

$(function () {
      var c = document.getElementById("canvas");
      var context = c.getContext("2d");
      var clickX = new Array();
      var clickY = new Array();
      var clickDrag = new Array();
      var paint;

      $('#canvas').mousedown(function (e) {
          var mouseX = e.pageX - this.offsetLeft;
          var mouseY = e.pageY - this.offsetTop;

          paint = true;
          addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);
          redraw();
      });


      $('#canvas').mousemove(function (e) {
          if (paint) {
              addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);
              redraw();
          }
      });

      $('#canvas').mouseup(function (e) {
          paint = false;
      });

      $('#canvas').mouseleave(function (e) {
          paint = false;
      });

      $('#clear').click(function (e) {
          e.stopPropagation();
          context.clearRect(0, 0, context.canvas.width, context.canvas.height); // Clears

      });

      function addClick(x, y, dragging) {
          clickX.push(x);
          clickY.push(y);
          clickDrag.push(dragging);
      }

      function redraw() {
          context.clearRect(0, 0, context.canvas.width, context.canvas.height); // Clears the canvas

          context.strokeStyle = "#000000";
          context.lineJoin = "round";
          context.lineWidth = 2;

          for (var i = 0; i < clickX.length; i++) {
              context.beginPath();
              if (clickDrag[i] && i) {
                  context.moveTo(clickX[i - 1], clickY[i - 1]);
              } else {
                  context.moveTo(clickX[i] - 1, clickY[i]);
              }
              context.lineTo(clickX[i], clickY[i]);
              context.closePath();
              context.stroke();
          }
      }

  });