Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 10, 2023 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/f88a485bbb298d359e6a5c4becd3ef58 to your computer and use it in GitHub Desktop.
Save code-boxx/f88a485bbb298d359e6a5c4becd3ef58 to your computer and use it in GitHub Desktop.
Javascript Watermark Images

JAVASCRIPT WATERMARK IMAGE

https://code-boxx.com/watermark-images-javascript/

IMAGES

source potato

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>JS Watermark</title>
</head>
<body>
<!-- (A) HTML CANVAS -->
<canvas id="demo"></canvas>
<script>
// (B) IMAGE + WATERMARK + CANVAS
var img = new Image(),
watermark = "Copyright",
canvas = document.getElementById("demo"),
ctx = canvas.getContext("2d");
// (C) ADD WATERMARK (WHEN IMAGE IS FULLY LOADED)
img.onload = () => {
// (C1) SET IMAGE
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
ctx.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight);
// (C2) ADD WATERMARK
ctx.font = "bold 24px Arial";
ctx.fillStyle = "rgba(255, 0, 0, 0.5)";
ctx.fillText(watermark, 10, 30);
// (C3) DOWNLOAD (IF YOU WANT)
let anchor = document.createElement("a");
anchor.href = canvas.toDataURL("image/webp");
anchor.download = "demoA.webp";
anchor.click();
anchor.remove();
};
// (D) GO
img.src = "source.png";
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JS Watermark</title>
</head>
<body>
<!-- (A) HTML CANVAS -->
<canvas id="demo"></canvas>
<script>
// (B) IMAGE + WATERMARK + CANVAS
var img = new Image(),
watermark = new Image(),
loaded = 0,
canvas = document.getElementById("demo"),
ctx = canvas.getContext("2d");
// (C) ADD WATERMARK (WHEN IMAGES ARE FULLY LOADED)
function mark () { loaded++; if (loaded==2) {
// (C1) SET IMAGE
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
ctx.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight);
// (C2) ADD WATERMARK
ctx.drawImage(watermark, 0, 0, watermark.naturalWidth, watermark.naturalHeight);
// (C3) DOWNLOAD (IF YOU WANT)
let anchor = document.createElement("a");
anchor.href = canvas.toDataURL("image/webp");
anchor.download = "demoB.webp";
anchor.click();
anchor.remove();
}}
// (D) GO
img.onload = mark;
watermark.onload = mark;
img.src = "source.png";
watermark.src = "potato.png";
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JS Watermark</title>
</head>
<body>
<!-- (A) HTML CANVAS -->
<canvas id="demo"></canvas>
<script>
// (B) IMAGE + WATERMARK + CANVAS
var img = new Image(),
watermark = new Image(),
loaded = 0,
canvas = document.getElementById("demo"),
ctx = canvas.getContext("2d");
// (C) ADD WATERMARK (WHEN IMAGES ARE FULLY LOADED)
function mark () { loaded++; if (loaded==2) {
// (C1) SET IMAGE
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
ctx.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight);
// (C2) CALCULATE CENTER
var wx = Math.floor(img.naturalWidth/2 - watermark.naturalWidth/2),
wy = Math.floor(img.naturalHeight/2 - watermark.naturalHeight/2);
// (C3) ADD WATERMARK
ctx.drawImage(watermark, wx, wy, watermark.naturalWidth, watermark.naturalHeight);
// (C4) DOWNLOAD (IF YOU WANT)
let anchor = document.createElement("a");
anchor.href = canvas.toDataURL("image/webp");
anchor.download = "demoC.webp";
anchor.click();
anchor.remove();
}}
// (D) GO - PROCEED ONLY WHEN IMAGES ARE LOADED
img.onload = mark;
watermark.onload = mark;
img.src = "source.png";
watermark.src = "potato.png";
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment