简体   繁体   English

在画布HTML5和Fabric js中的图像上添加删除图标

[英]Add remove icon on image in canvas HTML5 and Fabric js

I am using HTML5 and Fabric.js. 我正在使用HTML5和Fabric.js。 I am uploading multiple images. 我上传了多张图片。 But i want user can remove uploaded image. 但我希望用户可以删除上传的图像。 I will show you one screen shot. 我会告诉你一个屏幕截图。 在此输入图像描述

I want to add one remove icon on image when user clicked on image. 我想在用户点击图片时在图片上添加一个删除图标。

HTML code: HTML代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.min.js"></script>

<input type="file" id="file">
<input type="color" value="blue" id="fill" />
<select id="font">

Java Sript Code: Java Sript代码:

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);
      // add an event listener
      oImg.on('mousedown', function(){
        // bring the image to front
        oImg.bringToFront();
        });
    });
  };
  reader.readAsDataURL(file);
});

$('#fill').change(function(){
  var obj = canvas.getActiveObject();
  if(obj){
    obj.setFill($(this).val());
  }
  canvas.renderAll();
});

$('#font').change(function(){
  var obj = canvas.getActiveObject();
  if(obj){
    obj.setFontFamily($(this).val());
  }
  canvas.renderAll();
});

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

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

document.querySelector('#txt').onclick = function (e) {
  e.preventDefault();
  canvas.deactivateAll().renderAll();
  document.querySelector('#preview').src = canvas.toDataURL();
};

Css Code: Css代码:

canvas{
  border: 1px solid black;
}

You can see this link. 你可以看到这个链接。 Write custom text on image canvas with fabric.js and HTML5 使用fabric.js和HTML5在图像画布上编写自定义文本

After click on cross icon image should be remove. 点击十字图标后应删除图片。 Can we add remove icon. 我们可以添加删除图标。 If yes then give me idea about this. 如果是,请告诉我这个想法。

You'll need to draw icon image using selected object's coordinates and it's width and height, rather than having it attached to objects transformation point. 您需要使用所选对象的坐标及其宽度和高度绘制图标图像,而不是将其附加到对象转换点。 If you do su, you'll lost resizing functionality for that transformation point. 如果你做su,你将失去该转换点的大小调整功能。 Also, after object's rotation it will be hard to tell which transformation point is closest to upper right corner. 此外,在对象旋转之后,很难分辨哪个变形点最接近右上角。 I would preffer to have some exterlal toolbar button, that will do this, or have context menu with it. 我会考虑使用一些exterlal工具栏按钮,它会执行此操作,或者使用上下文菜单。

add a button, or some other element 添加按钮或其他元素

<button id="remove" style="display:none">remove</button>

and add this to your code 并将其添加到您的代码中

$('#remove').on('click', function(){
  canvas.getActiveObject().remove();
});

canvas.on('object:selected', function(o) {
  if (o.target.isType('image')) {
    $('#remove').show();
  }
});

canvas.on('selection:cleared', function() {
    $('#remove').hide();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM