From: dhrname Date: Tue, 26 Jul 2016 14:05:11 +0000 (+0900) Subject: Support the rotate attribute of the animateMotion element X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=6193feae7c888edd0253ace4c6d15c712b4fa948;p=sie%2Fsie.git Support the rotate attribute of the animateMotion element --- diff --git a/org/w3c/dom/smil.js b/org/w3c/dom/smil.js index 9f58e03..3dbb712 100644 --- a/org/w3c/dom/smil.js +++ b/org/w3c/dom/smil.js @@ -1699,7 +1699,7 @@ base("$calcMode").up("$attribute").mix( { }, /*図の現在の角度rを求めて、rotate(rの文字列(最後に括弧はいらない)で返すメソッド*/ - getRotate: function(path, advanceLength) { + getRotate: function(path, advanceLength, rotate) { /*パスセグメントの数値を求めてから、動いている図形の傾き角度r(ラジアンではなく度数)を算出する*/ var length = path.getPathSegAtLength(advanceLength), seg = path.pathSegList.getItem(length), @@ -1711,21 +1711,25 @@ base("$calcMode").up("$attribute").mix( { if (nextCommand === "M") { return ""; } else if (nextCommand === "L") { - return ") rotate(" +Math.atan2(nextSeg.y-seg.y, nextSeg.x-seg.x)/Math.Pi*180+ ""; + return ") rotate(" +(Math.atan2(nextSeg.y-seg.y, nextSeg.x-seg.x)/Math.Pi*180 + rotate)+ ""; } else if (nextCommand === "C") { + return ") rotate(" +(Math.atan2(nextSeg.y1-seg.y, nextSeg.x1-seg.x)/Math.Pi*180 + rotate)+ ""; } } else if ((command === "L") && (length-1 >= 0)) { var preSeg = path.pathSegList.getItem(length-1); - return ") rotate(" +Math.atan2(seg.y-preSeg.y, seg.x-preSeg.x)/Math.Pi*180+ ""; + return ") rotate(" +(Math.atan2(seg.y-preSeg.y, seg.x-preSeg.x)/Math.Pi*180 + rotate)+ ""; } else if (command === "C") { - /*3次ベジェ曲線は媒介曲線 - *x = (x4-3*(x3-x2)-x1)*t*t*t + 3*(x3-2*x2+x1)*t*t + 3*(x2-x1)*t + x1 - *y = (y4-3*(y3-y2)-y1)*t*t*t + 3*(y3-2*y2+y1)*t*t + 3*(y2-y1)*t + y1 - *なので、媒介曲線の微分dy/dxが接線の傾きとなる - *したがって、微分したx'と微分したy'をatan2に入力すれば、角度が算出できる*/ + /*3次ベジェ曲線を微分する方法はニュートン法など数値解析の必要があるので、 + * 以下の通り、別の方法を採用する。 + * 現在位置から一歩進んだ曲線上の点Bをとり、それを、現在の点Aと結んで線分ABとしたとき、 + * その直線の傾きからおおよその角度を求める*/ var point = path.getPointAtLength(advanceLength), x = point.x, y = point.y; + /*一歩進んだ点*/ + point = path.getPointAtLength(advanceLength+1); + console.log(Math.PI); + return ") rotate(" +(Math.atan2(point.y-y, point.x-x)/Math.PI*180 + rotate)+ ""; } }, @@ -1735,11 +1739,18 @@ base("$calcMode").up("$attribute").mix( { var path = this.path, advanceLength = advance * path.getTotalLength(); /*全体の距離から、現在進めている距離を算出して、そこから、現在点を導き出す*/ - var point = path.getPointAtLength(advanceLength); + var point = path.getPointAtLength(advanceLength), + rotate = 0; //追加すべき角度 if (this.rotate === "0") { return point.x+ "," +point.y; + } else if (this.rotate === "auto") { + rotate = 0; + } else if (this.rotate === "auto-reverse") { + rotate = 180; + } else { + rotate = +this.rotate; } - return point.x+ "," +point.y + this.getRotate(path, advanceLength); + return point.x+ "," +point.y + this.getRotate(path, advanceLength, rotate); } } ) .on("init", function (ele) {