【问题标题】:How to know the orientation of the tip of the arrow with fabric.js?如何用fabric.js知道箭头尖端的方向?
【发布时间】:2018-04-03 21:33:12
【问题描述】:

这是创建箭头的代码:

在开始或对箭头进行一些变换时,我需要检测箭头指向的方向

var Arrow = Fabric.util.createClass(Fabric.Line, Fabric.Observable, {
    type: 'arrow',
    positionArrow: 'rt',

    initialize: function (points, options) {
        this.callSuper('initialize', points, options);
        this.on('modified', function() {
            console.log(this.toObject())
        });
    },

    _render: function(ctx, noTransform) {
        this._setStrokeStyles(ctx);
        this._drawArrowHead(ctx);
        this.callSuper('_render', ctx, noTransform);
    },

    _drawArrowHead: function(ctx) {
        var p = this.calcLinePoints();
        var rot = Math.atan2(p.y2, p.x2);

        ctx.save();
        ctx.beginPath();
        ctx.translate(p.x1, p.y1);
        ctx.moveTo(0, 0);
        ctx.rotate(rot);
        ctx.lineTo(15, 7);
        ctx.lineTo(3, 0);
        ctx.lineTo(15, -7);
        ctx.lineTo(0, 0);
        ctx.stroke();
        ctx.closePath();
        ctx.restore();
    },

     _setStrokeStyles: function(ctx) {
         if (this.stroke) {
             ctx.lineWidth = this.strokeWidth;
             ctx.lineCap = this.strokeLineCap;
             ctx.lineJoin = this.strokeLineJoin;
             ctx.miterLimit = this.strokeMiterLimit;
             ctx.strokeStyle = this.stroke.toLive ? this.stroke.toLive(ctx) : this.stroke;
         }
     }
});

我已经在初始化中转换对象时添加了一个观察者,但是我找到了知道箭头指向哪个位置的方法。

【问题讨论】:

标签: javascript canvas fabricjs


【解决方案1】:

如果您知道应该将画布旋转多少来绘制箭头,您还应该能够重用该代码并添加对象角度来获得完整角度。

fabric.Arrow = fabric.util.createClass(fabric.Line, {
    type: 'arrow',
    positionArrow: 'rt',

    initialize: function (points, options) {
        this.callSuper('initialize', points, options);
        this.on('modified', function() {
          var p = this.calcLinePoints();
          var rot = Math.atan2(p.y2, p.x2);
          var rotDeg = fabric.util.radiansToDegrees(rot);
          console.log(this.angle + rotDeg)
        });
    },

    _render: function(ctx) {
        this.callSuper('_render', ctx);
        this._setStrokeStyles(ctx);
        this._drawArrowHead(ctx);
    },

    _drawArrowHead: function(ctx) {
        var p = this.calcLinePoints();
        var rot = Math.atan2(p.y2, p.x2);

        ctx.save();
        ctx.beginPath();
        ctx.translate(p.x1, p.y1);
        ctx.moveTo(0, 0);
        ctx.rotate(rot);
        ctx.lineTo(15, 7);
        ctx.lineTo(3, 0);
        ctx.lineTo(15, -7);
        ctx.lineTo(0, 0);
        ctx.stroke();
        ctx.closePath();
        ctx.restore();
    },

     _setStrokeStyles: function(ctx) {
         if (this.stroke) {
             ctx.lineWidth = this.strokeWidth;
             ctx.lineCap = this.strokeLineCap;
             ctx.lineJoin = this.strokeLineJoin;
             ctx.miterLimit = this.strokeMiterLimit;
             ctx.strokeStyle = this.stroke.toLive ? this.stroke.toLive(ctx) : this.stroke;
         }
     }
});

var canvas = new fabric.Canvas('c');
var arrow = new fabric.Arrow([10,10, 70,20]);
canvas.add(arrow);
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.2.3/fabric.js"></script>
<canvas id="c" width="300" height="300" ></canvas>

【讨论】:

  • 谢谢,虽然角度总是返回0的值,但我已经能够通过以下函数获得它:var matrix = this.calcTransformMatrix (), options = Fabric.util.qrDecompose (matrix) ; console.log (optionsObject.angle);但该值尚不准确,无法知道箭头的方向。
  • 我的小提琴不返回 0。它返回给你一个角度
  • 你的小提琴有链接吗?
  • 答案在这里,按运行 sn-p 并在控制台中查看已修改的对象
猜你喜欢
  • 2019-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-09
  • 2017-10-23
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多