Skip to content

Commit

Permalink
图片加水印
Browse files Browse the repository at this point in the history
  • Loading branch information
weilun0510 committed Oct 8, 2024
1 parent 0487257 commit cc836bf
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 0 deletions.
Binary file added assets/123.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/images/face1_part1.png
Binary file not shown.
Binary file removed assets/images/face1_part2.png
Binary file not shown.
Binary file removed assets/images/face1_part3.png
Binary file not shown.
Binary file removed assets/images/face1_part4.png
Binary file not shown.
Binary file removed assets/images/face2_part1.png
Binary file not shown.
Binary file removed assets/images/face2_part2.png
Binary file not shown.
Binary file removed assets/images/face2_part3.png
Binary file not shown.
Binary file removed assets/images/face2_part4.png
Binary file not shown.
Binary file added assets/v3.mp4
Binary file not shown.
Binary file added assets/下载.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions canvas/图片加水印/index.html
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>

0 comments on commit cc836bf

Please sign in to comment.