LayaAir 旋转的小球 抛物线效果 [未完善]

一个旋转的小球

class Ball extends Laya.Sprite{

    private static cached:boolean = false;
    private body:Laya.Sprite;

    constructor(){
        super();
        this.init();
    }

    public init():void{
        if(!Ball.cached){
            Ball.cached = true;
            this.body = new Laya.Sprite();
            this.body.loadImage("war/ball.png");
            this.body.pivot(12,12);
        }
        this.addChild(this.body);
        Laya.timer.frameLoop(1,this,this.animate);    
    }

    private animate(e):void{
		this.body.rotation += 10;
	}
}

通过贝塞尔曲线 获得一个投石车一样的效果

class Main{

    private ball:Ball;
    private targetX:number = 600;    
    private targetY:number = 300;  
    private centerX:number = 0;    
    private centerY:number = 0;  
    private angle:number = Math.PI;
    private angle2:number = 0;
    private radius:number = 150;  //半径
    private speed:number = .05;
    private points:Array<any> = [];
    private i:number = 0;

    constructor()
    {
			Laya.init(1100, 619, Laya.WebGL);
            Laya.loader.load("res/atlas/war.atlas",Laya.Handler.create(this,this.onLoaded),null,Laya.Loader.ATLAS);
    }

    private onLoaded():void{
            this.ball = new Ball();
            Laya.stage.addChild(this.ball);
		    this.ball.x = Laya.stage.width / 2;
		    this.ball.y = Laya.stage.height / 2;
            Laya.stage.on(Laya.Event.CLICK,this,this.onClick);
            Laya.timer.frameLoop(1,this,this.onEnterFrame);
    }

    private onEnterFrame():void{
        if(this.points.length>0){
            if(this.i<this.points.length){
                this.ball.x = this.points[this.i].x;
                this.ball.y = this.points[this.i].y;
                this.i++;
            }
        }        
    }

    private onClick():void{

        // //角度归位
        // this.angle = Math.PI;

        // //小球归位
         this.ball.x = Laya.stage.width / 2;
		 this.ball.y = Laya.stage.height / 2;

        // //获取鼠标位置
         this.targetX = Laya.stage.mouseX;
         this.targetY = Laya.stage.mouseY;

        // //获得偏移
        // let dx:number = this.targetX - this.ball.x;
        // let dy:number = this.targetY - this.ball.y;
        var ps = [{ x: 0, y: 200 }, { x: 300, y: 0 },{ x: 700, y: 300 }];
        this.points = CreateBezierPoints(ps,20);
        this.i = 0;


        // let distance:number = Math.sqrt(dx*dx+dy*dy)/2;  //中心点的距离 两点距离/2
        // this.angle2 = Math.atan2(dy,dx);  //中心点的角度 和目标点一直 在一条直线上

        // this.radius = distance;

        // //获取中心店的坐标
        // this.centerX = this.ball.x + Math.cos(this.angle2)*distance;
        // this.centerY = this.ball.y + Math.sin(this.angle2)*distance;
 
    }
}


function CreateBezierPoints(anchorpoints, pointsAmount) {
    var points = [];
    for (var i = 0; i < pointsAmount; i++) {
        var point = MultiPointBezier(anchorpoints, i / pointsAmount);
        points.push(point);
    }
    return points;
}

function MultiPointBezier(points, t) {
    var len = points.length;
    var x = 0, y = 0;
    var erxiangshi = function (start, end) {
        var cs = 1, bcs = 1;
        while (end > 0) {
            cs *= start;
            bcs *= end;
            start--;
            end--;
        }
        return (cs / bcs);
    };
    for (var i = 0; i < len; i++) {
        var point = points[i];
        x += point.x * Math.pow((1 - t), (len - 1 - i)) * Math.pow(t, i) * (erxiangshi(len - 1, i));
        y += point.y * Math.pow((1 - t), (len - 1 - i)) * Math.pow(t, i) * (erxiangshi(len - 1, i));
    }
    return { x: x, y: y };
}

new Main(); 

 

转载于:https://my.oschina.net/u/659068/blog/1564002

以下是使用Vue实现购物车小球抛物线动画效果的方法: 1. 首先,在Vue组件中定义一个数组,用于存储购物车中的商品信息。 2. 在购物车页面中,为每个商品添加一个按钮,点击按钮时触发一个方法,将该商品的信息添加到数组中。 3. 在页面中使用`v-for`指令遍历数组,将每个商品的信息渲染到页面上。 4. 在每个商品的元素上添加一个动画效果,使其在添加到购物车时产生抛物线运动的效果。 5. 在Vue组件中定义一个方法,用于计算抛物线的路径。该方法接收商品元素的位置信息和购物车元素的位置信息作为参数,根据这些信息计算出抛物线的路径。 6. 在添加商品到购物车的方法中,调用计算抛物线路径的方法,并将路径信息传递给动画效果。 7. 使用CSS动画或JavaScript动画库来实现抛物线动画效果。 下面是一个示例代码: ```html <template> <div> <div class="product" v-for="product in products" :key="product.id"> <img :src="product.image" alt="product image"> <button @click="addToCart(product)">Add to Cart</button> </div> <div class="cart"> <div class="ball" v-for="item in cartItems" :key="item.id" ref="ball"></div> </div> </div> </template> <script> export default { data() { return { products: [ { id: 1, image: 'product1.jpg' }, { id: 2, image: 'product2.jpg' }, { id: 3, image: 'product3.jpg' } ], cartItems: [] }; }, methods: { addToCart(product) { this.cartItems.push(product); this.animateBall(product); }, animateBall(product) { const ball = this.$refs.ball[this.cartItems.length - 1]; const productRect = product.$el.getBoundingClientRect(); const cartRect = this.$el.querySelector('.cart').getBoundingClientRect(); const startX = productRect.left + productRect.width / 2; const startY = productRect.top + productRect.height / 2; const endX = cartRect.left + cartRect.width / 2; const endY = cartRect.top + cartRect.height / 2; const curve = BezierEasing(0.42, 0, 0.58, 1); const duration = 1000; anime({ targets: ball, translateX: [startX, endX], translateY: [startY, endY], scale: [1, 0.5], opacity: [1, 0], easing: curve, duration: duration }); } } }; </script> <style> .product { display: inline-block; margin: 10px; } .cart { position: relative; width: 100px; height: 100px; background-color: #ccc; } .ball { position: absolute; width: 20px; height: 20px; background-color: red; border-radius: 50%; } </style> ``` 请注意,上述代码中的`product.image`和`product.$el`需要根据实际情况进行替换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值