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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:17:00+00:00 2026-06-10T20:17:00+00:00

I am working on a HTML5 Project.There is a drawing graphics API to draw

  • 0

I am working on a HTML5 Project.There is a drawing graphics API to draw Rectangle (fillRectStrokeRect).But how can i draw a SQUARE. I have tried the following way to draw it
CODE

getMouse(e);
x2=mx; y2=my;
var width=endX-startX;
var height=endY-startY;

annCanvasContext.beginPath();
annCanvasContext.lineWidth=borderWidth;
var centerX=width/2;
var centerY=width/2;
var radius=width/2;
annCanvasContext.arc(centerX+5, centerY+5, radius, 0, 2 * Math.PI, false);
annCanvasContext.stroke();
  • 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-10T20:17:01+00:00Added an answer on June 10, 2026 at 8:17 pm

    Use fillRect or strokeRect with the width and height being equal.

    var x = 0, y = 0,
        side = 10;
    
    ctx.fillRect(x, y, side, side);
    

    Demo

    As you say in the comments, if you want to fit the largest square in a circle, it’s more Math related than about code. I’ll trying explaining it to you, but you’ll probably find better, more visual explanations elsewhere on the Internet.

    Draw the diameter of the circle in a way that it divides your square into two equal parts. Now one part is a right angled triangle, which has two of its sides equal. We know the diameter. Using the Pythogorean theorem, you get this equation:
    side^2 + side^2 = diameter^2.

    Let’s find the side now.

    2(side^2) = diameter^2
    side^2 = (diameter^2)/2
    side = Math.sqrt( (diameter^2)/2 )

    Now, to turn this into code.

    var ctx = document.getElementById('canvas').getContext('2d'),
        radius = 20;
    
    ctx.canvas.addEventListener('click', function (e){
        ctx.fillStyle = 'black';
        ctx.arc(e.pageX, e.pageY, radius, 0, Math.PI*2, false);
        ctx.fill();
        ctx.beginPath();
        ctx.fillStyle = 'red';
        var diameter = radius * 2;
        var side = Math.sqrt( (diameter * diameter)/2 );
        ctx.fillRect(e.pageX - side/2, e.pageY - side/2, side, side);
        ctx.closePath();
    }, false);
    

    This would draw a square inside a circle wherever you click on the canvas.

    Demo

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

Sidebar

Related Questions

I have a django project, but for some reason basic jquery isn't working. <html>
I have been working on a project for a long time now and there
I am currently working on a university project, using HTML5. I have been writing
I'm currently working on a JavaScript project that uses the HTML5 canvas as a
I'm using the HTML5 elements and in a project i'm working on and the
I'm working on a project which needs to use youtube api. The html and
Project I'm working on uses jQuery. I have a series of Ajax calls being
I'm working on a HTML5 / CSS 3.0 / MVC 3 project in Visual
I'm using the great HTML5 boilerplate. It's a great project but I'm having some
I just started working on a huge gwt project and asked myself if there

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.