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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:49:03+00:00 2026-06-15T21:49:03+00:00

http://jsfiddle.net/cs5Sg/11/ I want to do the scalable canvas. I created two circles (arcs) and

  • 0

http://jsfiddle.net/cs5Sg/11/

I want to do the scalable canvas. I created two circles (arcs) and one line, when you click on circle and move it, the line will follow and change position. The problem is when I added code for resize:

var canvas = document.getElementById('myCanvas'),
    context = canvas.getContext('2d'),
    radius = 12,
    p = null,
    point = {
        p1: { x:100, y:250 },
        p2: { x:400, y:100 }
    },
    moving = false;

window.addEventListener("resize", OnResizeCalled, false);

function OnResizeCalled() {
    var gameWidth = window.innerWidth;
    var gameHeight = window.innerHeight;
    var scaleToFitX = gameWidth / 800;
    var scaleToFitY = gameHeight / 480;

    var currentScreenRatio = gameWidth / gameHeight;
    var optimalRatio = Math.min(scaleToFitX, scaleToFitY);

    if (currentScreenRatio >= 1.77 && currentScreenRatio <= 1.79) {
        canvas.style.width = gameWidth + "px";
        canvas.style.height = gameHeight + "px";
    }
    else {
        canvas.style.width = 800 * optimalRatio + "px";
        canvas.style.height = 480 * optimalRatio + "px";
    }
}

function init() {
        return setInterval(draw, 10);
}

canvas.addEventListener('mousedown', function(e) {
    for (p in point) {
        var
            mouseX = e.clientX - 1,
            mouseY = e.clientY - 1,
            distance = Math.sqrt(Math.pow(mouseX - point[p].x, 2) + Math.pow(mouseY - point[p].y, 2));

        if (distance <= radius) {
            moving = p;
            break;
        }
    }
});

canvas.addEventListener('mouseup', function(e) {
    moving = false;
});

canvas.addEventListener('mousemove', function(e) {

    if(moving) {
        point[moving].x = e.clientX - 1; 
        point[moving].y = e.clientY - 1;
    }
});


function draw() {
    context.clearRect(0, 0, canvas.width, canvas.height);

    context.beginPath();
    context.moveTo(point.p1.x,point.p1.y);
    context.lineTo(point.p2.x,point.p2.y);

    context.closePath();

    context.fillStyle = '#8ED6FF';
    context.fill();
    context.stroke();

    for (p in point) {
        context.beginPath();
        context.arc(point[p].x,point[p].y,radius,0,2*Math.PI);
        context.fillStyle = 'red';
        context.fill();
        context.stroke();
    }
    context.closePath();
}

init();

