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 draw image examples

By admin on Jan 30, 2016

HTML5 canvas draw image examples using drawImage().

Using image from html

<img id="img1" src="/img/elephant1_small.jpg">
<canvas id="canvas1" width="200" height="100" style="background-color:lightgray;"></canvas>
<script type="text/javascript">
window.addEventListener("load", draw);
function draw() {
  var canvas = document.querySelector('#canvas1');
  var ctx = canvas.getContext('2d');
  var img1 = document.querySelector("#img1");
  ctx.drawImage(img1, 20, 10);
}
</script>
refresh done
try it online

Using javascript Img object

<canvas id="canvas1" width="200" height="100" style="background-color:lightgray;"></canvas>
<script type="text/javascript">
window.addEventListener("load", draw);
function draw() {
  var canvas = document.querySelector('#canvas1');
  var ctx = canvas.getContext('2d');
  var img1 = new Image();
  img1.onload = function() {ctx.drawImage(img1, 40, 10);}
  img1.src = "/img/elephant1_small.jpg";
}
</script>
refresh done
try it online

Suggested posts:

  1. HTML5 canvas – draw image using base 64 encoded png string
  2. Convert svg xml text to png image using canvas
  3. how to debug javascript in Chrome
  4. Javascript DOMContentLoaded listener example
  5. HTML5 canvas line example
  6. PHP – call a function with arguments in array examples
  7. HTML5 canvas rectangle example
  8. HTML5 shadow dom example
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