Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9168653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:40:35+00:00 2026-06-17T15:40:35+00:00

I have some functions to draw rectangles on a canvas element. When the element

  • 0

I have some functions to draw rectangles on a canvas element. When the element is drawn, I want to be able to resize it by dragging its corners.

var canvas = document.getElementById('canvas'),
  ctx = canvas.getContext('2d'),
  rect = {},
  drag = false;

function init() {
  canvas.addEventListener('mousedown', mouseDown, false);
  canvas.addEventListener('mouseup', mouseUp, false);
  canvas.addEventListener('mousemove', mouseMove, false);
}

function mouseDown(e) {
  rect.startX = e.pageX - this.offsetLeft;
  rect.startY = e.pageY - this.offsetTop;
  drag = true;
}

function mouseUp() {
  drag = false;
}

function mouseMove(e) {
  if (drag) {
    rect.w = (e.pageX - this.offsetLeft) - rect.startX;
    rect.h = (e.pageY - this.offsetTop) - rect.startY;
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    draw();
  }
}

function draw() {
  ctx.fillRect(rect.startX, rect.startY, rect.w, rect.h);
}

init();
<canvas id="canvas" width="500" height="500"></canvas>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:40:37+00:00Added an answer on June 17, 2026 at 3:40 pm

    Make sure to use some kind of threshold value to check for dragging on corners, use a closeEnough variable to hold this threshold then check corners by seeing if the absolute value of the difference between corner point and mouse point is less than the threshold. Apart from that, it is just a lot of cases to go through. Here is a jsFiddle of it

    var canvas = document.getElementById('canvas'),
        ctx = canvas.getContext('2d'),
        rect = {},
        drag = false,
        mouseX, 
        mouseY,
        closeEnough = 10,
        dragTL=dragBL=dragTR=dragBR=false;
    
    function init() {
      canvas.addEventListener('mousedown', mouseDown, false);
      canvas.addEventListener('mouseup', mouseUp, false);
      canvas.addEventListener('mousemove', mouseMove, false);
    }
    
    function mouseDown(e) {
      mouseX = e.pageX - this.offsetLeft;
      mouseY = e.pageY - this.offsetTop;
    
      // if there isn't a rect yet
      if(rect.w === undefined){
        rect.startX = mouseY;
        rect.startY = mouseX;
        dragBR = true;
      }
    
      // if there is, check which corner
      //   (if any) was clicked
      //
      // 4 cases:
      // 1. top left
      else if( checkCloseEnough(mouseX, rect.startX) && checkCloseEnough(mouseY, rect.startY) ){
        dragTL = true;
      }
      // 2. top right
      else if( checkCloseEnough(mouseX, rect.startX+rect.w) && checkCloseEnough(mouseY, rect.startY) ){
        dragTR = true;
    
      }
      // 3. bottom left
      else if( checkCloseEnough(mouseX, rect.startX) && checkCloseEnough(mouseY, rect.startY+rect.h) ){
        dragBL = true;
    
      }
      // 4. bottom right
      else if( checkCloseEnough(mouseX, rect.startX+rect.w) && checkCloseEnough(mouseY, rect.startY+rect.h) ){
        dragBR = true;
    
      }
      // (5.) none of them
      else {
        // handle not resizing
      }
    
      ctx.clearRect(0,0,canvas.width,canvas.height);
      draw();
    
    }
    
    function checkCloseEnough(p1, p2){
      return Math.abs(p1-p2)<closeEnough;
    }
    function mouseUp() {
      dragTL = dragTR = dragBL = dragBR = false;
    }
    
    function mouseMove(e) {
      mouseX = e.pageX - this.offsetLeft;
      mouseY = e.pageY - this.offsetTop;
      if(dragTL){
        rect.w += rect.startX-mouseX;
        rect.h += rect.startY-mouseY;
        rect.startX = mouseX;
        rect.startY = mouseY;
      } else if(dragTR) {
        rect.w = Math.abs(rect.startX-mouseX);
        rect.h += rect.startY-mouseY;
        rect.startY = mouseY;
      } else if(dragBL) {
        rect.w += rect.startX-mouseX;
        rect.h = Math.abs(rect.startY-mouseY);
        rect.startX = mouseX;  
      } else if(dragBR) {
        rect.w = Math.abs(rect.startX-mouseX);
        rect.h = Math.abs(rect.startY-mouseY);
      }
        ctx.clearRect(0,0,canvas.width,canvas.height);
        draw();
    }
    
    function draw() {
      ctx.fillRect(rect.startX, rect.startY, rect.w, rect.h);
    }
    
    init();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am playing with the HTML5 canvas element, using JS to draw some rectangles
I have some functions that use opengl to draw lines on the screen (health
i have some functions(charfreq,wordfreq,charcount,wordcount,parerror) and i want to use it in dataStructure with the
I have winform with a TextBox and I want to draw some GDI graphics
I have some concerns related to the fact of testing some functions containing the
In the process of changing some code, I have spilt some functions into multiple
I have written some general functions to convert between decimal and any other base-n
In a RIA Domain service I have added some utility functions. For instance we
In an iPhone application, I have some plain C functions. Is it possible to
I have the following code: Some functions: A::A(int i_a) {cout<<int Ctor\n;} //conversion constructor void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.