JSDM

HTML

 
1
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js" type="text/javascript" charset="utf-8"></script>
2
<script src="https://cdn.bootcss.com/html2canvas/0.5.0-beta4/html2canvas.js" type="text/javascript" charset="utf-8"></script>
3
<div id="app">
4
  <div class="head">
5
    <h1>绫绵院-照片添加水印工具</h1>
6
    <p v-if="tips === 'ok'">水印添加完成,<span @click="download">下载</span></p>
7
    <p v-text="tips" v-else></p>
8
    <span class="renovate" @click="reload">刷新</span>
9
  </div>
10
  <div class="box" @drop="dropImg " @dragover="dragover">
11
    <div class="img" :id="'capture'+index" v-for="(item, index) in imageUrl" :key="index">
12
      <img :src="item">
13
      <div class="watermark"></div>
14
    </div>
15
    <b>拖拽图片到这里(支持多张)</b>
16
  </div>
17
</div>
18
 <script type="text/javascript" charset="utf-8">
19
   let vue = new Vue({
20
      el: '#app',
21
      data: {
22
        tips: '',
23
        imageUrl: [],
24
        fileList: []
25
      },
26
      methods: {
27
        // 阻止默认事件
28
        dragover(e) {
29
          e.stopPropagation();
30
          e.preventDefault();
31
          e.dataTransfer.dropEffect = 'copy';
32
        },
33
        // 拖拽
34
        dropImg(e) {
35
          this.tips = '开始添加水印'
36
          e.stopPropagation();
37
          e.preventDefault();
38
          this.fileList.push(...e.dataTransfer.files)
39
          this.fileList.forEach((item, index) => {
40
            let reader = new FileReader()
41
            let imgResult = ''
42
            reader.readAsDataURL(item)
43
            reader.onloadend = () => {
44
              this.imageUrl.push(reader.result)
45
              if (this.imageUrl.length >= this.fileList.length) {
46
                this.tips = 'ok'
47
              } else {
48
                this.tips = `正在添加水印(${this.imageUrl.length}/${this.fileList.length})`
49
              }
50
            }
51
          })
52
        },
53
        // 刷新
54
        reload() {
55
          window.location.reload();
56
        },
57
        // 点击下载
58
        download() {
59
          this.tips = '开始下载,请稍等'
60
          setTimeout(() => {
61
            this.fileList.forEach((item, index) => {
62
              this.downloadImg(item, index)
63
            })
64
          }, 10)
65
        },
66
        // 下载
67
        downloadImg(item, index) {
68
          html2canvas(document.querySelector("#capture" + index), {
69
             useCORS: true,//Whether to attempt to load cross-origin images as CORS served, before reverting back to proxy
70
                       allowTaint: false,//Whether to allow cross-origin images to taint the canvas
71
          }).then(canvas => {
72
            var url = canvas.toDataURL(item.type);
73
            var a = document.createElement("a");
74
            var event = new MouseEvent("click", {
75
              bubbles: true,
76
              cancelable: true,
77
              view: window
78
            });
79
            a.download = item.name;
80
            a.href = url;
81
            a.dispatchEvent(event);
82
            if (index === this.fileList.length - 1) {
83
              this.tips = '下载完成,请刷新页面'
84
            }
85
          })
86
        }
87
      }
88
    })
89
</script>

CSS

x
 
1
* {
2
  margin: 0;
3
  padding: 0;
4
  box-sizing: border-box;
5
}
6
7
#app {
8
  margin: 20px;
9
  text-align: center;
10
}
11
.head{
12
  text-align: left;
13
  font-size:20px;
14
  position: relative;
15
  width: 600px;
16
  margin: 0 auto;
17
  line-height: 1;
18
}
19
.head h1{
20
  display: inline;
21
  font-size: 20px;
22
}
23
.head p{
24
  display: inline;
25
  font-size: 16px;
26
  padding-left: 10px;
27
}
28
.head p span{
29
  padding: 0 5px;
30
  cursor: pointer;
31
  color: #19aaff;
32
  font-size: 16px;
33
}
34
.head .renovate{
35
  color: #d70000;
36
  font-weight: normal;
37
  position: absolute;
38
  right: 0;
39
  bottom:0;
40
  font-size: 16px;
41
  cursor: pointer;
42
}
43
44
.box {
45
  border: 2px solid #000;
46
  min-height: 200px;
47
  min-width: 600px;
48
  border-style: dashed;
49
  text-align: center;
50
  padding:20px;
51
  position: relative;
52
  z-index: 1;
53
  display: table;
54
  margin: 20px auto 0;
55
}
56
.box b{
57
  position: absolute;
58
  z-index: 1;
59
  top:0;
60
  left:0;
61
  right:0;
62
  text-align: center;
63
  line-height: 200px;
64
  font-weight: normal;
65
  color: #999;
66
}
67
68
.box .img {
69
  position: relative;
70
  display: table;
71
  margin: 0 auto;
72
  z-index: 2;
73
}
74
75
.box .img img {
76
  display: block;
77
}
78
79
.watermark {
80
  position: absolute;
81
  top: 0;
82
  left: 0;
83
  right: 0;
84
  bottom: 0;
85
  z-index: 2;
86
  background: url(./watermark.png) center/ 20% auto;
87
  opacity: 0.2;
88
}
? ?
? ?
必须是有效的URL
+ 添加另一个资源

JS

xxxxxxxxxx
1
 
1
必须是有效的URL
+ 添加另一个资源
Close

文件管理 点击文件查看URL

图片

  1. 暂无文件

CSS

  1. 暂无文件

JavaScript

  1. 暂无文件

其他

  1. 暂无文件
拖动文件到上面的区域或者:
加载中 ..................