如何覆盖/替换现有对象

时间:2018-08-22 14:34:54

标签: javascript paperjs

由于需求,我给出了与onmouseClick相关的所有代码:

首先,我将svg的每个新层导入画布:

//imports svg from external js script
var svgimportSVG = function (elem,x) {

// creates a new layer for each imported svg
var layer = new Layer();
layer.name = "Layer" +x;
layer.activate();

// import here
rect = paper.project.importSVG(elem,function(item){
    console.log("Elem: " + elem);        
});

然后我将“框架”构建为边界路径:

// build a frame
rect.data.showBounds = new Path.Rectangle({
    rectangle: rect.bounds,
    strokeColor: 'aqua'
});

return rect;
}

我希望只有单击的元素或导入最少的元素具有可见的框架:

tool.onMouseDown = function(e) {
var hitResult = project.hitTest(e.point, hitOptions);
if(hitResult){
    var SVGGroup = searchSVGGroup(hitResult.item,"svg");
    var SVGLayer = searchSVGGroup(hitResult.item,"layer");

    // Layer of clicked object shall be in front or last element in project.layers

    console.log(SVGLayer.name);
    var IndexOfLayer = project.layers.indexOf(SVGLayer);
    project.layers.splice(IndexOfLayer,1);
    project.layers.push(SVGLayer);
    var LayersLengthnew = project.layers.length;

    //Clicked object can be influenced ouside paperscope 
    window.globals.extExportOnMouseClick(project.layers[LayersLengthnew -1].name);

    SVGGroup.data.showBounds = new Path.Rectangle({
        rectangle: rect.bounds,
        strokeColor: 'aqua'
    });

    // sent as old clicked object
    svgSetRemoveFrameofGroup(SVGGroup);
}
else{...}
}

//create an empty object that holds the last clicked object 
var svgFramedGroup = {};

var svgSetRemoveFrameofGroup = function(rect){

  var rectold = {};
  console.log("rect name: "+ rect.name); // gives correct name    
  if(svgFramedGroup.data){
    rectold = svgFramedGroup;
    console.log("rect old name: "+ rectold.name); // gives correct name
  }

  svgFramedGroup = rect;

  if(!svg_isEmpty(rectold)){
    //... showBounds should go to Garbage collector
    rectold = rectold.data.showBounds.remove();
  }

  return rectold;
} 

但是点击更多的元素仍然具有边界,这是一条路径!?

1 个答案:

答案 0 :(得分:1)

我为您here回答了同样的问题。

唯一的不同是,这里您要求的是单击元素时显示的“框架”,而您要求的是单击元素时将其隐藏(这就是我在示例中所做的)

但是无论如何,您都应该能够理解逻辑。