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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:28:10+00:00 2026-06-05T23:28:10+00:00

I have some html5 canvas code to make an image, the current fillstyle is

  • 0

I have some html5 canvas code to make an image, the current fillstyle is green, but when I link is clicked, it should change to red, but it’s not working. How do I fix this? thanks

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>  
 <head>  
    <script>
        window.onload = function() {


            var canvas = document.getElementById('myCanvas');
            var context  = canvas.getContext('2d');  

            context.fillStyle = "rgb(0,200,0)";  
            context.fillRect (0, 0, 608, 105);  

            var img = new Image();  
            img.src = 'Round Borders.png';  
            context.drawImage(img,0,0);  


            var dataURL = canvas.toDataURL();
            document.getElementById("canvasImg").src = dataURL;


        };

  </script>  

  <script type="text/javascript">
        function change_bg_color(val) {
            document.getElementById('myCanvas').getContext('2d').fillStyle = val;
        }
  </script>

 </head>  
 <body onmousedown="return false;">
   <canvas id="myCanvas" width="608" height="105" style="display:none;"></canvas>  
   <img id="canvasImg" title="Right click to save me!">
   <br/>
   <a href="#" onclick="change_bg_color('rgb(200,0,0)');">change to red</a>
 </body>  
</html> 
  • 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-05T23:28:13+00:00Added an answer on June 5, 2026 at 11:28 pm

    Working Demo

    Think of fillStyle like choosing your paint for an actual painters canvas. You have to choose your paint first, then begin painting. If you want to change your color you have to dip your brush and repaint.

    So basically you need to redraw the everything on the canvas with the new fillStyle like the following.

    var link = document.getElementsByTagName("a")[0],
        canvas = document.getElementById('myCanvas'),
        context  = canvas.getContext('2d'); 
    
    link.onclick = function(){
        draw('rgb(200,0,0)');
    }
    
    function draw(val){
            context.fillStyle = val;
            context.fillRect (0, 0, 608, 105);  
            var img = new Image();  
            img.src = 'Round Borders.png';  
            context.drawImage(img,0,0);  
            var dataURL = canvas.toDataURL();
            document.getElementById("canvasImg").src = dataURL;
    }
    
    draw("rgb(0,200,0)");
    

    Just pass the color to draw and it will repaint with the chosen color.

    ​

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

Sidebar

Related Questions

I have external html5 canvas that you can paint on some lines using your
i have some coordinates, which i want to draw on a HTML5 canvas step-by-step.
I have drawn shape on html5 canvas with some hardcoded pixels. now I want
I have recently focused on drawing some cool shapes in a HTML 5.0 canvas.
I have some HTML5 based pages i want it to deploy it in sdcard
I have a html5 form with about 6 input fields, some being required and
I have some html code having hierarchy as <div id=a> <span> <span id=s1>...</span> <span
Let's say I'm doing some animations with the HTML5 Canvas. If I'm looking to
I have a small project I am working on HTML5 canvas and I wanted
I have the script that works with pixels in html5 canvas element. And 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.