简体   繁体   English

我怎样才能在画布中的矩形下方移动图像?

[英]How can i able to move image beneath rectangle in canvas?

I could select the background image from inside rectangle, but not able to move.我可以从矩形内部选择背景图像,但无法移动。 To move the image, i should go outside of the rectangle.要移动图像,我应该在矩形之外。 I know that the rectangle is created over the image, I'm looking for any option to move the image as it is selectable from inside rectangle.我知道矩形是在图像上创建的,我正在寻找任何移动图像的选项,因为它可以从矩形内部进行选择。

My canvas screenshot我的画布截图

This is how i set the image and rectangle.这就是我设置图像和矩形的方式。

changeImage(innercanvasHeight, innercanvasWidth) {

  const base_image = new Image();
  base_image.crossOrigin = 'Anonymous';
  base_image.src = 'assets/images/1wal.jpg';

  fabric.Image.fromURL(base_image.src, (myImg) => {
    const img1 = myImg.set({left: 160, top: 80, width: 600, height: 
    400, id: 'wallpaper'});
    this.FabriCanvas.add(img1).setActiveObject(img1);
    const hiddenImg = document.createElement('img');
    hiddenImg.src = this.FabriCanvas.getActiveObject().toDataURL();
    hiddenImg.id = 'target';
    hiddenImg.style.display = 'none';
    document.body.appendChild(hiddenImg);
    this.innerCanvas(innercanvasHeight, innercanvasWidth);
});


innerCanvas(height, width) {
 this.innercanvas = this.FabriCanvas.add(new fabric.Rect({
  left: 160,
  top: 80,
  id: 'innerCan',
  fill: 'transparent',
  stroke: '#fff',
  strokeWidth: 1,
  width: width,
  height: height,
  selectable: false
 }));
 this.FabriCanvas.renderAll();
}

Use preserveObjectStacking so it wont come up while dragging, and use perPixelTargetFind to click through the object if it is transparent.使用preserveObjectStacking使其在拖动时不会出现,如果对象是透明的,则使用perPixelTargetFind单击该对象。

DEMO演示

 var canvas = new fabric.Canvas('canvas',{ preserveObjectStacking: true }); var image = new fabric.Image(''); var rect = new fabric.Rect({ left: 160, top: 80, id: 'innerCan', fill: 'transparent', stroke: '#fff', strokeWidth: 1, width: 100, height: 100, selectable: false, perPixelTargetFind : true }); canvas.add(image,rect); image.setSrc('//fabricjs.com/assets/pug.jpg',function(img){ img.set({ scaleX:canvas.width/img.width,scaleY: canvas.height/img.height}); img.setCoords(); canvas.renderAll(); })
 canvas{ border: 2px solid #000; }
 <script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script> <canvas id='canvas' width=300 height=300></canvas>

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

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