Created
April 9, 2013 02:10
-
-
Save gaina-jp/5342357 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Bezier2 = (function(){ | |
/** | |
* class Point | |
* @param {number} x | |
* @param {number} y | |
*/ | |
var Point = (function(){ | |
function Point(x, y){ | |
this.x = x || 0; | |
this.y = y || 0; | |
} | |
return Point | |
})(); | |
/** | |
* class Bezier2 2次ベジェ | |
* @param targetElm | |
* @param {number} x1 | |
* @param {number} y1 | |
* @param {number} x2 | |
* @param {number} y2 | |
* @param {number} x3 | |
* @param {number} y3 | |
* @constructor | |
*/ | |
function Bezier2(targetElm, x1, y1, x2, y2, x3, y3){ | |
this.target = targetElm; | |
this.p1 = new Point(x1 || 0, y1 || 0); | |
this.p2 = new Point(x2 || 0, y2 || 0); | |
this.p3 = new Point(x3 || 0, y3 || 0); | |
} | |
/** | |
* __X__ 2次ベジェ X座標算出 | |
* @param {number} t | |
* @return {number} | |
*/ | |
Bezier2.prototype.__X__ = function(t){ | |
var _x1 = (1 - t) * (1 - t) * this.p1.x; | |
var _x2 = 2 * (1 - t) * t * this.p2.x; | |
var _x3 = t * t * this.p3.x; | |
return _x1 + _x2 + _x3; | |
}; | |
/** | |
* __Y__ 2次ベジェ Y座標算出 | |
* @param {number} t | |
* @return {number} | |
*/ | |
Bezier2.prototype.__Y__ = function(t){ | |
var _y1 = (1 - t) * (1 - t) * this.p1.y; | |
var _y2 = 2 * (1 - t) * t * this.p2.y; | |
var _y3 = t * t * this.p3.y; | |
return _y1 + _y2 + _y3; | |
}; | |
/** | |
* putCoor 座標を取得する | |
* @param {Number} t 0 <= t <= 1 | |
* @return {Point} | |
*/ | |
Bezier2.prototype.putCoor = function(t){ | |
return new Point(this.__X__(t), this.__Y__(t)); | |
}; | |
/** | |
* pos ターゲット要素のposition値を設定 | |
* @param {Number} t 0 <= t <= 1 | |
*/ | |
Bezier2.prototype.pos = function(t){ | |
this.target.css({top :this.__Y__(t), left :this.__X__(t)}); | |
}; | |
/** | |
* animateBizier | |
* @param {Number} msec | |
* @param {Number} fps | |
*/ | |
Bezier2.prototype.animateBizier = function(msec, fps){ | |
fps = 1000 / fps || 1000 / 60; | |
var _self = this; | |
var _count = 0; | |
var _frame = 0; | |
var _end = (msec / fps) | 0; | |
var timer = setInterval(function(){ | |
_frame = fps / msec * _count; | |
_self.pos(_frame); | |
if(_end === _count){ | |
clearInterval(timer); | |
_self.pos(1); | |
} | |
_count++; | |
}, fps); | |
}; | |
return Bezier2; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment