DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > How To Add Images to HTML Canvas

How To Add Images to HTML Canvas

Let's look at how to add images to HTML Canvas

Johnny Simpson user avatar by
Johnny Simpson
CORE ·
Apr. 05, 22 · Web Dev Zone · Tutorial
Like (2)
Save
Tweet
2.37K Views

Join the DZone community and get the full member experience.

Join For Free

When working with HTML Canvas, sometimes it's desirable to add images. In this article, let's look at how you can easily add images (like .jpeg and .png) to your HTML canvas.

If you're brand new to HTML Canvas, start with our "Getting Started with HTML Canvas" guide.

How To Add Images to HTML Canvas

Adding images to HTML canvas depends upon the Image() constructor, which lets us interact with images in Javascript. For this guide, I'll be using an image from Pexels by figenkokol.

To start, create your HTML canvas element as you normally would:

HTML
 
<canvas id="canvas" width="300" height="300"></canvas>

Now, let's look at the HTML. We first create a new Image(), and then set its URL (i.e. src) to the image we want to show:

JavaScript
 
let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');

// Create our image
let newImage = new Image();
newImage.src = 'https://fjolt.com/images/misc/202203281.png'

// When it loads
newImage.onload = () => {
    // Draw the image onto the context
    ctx.drawImage(newImage, 0, 0, 250, 208);
}

When the image loads (newImage.onload), then we draw the image onto our canvas. To do that, we use ctx.drawImage(). The syntax is shown below.

JavaScript
 
ctx.drawImage(image, x, y, width, height)

If declared like this, ctx.drawImage() only has 5 arguments:

  • image - the image we want to use, is generated from our new Image() constructor.
  • x - the x position on the canvas for the top left corner of the image.
  • y - the y position on the canvas for the top left corner of the image.
  • width - the width of the image. If left blank, the original image width is used.
  • height - the height of the image. If left blank, the original image height is used.

The above code will produce the following canvas:

Now we've successfully drawn an image onto an HTML canvas element, using just Javascript.

Cropping Images in HTML Canvas

Using ctx.drawImage function, we can also crop images. This version of the function accepts a slightly different syntax but lets us crop an image as we see fit.

ctx.drawImage(image, cx, cy, sw, sh, x, y, width, height)
If declared like this, ctx.drawImage() has 9 arguments:

  • image - the image we want to use, generated from our new Image() constructor.
  • cx - this is how far from the top left we want to crop the image. So if it is 50, the image will be cropped 50 pixels from the left-hand side.
  • cy - this is how far from the top we want to crop the image. So if it is 50, the image will be cropped 50 pixels from the top side.
  • sw - this is how big we want the image to be from the point of cx. So if 100, the image will continue for 100px from cx, and then be cropped at that point.
  • sh - this is how big we want the image to be from the point of ch. So if 100, the image will continue for 100px from ch, and then be cropped at that point.
  • x - the x position on the canvas for the top left corner of the image.
  • y - the y position on the canvas for the top left corner of the image.
  • width - the width of the image. If left blank, the original image width is used.
  • height - the height of the image. If left blank, the original image height is used.

If you prefer the visual, here is how cropping works with this method:

How Cropping Works With HTML Canvas

Let's look at an example. Nothing really changes, except for the syntax of ctx.drawImage.

JavaScript
 
let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');

// Create our image
let newImage = new Image();
newImage.src = 'https://fjolt.com/images/misc/202203281.png'

// When it loads
newImage.onload = () => {
    // Draw the image onto the context with cropping
    ctx2.drawImage(newImage, 20, 20, 500, 500, 0, 0, 250, 208);
}

Note: the cropping effect will use the original image size - so if your image is 1000px wide, as this one is, we have to crop it according to those dimensions. We can then use x, y, width, and height to draw the image in any size we like.

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 3 Reasons Why You Should Centralize Developer Tools, Processes, and People
  • Quantum Computers Explained
  • How to Get GDPR and Customer Communications Right
  • Event-Driven Microservices?

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo