【问题标题】:How to delete image(element) from canvas如何从画布中删除图像(元素)
【发布时间】:2015-02-14 14:56:54
【问题描述】:

我正在借助动力学 js 使用画布开发酒店楼层视图。我想提供一个选项来删除画布上当前拖放的图像。

这是我的代码,仅用于拖放图像而不是删除选项:

// get a reference to the house icon in the toolbar
// hide the icon until its image has loaded
var $house=$("#house");
$house.hide();

// get the offset position of the kinetic container
var $stageContainer=$("#container");
var stageOffset=$stageContainer.offset();
var offsetX=stageOffset.left;
var offsetY=stageOffset.top;

// create the Kinetic.Stage and layer
var stage = new Kinetic.Stage({
  container: 'container',
  width: 350,
  height: 350
});
var layer = new Kinetic.Layer();
stage.add(layer);

// start loading the image used in the draggable toolbar element
// this image will be used in a new Kinetic.Image
var image1=new Image();
image1.onload=function(){
  $house.show();
}
image1.src="https://dl.dropboxusercontent.com/u/139992952/multple/4top.png";

// make the toolbar image draggable
$house.draggable({
  helper:'clone',
});

// set the data payload
$house.data("url","house.png"); // key-value pair
$house.data("width","32"); // key-value pair
$house.data("height","33"); // key-value pair
$house.data("image",image1); // key-value pair

// make the Kinetic Container a dropzone
$stageContainer.droppable({
  drop:dragDrop,
});

// handle a drop into the Kinetic container
function dragDrop(e,ui){

  // get the drop point
  var x=parseInt(ui.offset.left-offsetX);
  var y=parseInt(ui.offset.top-offsetY);

  // get the drop payload (here the payload is the image)
  var element=ui.draggable;
  var data=element.data("url");
  var theImage=element.data("image");

  // create a new Kinetic.Image at the drop point
  // be sure to adjust for any border width (here border==1)
  var image = new Kinetic.Image({
    name:data,
    x:x,
    y:y,
    image:theImage,
    draggable: true
  });
  layer.add(image);
  layer.draw();
}
body{padding:20px;}
#container{
  border:solid 1px #ccc;
  margin-top: 10px;
  width:350px;
  height:350px;
}
#toolbar{
  width:350px;
  height:35px;
  border:solid 1px blue;
}
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.7.2.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>

<h4>Drag from toolbar onto canvas. Then drag around canvas.</h4>
<div id="toolbar">
  <img id="house" width=32 height=32 src="https://dl.dropboxusercontent.com/u/139992952/multple/4top.png"><br>
</div>
<div id="container"></div>

【问题讨论】:

    标签: javascript jquery html canvas html5-canvas


    【解决方案1】:

    您可以使用 Kinetic 到 image.remove(),然后 layer.draw() 刷新画布,与 this answer 类似。

    【讨论】:

      【解决方案2】:

      如果您想暂时删除图像(然后重新添加),请使用:

      // temporarily remove the image from the layer
      yourImage.remove();
      yourLayer.draw();
      
      // and later re-add it
      yourLayer.add(yourImage);
      yourLayer.draw();
      

      但是,如果您想永久删除您使用的图像(并恢复其所有资源):

      // permanently delete the image and reclaim all the resources it used
      yourImage.destroy();
      yourLayer.draw();
      

      【讨论】:

        猜你喜欢
        • 2014-04-13
        • 1970-01-01
        • 2020-10-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-15
        • 1970-01-01
        • 2015-07-29
        • 2021-04-30
        相关资源
        最近更新 更多