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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:24:58+00:00 2026-05-24T14:24:58+00:00

I am trying to draw a string at a specific point based on the

  • 0

I am trying to draw a string at a specific point based on the mouse position (only when I am dragging).

The problem is, that it seems to scale the x and y. When I am at 0,0 with the mouse, the string displays in the right spot, but as I move away, the string draws farther and farther away from my mouse which makes me think there is a hidden scaling thing going on…

I am using event.getX() and the position of the canvas, should I be using some other combination to get the correct mouse position?

below is the code

        final Canvas canvas = Canvas.createIfSupported();
        final AsyncBool mouseDown = new AsyncBool(false);

        final Context2d context = canvas.getContext2d();
        context.setFont("bold 8px sans-serif"); 

        canvas.setWidth("100%");
        canvas.setHeight("100%");

        canvas.addMouseMoveHandler(new MouseMoveHandler() {
            @Override
            public void onMouseMove(MouseMoveEvent event)
            {
                if(mouseDown.getBool())
                {

                    context.fillText("Bored.", event.getX() - canvas.getAbsoluteLeft(), event.getY() - canvas.getAbsoluteTop());
                }

            }
        });
        canvas.addMouseDownHandler(new MouseDownHandler() {
            @Override
            public void onMouseDown(MouseDownEvent event)
            {
                mouseDown.setBool(true);
            }
        });
        canvas.addMouseUpHandler(new MouseUpHandler() {
            @Override
            public void onMouseUp(MouseUpEvent event)
            {
                mouseDown.setBool(false);
            }
        });



        RootPanel.get().add(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-05-24T14:25:00+00:00Added an answer on May 24, 2026 at 2:25 pm

    Your code should work for a canvas which is not resized. But if you resize your canvas, you need to update the canvas coordinate space accordingly as well. For instance, for a 500px, 500px size canvas you can achieve this by :

    canvas.setSize("500px","500px");
    canvas.setCoordinateSpaceHeight(500);
    canvas.setCoordinateSpaceWidth(500);
    

    in your case, you size your canvas using percentage values, you can use :

    canvas.setSize("100%","100%");
    canvas.setCoordinateSpaceHeight(Window.getClientHeight()); 
    canvas.setCoordinateSpaceWidth(Window.getClientWidth());
    

    since these methods don’t accept percentage values, you need to calculate the parent container’s width, and height in pixels.

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

Sidebar

Related Questions

Im trying to build a small frame that displays an image. My problem is
I'm trying to draw a String inside a canvas. The text and the position
I am trying to draw a string using quartz 2d. What i am doing
I am trying to redirect to a specific path based on HTTP_HOST or SERVER_NAME
I am trying to use this method to draw a string into a custom
I'm trying to write a command in LaTeX that takes a string such as
I want to use core text to draw string that can span over 100
I'm trying to write code that can print only filled text boxes in the
I'm trying to draw a string with a background on a HTML5 canvas. I'm
I'm trying to draw a polygon using c# and directx All I get is

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.