iT邦幫忙

DAY 10
3

c#應用系列 第 12

C#應用(12)把jQuery Easing Plugin搬到C#來用

  • 分享至 

  • xImage
  •  

*Easing函數的C#版本, 用於計算控制項的Size或坐標, 藉以產生動畫效果

這篇真的都沒測過, 隨便寫寫
如果有寫錯的地方, 要用的人請自行修正bug

回目錄
*jQuery Easing Plugin 是一個動畫效果的運算外掛, 原本是在JavaScript下配合jQuery使用
*雖然在.NET FremeWork 4 / 4.5 的WPF 有個效果類似的Easing 函式 但這裡並不用它

*檔案 Easing.cs

using System;

namespace Plugin.Animation
{
    public class Easing
    {
        public static double linear(
            double t, double b, double c, double d)
        {
            return c * t / d + b;
        }

        public static double easeInQuad(
            double t, double b, double c, double d)
        {
		    return c*(t/=d)*t + b;
	    }
	    
        public static double easeOutQuad(
            double t, double b, double c, double d)
        {
		    return -c *(t/=d)*(t-2) + b;
	    }

	    public static double easeInOutQuad(
            double t, double b, double c, double d)
        {
		    if ((t/=d/2) < 1) return c/2*t*t + b;
		    return -c/2 * ((--t)*(t-2) - 1) + b;
	    }
	    
        public static double easeInCubic(
            double t, double b, double c, double d)
        {
		    return c*(t/=d)*t*t + b;
	    }
	    
        public static double easeOutCubic(
            double t, double b, double c, double d)
        {
		    return c*((t=t/d-1)*t*t + 1) + b;
	    }
        
	    public static double easeInOutCubic(
            double t, double b, double c, double d)
        {
		    if ((t/=d/2) < 1) return c/2*t*t*t + b;
		    return c/2*((t-=2)*t*t + 2) + b;
	    }
	
        public static double easeInQuart(
            double t, double b, double c, double d)
        {
    		return c*(t/=d)*t*t*t + b;
    	}
	
        public static double easeOutQuart(
            double t, double b, double c, double d)
        {
		    return -c * ((t=t/d-1)*t*t*t - 1) + b;
	    }
	
        public static double easeInOutQuart(
            double t, double b, double c, double d)
        {
		    if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		    return -c/2 * ((t-=2)*t*t*t - 2) + b;
	    }
	
        public static double easeInQuint(
            double t, double b, double c, double d)
        {
		    return c*(t/=d)*t*t*t*t + b;
	    }
	
        public static double easeOutQuint(
            double t, double b, double c, double d)
        {
		    return c*((t=t/d-1)*t*t*t*t + 1) + b;
	    }
	
        public public static double easeInOutQuint(
            double t, double b, double c, double d)
        {
		    if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		    return c/2*((t-=2)*t*t*t*t + 2) + b;
	    }
	
        public static double easeInSine(
            double t, double b, double c, double d)
        {
		    return -c * Math.Cos(t/d * (Math.PI/2)) + c + b;
	    }
	    
        public static double easeOutSine(
            double t, double b, double c, double d)
        {
		    return c * Math.Sin(t/d * (Math.PI/2)) + b;
	    }
	
        public static double easeInOutSine(
            double t, double b, double c, double d)
        {
		    return -c/2 * (Math.Cos(Math.PI*t/d) - 1) + b;
	    }
	    
        public static double easeInExpo(
            double t, double b, double c, double d)
        {
		    return (t==0) ? b : c * Math.Pow(2, 10 * (t/d - 1)) + b;
	    }
	    
        public static double easeOutExpo(
            double t, double b, double c, double d)
        {
		    return (t==d) ? b+c : c * (-Math.Pow(2, -10 * t/d) + 1) + b;
	    }
	
        public static double easeInOutExpo(
            double t, double b, double c, double d)
        {
		    if (t==0) return b;
		    if (t==d) return b+c;
		    if ((t/=d/2) < 1) return c/2 * Math.Pow(2, 10 * (t - 1)) + b;
		    return c/2 * (-Math.Pow(2, -10 * --t) + 2) + b;
	    }
	
        public static double easeInCirc(
            double t, double b, double c, double d)
        {
		    return -c * (Math.Sqrt(1 - (t/=d)*t) - 1) + b;
	    }
	    