The canvas is scalable, but the problem is with the points (circles). When you change the window size, they still have the same position on the canvas area, but the distance change (so the click option fails). How to fix that?

  • 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-15T21:49:04+00:00Added an answer on June 15, 2026 at 9:49 pm

    Live Demo

    Basically you just need a scaler value that takes into account the actual dimensions of the canvas, and the css dimensions like so

    var scaledX = canvas.width/ canvas.offsetWidth,
        scaledY = canvas.height/ canvas.offsetHeight;
    

    And then every time the window is resized you need to make sure to update the scaler values.

    function OnResizeCalled() {
    
        scaledX = canvas.width/ canvas.offsetWidth;
        scaledY = canvas.height/ canvas.offsetHeight;
    }
    

    To get the correct coords you need to multiply the clientX and clientY by the scaler in all of your mouse events

    canvas.addEventListener('mousedown', function(e) {
        for (p in point) {
            var 
                mouseX = e.clientX*scaledX,
                mouseY = e.clientY*scaledY,
                distance = Math.sqrt(Math.pow(mouseX - point[p].x, 2) + Math.pow(mouseY - point[p].y, 2));
    
            if (distance <= radius) {
                moving = p;
                break;
            }
        }
    });
    
    
    canvas.addEventListener('mousemove', function(e) {
        var mouseX = e.clientX*scaledX,
            mouseY = e.clientY*scaledY;
    
        if(moving) {
            point[moving].x = mouseX;
            point[moving].y = mouseY;
        }
    });
    

    Full code

    var canvas = document.getElementById('myCanvas'),
        context = canvas.getContext('2d'),
        radius = 12,
        p = null,
        point = {
            p1: { x:100, y:250},
            p2: { x:400, y:100}
        },
        moving = false,
        scaledX = canvas.width/ canvas.offsetWidth,
        scaledY = canvas.height/ canvas.offsetHeight;
    
    window.addEventListener("resize", OnResizeCalled, false);
    
    function OnResizeCalled() {
        var gameWidth = window.innerWidth;
        var gameHeight = window.innerHeight;
        var scaleToFitX = gameWidth / 800;
        var scaleToFitY = gameHeight / 480;
    
        var currentScreenRatio = gameWidth / gameHeight;
        var optimalRatio = Math.min(scaleToFitX, scaleToFitY);
    
        if (currentScreenRatio >= 1.77 && currentScreenRatio <= 1.79) {
            canvas.style.width = gameWidth + "px";
            canvas.style.height = gameHeight + "px";
        }
        else {
            canvas.style.width = 800 * optimalRatio + "px";
            canvas.style.height = 480 * optimalRatio + "px";
        }
    
        scaledX = canvas.width/ canvas.offsetWidth;
        scaledY = canvas.height/ canvas.offsetHeight;
    }
    
    function init() {
            return setInterval(draw, 10);
    }
    
    canvas.addEventListener('mousedown', function(e) {
        for (p in point) {
            var 
                mouseX = e.clientX*scaledX,
                mouseY = e.clientY*scaledY,
                distance = Math.sqrt(Math.pow(mouseX - point[p].x, 2) + Math.pow(mouseY - point[p].y, 2));
    
            if (distance <= radius) {
                moving = p;
                break;
            }
        }
    });
    
    canvas.addEventListener('mouseup', function(e) {
        moving = false;
    });
    
    canvas.addEventListener('mousemove', function(e) {
        var mouseX = e.clientX*scaledX,
            mouseY = e.clientY*scaledY;
    
        if(moving) {
            point[moving].x = mouseX; //1 is the border of your canvas
            point[moving].y = mouseY;
        }
    });
    
    
    function draw() {
        context.clearRect(0, 0, canvas.width, canvas.height);
    
        context.beginPath();
        context.moveTo(point.p1.x,point.p1.y);
        context.lineTo(point.p2.x,point.p2.y);
    
        context.closePath();
    
        context.fillStyle = '#8ED6FF';
        context.fill();
        context.stroke();
    
        for (p in point) {
            context.beginPath();
            context.arc(point[p].x,point[p].y,radius,0,2*Math.PI);
            context.fillStyle = 'red';
            context.fill();
            context.stroke();
        }
        context.closePath();
    }
    
    init();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://jsfiddle.net/cs5Sg/ As you can see, I'm trying to make two circles and one line.
http://jsfiddle.net/NV6Cr/1/ If you click Create Canvas, it'll draw the SVG text at the bottom
http://jsfiddle.net/herrturtur/ExWFH/ To try this: click the plus button, double click the newly created name
http://jsfiddle.net/smhz/vFSV2/2/ When i click on edit checkboxes appear but i want when i check
http://jsfiddle.net/DaJWC/ i can't figure out how to make one of the links reverse so
http://jsfiddle.net/JamesKyle/HQDu6/ I've created a short function based on Mathias Bynens Optimization of the Google
http://jsfiddle.net/asif097/AzRtC/3 As you can see there are two buttons when the 1st button is
http://jsfiddle.net/jqrmh/ $(.one).position({ my: right top, at: right top, of: $(.main), }); I need to
http://jsfiddle.net/X47f5/1/ html: <a href=javascript:void(0);>test link</a>​ javascript: $('a').bind('click mouseup mousedown',function(e){ e.preventDefault(); return false; });​ Is
http://jsfiddle.net/VTD9P/2/ $(button).click(function(){ $(body).append('<div class=vidminclose>X</div>'); }); $(.vidminclose).click(function(){ $(this).remove(); }); ​ ​why is it when 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.