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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:05:44+00:00 2026-06-17T14:05:44+00:00

I Have the following code : <html> <head> </head> <body> <canvas id=canvasId width=300 height=300></canvas>

  • 0

I Have the following code :

<html>
 <head>


 </head>
 <body>
    <canvas id="canvasId" width="300" height="300"></canvas>
    <script>
function square() {

  var ctx = document.getElementById('canvasId').getContext('2d');
  var col= document.getElementById("clr");
      ctx.beginPath();
      ctx.rect(10, 10, 70, 70);
      ctx.fillStyle = 'yellow';
      ctx.fill();
      ctx.lineWidth = 2;
      ctx.strokeStyle = 'black';
      ctx.stroke();

}
function triangle () {

      var canvas = document.getElementById('canvasId');
      var context = canvas.getContext('2d');
        var col= document.getElementById("clr");
      context.beginPath();
      context.rect(85, 80, 200, 100);
      context.fillStyle = 'yellow';
      context.fill();
      context.lineWidth = 2;
      context.strokeStyle = 'black';
      context.stroke();

}

    </script>
  <p>Color <input type="color" id="clr" value="" /></p>
  <input type="button" value="square" onclick="square()" />
  <input type="button" value="triangle" onclick="triangle()" />
 </body>
</html>

I have a problem on this question, I want when the user choose the color and click on square or triangle, it will draw it with the color that he/she choose, what should I change and add ?

  • 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-17T14:05:45+00:00Added an answer on June 17, 2026 at 2:05 pm

    You need to replace the second rect(...) call with something that actually draws a triangle, and you can easily get the color from the input field by accessing its value attribute.

    Click here for my fork of your jsFiddle.

    The relevant lines for accessing the input value are here:

    var col= document.getElementById("clr");
    ctx.fillStyle = col.value;
    

    Now for the triangle drawing (assuming you wanted an isosceles triangle to fit the original rectangle):

    context.beginPath();
    context.moveTo(85, 180);
    context.lineTo(185, 80);
    context.lineTo(285, 180);
    context.closePath();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this following code: JS Fiddle <html> <head> <script> function increase(){ var a
I have following code <html> <head> <script src=jquery.js></script> <script> $(document).ready(function () { $(#div1).hide(); $(#div2).hide();
I have the following html and css code: <!DOCTYPE HTML> <html> <head> </head> <body>
I have the following code <xsl:result-document href=output1/output3/index.html format=html> <html> <head> <SCRIPT LANGUAGE=JavaScript> function getParams()
I have the following code sample upload3.php: <html> <head> <title>PHP Form Upload</title> </head> <body>
I have following code in html file: <html> <head> <title>My Great Website</title> </head> <body>
I have the following code: <!DOCTYPE html> <html xmlns=http://www.w3.org/1999/xhtml> <head> <title></title> </head> <body> <style
i have the following code in html, js and css. <html> <head> <script type=text/javascript>
I have the following html code <!DOCTYPE html> <html lang=en> <head> <meta name=viewport content=width=device-width;
I have the following code: <HTML> <head> <style>div{border:dashed 1px silver}</style> </head> <BODY style=background: #fff;>

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.