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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:54:36+00:00 2026-05-30T00:54:36+00:00

I was trying a script to draw graph paper like grids on the canvas

  • 0

I was trying a script to draw graph paper like grids on the canvas from dive into html5. The result is supposed to draw a mesh with squares of side 10px but i’m getting the size as approximately 20px, and not exact squares.
Here is the code,`

   <html>
      <head>     
           <style>
    body{
        margin: 20px 20px 20px 20px;
    }

    canvas{
        width: 500px;
        height: 375px;
        border: 1px solid #000;
    }
    </style>
    <script type="text/javascript">
        function activate(){
            var canvas =document.getElementById("exp");
            var context = canvas.getContext("2d");

            for (var x=0.5;x<500;x+=10){
                context.moveTo(x,0);
                context.lineTo(x,375);
                console.log(x);
            }

            for (var y=0.5;y<375;y+=10){
                context.moveTo(0,y);
                context.lineTo(500,y);
            }

            context.strokeStyle="#000";
            context.stroke();
        }
    </script>
</head>

<body>
    <canvas id="exp"><script type="text/javascript">activate();</script></canvas>
</body
     </html>

And this is the output:
canvas rendered
while the actual output should be:
actual canvas
Note: i’m not worried about the color difference. what i don’t understand is why the space between 2 lines is ~20px(as checked by a measurement tool on firefox) instead of 10px.

Also, on printing the values of x, it gives the right value(i.e. incremented by 10 each time).

  • 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-30T00:54:38+00:00Added an answer on May 30, 2026 at 12:54 am

    you can not set the canvas’ size with css
    you should set in the DOM’s attribute.

    <canvas width="100" height="200"></canvas>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to modify my GreaseMonkey script from firing on window.onload to window.DOMContentLoaded, but
I am trying to load a bitmap into the canvas following the example here.
Hey guys I'm trying to replace the image loaded on the html5 canvas on
I am trying to write a MEL script that will draw simple implicit surfaces
I'm trying to make canvas draw code for the purposes of electronics education. Please
I'm trying to draw on some canvas elements and, of course, I need it
I am trying to draw two parallel lines on the canvas, but it seems
I'm trying to to change dynamically a context.clip(); in a HTML5 canvas, drawing a
I'm trying to script the shutdown of my VM Servers in a .bat. if
I get this error when trying ruby script/console Rails requires RubyGems >= . Please

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.