使用Html5对图片加水印及多图合成

前端开发   发布日期:2025年05月14日   浏览次数:188
                                                                         转载请注明原地址;       
                                                                    <!--水印设置,生成图片-->
  1. var img = new Image();
  2. img.crossOrigin="anonymous";
  3. img.src = "../imgs2/1.jpg";
  4. alert("图一已经加载")
  5. <!--图片加载完成后在绘制-->
  6. img.οnlοad=function(){
  7. <!--准备canvas环境-->
  8. var canvas=document.getElementById("myCanvas");
  9. var ctx=canvas.getContext("2d");
  10. canvas.width=1063;
  11. canvas.height=638;
  12. // 绘制图片
  13. ctx.drawImage(img,0,0);
  14. // 字体及颜色
  15. ctx.font="30px microsoft yahei";
  16. ctx.fillStyle = "#FF0000(255,255,255,0.8)";
  17. //文字位置
  18. ctx.fillText("(我是数字测试)",220,370);
  19. ctx.fillText("12345678978",250,530);
                                                                                        <!--多个图pain合成-->
  1. <!--html部分-->
  2. <div class="contentimg" >
  3. <img src="../imgs2/机贴1.jpg" class="imgBox" border="0" style="display:none;width:100%;height:100%;" />
  4. <img src="../imgs2/clipboard.png" class="imgCode" border="0" style="display:none;width:5.5rem;height:5.5rem" />
  5. <!--<canvas >Your browser does not support the HTML5 canvas tag.</canvas>-->
  6. <div ></div>
  7. </div>
  8. <!--jquery-->
  9. $(function() {
  10. //生成画布
  11. hecheng();
  12. function hecheng(){
  13. draw(function(){
  14. document.getElementById('myCanvas').innerHTML='<img src="'+base64[0]+'">';
  15. })
  16. }
  17. var base64=[];
  18. function draw(fn) {
  19. var imgArr = ["../imgs2/机贴1.jpg","../imgs2/clipboard.png"];
  20. var c = document.createElement('canvas'),
  21. ctx = c.getContext('2d'),
  22. len = imgArr.length;
  23. c.width = $(".contentimg").width();
  24. c.height = $(".contentimg").height();
  25. console.log(c.width,c.height);
  26. ctx.rect(0,0,c.width,c.height);
  27. ctx.fillStyle='#ccc';
  28. ctx.fill();
  29. function drawing(n) {
  30. if (n<len) {
  31. var img = new Image;
  32. img.src = imgArr[n];
  33. img.onload = function() {
  34. if (n==1) {
  35. var codeW = $(".imgCode").width(), codeH = $(".imgCode").height();
  36. ctx.drawImage(img,94,125,codeW,codeH);
  37. drawing(n+1);
  38. } else {
  39. ctx.drawImage(img,0,0,c.width,c.height);
  40. drawing(n+1);
  41. }
  42. }
  43. } else {
  44. base64.push(c.toDataURL("image/jpeg",0.8));
  45. fn();
  46. }
  47. }
  48. drawing(0);
  49. }
  50. })






以上就是使用Html5对图片加水印及多图合成的详细内容,更多关于使用Html5对图片加水印及多图合成的资料请关注九品源码其它相关文章!