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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:15:57+00:00 2026-06-08T23:15:57+00:00

My code is using a canvas that allows people to draw whatever they want,

  • 0

My code is using a canvas that allows people to draw whatever they want, what I’m trying to implement is changing the color of what their coloring on button click, default color is black I have two buttons to change ot red and green and also a clear canvas button, none of these seems to operate on button click however.

<h3>Draw Something</h3>
<!DOCTYPE html>

<html lang="en">
<head>
   <meta charset="utf-8">
   <title>Paint Canvas</title>
   <style type="text/css">
   <!--
     #container { position: relative; }
     #imageView { border: 1px solid #000; }
   -->
   </style>

</head>

<body>

    <div id="container">
        <canvas id="imageView" width="600" height="300">
               </p>
        </canvas>
    </div>

    <script type="text/javascript" src=".js"></script>

</html>

<body >
    <input type= "button" value= "Green" id= "green" onclick= "GreenRect()" />

    <input type= "button" value= "Red" id= "red" onclick= "RedRect()" />

        <input type= "button" value= "clear canvas" 
                 id= "clear" onclick= "ImgClr()" />


        <button id="howdy">Howdy!</button><br>
</body>
    function GreenRect () {
        context.strokeStyle= 'green';
        context.stroke();
        }

        function RedRect () {
        context.strokeStyle= 'red';
        context.stroke();
        }

        function ImgClr () {
        context.clearRect(0,0, 600, 300);  
        }
        </script>
  • 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-08T23:15:59+00:00Added an answer on June 8, 2026 at 11:15 pm

    You did have a lot of mis-formed html, like a second <body> tag as well as a </html> half way through your code, either of which would have completely confused a browser as you can see by Firefox’s noble attempt to make sense of your code:

    <html lang="en"><head></head><body><h3>Draw Something</h3>
       <meta charset="utf-8">
       <title>Paint Canvas</title>
       <style type="text/css">
       <!--
         #container { position: relative; }
         #imageView { border: 1px solid #000; }
       -->
       </style>
        <div id="container">
            <canvas id="imageView" width="600" height="300">
                   <p></p>
            </canvas>
        </div>
        <script type="text/javascript" src=".js"></script>
    
        <input type="button" value="Green" id="green" onclick="GreenRect()">
        <input type="button" value="Red" id="red" onclick="RedRect()">
        <input type="button" value="clear canvas" id="clear" onclick="ImgClr()">
        <button id="howdy">Howdy!</button><br>
    
        function GreenRect () {
            context.strokeStyle= 'green';
            context.stroke();
        }
    
        function RedRect () {
            context.strokeStyle= 'red';
            context.stroke();
        }
    
        function ImgClr () {
            context.clearRect(0,0, 600, 300);  
        }
    </body></html>
    

    Also there is:

    1. an <h3> tag outside of your <body> tag
    2. your javascript at the bottom of your original code is outside of the <body> and <head> tags as well as lacking an opening <script> tag.
    3. <p></p> tags within your canvas tags that don’t seem to be doing anything.

    I’d strongly recommend not looking at my code below, but first just trying to clean up your html according to the comments I’ve made about it. I think you would get a lot out of doing that for yourself.

    <!DOCTYPE html>
    
    <html lang="en">
        <head>
          <meta charset="utf-8">
          <title>Paint Canvas</title>
    
          <script type="text/javascript">
            //declare global namespace
            this.DGN = {
                prep: undefined,
                context: undefined,
                greenRect: undefined,
                redRect: undefined,
                imgClear: undefined,
                smileyFace: undefined
            };
    
            DGN.prep = function(){
            if(typeof DGN.context === "undefined") {
                var canvas = document.getElementById('imageView');
    
                    if (canvas.getContext){
                        DGN.context = canvas.getContext('2d');
                  } 
               }
            };
    
    
            DGN.greenRect = function  () {
                DGN.prep();
    
                DGN.context.strokeStyle = 'green';
                DGN.context.stroke();
            };
    
    
            DGN.redRect = function  () {
                DGN.prep();
    
                DGN.context.strokeStyle = 'red';
                DGN.context.stroke();
            };
    
    
            DGN.imgClear = function () {
                DGN.prep();
    
                DGN.context.clearRect(0, 0, 600, 300);  
            };
    
    
            DGN.smileyFace = function () {
                DGN.prep();
    
                console.log(DGN.context);
                DGN.context.beginPath();  
                DGN.context.arc(75,75,50,0,Math.PI*2,true); // Outer circle  
                DGN.context.moveTo(110,75);  
                DGN.context.arc(75,75,35,0,Math.PI,false);   // Mouth (clockwise)  
                DGN.context.moveTo(65,65);  
                DGN.context.arc(60,65,5,0,Math.PI*2,true);  // Left eye  
                DGN.context.moveTo(95,65);  
                DGN.context.arc(90,65,5,0,Math.PI*2,true);  // Right eye  
                DGN.context.stroke();
            };
    
    
            </script>
    
        </head>
        <body>
            <h3>Draw Something</h3>
            <div id="container">
                <canvas id="imageView" width="600" height="300">
                </canvas>
            </div>
    
            <input type="button" value="Green"        id="green"      onclick= "DGN.greenRect()" />
            <input type="button" value="Red"          id="red"        onclick= "DGN.redRect()" />
            <input type="button" value="clear canvas" id="clear"      onclick= "DGN.imgClear()" />
            <input type="button" value="Smiley Face"  id="smileyFace" onclick= "DGN.smileyFace()" />
    
        </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to draw a rectangle using canvas which change its size with different
This is what I'm trying to accomplish: I want to have a canvas that
So I am trying to clear the Canvas using canvas.drawColor(Color.BLACK) but if I just
I am trying to use Canvas.drawLine method to draw a polygon Here's the code
Using the code below, I am attempting to fill a Canvas with UIElements and
I'm making a simple app using the HTML5 <canvas> tag and compile the code
I'm trying to resize a HTML5 canvas element using jQuery. (NOTICE: Not the objects
I am using canvas,and I have this code - var myCanvas = document.getElementById(myCanvas), ctx
I have a developed a application that allows users to draw simple images on
I'm trying to create a canvas script that visually draws a cubic bezier-curve, but

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.