InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

HTML5 Canvas

  • canvas introduction
  • canvas line
  • canvas rectangle
  • canvas clear rectangle
  • canvas to png
  • canvas draw image
  • canvas draw image using base 64 string
  • canvas draw image using svg string
  • canvas svg xml text to png image
 
  • Home
  • > Tutorials
  • > HTML
  • > HTML5 Canvas

HTML5 canvas rectangle example

By admin on Jan 30, 2016

HTML5 canvas rectangle example using rect() and fillRect(). This example uses canvas path, lineWidth, lineStyle, fillStyle.

<canvas id="mycanvas1" width="200" height="100" 
style="background-color:lightgray;">
</canvas>
<script type="text/javascript">
window.addEventListener("load", draw);
function draw() {
  var canvas = document.querySelector('#mycanvas1');
  var context = canvas.getContext('2d');

  context.beginPath();
  context.lineWidth="5";
  context.strokeStyle="green";
  context.rect(10,10,60,60);
  context.stroke();
  context.closePath();

  context.fillStyle = "lightgreen";
  context.fillRect(10,10,60,60);
}
</script>
refresh done
try it online

Suggested posts:

  1. HTML5 canvas line example
  2. HTML5 shadow dom example
  3. Javascript DOMContentLoaded listener example
  4. HTML5 canvas – draw image using base 64 encoded png string
  5. Convert svg xml text to png image using canvas
  6. HTML5 canvas draw image examples
  7. how to debug javascript in Chrome
  8. PHP – call a function with arguments in array examples
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged HTML5, HTML5 Canvas, Javascript
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress