避免在画布fabricjs的剪裁区域溢出 [英] Avoid overflow in clip area from canvas fabricjs

查看:2259
本文介绍了避免在画布fabricjs的剪裁区域溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用剪辑在画布上定义绘图区域。当用户在定义的区域中移动内部对象时,元素不可见,但是当我将画布保存为图像他们进来的图片。如何避免溢出?或限制元素移动

i am using clip to define drawing area on canvas . when user moves inside object in out defined area then element are not visible but when i save canvas as image they are coming in picture . how can i avoid overflowing ? or restric elements move ??

页面屏幕截图:

保存的图片:

推荐答案

这可以通过两种方式实现:

This can be done using two ways:

1)在Rectangle中剪裁画布区域

1) Clipping the the canvas area within a Rectangle

canvas.clipTo = function(ctx) {                     
    ctx.beginPath();
    var rect = new fabric.Rect({
            fill: 'red',
            opacity: 0,
            left: 0,
            top: 0,
            width: canvas.width,
            height: canvas.height
    });
    ctx.strokeStyle = 'black';
    rect.render(ctx);
    ctx.stroke();
}

2)将对象限制为矩形边界

2) Restrict objects to a rectangular boundary

constrainToBounds = function (activeObject) {
        if(activeObject)
        {
            var angle = activeObject.getAngle() * Math.PI/180,
            aspectRatio = activeObject.width/activeObject.height,
            boundWidth = getBoundWidth(activeObject),
            boundHeight = getBoundHeight(activeObject);
            if(boundWidth > bounds.width) {
                boundWidth = bounds.width;
                var targetWidth = aspectRatio * boundWidth/(aspectRatio * Math.abs(Math.cos(angle)) + Math.abs(Math.sin(angle)));
                    activeObject.setScaleX(targetWidth/activeObject.width);
                    activeObject.setScaleY(targetWidth/activeObject.width);
                    boundHeight = getBoundHeight(activeObject);
                }
                if(boundHeight > bounds.height) {
                    boundHeight = bounds.height;
                    var targetHeight = boundHeight/(aspectRatio * Math.abs(Math.sin(angle)) + Math.abs(Math.cos(angle)));
                    activeObject.setScaleX(targetHeight/activeObject.height);
                    activeObject.setScaleY(targetHeight/activeObject.height);
                    boundWidth = getBoundWidth(activeObject);
                }
                //Check constraints
                if(activeObject.getLeft() < bounds.x + boundWidth/2)
                    activeObject.setLeft(bounds.x + boundWidth/2);
                if(activeObject.getLeft() > (bounds.x + bounds.width - boundWidth/2))
                    activeObject.setLeft(bounds.x + bounds.width - boundWidth/2);
                if(activeObject.getTop() < bounds.y + boundHeight/2)
                    activeObject.setTop(bounds.y + boundHeight/2);
                if(activeObject.getTop() > (bounds.y + bounds.height - boundHeight/2))
                    activeObject.setTop(bounds.y + bounds.height - boundHeight/2);
            }
    }



我们在tshirt应用程序中使用了这些 - a href =http://www.riaxe.com/html5-tshirt-designer-application/ =nofollow> http://www.riaxe.com/html5-tshirt-designer-application/

希望这有助于:)

这篇关于避免在画布fabricjs的剪裁区域溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