简体   繁体   English

拖动鼠标Fabric.js时移动背景

[英]Move the background when drag mouse Fabric.js

I'm using Fabric.js to present a floor plan, I use a background image and use Fabric.js to add icons for electrical devices, when i drag the mouse, only the icons moving and the background wallpaper standing still, so how can i make the background move as well ? 我正在使用Fabric.js来展示平面图,我使用背景图像,并使用Fabric.js来添加用于电气设备的图标,当我拖动鼠标时,只有图标移动并且背景墙纸保持静止,因此如何我也使背景移动了吗?

var canvas = new fabric.Canvas('c');
canvas.selection = false;

var circle = new fabric.Circle({
    radius: 20,
    fill: 'green',
    left: 100,
    top: 100,
    selectable: false
});

canvas.add(circle);

var panning = false;
canvas.on('mouse:up', function (e) {
    panning = false;
});

canvas.on('mouse:down', function (e) {
    panning = true;
});
canvas.on('mouse:move', function (e) {
    if (panning && e && e.e) {
        debugger;
        var units = 10;
        var delta = new fabric.Point(e.e.movementX, e.e.movementY);
        canvas.relativePan(delta);
    }
});

$(function () {
    $('#zoomIn').click(function () {
        canvas.setZoom(canvas.getZoom() * 1.1);
    });

    $('#zoomOut').click(function () {
        canvas.setZoom(canvas.getZoom() / 1.1);
    });
});

My fiddle 我的小提琴

Add image to canvas using fromURL 使用fromURL将图像添加到画布

fabric.Image.fromURL(imgURL, function(oImg) {
  oImg.set({
    "left": 0,
    "top": 0,
    "scaleX": canvas.width / oImg.width,
    "scaleY": canvas.height / oImg.height
  })
  canvas.renderAll();
});

Here is updated jsFiddle 这是更新的jsFiddle

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

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