• 使用canvas给图片添加水印


    css部分

        .clip {
            position: absolute;
            clip: rect(0 0 0 0);
        }
    

    html部分

        <input type="file" id="uploadFile" class="clip" accept="image/*">
        <label class="ui-button ui-button-primary" for="uploadFile">选择图片</label>
        <img id="imgCover" src="./images/googlelogo_color_272x92dp.png" class="clip">
        <p id="imgUploadX"></p>
    

    js部分

        var eleUploadFile = document.getElementById('uploadFile');
        var eleImgCover = document.getElementById('imgCover');
        var eleImgUploadX = document.getElementById('imgUploadX');
    
        eleUploadFile.addEventListener('change', function(event) {
            var file = event.target.files[0] || event.target.dataTransfer.files[0];
    
            var reader = new FileReader();
    
            reader.onload = function(e) {
                var base64 = e.target.result;
    
                // canvas压缩图片,并且回调显示
                imgTogether(base64, function(url) {
                    eleImgUploadX.innerHTML = '<img src="' + url + '"/>';
                })
            }
            reader.readAsDataURL(file);
        });
    
        var imgTogether = function(url, callback) {
            var canvas = document.createElement('canvas');
            var context = canvas.getContext('2d');
    
            // image实例对象
            var imageUpload = new Image();
            imageUpload.onload = function() {
                console.log('imageUpload', imageUpload);
                canvas.width = imageUpload.width;
                canvas.height = imageUpload.height;
                // canvas绘制图片
                context.drawImage(imageUpload, 0, 0);
                // 绘制水印
                 context.drawImage(eleImgCover, 0, 0);
                // 回调
                callback(canvas.toDataURL('image/jpeg'));
            }
            imageUpload.src = url;
        }
    
  • 相关阅读:
    最近面试遇到的技术问题
    Oracle 查看表空间使用情况
    流水账日记20150626
    Mantis及TestLink运维问题处理
    Mantis维护之显示姓名
    汤姆猫(。。。。)
    mac配置svn服务器
    如何使用TestFlight进行Beta测试
    artice与section的区别
    article元素设计网络新闻展示
  • 原文地址:https://www.cnblogs.com/White-Quality/p/10109381.html
Copyright © 2020-2023  润新知