babylon.js封装使用渐变色

babylon.js里使用渐变色

  // 渐变色
        class GradientRectangle extends BABYLON.GUI.Rectangle {
            constructor(name) {
                super(name);
                this._gradientStart = "black";
                this._gradientEnd = "white"
                this._gradientVertical = true;
            }
            set gradientStart(value) {
                this._gradientStart = value;
            }
            get gradientStart() {
                return this._gradientStart;
            }
            set gradientEnd(value) {
                this._gradientEnd = value;
            }
            get gradientEnd() {
                return this._gradientEnd;
            }
            set gradientVertical(value) {
                this._gradientVertical = value ? true : false;
            }
            get gradientVertical() {
                return this._gradientVertical;
            }
            _localDraw(context/*: CanvasRenderingContext2D*/) {
                context.save();

                if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
                    context.shadowColor = this.shadowColor;
                    context.shadowBlur = this.shadowBlur;
                    context.shadowOffsetX = this.shadowOffsetX;
                    context.shadowOffsetY = this.shadowOffsetY;
                }
                if (this._gradientStart && this._gradientEnd) {
                    if (this._gradientVertical) {
                        this._gradient = context.createLinearGradient(0, this._currentMeasure.top, 0, this._currentMeasure.top + this._currentMeasure.height);
                    }
                    else {
                        this._gradient = context.createLinearGradient(this._currentMeasure.left, 0, this._currentMeasure.left + this._currentMeasure.width, 0);
                    }
                    this._gradient.addColorStop(0, this._gradientStart);
                    this._gradient.addColorStop(1, this._gradientEnd);

                    context.fillStyle = this._gradient;

                    if (this._cornerRadius) {
                        this._drawRoundedRect(context, this._thickness / 10);
                        context.fill();
                    } else {
                        context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
                    }
                }

                if (this._thickness) {
                    if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
                        context.shadowBlur = 0;
                        context.shadowOffsetX = 0;
                        context.shadowOffsetY = 0;
                    }

                    if (this.color) {
                        context.strokeStyle = this.color;
                    }
                    context.lineWidth = this._thickness;

                    if (this._cornerRadius) {
                        this._drawRoundedRect(context, this._thickness / 2);
                        context.stroke();
                    } else {
                        context.strokeRect(this._currentMeasure.left + this._thickness / 2, this._currentMeasure.top + this._thickness / 2,
                            this._currentMeasure.width - this._thickness, this._currentMeasure.height - this._thickness);
                    }
                }

                context.restore();
            }
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值