-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.html
191 lines (173 loc) · 6.08 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
<!DOCTYPE html>
<html>
<head>
<title>tinycrop: Lightweight pure JavaScript image crop</title>
<meta name="viewport" content="initial-scale=1.0">
<link rel="stylesheet" href="styles/normalize.css">
<link rel="stylesheet" href="styles/style.css">
<script src="dist/tinycrop.min.js"></script>
</head>
<body>
<header class="header">
<h1>tinycrop <img src="images/icon.svg"/></h1>
<p><a href="https://github.com/willbamford/tinycrop">Lightweight pure JavaScript image crop</a></p>
</header>
<div class="container">
<div id="mount" class="mount"></div><!--
--><div class="info">
<div class="field-pair">
<div class="field">
<input type="text" id="input-x" value="0"/>
<label for="input-x">X</label>
</div>
<div class="field">
<input type="text" id="input-y" value="0"/>
<label for="input-x">Y</label>
</div>
</div><!--
--><div
class="field-pair">
<div class="field">
<input type="text" id="input-width" value="0"/>
<label for="input-x">Width</label>
</div>
<div class="field">
<input type="text" id="input-height" value="0"/>
<label for="input-x">Height</label>
</div>
</div><!--
--><div class="field-pair">
<div class="field">
<input type="number" id="input-rotation" value="0"/>
<label for="input-rotation">Rotation</label>
</div>
</div>
<button id="swap-image">Swap image</button><!--
--><button id="aspect-16-by-9">16:9 aspect ratio</button><!--
--><button id="aspect-square">Square aspect ratio</button><!--
--><button id="aspect-free">Free aspect ratio</button><!--
--><button id="container-fit-to-image">Fit container to image</button><!--
--><button id="container-square">Square container</button><!--
--><button id="container-2-by-1">2:1 container</button><!--
--><button id="change-background-colors">Change background colors</button>
</div>
<footer class="footer">Visit the <a href="https://github.com/willbamford/tinycrop">GitHub project</a> page.</footer>
</div>
<script>
var crop = tinycrop.create({
parent: '#mount',
image: 'images/portrait.jpg',
bounds: {
width: '100%',
height: '50%'
},
// backgroundColors: ['#fff', '#f0f0f0'],
selection: {
color: 'yellow',
activeColor: 'red',
borderColor: 'blue',
activeBorderColor: 'black',
// aspectRatio: 4 / 3,
// minWidth: 200,
// minHeight: 300
// width: 400,
// height: 500,
// x: 100,
// y: 500
}
})
function getId(id) {
return document.getElementById(id)
}
var inputX = getId('input-x')
var inputY = getId('input-y')
var inputWidth = getId('input-width')
var inputHeight = getId('input-height')
var buttonSwapImage = getId('swap-image')
buttonSwapImage.addEventListener('click', function (e) {
e.preventDefault()
crop.setImage(
crop.getImage().src !== 'images/landscape2.jpg' ?
'images/landscape2.jpg' :
'images/portrait.jpg'
)
})
var buttonAspect16By9 = getId('aspect-16-by-9')
buttonAspect16By9.addEventListener('click', function (e) {
e.preventDefault()
crop.setAspectRatio(16 / 9)
})
var buttonAspect1By1 = getId('aspect-square')
buttonAspect1By1.addEventListener('click', function (e) {
e.preventDefault()
crop.setAspectRatio(1)
})
var buttonAspectFree = getId('aspect-free')
buttonAspectFree.addEventListener('click', function (e) {
e.preventDefault()
crop.setAspectRatio(null)
})
var buttonContainerFitImage = getId('container-fit-to-image')
buttonContainerFitImage.addEventListener('click', function (e) {
e.preventDefault()
crop.setBounds({width: '100%', height: 'auto'})
})
var buttonContainerSquare = getId('container-square')
buttonContainerSquare.addEventListener('click', function (e) {
e.preventDefault()
crop.setBounds({width: '100%', height: '100%'})
})
var buttonContainer2By1 = getId('container-2-by-1')
buttonContainer2By1.addEventListener('click', function (e) {
e.preventDefault()
crop.setBounds({width: '100%', height: '50%'})
})
var inputRotationFld = getId('input-rotation');
inputRotationFld.addEventListener('input', function(e) {
crop.rotate(inputRotationFld.value)
})
var backgroundColorPreset = 0
var buttonChangeBackgroundColors = getId('change-background-colors')
buttonChangeBackgroundColors.addEventListener('click', function (e) {
e.preventDefault()
backgroundColorPreset = (backgroundColorPreset + 1) % 4
switch (backgroundColorPreset) {
case 0:
crop.setBackgroundColors(['#ffffff', '#f0f0f0'])
break
case 1:
crop.setBackgroundColors(['#000000', '#202020'])
break
case 2:
crop.setBackgroundColors(['#38f'])
break
case 3:
crop.setBackgroundColors(null)
break
}
})
function setInputsFromRegion (region) {
inputX.value = region.x
inputY.value = region.y
inputWidth.value = region.width
inputHeight.value = region.height
}
crop
.on('start', function (region) {
setInputsFromRegion(region)
})
.on('move', function (region) {
setInputsFromRegion(region)
})
.on('resize', function (region) {
setInputsFromRegion(region)
})
.on('change', function (region) {
setInputsFromRegion(region)
})
.on('end', function (region) {
setInputsFromRegion(region)
})
</script>
</body>
</html>