简体   繁体   English

canvas setDimension()打破了图形的拖动功能

[英]canvas setDimension() breaks drag functionality of figure

I am using draw2dtouch 6.1.66 version (latest version). 我使用的是draw2dtouch 6.1.66 version (最新版本)。 Initially width and height of canvas was 1000px and 800px respectively. 最初画布的宽度和高度分别为1000px800px On button click, I am changing the canvas width and height to 2170px and 902px respectively. 单击按钮,我将画布宽度和高度分别更改为2170px902px

Now I cannot drag rectangle figure beyond 1000px width-wise. 现在我无法将矩形图拖过1000px宽度方向。 It gets stuck to 1000px which was initial width. 它被卡在1000px这是初始宽度。

Note: I check my html, both canvas div and svg shows width:2170px and height:902px . 注意:我检查我的html,canvas div和svg都显示width:2170pxheight:902px

<div _ngcontent-awi-17="" draw2d-canvas="" id="canvas" style="height: 902px; cursor: default; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); width: 2170px;">

<svg height="902" version="1.1" width="2170" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="overflow: hidden; position: absolute; left: -0.828125px;">

Here is my code: 这是我的代码:

$("#canvas").css("height",902);
$("#canvas").css("width", 2170);
canvas.setDimension(new draw2d.geo.Rectangle(0, 0, 2170, 902));

Setting the width and height of a canvas using css, will result in scale like effect, because the underlying imageData array is not updated to the new dimensions. 使用css设置画布的widthheight将导致像效果一样的缩放,因为底层的imageData数组未更新为新的尺寸。

So if you need to scale your canvas, use the attributes of the canvas, like so: 因此,如果您需要缩放画布,请使用画布的属性,如下所示:

$('#canvas').attr({width: <width>, height: <height>});

Keep in mind that changing those values will clear the canvas and you need to redraw everything. 请记住,更改这些值将清除画布,您需要重绘所有内容。

It may be and old post, but I had the same issue using same version, and my solution might be useful for some other people coming back. 它可能是旧的帖子,但我使用相同版本时遇到了同样的问题,我的解决方案可能对其他人回来有用。

It seems when the canvas is created Canvas.regionDragDropConstraint is initialized with the original size of the canvas, restrincting the area in which objects can be dragged inside. 在创建画布时,似乎Canvas.regionDragDropConstraint使用画布的原始大小进行初始化,并重新绘制可以在其中拖动对象的区域。 I had to modify the method Canvas.setDimension, and added the update of the attribute, so when resized, it's updated in all the figures referencing the same object. 我不得不修改方法Canvas.setDimension,并添加了属性的更新,因此在调整大小时,它会在引用同一对象的所有图形中更新。

    /**
     * @method
     * Tells the canvas to resize. If you do not specific any parameters 
     * the canvas will attempt to determine the height and width by the enclosing bounding box 
     * of all elements and set the dimension accordingly. If you would like to set the dimension 
     * explicitly pass in an draw2d.geo.Rectangle or an object with <b>height</b> and <b>width</b> properties.
     * 
     * @since 4.4.0
     * @param {draw2d.geo.Rectangle} [dim] the dimension to set or null for autodetect
     */
    setDimension: function(dim, height)
    {
        if (typeof dim === "undefined"){
            var widths  = this.getFigures().clone().map(function(f){ return f.getAbsoluteX()+f.getWidth();});
            var heights = this.getFigures().clone().map(function(f){ return f.getAbsoluteY()+f.getHeight();});
            this.initialHeight = Math.max.apply(Math,heights.asArray());
            this.initialWidth  = Math.max.apply(Math,widths.asArray());
        }
        else if(dim instanceof draw2d.geo.Rectangle){
            this.initialWidth  = dim.w;
            this.initialHeight = dim.h;
        }
        else if(typeof dim.width ==="number" && typeof dim.height ==="number"){
            this.initialWidth  = dim.width;
            this.initialHeight = dim.height;
        }
        else if(typeof dim ==="number" && typeof height ==="number"){
            this.initialWidth  = dim;
            this.initialHeight = height;
        }
        this.html.css({"width":this.initialWidth+"px", "height":this.initialHeight+"px"});
        this.paper.setSize(this.initialWidth, this.initialHeight);
        this.setZoom(this.zoomFactor, false);

        // MODIFICATION START TO CORRECT DRAG AND DROP PROBLEM
        // Add modification to the Region Drap and Drop Policy, so it does not restricts objects movements on resize.
        this.regionDragDropConstraint.constRect.w = this.getWidth();
        this.regionDragDropConstraint.constRect.h = this.getHeight();
        // MODIFICATION END

        return this;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM