将水印添加到已有图像的画布 - HTML5,Canvas

时间:2014-10-13 09:53:09

标签: javascript html5 html5-canvas overlay watermark

我有相机视频输入和画布。

当用户点击提交

时,画布会获取Feed的图像
     <video id="video" width="300" height="200" autoplay></video>
     </section>
      <section class="btn">
      <button id="btnClick">Submit</button><br>
      </section>

     <section>
      <canvas id="canvas" width="300" height="300">
      </section>

用户点击提交后,可以点击“分享”上传图片。

        <input type="button" onclick="uploadEx()" value="Share" />

        <form method="post" accept-charset="utf-8" name="form1">
        <input name="hidden_data" id='hidden_data' type="hidden"/>
         </form>

我希望能够在用户通过点击分享按钮提交第一个快照之前在图像顶部叠加另一个png。

JS用于上传pic:

           function uploadEx() {
            var canvas = document.getElementById("canvas");
            var dataURL = canvas.toDataURL("image/png");
            document.getElementById('hidden_data').value = dataURL;

            var fd = new FormData(document.forms["form1"]);

            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'uploadscript.php', true);

            xhr.upload.onprogress = function(e) {
                if (e.lengthComputable) {
                    var percentComplete = (e.loaded / e.total) * 100;
                    console.log(percentComplete + '% uploaded');
                    alert('Image uploaded');
                }
            };

            xhr.onload = function() {

            };
            xhr.send(fd);
        };

如何在上传时将第二张图片叠加在上面(如水印)?

1 个答案:

答案 0 :(得分:17)

以下是使用临时画布的一种方法:

  • 创建临时的内存中画布:document.createElement('canvas')

  • 将主画布绘制到临时画布上:tempContext.drawImage(mainCanvas,0,0)

  • 添加一些重叠的文字(或其他内容)作为水印:tempContext.fillText('Mine!',0,0)

  • 获取临时画布的dataURL:tempCanvas.toDataURL()

enter image description here

示例代码和演示:

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;

var img=new Image();
img.crossOrigin='anonymous';
img.onload=start;
img.src="https://dl.dropboxusercontent.com/u/139992952/earth.jpg";
function start(){
  canvas.width=img.width;
  canvas.height=img.height;
  ctx.drawImage(img,0,0);
  var dataURL=watermarkedDataURL(canvas,"It's Mine!");
}


function watermarkedDataURL(canvas,text){
  var tempCanvas=document.createElement('canvas');
  var tempCtx=tempCanvas.getContext('2d');
  var cw,ch;
  cw=tempCanvas.width=canvas.width;
  ch=tempCanvas.height=canvas.height;
  tempCtx.drawImage(canvas,0,0);
  tempCtx.font="24px verdana";
  var textWidth=tempCtx.measureText(text).width;
  tempCtx.globalAlpha=.50;
  tempCtx.fillStyle='white'
  tempCtx.fillText(text,cw-textWidth-10,ch-20);
  tempCtx.fillStyle='black'
  tempCtx.fillText(text,cw-textWidth-10+2,ch-20+2);
  // just testing by adding tempCanvas to document
  document.body.appendChild(tempCanvas);
  return(tempCanvas.toDataURL());
}
body{ background-color: ivory; padding:20px;}
canvas{border:1px solid red;}
<canvas id="canvas" width=300 height=300></canvas>
<h2>Watermarked...</h2>