利用cocos creator制作函数曲线

利用Cocos Creator制作函数曲线—正弦函数

原理

摘自百度百科

代码


cc.Class({
    extends: cc.Component,

    properties: {
        lineNode: cc.Node,
        //振幅
        amplitude: 1,
        //周期
        cycle: 1,
        //波形与x轴位置关系或横向移动距离
        offsetX: 0,
        offsetY: 0,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.drawLine();
    },

    drawLine () {
        let line = this.lineNode.getComponent(cc.Graphics);
        //设置线条宽度
        line.lineWidth = 1;
        //设置线条颜色
        line.strokeColor = cc.Color.GREEN;
        for (let i = 0; i <= cc.winSize.width; i += 1) {
            let posY = this.createPoint(i);
            if (i === 0) {
                //创建线条起始点
                line.moveTo(0, posY);
            }
            else {
                //与前一段线条相连
                line.lineTo(i, posY);
            }
        }
        line.stroke();        
    },

    createPoint (x) {
    	//将x转换为弧度
        let result = this.amplitude * Math.sin(this.cycle * x * Math.PI / 180 + this.offsetX) + this.offsetY;
        return result;
    },

    start () {

    },

    // update (dt) {},
});

节点摆放及参数调整

节点摆放及参数调整

效果

在这里插入图片描述

拓展

对代码稍加改造可以模拟心电图玩


cc.Class({
    extends: cc.Component,

    properties: {
        //振幅
        amplitude: 1,
        //周期
        cycle: 1,
        //波形与x轴位置关系或横向移动距离
        offsetX: 0,
        offsetY: 0,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.drawLine();
    },

    drawLine () {
        this.lineNode = new cc.Node();
        this.lineNode.parent = this.node;
        this.lineNode.position = cc.v2(-cc.winSize.width / 2, 0);
        let line = this.lineNode.addComponent(cc.Graphics);
        //设置线条宽度
        line.lineWidth = 1;
        //设置线条颜色
        line.strokeColor = cc.Color.GREEN;
        this.count = 0;
        this.schedule(() => {
            line.clear();
            for (let i = 0; i <= cc.winSize.width; i += 1) {
                let posY = this.createPoint(i + this.count);
                if (i === 0) {
                    //创建线条起始点
                    line.moveTo(0, posY);
                }
                else {
                    //与前一段线条相连
                    line.lineTo(i, posY);
                }
                console.log(posY);
            }
            this.count-=20;
            line.stroke();
        }, 1);

    },

    createPoint (x) {
        let result = this.amplitude * Math.sin(this.cycle * x * Math.PI / 180 + this.offsetX) + this.offsetY;
        return result;
    },

    start () {

    },

    // update (dt) {},
});

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值