画布fabric.js中的图层

时间:2016-02-24 08:30:36

标签: html5 canvas html5-canvas fabricjs

我可以在画布上创建图层吗?我需要在对象和其他织物层上方的图层以及在该图层内移动的对象。可能吗? 我使用fabric.js

编辑: 该小组不为我服务。我需要在画布中创建区域,我可以在其中移动对象而不是其余区域。并了解该领域的相关立场。 https://github.com/kangax/fabric.js/wiki/How-fabric-canvas-layering-works

1 个答案:

答案 0 :(得分:1)

至少对于布料,您可以对形状进行分组,并将作为一个整体移动:



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

var rect1 = new fabric.Rect({ width: 100, height: 100, fill: '#0bda51' });
var circle = new fabric.Circle({ radius: 100, fill: '#4d5d53' });
var group1 = new fabric.Group([ circle, rect1 ], { left: 100, top: 100 });
canvas.add(group1);

var circle1 = new fabric.Circle({ radius: 100, fill: '#a2add0' });
var circle2 = new fabric.Circle({ radius: 75, fill: '#0038a8' });
var circle3 = new fabric.Circle({ radius: 50, fill: '	#bf94e4' });

var group2 = new fabric.Group([ circle1, circle2, circle3 ], { left: 200, top: 200 });
canvas.add(group2);

var shadow = { color: 'rgba(0,0,0,0.6)', blur: 20, offsetX: 10, offsetY: 10, opacity: 0.6, fillShadow: true, strokeShadow: true }
var rect = new fabric.Rect({ left: 100, top: 100, fill:  "#8ccd7b", stroke: "#000", width: 100,      height: 100, strokeWidth: 10, opacity: .8 });
rect.setShadow(shadow);
canvas.add(rect);

<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.min.js"></script>
<canvas id="c" width="400" height="400"></canvas>
&#13;
&#13;
&#13;