svg.js元素的动画旋转会产生意想不到的结果(可见" jiggling")

时间:2018-02-25 11:27:36

标签: javascript animation svg svg.js

我正在使用svg.js来创建一个自行车骑手的动画。这里有半完整版:https://pedalfuriously.neocities.org/。我在使用requestAnimationFrame创建的动画(而不是动画中内置的svg.js)中移动和旋转svg元素时遇到了一些问题。

如果您查看链接,并使用cadence滑块使骑手踏板非常快,然后将滑块快速翻到零,您可以看到他的小腿"摇晃& #34;以断开的方式。真正做到的是,根据与曲柄旋转的绝对关系确定每个框架中腿的位置(而不是采用一些增量时间值来确定在该帧上的移动)。 / p>

我想我已经能够确认我的代码的哪个方面导致了问题。这是一个没有表现出确切行为的最小例子,但我想这说明了我认为应该负责的事情:



var draw = SVG("drawing").viewbox(0, 0, 400, 400)
var origin = {
  x: 70,
  y: 70
}
var length = 60

var blueLine = draw.group()
blueLine.line(0, 0, 0 + length, 0).move(origin.x, origin.y)
  .stroke({
    color: "#00f",
    width: 4
  })
blueLine.angle = 0

var greenLine = draw.group()
greenLine.line(0, 0, 0 + length, 0).move(origin.x, origin.y)
  .stroke({
    color: "#0f0",
    width: 4
  })
greenLine.angle = 0

var previous = 0
var dt = 0
var step = function(timestamp) {
  dt = timestamp - previous
  previous = timestamp
  blueLine.angle += 0.18 * dt
  blueLine.rotate(blueLine.angle, origin.x, origin.y)
  var endX = Math.cos(toRad(blueLine.angle)) * length
  var endY = Math.sin(toRad(blueLine.angle)) * length

  // Comment out this line, and rotation works fine
  greenLine.move(endX, endY)

  greenLine.angle = blueLine.angle - 10

  // Comment out this line, and movement works fine
  greenLine.rotate(greenLine.angle, origin.x, origin.y)

  // But they don't work together. If I both move and rotate 
  // the green line, it goes in this crazy huge arc, rather 
  // than rotating neatly around the end of the blue line 
  // as expected.
  window.requestAnimationFrame(step)
}
window.requestAnimationFrame(step)

function toRad(deg) {
  return deg * (Math.PI / 180)
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.6.4/svg.js"></script>
<div id="drawing"></div>
&#13;
&#13;
&#13;

我用实际代码注意到的其他事情是,如果我移动腿的位置,它会改变问题的严重程度,甚至完全停止。如果臀部一直定位在自行车前部附近,问题就不那么严重了。此外,如果我禁用小腿旋转,则没有摇晃。在某些位置,即使在任何动作开始之前,小腿也会在负载时立即旋转出屏幕。

我希望得到一些指导,因为我误解了操纵元素的工作方式,特别是在svg.js或SVG中。

谢谢亲切的矢量图形专家!

这是腿的实际代码。 step()函数可能是最相关的。不确定它是否有用:

Rider.Leg = function(foot, front, xOffset, yOffset) {
    var upper = front ? SVGE.upperLeg : SVGE.upperLegBack
    var lower = front ? SVGE.lowerLeg : SVGE.lowerLegBack
    this.foot = foot
    this.draw = foot.draw
    this.geo = {
        upper: {
            x: this.foot.pedal.gear.x + 150,
            y: this.foot.pedal.gear.y - 750,
            length: 396
        },
        lower: {
            length: 390
        }
    }
    this.upper = this.draw.group().svg(upper).move(this.geo.upper.x, this.geo.upper.y)
        .transform({ scale: 0.95, cx: 0, cy: 0 })
    this.lower = this.draw.group().svg(lower).move(this.geo.upper.x, this.geo.upper.y)
}

// Step function does not take in a time argument. Positioning of legs is based only on
// the absolute position of other elements, none of which jiggle.
Rider.Leg.prototype.step = function () {
    var angle = this.pedalAngle() - Math.PI
    var ha = this.scaleneAngle(this.geo.lower.length, this.geo.upper.length, this.pedalDistance())
    var ka = this.scaleneAngle(this.pedalDistance(), this.geo.lower.length, this.geo.upper.length)
    var x = this.geo.upper.length * Math.cos(ha + angle)
    var y = this.geo.upper.length * Math.sin(ha + angle)
    this.upper.rotate(Drive.toDeg(angle + ha), 0, 0)
    this.lower.move(this.geo.upper.x + x, + this.geo.upper.y + y)
    this.lower.rotate(Drive.toDeg(angle + ha + ka - Math.PI), 0, 0)

}

// Gets the distance between the hip joint and the pedal
Rider.Leg.prototype.pedalDistance = function () {
    var pos = this.foot.getPos()
    var xDist = this.geo.upper.x - pos.x
    var yDist = this.geo.upper.y - pos.y
    return Math.hypot(xDist, yDist)
}

// Gets the angle between the hip joint and the pedal 
Rider.Leg.prototype.pedalAngle = function () {
    var pos = this.foot.getPos()
    var xDist = this.geo.upper.x - pos.x
    var yDist = this.geo.upper.y - pos.y
    return Math.atan2(yDist, xDist)
}

Rider.Leg.prototype.scaleneAngle = function (a, b, c) {
    return Math.acos(((b * b) + (c * c) - (a * a)) / (2 * b * c))
}

1 个答案:

答案 0 :(得分:0)

当您在某个组上致电move()时,它会在内部表示为翻译。 svg.js找出了将对象转换到新位置的疯狂方法,而不改变任何其他转换。这通常不会奏效。尤其不是,当你旋转时。

这就是为什么你应该避免这些绝对的转变,并与相关的转变。只需在每次移动前拨打untransform,然后从零开始。然后你可以这样做:

greenLine.transform({x:endX, y:endY, relative: true})

将线移动一定量。这应该会更好。