2

I'm trying to make an editable textbox like those in powerpoint and other presentation editors.

This is my feeble attempt.

I'm having trouble with the initial rescaling. To be concise, the first instant when I adjust the drag handle, the box does not scale to the correct height and width.

Any idea why is that so?

element.find('span').bind('mousedown', function(event) {
                console.log('resizing...');
                event.preventDefault();
                var domElement = element[0];

                var rect = domElement.getBoundingClientRect();
                startX = rect.left;
                startY = rect.bottom;
                console.log(startX + " " + startY);

                $document.bind('mousemove', resize);
                $document.bind('mouseup', mouseup2);
                event.cancelBubble = true;
            });


function resize(event) {

                var height = event.screenY - startY;
                var width = event.screenX - startX;
                scope.$apply(function() {
                    scope.tb.height = height + 'px';
                    scope.tb.width = width + 'px';
                });
            }

1 Answer 1

2

I updated your fiddle implementing this:

  1. tbW and tbH hold the current box size (inital 200px x 100px):

    var tbW = 200,
        tbH = 100;
    
    scope.tb.width = tbW + 'px',
    scope.tb.height = tbH + 'px';
    
  2. On mousedown startX and startY are assigned to the current mouse postion

  3. In the resize function the delta of the mouse movement is calculated and added to the width and height of your tbox:

    tbH += event.screenY - startY;
    startY = event.screenY;
    tbW += event.screenX - startX;
    startX = event.screenX;
    scope.$apply(function () {
      scope.tb.height = tbH + 'px';
      scope.tb.width = tbW + 'px';
    });
    
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant! Apologize for the late response. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.