
const FitType = {
    Scale: 0,//等比缩放，一般用于背景图
    FullScreen: 1,//铺满全屏，拉伸
    FitMode: 2,//更改canvas适配策略，只能挂在canvas上
};
const designSize = cc.size(1280, 720);
cc.Class({
    extends: cc.Component,

    properties: {
        FitType: {
            type: cc.Enum(FitType),
            default: 0
        },
    },

    onLoad: function () {
        if (this.FitType == FitType.Scale) {
            let winSize = cc.view.getFrameSize();
            let scale1 = designSize.width / designSize.height;
            let scale2 = winSize.width / winSize.height;
            let scale = scale2 / scale1;
            if (scale > 1) {
                this.node.scale = scale;
            } else {
                this.node.scale = 1 / scale;
            }
        } else if (this.FitType == FitType.FullScreen) {
            let visiblesize = cc.view.getVisibleSize();
            this.node.width = visiblesize.width;
            this.node.height = visiblesize.height;
        } else {
            let wsize = cc.view.getFrameSize();
            let scaleW = wsize.width / designSize.width;
            let scaleH = wsize.height / designSize.height;

            let pCanvas = this.node.getComponent(cc.Canvas);
            if (pCanvas) {
                if (scaleW > scaleH) { //更宽了要fitHeight, 否则height就留黑边了;
                    pCanvas.fitHeight = true;
                    pCanvas.fitWidth = false;
                } else { //更高;
                    pCanvas.fitHeight = false;
                    pCanvas.fitWidth = true;
                }
            } else {
                console.error("fitmode模式只能用在canvas节点上");
            }
        }
    },
});
