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 6933113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:47:26+00:00 2026-05-27T11:47:26+00:00

This might be more a geometry related question, but I’m trying to constrain a

  • 0

This might be more a geometry related question, but I’m trying to constrain a controller within an area of a circle. I know I have to touch the Math.sin() and Math.cos() methods, but my attemps so far have been fruitless so far.

enter image description here

Here is the jsfiddle:
So far I’ve been able to constrain it to an invisible square. http://jsfiddle.net/maGVK/

  • 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-05-27T11:47:27+00:00Added an answer on May 27, 2026 at 11:47 am

    So I finally was able to complete this with a bit of everyone’s help.

    var pointerEl = document.getElementById("pointer");
    var canvasEl = document.getElementById("canvas");
    var canvas = {
        width: canvasEl.offsetWidth,
        height: canvasEl.offsetHeight,
        top: canvasEl.offsetTop,
        left: canvasEl.offsetLeft
    };
    canvas.center = [canvas.left + canvas.width / 2, canvas.top + canvas.height / 2];
    canvas.radius = canvas.width / 2;
    
    
    window.onmousemove = function(e) {
        var result = limit(e.x, e.y);
            pointer.style.left = result.x + "px";
            pointer.style.top = result.y + "px";
    }
    
    function limit(x, y) {
        var dist = distance([x, y], canvas.center);
        if (dist <= canvas.radius) {
            return {x: x, y: y};
        } 
        else {
            x = x - canvas.center[0];
            y = y - canvas.center[1];
            var radians = Math.atan2(y, x)
               return {
                   x: Math.cos(radians) * canvas.radius + canvas.center[0],
                   y: Math.sin(radians) * canvas.radius + canvas.center[1]
               }
            } 
        }
    
    function distance(dot1, dot2) {
        var x1 = dot1[0],
            y1 = dot1[1],
            x2 = dot2[0],
            y2 = dot2[1];
        return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
    }
    

    You can see the result here:

    http://jsfiddle.net/7Asn6/

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

Sidebar

Related Questions

OK. This might be more of a math question but here goes. I have
This might be a little bit odd question, but Im trying to figure out
This might be more of a CYGWIN question than a Nodejs but here goes.
This might be more Math related than C#, but I need a C# solution
I folks. This might be more of a design question but I thought I'd
This might be more of a subjective question, but is it generally best practice
I know this might be more appropriate at Ask Different, but as I tried
This might be more of a philosophical question, but is there a compelling reason
This might be basic question but how do I create a list of lists
This might be a simple question, but i can't seem to grasp it. I

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.