Fabric JS Stickman-以编程方式为四肢动画,并保持所有圆和线之间的关节

时间:2019-01-04 11:28:07

标签: animation html5-canvas fabricjs

Example of output

我目前正在用Fabric Js开发一个火柴人,它将通过程序控制。按照Fabric Stickman演示,您可以拖放圆圈。但是,当您尝试以编程方式一次完成一个以上的圆时,关节将断裂-线不再跟随圆的位置。

这是一段代码,我在其中遍历一个对象,以动画化每个肢体。

let positionStateStore = {
    ARM_RAISE: {
        leftElbowCircle: {
            ref: 'leftElbowCircle',
            top: 152,
            left: 206
        },
        rightElbowCircle: {
            ref: 'rightElbowCircle',
            top: 158,
            left: 286
        },
        rightHandCircle: {
            ref: 'rightHandCircle',
            top: 110,
            left: 309
        },
        leftHandCircle: {
            ref: 'leftHandCircle',
            top: 111,
            left: 191
        }
    }
}

var _keys = ['leftElbowCircle', 'rightElbowCircle', 'rightHandCircle', 'leftHandCircle'];

for (var i = 0; i < _keys.length; i++) {
    var obj = positionStateStore.ARM_RAISE[_keys[i]];
    var ref = circles[_keys[i]];
    ref.animate('top', obj.top, {
        duration: 1000,
        onChange: function (){ animateObj(ref); },
        onComplete: function() {}
    });
}

有人知道我如何确保它们保持同步吗?谢谢你提供的所有帮助。

2 个答案:

答案 0 :(得分:0)

您尝试达到这样的效果吗?:

(function() {
  var canvas = this.__canvas = new fabric.Canvas('c');
  fabric.Object.prototype.originX = fabric.Object.prototype.originY = 'center';

  function makeCircle(left, top, line1, line2, line3, line4) {
    var c = new fabric.Circle({
      left: left,
      top: top,
      strokeWidth: 5,
      radius: 12,
      fill: '#fff',
      stroke: '#666'
    });
    c.hasControls = c.hasBorders = false;

    c.line1 = line1;
    c.line2 = line2;
    c.line3 = line3;
    c.line4 = line4;

    return c;
  }

  function makeLine(coords) {
    return new fabric.Line(coords, {
      fill: 'red',
      stroke: 'red',
      strokeWidth: 5,
      selectable: false,
      evented: false,
    });
  }

  var line = makeLine([ 250, 125, 250, 175 ]),
      line2 = makeLine([ 250, 175, 250, 250 ]),
      line3 = makeLine([ 250, 250, 300, 350]),
      line4 = makeLine([ 250, 250, 200, 350]),
      line5 = makeLine([ 250, 175, 175, 225 ]),
      line6 = makeLine([ 250, 175, 325, 225 ]);

  canvas.add(line, line2, line3, line4, line5, line6);

  canvas.add(
    makeCircle(line.get('x1'), line.get('y1'), null, line),
    makeCircle(line.get('x2'), line.get('y2'), line, line2, line5, line6),
    makeCircle(line2.get('x2'), line2.get('y2'), line2, line3, line4),
    makeCircle(line3.get('x2'), line3.get('y2'), line3),
    makeCircle(line4.get('x2'), line4.get('y2'), line4),
    makeCircle(line5.get('x2'), line5.get('y2'), line5),
    makeCircle(line6.get('x2'), line6.get('y2'), line6)
  );

  canvas.on('object:moving', function(e) {
 
    var p = e.target;
    if(p.type == 'activeSelection'){
    	for(i=0;i<p._objects.length;i++){
         p._objects[i].line1 && p._objects[i].line1.set({ 'x2': p.left+p._objects[i].left, 'y2': p.top+p._objects[i].top });
    		 p._objects[i].line2 && p._objects[i].line2.set({ 'x1': p.left+p._objects[i].left, 'y1': p.top +p._objects[i].top});
    		 p._objects[i].line3 && p._objects[i].line3.set({ 'x1': p.left+p._objects[i].left, 'y1': p.top +p._objects[i].top});
    		 p._objects[i].line4 && p._objects[i].line4.set({ 'x1': p.left+p._objects[i].left, 'y1': p.top +p._objects[i].top});
      }
    }else{
      p.line1 && p.line1.set({ 'x2': p.left, 'y2': p.top });
    	p.line2 && p.line2.set({ 'x1': p.left, 'y1': p.top });
    	p.line3 && p.line3.set({ 'x1': p.left, 'y1': p.top });
    	p.line4 && p.line4.set({ 'x1': p.left, 'y1': p.top });
    }
    
    canvas.renderAll();
  });
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.6/fabric.js"></script>
<canvas id="c" width="500" height="600"></canvas>

答案 1 :(得分:0)

对于希望使用Fabric创建动画棍子人的任何人,请参见以下链接-该链接已发展为一种免费的Alexa技能。

https://github.com/nicktaras/stick-person