gpt4 book ai didi

button - 如何使用fabric.Textbox 类覆盖在fabric js 中创建自定义类?

转载 作者:行者123 更新时间:2023-12-03 19:15:08 29 4
gpt4 key购买 nike

  • 我正在使用 FabricJS 版本:3.6.3
  • 我想创建新的 FabricJS 类:Button
  • 这样我就扩展了一个名为 Textbox 的类来自fabric js,这将抽奖 Rectangle后面 Text它看起来像一个按钮。
  • 但问题是,我无法设置 height到那个 Button因为 height不允许在 Texbox目的。
  • 我要设置HeightWidthButton目的。 Width由于 Textbox 工作正常.如果宽度保持小于文本宽度,它也会扭曲文本,并且可以通过双击它进行编辑。但唯一的问题是不能设置Heightobject
  • 应该是 Text vertically centerHeight是增加。

  • 简而言之,我想使用对象定制在 fabric js 中实现这种功能。

    预期输出:

    enter image description here

    但是 实际输出:

    enter image description here

    这是我创建按钮的代码:

    // fabric js custom button class
    (function (fabric) {
    "use strict";

    // var fabric = global.fabric || (global.fabric = {});

    fabric.Button = fabric.util.createClass(fabric.Textbox, {
    type: "button",
    stateProperties: fabric.Object.prototype.stateProperties.concat(
    "buttonRx",
    "buttonRy",
    "buttonFill",
    "buttonPadding",
    "buttonStrokeColor",
    "buttonStrokeWidth"
    ),
    buttonRx: 0,
    buttonRy: 0,
    buttonFill: "#ffffff00",
    buttonPadding: 0,
    buttonHeight: 0,
    buttonWidth: 0,
    textAlign: "center",
    buttonStrokeColor: "#000000",
    buttonStrokeWidth: 0,
    _dimensionAffectingProps: fabric.Text.prototype._dimensionAffectingProps.concat(
    "width",
    "fontSize"
    ),
    cacheProperties: fabric.Object.prototype.cacheProperties.concat(
    "buttonRx",
    "buttonRy",
    "buttonFill",
    "buttonPadding",
    "buttonStrokeColor",
    "buttonStrokeWidth"
    ),
    initialize: function (text, options) {
    this.text = text;
    this.callSuper("initialize", text, options);
    /* this.on("scaling", function () {
    console.log('scaling', this.getScaledHeight());

    this.set({
    height: this.getScaledHeight(),
    scaleY: 1,
    });
    }); */

    this._initRxRy();
    },

    _initRxRy: function () {
    if (this.buttonRx && !this.buttonRy) {
    this.buttonRy = this.buttonRx;
    } else if (this.buttonRy && !this.buttonRx) {
    this.buttonRx = this.buttonRy;
    }
    },
    /* _setCenter(){

    }, */
    _render: function (ctx) {
    // 1x1 case (used in spray brush) optimization was removed because
    // with caching and higher zoom level this makes more damage than help
    // this.width = this.width * this.scaleX;
    // this.height = this.height * this.scaleY;
    // (this.scaleX = 1), (this.scaleY = 1);
    var rx = this.buttonRx ? Math.min(this.buttonRx, this.width / 2) : 0,
    ry = this.buttonRy ? Math.min(this.buttonRy, this.height / 2) : 0,
    w = this.width + this.buttonPadding,
    h = this.height + this.buttonPadding,
    x = -this.width / 2 - this.buttonPadding / 2,
    y = -this.height / 2 - this.buttonPadding / 2,
    isRounded = rx !== 0 || ry !== 0,
    /* "magic number" for bezier approximations of arcs (http://itc.ktu.lt/itc354/Riskus354.pdf) */
    k = 1 - 0.5522847498;
    ctx.beginPath();

    ctx.moveTo(x + rx, y);

    ctx.lineTo(x + w - rx, y);
    isRounded &&
    ctx.bezierCurveTo(x + w - k * rx, y, x + w, y + k * ry, x + w, y + ry);

    ctx.lineTo(x + w, y + h - ry);
    isRounded &&
    ctx.bezierCurveTo(
    x + w,
    y + h - k * ry,
    x + w - k * rx,
    y + h,
    x + w - rx,
    y + h
    );

    ctx.lineTo(x + rx, y + h);
    isRounded &&
    ctx.bezierCurveTo(x + k * rx, y + h, x, y + h - k * ry, x, y + h - ry);

    ctx.lineTo(x, y + ry);
    isRounded && ctx.bezierCurveTo(x, y + k * ry, x + k * rx, y, x + rx, y);

    ctx.closePath();
    ctx.save();
    if (this.buttonFill) {
    ctx.fillStyle = this.buttonFill;
    if (this.fillRule === "evenodd") {
    ctx.fill("evenodd");
    } else {
    ctx.fill();
    }
    }
    if (this.buttonStrokeWidth > 0) {
    if (this.strokeUniform) {
    ctx.scale(1 / this.scaleX, 1 / this.scaleY);
    }
    if (this.shadow && !this.shadow.affectStroke) {
    this._removeShadow(ctx);
    }
    if (this.buttonStrokeColor) {
    ctx.lineWidth = this.buttonStrokeWidth;
    ctx.strokeStyle = this.buttonStrokeColor;
    ctx.stroke();
    } else {
    ctx.lineWidth = this.buttonStrokeWidth;
    ctx.stroke();
    }
    }
    ctx.restore();

    this.clearContextTop();
    this._clearCache();
    this.height = this.calcTextHeight();
    this.saveState({ propertySet: "_dimensionAffectingProps" });
    // this._renderPaintInOrder(ctx);

    this._setTextStyles(ctx);
    this._renderTextLinesBackground(ctx);
    this._renderTextDecoration(ctx, "underline");
    this._renderText(ctx);
    this._renderTextDecoration(ctx, "overline");
    this._renderTextDecoration(ctx, "linethrough");
    this.initDimensions();
    // this.callSuper('render', ctx);
    },
    toObject: function (propertiesToInclude) {
    return this.callSuper(
    "toObject",
    [
    "buttonRx",
    "buttonRy",
    "buttonFill",
    "buttonPadding",
    "buttonStrokeColor",
    "buttonStrokeWidth",
    "objectCaching",
    ].concat(propertiesToInclude)
    );
    },
    });

    fabric.Button.fromObject = function (object, callback) {
    return fabric.Object._fromObject("Button", object, callback, "text");
    };
    })(fabric);

    // fabric js class finish here


    var canvas = [];
    var cotainer = document.getElementById("canvas-container");
    for (let i = 0; i < 1; i++) {
    var width = 500,
    height = 500;
    var canvasEl = document.createElement("canvas");
    canvasEl.id = "canvas-" + i;
    cotainer.append(canvasEl);
    var fabCanvas = new fabric.Canvas(canvasEl, {});
    fabCanvas.setHeight(height);
    fabCanvas.setWidth(width);
    canvas.push(fabCanvas);
    }

    canvas.forEach((c) => {
    var button = new fabric.Button("Click Me", {
    text: "Click Me",
    buttonStrokeColor: "#f00",
    buttonStrokeWidth: 2,
    width: 110,
    fill: "#f00",
    fontSize: 50,
    width: 400,
    buttonFill: "#42A5F5",
    buttonRx: 15,
    buttonRy: 15,
    objectCaching: false,
    fontFamily: "verdana",
    });
    c.add(button);
    c.renderAll();
    });
    canvas{
    border: 1px solid black
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.js"></script>
    <div id="canvas-container">
    </div>

    最佳答案

    解决方案是在缩放按钮对象时将按钮文本的 scaleX 和 scaleY 设置为 1,并将文本的字体大小设置为其比例。

    关于button - 如何使用fabric.Textbox 类覆盖在fabric js 中创建自定义类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61058890/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com