[英]how to calculate control points on a bezier curve?
我確實有一個貝塞爾曲線,在某個點上,我想要第二個貝塞爾曲線以平滑的方式“分支”第一條曲線。 與計算相交點(遵循Bezier曲線的百分比)一起,我還需要控制點(切線和權重)。 相交點是使用以下javascript計算的:
getBezier = function getBez(percent,p1,cp1,cp2,p2) {
function b1(t) { return t*t*t }
function b2(t) { return 3*t*t*(1-t) }
function b3(t) { return 3*t*(1-t)*(1-t) }
function b4(t) { return (1-t)*(1-t)*(1-t) }
var pos = {x:0,y:0};
pos.x = p1.x*b1(percent) + cp1.x*b2(percent) + cp2.x*b3(percent) + p2.x*b4(percent);
pos.y = p1.y*b1(percent) + cp1.y*b2(percent) + cp2.y*b3(percent) + p2.y*b4(percent);
return pos;
}
(非IE瀏覽器可以在http: //www.iscriptdesign.com- >教程->組和路徑中查看它的運行情況)。 我現在需要的只是分支點的控制點或(切線和權重)(我不知道從哪里開始,我希望有人可以指向一些代碼或數學方程式,如果可能的話,可以使用相同的參數作為函數)就像上面的getBezier函數一樣)。
找到並實現它: de-Casteljau
算法被證明是最快的可實現解決方案。 當前位於: iScriptDesign (教程-> Spit Bezier)。
用法示例(我認為,我需要@drjerry的幫助)
我有一個css3計時函數,這稱為ease-in-out: cubic-bezier(.42,0,.58,1)
。 圖形上看起來像: http : //cubic-bezier.com/#.42,0,.58,1
我想創建一個新的計時函數,該函數只是此圖的下半部分,然后是上半部分。
所以下半部分是ease-in
: cubic-bezier(.42,0,1,1)
。 圖形: http : //cubic-bezier.com/#.42,0,1,1
上半部分是ease-out
: cubic-bezier(0,0,.58,1)
。 圖形: http : //cubic-bezier.com/#0,0,.58,1
所以現在令我困惑的是,根據iScriptDesign上的腳本,它告訴我它們是不同的。
iScriptDeisgn表示, ease-in-out
的開始一半是( ease-in
): cubic-bezier(.21, 0, .355, .25)
。 圖形: http : //cubic-bezier.com/#.21,0,.35,.25
iScriptDeisgn表示, ease-in-out
后半部分(即ease-out
)為: cubic-bezier(.145, .25, .29, .5)
。 圖形: http : //cubic-bezier.com/#.14,.25,.29,.5
為什么ease-in
和ease-out
由iScriptDesign返回拆分功能與現實不同的ease-in
和真正的ease-out
? 我不明白
此示例代碼。
//////////////////START FROM ISCRIPTDESIGN
var splitBezier = function(array, perc) {
array.unshift({x:0, y:0});
var coll = [];
while (array.length > 0) {
for (var i = 0;i < array.length-1; i++) {
coll.unshift(array[i]);
array[i] = interpolate(array[i], array[i+1], perc);
}
coll.unshift(array.pop());
}
return {b1: [{x:coll[5].x, y:coll[5].y}, {x:coll[2].x, y:coll[2].y},{x:coll[0].x, y:coll[0].y}]
, b2: [{x:coll[1].x - coll[0].x,y:coll[1].y-coll[0].y}, {x:coll[3].x - coll[0].x,y:coll[3].y-coll[0].y}, {x:coll[6].x - coll[0].x,y:coll[6].y-coll[0].y}]};
}
var interpolate = function (p0, p1, percent) {
if (typeof percent === 'undefined') percent = 0.5;
return {x: p0.x + (p1.x - p0.x) * percent
, y: p0.y + (p1.y - p0.y) * percent};
}
//////////////////END FROM ISCRIPTDESIGN
var coord = function (x,y) {
if(!x) var x=0;
if(!y) var y=0;
return {x: x, y: y};
}
var easeInOut = [new coord(.42,0), new coord(.58,1), new coord(1,1)];
var split50percent = splitBezier(easeInOut.slice(), .5);
因此split50percent
設置為:
({
b1: [{
x: 0.21,
y: 0
}, {
x: 0.355,
y: 0.25
}, {
x: 0.5,
y: 0.5
}],
b2: [{
x: 0.14500000000000002,
y: 0.25
}, {
x: 0.29000000000000004,
y: 0.5
}, {
x: 0.5,
y: 0.5
}]
})
easeInOutSine
easeInOutSine
:.44,.05,.55,.95 easeInSine
: easeInSine
easeOutSine
: easeOutSine
easeInSine
:.22,.03,.36,.26 easeOutSine
:.14,.24,.28,.48
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.