-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathindex.html
434 lines (420 loc) · 12.5 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script type="text/javascript" src="https://cdn.staticfile.org/touchjs/0.2.14/touch.min.js"></script>
<script type="text/javascript" src="https://libs.baidu.com/jquery/1.9.0/jquery.min.js"></script>
<script src="./js/index.js"></script>
<script type="text/javascript">
if(/Android (\d+\.\d+)/.test(navigator.userAgent)){
var version = parseFloat(RegExp.$1);
if(version>2.3){
var phoneScale = parseInt(window.screen.width)/750;
document.write('<meta name="viewport" content="width=750, minimum-scale = '+ phoneScale +', maximum-scale = '+ phoneScale +', target-densitydpi=device-dpi">');
}else{
document.write('<meta name="viewport" content="width=750, target-densitydpi=device-dpi">');
}
}else{
document.write('<meta name="viewport" content="width=750, user-scalable=no, target-densitydpi=device-dpi">');
}
</script>
<script type="text/javascript">
window.onload = function () {
var canvasBox = document.querySelector('#picBox');
var canvas = new _Canvas({
box: canvasBox, // 容器
bgColor: '#000', // 背景色
bgPhoto: 'none', // 背景图
photoModel: 'adaption', // 载入图片模式(设置后添加图片时默认为当前设置模式)
model: 'Cascade' // 编辑模式
})
var timer;
canvas.init({
dragEvent: function (e1, e2) {
clearTimeout(timer);
$('.event').text('当前操作事件:正在拖拽');
timer = setTimeout(function () {
$('.event').text('当前操作事件:无操作');
}, 10)
},
zoomEvent: function (e1, e2) {
clearTimeout(timer);
$('.event').text('当前操作事件:正在缩放');
timer = setTimeout(function () {
$('.event').text('当前操作事件:无操作');
}, 10)
},
rotateEvent: function (e1, e2) {
clearTimeout(timer);
$('.event').text('当前操作事件:正在旋转');
timer = setTimeout(function () {
$('.event').text('当前操作事件:无操作');
}, 10)
},
callback: function () {
console.log('图片初始化完成...')
}
});
// 添加或移除背景图
var changeBgFlag = false;
$('.changeBg').click(function () {
if (!changeBgFlag) {
canvas.changeBg({
photo: './img/pic2.jpg'
})
} else {
canvas.changeBg({
photo: 'none'
})
}
changeBgFlag = !changeBgFlag;
})
// 修改背景色
$('.bgColor').click(function () {
var color = '#' + parseInt(Math.random() * 10) + parseInt(Math.random() * 10) + parseInt(Math.random() * 10)
canvas.changeBg({
photo: 'none',
color: color
})
})
// 添加一张满屏的图片
$('.addFullPic').click(function () {
canvas.addPhoto({
url: './img/pic3.jpg',
model: 'covered',
callback: function () {
console.log('成功加一张满屏的图片')
}
})
})
// 添加一张自适应的图片
$('.addAutoPic').click(function () {
canvas.addPhoto({
url: './img/pic4.jpg',
model: 'adaption',
callback: function () {
console.log('成功添加一张自适应的图片')
}
})
})
// 添加一张200px的图片
$('.addFixedPic').click(function () {
canvas.addPhoto({
url: './img/pic5.jpg',
model: 200,
callback: function () {
console.log('成功添加200px的图片')
}
})
})
// 添加一张禁止编辑的图片
$('.addEnablePic').click(function () {
canvas.addPhoto({
url: './img/pic6.jpg',
model: 200,
enable: true,
callback: function () {
console.log('成功添加一张禁止编辑的图片')
}
})
})
// 添加本地图片
$('.file').change(function () {
canvas.addPhoto({
url: getUrl(this.files[0]),
model: 200,
callback: function () {
console.log('成功添加本地图片')
}
})
})
// 获取最底层的图片并提升到最高
$('.changeHierarchy').click(function () {
canvas.photos[0].changeInfo({
hierarchy: 1000,
callback: function () {
console.log('成功获取最底层的图片并提升到最高')
}
})
})
// Cascade模式<
$('.CascadeModel').click(function () {
canvas.changeParams({
model: 'Cascade'
})
$('.nowModel').text('Cascade模式')
console.log('成功切换到Cascade模式')
})
// autoHierarchy模式
$('.autoHierarchyModel').click(function () {
canvas.changeParams({
model: 'autoHierarchy'
})
$('.nowModel').text('autoHierarchy模式')
console.log('成功切换autoHierarchy模式')
})
// 获取当前图片信息
$('.getPhotoInfo').click(function () {
var nowPhoto = canvas.getNowPhoto();
if (!nowPhoto) {
alert('未选中任何图片');
return;
} else {
var nowPhotoInfo = nowPhoto.getPhotoInfo();
$('.photoInfo').val(JSON.stringify(nowPhotoInfo))
}
})
// 旋转
$('.rotate').click(function () {
var nowPhoto = canvas.getNowPhoto();
if (!nowPhoto) {
alert('未选中任何图片');
return;
} else {
var nowPhotoInfo = nowPhoto.getPhotoInfo();
nowPhoto.changeInfo({
rotate: nowPhotoInfo.rotate + 90,
callback: function () {
console.log('成功旋转')
}
})
}
})
// 放大
$('.enlarge').click(function () {
var nowPhoto = canvas.getNowPhoto();
if (!nowPhoto) {
alert('未选中任何图片');
return;
} else {
var nowPhotoInfo = nowPhoto.getPhotoInfo();
nowPhoto.changeInfo({
scale: nowPhotoInfo.scale * 1.1,
callback: function () {
console.log('成功放大')
}
})
}
})
// 缩小
$('.narrow').click(function () {
var nowPhoto = canvas.getNowPhoto();
if (!nowPhoto) {
alert('未选中任何图片');
return;
} else {
var nowPhotoInfo = nowPhoto.getPhotoInfo();
nowPhoto.changeInfo({
scale: nowPhotoInfo.scale / 1.1,
callback: function () {
console.log('成功缩小')
}
})
}
})
// 更换url
$('.changeUrl').click(function () {
var nowPhoto = canvas.getNowPhoto();
if (!nowPhoto) {
alert('未选中任何图片');
return;
} else {
var nowPhotoInfo = nowPhoto.getPhotoInfo();
nowPhoto.changeInfo({
img: './img/pic7.jpg',
callback: function () {
console.log('成功更换url')
}
})
}
})
// 重置
$('.autoScale').click(function () {
var nowPhoto = canvas.getNowPhoto();
if (!nowPhoto) {
alert('未选中任何图片');
return;
} else {
var nowPhotoInfo = nowPhoto.getPhotoInfo();
nowPhoto.init(function () {
console.log('成功重置')
})
}
})
// 删除图片
$('.deletePic').click(function () {
var nowPhoto = canvas.getNowPhoto();
if (!nowPhoto) {
alert('未选中任何图片');
return;
} else {
var nowPhotoInfo = nowPhoto.getPhotoInfo();
nowPhoto._delete();
console.log('成功重置')
}
})
// 更改画布宽高
$('.changeWH1').click(function () {
canvas.changeParams({
width: 500,
height: 400
})
})
// 复原
$('.changeWH2').click(function () {
canvas.changeParams({
width: 750,
height: 600
})
})
// 清空画布
$('.clearCanvas').click(function () {
canvas.clearCanvas();
})
// 输出0.5倍图
$('.outputmodel1').click(function () {
canvas.toDataURL({
width: 375,
height: 300,
callback: function (url) {
$('.outputPic').attr('src', url);
console.log('成功输出0.5倍图')
}
})
})
// 输出1倍png图
$('.outputmodel2').click(function () {
canvas.toDataURL({
width: 750,
height: 600,
type: 'image/png',
callback: function (url) {
$('.outputPic').attr('src', url);
console.log('成功输出1倍png图')
}
})
})
// 输出2倍jpg图
$('.outputmodel3').click(function () {
canvas.toDataURL({
width: 1500,
height: 1200,
type: 'image/jpeg',
callback: function (url) {
$('.outputPic').attr('src', url);
console.log('成功输出2倍jpg图')
}
})
})
function getUrl (file) {
var url = null;
if (window.createObjectURL !== undefined) {
url = window.createObjectURL(file);
} else if (window.URL !== undefined) {
url = window.URL.createObjectURL(file);
} else if (window.webkitURL !== undefined) {
url = window.webkitURL.createObjectURL(file);
}
return url
}
}
</script>
<title>dray</title>
<style media="screen">
* {margin: 0; padding: 0}
#picBox {
width: 100%;
height: 600px;
position: fixed;
top: 0;
}
section {
padding-top: 600px;
}
.set {
font: 36px/50px "";
margin-top: 20px;
}
button {
font: 36px/50px "";
margin-bottom: 10px;
}
.photoInfo {
width: 100%;
font: 24px/50px "";
}
.outputPic {
display: block;
}
.event {
font: 20px/40px "";
color: #f00;
}
</style>
</head>
<body>
<div id="picBox">
<img src="./img/pic1.jpg" alt="" crossOrigin="anonymous">
</div>
<section>
<span>功能:支持多张图片多种模式编辑,图片四角除了一个删除,其余拖拽可缩放旋转;图片支持手势缩放旋转</span>
<span class="event">当前操作事件:无操作</span>
<div class="set">
<h3>更改背景:</h3>
<button class="changeBg">添加/移除背景</button>
<button class="bgColor">更改背景色</button>
</div>
<div class="set">
<h3>添加图片:</h3>
<p>
<button class="addFullPic">添加一张满屏的图片</button>
<button class="addAutoPic">添加一张自适应的图片</button>
<button class="addFixedPic">添加一张200px的图片</button>
<button class="addEnablePic">添加一张禁止编辑的图片</button>
</p>
<label style="border: 1px #888 solid;">
<input type="file" class="file" style="display: none;">
添加本地图片
</label>
</div>
<div class="set">
<h3>获取图片并改变层级:</h3>
<button class="changeHierarchy">获取最底层的图片并提升到最高</button>
</div>
<div class="set">
<h3>更改编辑模式:(<span class="nowModel">Cascade模式</span>)</h3>
<button class="CascadeModel">Cascade模式</button>
<button class="autoHierarchyModel">autoHierarchy模式</button>
</div>
<div class="set">
<h3>更改画布宽高:</h3>
<button class="changeWH1">500*400</button>
<button class="changeWH2">复原</button>
</div>
<div class="set">
<h3>清空画布:</h3>
<button class="clearCanvas">清空</button>
</div>
<div class="set">
<h3>更改选中图片属性:</h3>
<button class="getPhotoInfo">获取当前选中图片的位置信息</button>
<p><input type="text" class="photoInfo"></p>
<button class="rotate">旋转选中图片</button>
<button class="enlarge">放大选中图片</button>
<button class="narrow">缩小选中图片</button>
<button class="changeUrl">更改选中图片的url</button>
<button class="autoScale">重置选中图片</button>
<button class="deletePic">删除选中图片</button>
</div>
<div class="set">
<h3>输出图片:</h3>
<button class="outputmodel1">输出0.5倍图片</button>
<button class="outputmodel2">输出1倍图片</button>
<button class="outputmodel3">输出2倍图片</button>
</div>
<img src="" alt="" class="outputPic">
</section>
</body>
</html>