Fabric Text with Modal

by Anand Rathod

HTML

<script src="https://rawgithub.com/kangax/fabric.js/master/dist/fabric.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">
<button type="button" class="btn btn-info btn-lg" onclick="doModal();">Open Modal</button>


<div id="myModal" class="modal fade in">
  <div class="modal-dialog">
    <div class="modal-content">
      <!-- dialog body -->
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Custom Bootstrap Dialog</h4> </div>
      <div class="modal-body">
        <canvas id="c" width="300" height="300"></canvas>
      </div>
      <!-- dialog buttons -->
      <div class="modal-footer">
        <button type="button" class="btn btn-primary">OK</button>
      </div>
    </div>
  </div>
</div>

CSS

canvas {
  border: 1px solid #999;
}

JavaScript

/**
 * fabric.js template for bug reports
 *
 * Please update the name of the jsfiddle (see Fiddle Options).
 * This templates uses latest dev verison of fabric.js (https://rawgithub.com/kangax/fabric.js/master/dist/fabric.js).
 */

function doModal() {
  $('#myModal').modal('show');

  // initialize fabric canvas and assign to global windows object for debug
  var canvas = window._canvas = new fabric.Canvas('c');
  var text = new fabric.Textbox('Enter Text Here', {
    fontSize: 20,
    left: 10,
    top: 10,
    fontFamily: 'helvetica',
    angle: 0,
    fill: 'red',
    fontWeight: '',
    originX: 'left',
    width: 300,
    hasRotatingPoint: false,
    centerTransform: true
  });
  var rect = new fabric.Rect({
			left: 0 + numObjects ,
			top: 0 + numObjects ,
			fill: 'red',
			width: 30,
			height: 30
		});
		

  canvas.add(rect).setActiveObject(rect);
  canvas.add(text).setActiveObject(text);
  canvas.setActiveObject(rect);
  text.enterEditing();
  text.selectAll();
  // ADD YOUR CODE HERE
}