画布旋转 - 织物js

时间:2018-05-29 11:48:52

标签: canvas fabricjs

我创建了一个canvas元素,并使用fabric js完成了旋转功能。一切都很好。但我看到一组虚线仅指向Windows 7的Chrome浏览器中的画布中心,在某些设备中也是如此,而不是在所有Windows 7和Chrome组合中。请参阅以下链接" canvas_rotate"。有没有人遇到这个问题。? canvas_rotate

fabric.loadSVGFromString(svgStr, function(objects, options) {
        console.log("fabric.loadSVGFromString",svgStr)
            var obj = fabric.util.groupSVGElements(objects, options);
            obj.scaleToHeight(70) // Scales it down to some small size
            .scaleToWidth(70)
            .setTop(_t) // Centers it (no s**t, Sherlock)
            .setLeft(_l)

            // Centers it (no s**t, Sherlock)
            .setCoords();
            obj.set({lockUniScaling: true, shapeType:shapeType, cornerColor:'rgba(71,71,71,1)',  cornerSize: 17, cornerStyle : 'circle', transparentCorners: false, borderColor: 'rgba(71,71,71,1)', padding: -1});
            /*Rotation Icon*/
            obj.drawControls = function (ctx) {
                if (!this.hasControls) {return this;}
                var wh = this._calculateCurrentDimensions(),
                width = wh.x,
                height = wh.y,
                scaleOffset = this.cornerSize,
                left = -(width + scaleOffset) / 2,
                top = -(height + scaleOffset) / 2,
                methodName = this.transparentCorners ? 'stroke' : 'fill';

                ctx.save();
                ctx.strokeStyle = ctx.fillStyle = this.cornerColor;
                if (!this.transparentCorners) {
                    ctx.strokeStyle = this.cornerStrokeColor;
                }
                this._setLineDash(ctx, this.cornerDashArray, null);
                // top-left
                this._drawControl('tl', ctx, methodName,
                left,
                top);

                // top-right
                this._drawControl('tr', ctx, methodName,
                left + width,
                top);

                // bottom-left
                this._drawControl('bl', ctx, methodName,
                left,
                top + height);

                // bottom-right
                this._drawControl('br', ctx, methodName,
                left + width,
                top + height);
                if (this.hasRotatingPoint) {
                    var rotate = new Image(), rotateLeft, rotateTop;
                    rotate.src = 'content/assets/image/icon_rotate_canvas.png';
                    rotateLeft = left + width / 2;
                    rotateTop = top - this.rotatingPointOffset;
                    ctx.drawImage(rotate, rotateLeft, rotateTop, 17, 17);
                }
                ctx.restore();
                return this;
            }
            /*Rotation Icon Ends */
            canvasWrap.add(obj);

            canvasWrap.discardActiveObject();
            canvasWrap.setActiveObject(obj);
            canvasWrap.selection = false;
            canvasWrap.renderAll();
        });

我也尝试了一个简单的例子,但面临同样的问题。虚线指向画布中心点的中心。

screenshot of simple_page_rotate

<html>
<head>
    <title>Page Title</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.22/fabric.min.js"></script>
    <script src="custom.js"></script>
</head>
<body>
    <canvas id="c" width="700px" height="500px" style="outline:1px solid red"></canvas>
    <!-- source: http://commons.wikimedia.org/wiki/File:Bort_znamia_3.svg -->
    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
         width="57.08px" height="74.223px" viewBox="0 0 57.08 74.223" enable-background="new 0 0 57.08 74.223" xml:space="preserve">
    <path d="M6.572,43.413l0.505,7.576l50.003,0.505l-0.505-7.071L6.572,43.413z"/>
    <path d="M7.077,54.02l0.505,7.576l49.498,1.01l-0.505-7.071L7.077,54.02z"/>
    <path d="M7.582,65.131l0.505,8.586l48.993,0.505v-6.566L7.582,65.131z"/>
    </svg>
</body>

setTimeout(function(){ 

var svgEl = document.body.getElementsByTagName('svg')[0];
var serializer = new XMLSerializer();
var svgStr = serializer.serializeToString(svgEl);

var canvas = new fabric.Canvas('c');

var path = fabric.loadSVGFromString(svgStr,function(objects, options) {
  var obj = fabric.util.groupSVGElements(objects, options);
  obj.set({ left: 0, top: 0 })
    .setCoords();

  canvas.add(obj).renderAll();
});}, 0);

提示链接供您参考simple page for rotate

1 个答案:

答案 0 :(得分:0)

使用2.x版本的结构js解决了这个问题。请参阅下文。 https://github.com/fabricjs/fabric.js/issues/5012