var topCanvas = new fabric.Canvas("top", {renderOnAddRemove:false});
var bottomCanvas = new fabric.Canvas("bottom", {renderOnAddRemove:false});
fabric.Object.prototype.originX = 'center';
fabric.Object.prototype.originY = 'center';
var update = function() {
bottomCanvas.clear().add.apply(bottomCanvas, topCanvas.getObjects().map(function (o) {
var clone = o.clone();
clone.set({
selectable: false,
hasBorders: false,
hasControls: false,
fill: "rgba(255,0,0,.5)",
globalCompositeOperation: "xor",
});
if (o.group) {
var matrix = o.calcTransformMatrix();
var transforms = fabric.util.qrDecompose(matrix);
clone.set({
angle: transforms.angle,
skewX: transforms.skewX,
skewY: transforms.skewY,
scaleX: transforms.scaleX,
scaleY: transforms.scaleY,
top: transforms.translateY,
left: transforms.translateX,
});
}
return clone;
})).renderAll();
};
topCanvas.on({
"object:modified": update
});
topCanvas
.add(new fabric.Rect({
top: 50,
left: 50,
width: 100,
height: 100,
fill: "transparent"
}))
.add(new fabric.Rect({
top: 100,
left: 100,
width: 100,
height: 100,
fill: "transparent"
}))
.add(new fabric.Rect({
top: 150,
left: 150,
width: 100,
height: 100,
fill: "transparent"
}))
.renderAll();
update();
#cont {
position: relative;
width: 400px;
height: 300px;
border: 1px solid;
}
canvas, .canvas-container {
position: absolute!important;
left: 0!important;
top: 0!important;
width: 100%!important;
height: 100%!important;
}
<div id="cont">
<canvas id="bottom" width="400" height="300"></canvas>
<canvas id="top" width="400" height="300"></canvas>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.6.3/fabric.min.js"></script>
這個偉大的工程,謝謝:)當移動一組左/頂值似乎是1像素了。再次移動單個對象時會得到糾正。有一個想法,這可能來自哪裏?無論如何,好的答案! – Fidel90
我看到了另一個問題:在分組之前縮放其中一個rects時,這個rects位置在移動組之後關閉得更多:(變化的旋轉也會發生這種情況 – Fidel90
woops。這與組如何轉換我會盡快採取另一種解決方法 – StefanHayden