-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0487257
commit cc836bf
Showing
12 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<canvas id="watermarkCanvas" style="display: none"></canvas> | ||
<img id="imageWithWatermark" src="" alt="Image with Watermark" /> | ||
|
||
<script> | ||
function addWatermarkToImage(imageSrc, watermarkText) { | ||
const canvas = document.getElementById('watermarkCanvas'); | ||
const ctx = canvas.getContext('2d'); | ||
const img = new Image(); | ||
|
||
img.src = imageSrc; | ||
img.onload = () => { | ||
// 设置canvas大小与图片一致 | ||
canvas.width = img.width; | ||
canvas.height = img.height; | ||
|
||
// 绘制图片 | ||
ctx.drawImage(img, 0, 0); | ||
|
||
// 设置水印样式 | ||
ctx.font = '48px sans-serif'; | ||
ctx.fillStyle = 'rgba(255, 255, 255, 0.5)'; | ||
ctx.textAlign = 'right'; | ||
ctx.textBaseline = 'bottom'; | ||
|
||
// 绘制水印 | ||
ctx.fillText(watermarkText, img.width - 20, img.height - 20); | ||
|
||
// 将带水印的图片显示到页面上 | ||
const imageWithWatermark = document.getElementById('imageWithWatermark'); | ||
imageWithWatermark.src = canvas.toDataURL('image/png'); | ||
}; | ||
} | ||
|
||
// Example usage | ||
addWatermarkToImage('../../assets/123.jpg', 'Your Watermark Text'); | ||
</script> |