Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

html - Fabric.js clipping individual objects dynamically

I am dynamically adding a rect element to crop/clip (using clipTo) a selected element, It works perfect at the first time but when i add a second element to the canvas and begin to crop the second element the first element looses the crop/clip. Below is my code, there are 2 buttons 1 to start crop/clip (places a dynamic rect over the element to be cropped) second to do the cropping/clipping.

$('#crop').on('click', function (event) {


    var left = el.left - object.left;
    var top = el.top - object.top;

    left *= 1;
    top *= 1;

    var width = el.width * 1;
    var height = el.height * 1;

  object.clipTo = function (ctx) 
  {
        ctx.rect(-(el.width/2)+left, -(el.height/2)+top, parseInt(width*el.scaleX), parseInt(el.scaleY*height));
  }



    for(var i=0; i<$("#layers li").size();i++)
        {
            canvas.item(i).selectable= true;
        }
    disabled = true;

    canvas.remove(canvas.getActiveObject());
    lastActive = object;
    canvas.renderAll();



});

$('#startCrop').on('click',function(){

    canvas.remove(el);
    if(canvas.getActiveObject())
    {

    object=canvas.getActiveObject();    
    if (lastActive && lastActive !== object) {
        lastActive.clipTo = null;
    }



        el = new fabric.Rect
        ({
            fill: 'rgba(0,0,0,0.3)',
            originX: 'left',
            originY: 'top',
            stroke: '#ccc',
            strokeDashArray: [2, 2],
            opacity: 1,
            width: 1,
            height: 1,
            borderColor: '#36fd00',
            cornerColor: 'green',
            hasRotatingPoint:false
        });

        el.left=canvas.getActiveObject().left;
        selection_object_left=canvas.getActiveObject().left;
        selection_object_top=canvas.getActiveObject().top;
        el.top=canvas.getActiveObject().top;
        el.width=canvas.getActiveObject().width*canvas.getActiveObject().scaleX;
        el.height=canvas.getActiveObject().height*canvas.getActiveObject().scaleY;


        canvas.add(el);
        canvas.setActiveObject(el);
        for(var i=0; i<$("#layers li").size();i++)
            {
                canvas.item(i).selectable= false;
            }
    }

    else{
            alert("Please select an object or layer");
        }

});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Is this what you're looking for? http://jsfiddle.net/86bTc/4/

You set the lastActive.clipTo = null;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...