Add Watermark To Images In Javascript (Simple Examples)

Welcome to a tutorial on how to add a watermark to images in Javascript. Want to lessen the server load? Add a watermark to an image before uploading? Yes, it is possible using a canvas.

To add a watermark to images in Javascript:

  • Create a canvas element and image object.
    • var ctx = document.createElement("canvas").getContext("2d");
    • var img = new Image();
  • Add watermark to the image on load.
    • img.onload = () => {
    • ctx.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight);
    • ctx.fillText("COPYRIGHT", 10, 30);
    • };
  • Set the image source to start – img.src = "IMAGE.JPG";

That covers the quick basics, but read on for the detailed examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Watermark Image Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

JAVASCRIPT WATERMARK IMAGES

All right, let us now get into the examples of adding a watermark to images in Javascript.

 

EXAMPLE 1) TEXT WATERMARK

1-text.html
<!-- (A) HTML CANVAS -->
<canvas id="demo"></canvas>
 
<script>
// (B) IMAGES + CANVAS
var iBack = new Image(),
    iMark = "Copyright",
    canvas = document.getElementById("demo"),
    ctx = canvas.getContext("2d");
 
// (C) WATERMARK
function cmark () {
  // (C1) ADD BACKGROUND IMAGE
  canvas.width = iBack.naturalWidth;
  canvas.height = iBack.naturalHeight;
  ctx.drawImage(iBack, 0, 0, iBack.naturalWidth, iBack.naturalHeight);
 
  // (C2) ADD WATERMARK
  ctx.font = "bold 24px Arial";
  ctx.fillStyle = "rgba(255, 0, 0, 0.5)";
  ctx.fillText(iMark, 10, 30);
 
  /* (C3) DOWNLOAD (IF YOU WANT)
  let anchor = document.createElement("a");
  anchor.href = canvas.toDataURL("image/png");
  anchor.download = "marked.png";
  anchor.click();
  anchor.remove();*/
}
 
// (D) GO - PROCEED ONLY WHEN IMAGES ARE LOADED
iBack.onload = cmark;
iBack.src = "eggs.jpg";
</script>

  1. An HTML <canvas>. We can actually use var canvas = document.createElement("canvas") to create a “hidden canvas”, but we will just display it for this example.
  2. Create an image object, define your text watermark, and get the canvas itself.
  3. As in the introduction itself.
    • We use ctx.drawImage() to draw the background image first.
    • Then, use ctx.fillText() to draw the watermark text on the image.
  4. To be safe, we wait until the image is fully loaded before proceeding – IMAGE.onload = ADD WATERMARK.

 

 

EXAMPLE 2) IMAGE WATERMARK

2-image.html
<!-- (A) HTML CANVAS -->
<canvas id="demo"></canvas>
 
<script>
// (B) IMAGES + CANVAS
var iBack = new Image(),
    iMark = new Image(),
    loaded = 0,
    canvas = document.getElementById("demo"),
    ctx = canvas.getContext("2d");
 
// (C) WATERMARK
function cmark () { iLoaded++; if (iLoaded==2) {
  // (C1) ADD BACKGROUND IMAGE
  canvas.width = iBack.naturalWidth;
  canvas.height = iBack.naturalHeight;
  ctx.drawImage(iBack, 0, 0, iBack.naturalWidth, iBack.naturalHeight);
 
  // (C2) ADD WATERMARK
  ctx.drawImage(iMark, 0, 0, iMark.naturalWidth, iMark.naturalHeight);
 
  /* (C3) DOWNLOAD (IF YOU WANT)
  let anchor = document.createElement("a");
  anchor.href = canvas.toDataURL("image/png");
  anchor.download = "marked.png";
  anchor.click();
  anchor.remove();*/
}
 
// (D) GO - PROCEED ONLY WHEN IMAGES ARE LOADED
iBack.onload = cmark;
iMark.onload = cmark;
iBack.src = "eggs.jpg";
iMark.src = "potato.png";
</script>

Look no further – This is pretty much the same as the text watermark. The only difference here is that we are adding a watermark image on top, instead of text.

 

 

EXTRA) WATERMARK POSITION

3-pos.html
 // (C) WATERMARK
function cmark () { iLoaded++; if (iLoaded==2) {
  // (C1) ADD BACKGROUND IMAGE
  canvas.width = iBack.naturalWidth;
  canvas.height = iBack.naturalHeight;
  ctx.drawImage(iBack, 0, 0, iBack.naturalWidth, iBack.naturalHeight);
 
  // (C2) CALCULATE BOTTOM RIGHT COORDINATES
  var wx = Math.floor(iBack.naturalWidth - iMark.naturalWidth),
      wy = Math.floor(iBack.naturalHeight - iMark.naturalHeight);
 
  // (C3) ADD WATERMARK
  ctx.drawImage(iMark, wx, wy, iMark.naturalWidth, iMark.naturalHeight);
}}

As you can see, the position of the watermark is set with X, Y coordinates. To specify an exact position, things are sadly going to be very Mathematical. For example, to set the watermark in the bottom-right corner:

  • The X coordinate is Image Width - Watermark Width.
  • The Y coordinate is Image Height - Watermark Height.

Brutal, but that’s how it works.

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

COMPATIBILITY CHECKS

The examples should work on all modern browsers.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

Add Watermark In Javascript (Click To Enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *