function random(a, b) {
return rand.randNumber(a, b);
}
function bezier(time_ms, t, pt) {
var gesture1 = new path();
gesture1.setStarTime(10);
var fps = 1 / t;
for (let i = 0; i <= fps; i++) {
let copyArr = Array.from(pt, row => row.slice());
var xy = calculateBezier(i * t, copyArr);
gesture1.addPoint(xy[0], xy[1]);
}
gesture1.setDurTime(time_ms);
auto.dispatchGesture([gesture1]);
}
function calculateBezier(t, point2Fs) {
let len = point2Fs.length;
for (let i = len - 1; i > 0; i--) {
for (let j = 0; j < i; j++) {
point2Fs[j][0] = point2Fs[j][0] + (point2Fs[j + 1][0] - point2Fs[j][0]) * t;
point2Fs[j][1] = point2Fs[j][1] + (point2Fs[j + 1][1] - point2Fs[j][1]) * t;
}
}
return [parseInt(point2Fs[0][0]), parseInt(point2Fs[0][1])];
}
var CtrlPt1 = [
[167, 625],
[200, 200],
[500, 500],
[300, 100]
];
var CtrlPt2 = [
[309, 833],
[500, 700],
[600, 600],
[500, 300]
];
var CtrlPt3 = [
[151, 892],
[300, 880],
[450, 895],
[600, 885]
];
bezier(4000, 0.005, CtrlPt1);
bezier(4000, 0.005, CtrlPt2);
bezier(4000, 0.005, CtrlPt3);
bezier(time_ms, t, pt) º¯Êý
Õâ¸öº¯ÊýÓÃÓÚÉú³É²¢Ö´ÐÐÒ»¸ö±´Èû¶ûÇúÏßÊÖÊÆ¡£¾ßÌå²ÎÊý½âÊÍÈçÏ£º time_ms £º
- ×÷Ó㺶¨ÒåÊÖÊÆ´Ó¿ªÊ¼µ½½áÊøµÄ×Üʱ¼ä£¬µ¥Î»ÊǺÁÃë¡£
- ʾÀý£º
bezier(4000, 0.005, CtrlPt1); ±íʾÕû¸öÊÖÊƽ«ÔÚ4000ºÁÃ루4Ã룩ÄÚÍê³É¡£
t £º
- ×÷Ó㺶¨Òå¼ä¸ôϵÊý£¬ÓÃÓÚ¿ØÖƱ´Èû¶ûÇúÏߵļÆË㾫¶ÈºÍÏßÌõµÄÔ²Èó¶È¡£
t µÄÈ¡Öµ·¶Î§ÊÇ0µ½1Ö®¼ä¡£ - ¾«¶ÈÓëÔ²Èó¶È£º½ÏСµÄ
t ÖµÒâζ׿ÆËãµÄ¼ä¸ô¸üС£¬Éú³ÉµÄÇúÏ߸üÔ²È󣬵«¼ÆËãÁ¿Ò²»áÏàÓ¦Ôö¼Ó¡£½Ï´óµÄ t ÖµÔò±íʾ¼ÆËã¼ä¸ô¸ü´ó£¬ÇúÏß¿ÉÄÜ»áÏԵøüÕÛÏß»¯£¬¼ÆËãÁ¿½ÏÉÙ¡£ - ʾÀý£º
bezier(4000, 0.005, CtrlPt1); ÖÐµÄ t Ϊ0.005£¬±íʾÿ0.005µÄʱ¼äµ¥Î»¼ÆËãÒ»´Î±´Èû¶ûÇúÏßÉϵĵ㡣
pt £º
- ×÷Ó㺶¨Òå±´Èû¶ûÇúÏߵĿØÖƵã×ø±ê¡£¿ØÖƵã¾ö¶¨Á˱´Èû¶ûÇúÏßµÄÐÎ×´¡£ÆðµãºÍÖÕµãÊÇÇúÏßµÄÁ½¶Ë£¬¶øÖмäµÄ¿ØÖƵã¾ö¶¨ÁËÇúÏßÈçºÎÍäÇú¡£
- ¸ñʽ£º
pt ÊÇÒ»¸ö¶þάÊý×飬ÆäÖÐÿ¸ö×ÓÊý×é°üº¬Á½¸öÊý×Ö£¬·Ö±ð±íʾ¿ØÖƵãµÄX×ø±êºÍY×ø±ê¡£ - ʾÀý£º
CtrlPt1 ¶¨ÒåÁËÆðµã [167, 625] £¬Á½¸ö¿ØÖƵã [200, 200] ºÍ [500, 500] £¬ÒÔ¼°ÖÕµã [300, 100] ¡£CtrlPt2 ¶¨ÒåÁËÆðµã [309, 833] £¬Á½¸ö¿ØÖƵã [500, 700] ºÍ [600, 600] £¬ÒÔ¼°ÖÕµã [500, 300] ¡£CtrlPt3 ¶¨ÒåÁËÆðµã [151, 892] £¬Á½¸ö¿ØÖƵã [300, 880] ºÍ [450, 895] £¬ÒÔ¼°ÖÕµã [600, 885] ¡£
calculateBezier(t, point2Fs) º¯Êý
Õâ¸öº¯ÊýÓÃÓÚ¼ÆËã±´Èû¶ûÇúÏßÔÚij¸öʱ¼äµã t ÉϵÄ×ø±ê¡£¾ßÌå²ÎÊý½âÊÍÈçÏ£º t £º
- ×÷Ó㺱íʾʱ¼ä²ÎÊý£¬È¡Öµ·¶Î§ÊÇ0µ½1Ö®¼ä¡£
t=0 ¶ÔÓ¦ÇúÏßµÄÆðµã£¬t=1 ¶ÔÓ¦ÇúÏßµÄÖյ㡣 - ¼ÆËã¹ý³Ì£ºÍ¨¹ýµÝ¹éµØ²åÖµ¼ÆË㣬×îÖյõ½
t ʱ¿ÌµÄÇúÏß×ø±ê¡£
point2Fs £º
- ×÷Ó㺰üº¬±´Èû¶ûÇúÏߵĿØÖƵã×ø±ê¡£
- ¸ñʽ£º¶þάÊý×飬ÿ¸ö×ÓÊý×é°üº¬Á½¸öÊý×Ö£¬·Ö±ð±íʾ¿ØÖƵãµÄX×ø±êºÍY×ø±ê¡£
¼ÆËã±´Èû¶ûÇúÏßµÄÏêϸ¹ý³Ì±´Èû¶ûÇúÏߵļÆËãͨ³£Ê¹ÓõݹéµÄ²åÖµ·½·¨¡£ÔÚ calculateBezier º¯ÊýÖУ¬¾ßÌå²½ÖèÈçÏ£º ¿½±´¿ØÖƵãÊý×飺 - ÓÉÓÚ JavaScript ÖÐÊý×éÊÇÒýÓô«µÝµÄ£¬ÎªÁ˱ÜÃâÐÞ¸ÄÔʼ¿ØÖƵãÊý×飬Ê×ÏÈÐèÒª¿½±´Ò»·Ý
point2Fs ¡£
µÝ¹é²åÖµ¼ÆË㣺 - ¶ÔÓÚÿ¸ö¿ØÖƵ㣬ͨ¹ýÏßÐÔ²åÖµ¼ÆËãеĵ㣬ֱµ½Ö»Ê£ÏÂÒ»¸öµãΪֹ¡£
- ¾ßÌ幫ʽΪ£º
point2Fs[j][0] = point2Fs[j][0] + (point2Fs[j + 1][0] - point2Fs[j][0]) * t; - ÕâÀïµÄ
t Êǵ±Ç°Ê±¼ä²ÎÊý£¬point2Fs[j][0] ºÍ point2Fs[j + 1][0] ÊÇÏàÁÚ¿ØÖƵãµÄX×ø±ê¡£ - ͨ¹ýÕâ¸ö¹«Ê½¼ÆËãеÄX×ø±ê¡£
- ͬÑùµØ£¬¼ÆËãеÄY×ø±ê£º
point2Fs[j][1] = point2Fs[j][1] + (point2Fs[j + 1][1] - point2Fs[j][1]) * t;
·µ»Ø½á¹û£º - ×îÖÕ£¬
point2Fs[0] ½«°üº¬±´Èû¶ûÇúÏßÔÚ t ʱ¼äµãµÄ×ø±ê¡£ - ·µ»ØÕâ¸ö×ø±ê
[parseInt(point2Fs[0][0]), parseInt(point2Fs[0][1])] £¬ÆäÖÐ parseInt ÓÃÓÚ½«×ø±êת»»ÎªÕûÊý¡£
Ö´Ðб´Èû¶ûÊÖÊÆÔÚ bezier º¯ÊýÖУ¬Í¨¹ýÑ»·µ÷Óà calculateBezier º¯Êý£¬Éú³É´ÓÆðµãµ½ÖÕµãµÄһϵÁе㣬²¢½«ÕâЩµãÌí¼Óµ½ gesture1 ¶ÔÏóÖУ¬×îºóͨ¹ý auto.dispatchGesture([gesture1]); Ö´ÐÐÕâ¸öÊÖÊƲÙ×÷¡£ |