/
set-coordinates-example.vue
129 lines (126 loc) · 3.54 KB
/
set-coordinates-example.vue
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
<script>
import { Cropper } from 'vue-advanced-cropper';
import ExampleWrapper from './Components/ExampleWrapper';
import VerticalButtons from './Components/VerticalButtons';
import SquareButton from './Components/SquareButton';
export default {
components: {
ExampleWrapper,
Cropper,
VerticalButtons,
SquareButton,
},
data() {
return {
image:
require('../assets/pictures/photo-1532182657011-d3d31357b5d8.jpg'),
size: {
width: null,
height: null,
},
};
},
methods: {
updateSize({ coordinates }) {
this.size.width = Math.round(coordinates.width);
this.size.height = Math.round(coordinates.height);
},
resize(width = 1, height = 1) {
let startCoordinates;
this.$refs.cropper.setCoordinates([
({ coordinates, imageSize }) => {
startCoordinates = coordinates;
return {
width: coordinates.width * width,
height: coordinates.height * height,
};
},
({ coordinates, imageSize }) => ({
left: startCoordinates.left + (startCoordinates.width - coordinates.width) / 2,
top: startCoordinates.top + (startCoordinates.height - coordinates.height) / 2,
}),
]);
},
center() {
this.$refs.cropper.setCoordinates(({ coordinates, imageSize }) => ({
left: imageSize.width / 2 - coordinates.width / 2,
top: imageSize.height / 2 - coordinates.height / 2,
}));
},
maximize() {
const { cropper } = this.$refs;
const center = {
left: cropper.coordinates.left + cropper.coordinates.width / 2,
top: cropper.coordinates.top + cropper.coordinates.height / 2,
};
cropper.setCoordinates([
({ coordinates, imageSize }) => ({
width: imageSize.width,
height: imageSize.height,
}),
({ coordinates, imageSize }) => ({
left: center.left - coordinates.width / 2,
top: center.top - coordinates.height / 2,
}),
]);
},
},
};
</script>
<template>
<example-wrapper
class="set-coordinates-example"
href="https://github.com/advanced-cropper/vue-advanced-cropper/blob/master/example/docs/.vuepress/components/set-coordinates-example.vue"
>
<cropper
ref="cropper"
class="coodinates-cropper"
:src="image"
:stencil-props="{
minAspectRatio: 10 / 20,
}"
:transitions="true"
@change="updateSize"
/>
<vertical-buttons>
<square-button title="Resize (x2)" @click="resize(2, 2)">
<img :src="require('../assets/icons/resize.svg')" />
</square-button>
<square-button title="Resize height (x2)" @click="resize(1, 2)">
<img :src="require('../assets/icons/resize-vertical.svg')" />
</square-button>
<square-button title="Resize width (x2)" @click="resize(2, 1)">
<img :src="require('../assets/icons/resize-horizontal.svg')" />
</square-button>
<square-button title="Resize (x1/2)" @click="resize(0.5, 0.5)">
<img :src="require('../assets/icons/resize-reduce.svg')" />
</square-button>
<square-button title="Maximize" @click="maximize()">
<img :src="require('../assets/icons/resize-maximize.svg')" />
</square-button>
<square-button title="Center" @click="center()">
<img :src="require('../assets/icons/center.svg')" />
</square-button>
</vertical-buttons>
<div class="size-info" v-if="size.width && size.height">
<div>Width: {{ size.width }}px</div>
<div>Height: {{ size.height }}px</div>
</div>
</example-wrapper>
</template>
<style lang="scss">
.set-coordinates-example {
.coodinates-cropper {
min-height: 400px;
background: black;
}
.size-info {
color: white;
position: absolute;
font-size: 10px;
right: 10px;
bottom: 10px;
opacity: 0.5;
}
}
</style>