3

I am using HTML5 and fabric.js for uploading multiple images. I want to add custom text on this image canvas. Right now I am uploading multiple images into canvas one by one. After uploading images I want to add custom text on canvas.

var canvas = new fabric.Canvas('canvas');
document.getElementById('file').addEventListener("change",function (e) {
  var file = e.target.files[0];
  var reader = new FileReader();
  console.log("reader   " + reader);
  reader.onload = function (f) {
    var data = f.target.result;
    fabric.Image.fromURL(data, function (img) {
      var oImg = img.set({left: 70, top: 100, width: 250, height: 200, angle: 0}).scale(0.9);
      canvas.add(oImg).renderAll();
      var a = canvas.setActiveObject(oImg);
      var dataURL = canvas.toDataURL({format: 'png', quality: 0.8});
    });
  };
  reader.readAsDataURL(file);
});
document.querySelector('#txt').onclick = function (e) {
  e.preventDefault();
  canvas.deactivateAll().renderAll();
  document.querySelector('#preview').src = canvas.toDataURL();
};
canvas{
  border: 1px solid black;
}
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.min.js"></script>
<input type="file" id="file">
<canvas id="canvas" width="750" height="550"></canvas>
<a href='' id='txt' target="_blank">Click Me!!</a><br />
<img id="preview" />

https://jsfiddle.net/varunPes/vb1weL93/

I have seen some links:

Update

I need that the user can choose the text's color and the font-family.

After clicking on Click Me!! button, the custom text and image should come together in one image.

AndreaBogazzi
  • 14,323
  • 3
  • 38
  • 63
Varun Sharma
  • 4,632
  • 13
  • 49
  • 103

1 Answers1

7

You can use IText like this:

canvas.add(new fabric.IText('Tap and Type', {
  fontFamily: 'arial black',
  left: 100,
  top: 100,
}));

And the full code:

var canvas = new fabric.Canvas('canvas');

document.getElementById('file').addEventListener("change", function (e) {
  var file = e.target.files[0];
  var reader = new FileReader();
  console.log("reader   " + reader);
  reader.onload = function (f) {
    var data = f.target.result;
    fabric.Image.fromURL(data, function (img) {
      var oImg = img.set({left: 70, top: 100, width: 250, height: 200, angle: 0}).scale(0.9);
      canvas.add(oImg).renderAll();
      canvas.setActiveObject(oImg);  
    });
  };
  reader.readAsDataURL(file);
});

$('#fill').change(function(){
  var obj = canvas.getActiveObject();

  if(obj){
    // old api
    // obj.setFill($(this).val());
    obj.set("fill", this.value);
  }
  canvas.renderAll();
});

$('#font').change(function(){
  var obj = canvas.getActiveObject();
  
  if(obj){
    // old api
    // obj.setFontFamily($(this).val());
    obj.set("fontFamily", this.value);
  }
  
  canvas.renderAll();
});

function addText() {
  var oText = new fabric.IText('Tap and Type', { 
    left: 100, 
    top: 100 ,
  });

  canvas.add(oText);
  oText.bringToFront();
  canvas.setActiveObject(oText);
  $('#fill, #font').trigger('change');
}

document.querySelector('#txt').onclick = function (e) {
  e.preventDefault();
  canvas.deactivateAll().renderAll();
  document.querySelector('#preview').src = canvas.toDataURL();
};
canvas{
  border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.3.6/fabric.min.js"></script>

<input type="file" id="file">
<input type="color" value="blue" id="fill" />
<select id="font">
  <option>arial</option>
  <option>tahoma</option>
  <option>times new roman</option>
</select>
<button onclick="addText()">Add Custom Text</button>
<br />
<canvas id="canvas" width="750" height="550"></canvas>
<a href='' id='txt' target="_blank">Click Me!!</a>
<br />
<img id="preview" />

Update: To keep the text always in the front you need to use .bringToFront() function.
(Thanks to @Amit Sharma in Fabric.js - problem with drawing multiple images zindex)

http://jsbin.com/qicesa/edit?html,css,js,output

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
  • I want to only one time custom text. In your code have multiple custom tag for multiple image. And i want text color, font style. – Varun Sharma Jan 12 '16 at 09:53
  • `one time custom text` Do you mean that the first time the user upload an image? `i want text color, font style` Do you mean that you want to control the color and the font? If so, see the snippet: `fontFamily: 'arial black',`. Also, read the doc about it (Click on `IText`) – Mosh Feu Jan 12 '16 at 09:58
  • You have anyother idea? – Varun Sharma Jan 12 '16 at 10:12
  • Because i want only text color and font-style choose by user. – Varun Sharma Jan 12 '16 at 10:16
  • your answer is correct. But when i upload next image then custom text will hide.Custom text should be above of all images – Varun Sharma Jan 12 '16 at 10:40
  • Yes it is correct. Can you add one more functionality. When click on button then show custom text area. button should be out side from canvas. – Varun Sharma Jan 12 '16 at 10:53
  • If you don't mind. Could you see this link. https://stackoverflow.com/questions/47568013/get-more-than-1000-row-in-the-data-table-in-angular-js-from-mongodb – Varun Sharma Jan 19 '18 at 13:12
  • @Michel, I just updated my answer. The API is changed so I'm using the new API now. Don't worry, be happy :) – Mosh Feu Aug 22 '18 at 07:53