        public static double easeOutCirc(
            double t, double b, double c, double d)
        {
		    return c * Math.Sqrt(1 - (t=t/d-1)*t) + b;
	    }
	
        public static double easeInOutCirc(
            double t, double b, double c, double d)
        {
		    if ((t/=d/2) < 1) return -c/2 * (Math.Sqrt(1 - t*t) - 1) + b;
		    return c/2 * (Math.Sqrt(1 - (t-=2)*t) + 1) + b;
	    }

        public static double easeInElastic(
            double t, double b, double c, double d)
        {
		    double s=1.70158;
            double p;
            double a=c;
		    if (t==0) return b;  
            if ((t/=d)==1) return b+c;  
            p=d*0.3;
		    if (a < Math.Abs(c)) { a=c; s=p/4; }
		    else s = p/(2*Math.PI) * Math.Asin (c/a);
		    return -(a*Math.Pow(2,10*(t-=1)) * Math.Sin( (t*d-s)*(2*Math.PI)/p )) + b;
	    }
	
        public static double easeOutElastic(
            double t, double b, double c, double d)
        {
		    double s=1.70158;
            double p;
            double a=c;
		    if (t==0) return b;  
            if ((t/=d)==1) return b+c;  
            p=d*0.3;
		    if (a < Math.Abs(c)) { a=c; s=p/4; }
		    else s = p/(2*Math.PI) * Math.Asin (c/a);
		    return a*Math.Pow(2,-10*t) * Math.Sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	    }
	
        public static double easeInOutElastic(
            double t, double b, double c, double d)
        {
		    double s=1.70158;
            double p;
            double a=c;
		    if (t==0) return b;  
            if ((t/=d/2)==2) return b+c;  
            p=d*(0.3*1.5);
		    if (a < Math.Abs(c)) { a=c; s=p/4; }
		    else s = p/(2*Math.PI) * Math.Asin (c/a);
		    if (t < 1) return -.5*(a*Math.Pow(2,10*(t-=1)) * Math.Sin( (t*d-s)*(2*Math.PI)/p )) + b;
		    return a*Math.Pow(2,-10*(t-=1)) * Math.Sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	    }
	
        public static double easeInBack(
            double t, double b, double c, double d, double? s = null)
        {
            double s2 = (s == null) ? (s2 = 1.70158) : (double)s;
		    return c*(t/=d)*t*((s2+1)*t - s2) + b;
	    }
	
        public static double easeOutBack(
            double t, double b, double c, double d, double? s = null)
        {
            double s2 = (s == null) ? (s2 = 1.70158) : (double)s;
		    return c*((t=t/d-1)*t*((s2+1)*t + s2) + 1) + b;
	    }
	
        public static double easeInOutBack(
            double t, double b, double c, double d, double? s = null)
        {
		    double s2 = (s == null) ? (s2 = 1.70158) : (double)s;
		    if ((t/=d/2) < 1) return c/2*(t*t*(((s2*=(1.525))+1)*t - s2)) + b;
		    return c/2*((t-=2)*t*(((s2*=(1.525))+1)*t + s2) + 2) + b;
	    }
	
        public static double easeInBounce(
            double t, double b, double c, double d)
        {
		    return c - Easing.easeOutBounce(d-t, 0, c, d) + b;
	    }

        public static double easeOutBounce(
            double t, double b, double c, double d)
        {
		    if ((t/=d) < (1/2.75)) {
    			return c*(7.5625*t*t) + b;
	    	} else if (t < (2/2.75)) {
		    	return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		    } else if (t < (2.5/2.75)) {
			    return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		    } else {
			    return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		    }
	    }
	
        public static double easeInOutBounce(
            double t, double b, double c, double d)
        {
            if (t < d / 2) return Easing.easeInBounce(t * 2, 0, c, d) * .5 + b;
            return Easing.easeOutBounce(t * 2 - d, 0, c, d) * .5 + c * .5 + b;
	    }
    }
}

*使用範例
從缺 (請自定Timer配合使用)

傳入參數分別為
t: current time
b: begInnIng value
c: change In value
d: duration


上一篇
C#應用(11)ActiveX
下一篇
C#應用(13)Mail Server
系列文
c#應用13
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言