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

  • SEARCH
  • Home
  • 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 8055729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:29:04+00:00 2026-06-05T08:29:04+00:00

First off, the square draws fine and it does work, but there are still

  • 0

First off, the square draws fine and it does work, but there are still a couple of issues. I have two problems… the first one is that when I draw the square, it’s unable to collapse back on itself to a single point (and get smaller) if you made your square too big. The second problems is that when I draw the square, it shows up about a centimeter below my finger, rather than directly underneath it.

Can anyone help me out with these problems?


Here’s the code:

JAVASCRIPT

// "Draw Rectangle" Button
function rect(){
var canvas = document.getElementById('canvasSignature'), ctx = canvas.getContext('2d'), rect = {}, drag = false;

function init() {
  canvas.addEventListener("touchstart", touchHandler, false);
  canvas.addEventListener("touchmove", touchHandler, false);
  canvas.addEventListener("touchend", touchHandler, false);
}


function touchHandler(event) {
  if (event.targetTouches.length == 1) { //one finger touche
    var touch = event.targetTouches[0];

    if (event.type == "touchstart") {
      rect.startX = touch.pageX;
      rect.startY = touch.pageY;
      drag = true;
    } else if (event.type == "touchmove") {
      if (drag) {
        rect.w = touch.pageX - rect.startX;
        rect.h = touch.pageY - rect.startY ;
        draw();
      }
    } else if (event.type == "touchend" || event.type == "touchcancel") {
      drag = false;
    }
  }
}

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

init();
}

Thanks,
Wardenclyffe

  • 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-05T08:29:06+00:00Added an answer on June 5, 2026 at 8:29 am

    The first problem is caused by you not clearing the canvas. The previous data will remain in the canvas unless you clear it, and so the previously-drawn rectangle will remain. You can clear the canvas like this:

    ctx.clearRect(0, 0, canvas.width, canvas.height);
    

    I assume your second problem is that you’re using the page-relative X and Y and expecting it to be the canvas-relative X and Y. This is not the case unless the canvas’s upper left hand corner is the same as the document’s upper left hand corner. You can transform a page X and Y into a canvas X and Y in newer browsers like this:

    var clientRect = canvas.getBoundingClientRect();
    var canvasX = touch.pageX - clientRect.left - window.pageXOffset,
        canvasY = touch.pageY - clientRect.top  - window.pageYOffset;
    

    This will need further tweaking if the canvas is in an element with a scrollbar.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, I am not a DBA, but I do work in an environment
First off: Everything works , but I would like to fine tune this a
First off, let me clarify the platforms we are using. We have an ASP.NET
First off, I'm aware this is a bad practice and I have answered many
First off, I did look on google and stackoverflow, but nothing seem to really
First off, please accept my apologies if this question is basic, I mainly have
First off I apologize if there is another post out there that answers this,
First off I have an app that provides the user information about the amount
First off this is in Microsoft Office 2010. I'm comparing two different worksheets each
First off, the jQuery datepicker works fine with classes when doing a fresh page

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.