-
Notifications
You must be signed in to change notification settings - Fork 57
/
UserRuler.vue
187 lines (177 loc) · 4.46 KB
/
UserRuler.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
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
<template>
<div class="wrapper" id="wrapper">
<SketchRule
:lang="lang"
:thick="thick"
:scale="scale"
:width="width"
:height="height"
:startX="startX"
:startY="startY"
:shadow="shadow"
:horLineArr="lines.h"
:verLineArr="lines.v"
:cornerActive="true"
@handleLine="handleLine"
@onCornerClick="handleCornerClick"
>
</SketchRule>
<div
ref='screensRef'
id="screens"
@wheel="handleWheel"
@scroll="handleScroll"
>
<div ref="containerRef" class="screen-container">
<div id="canvas" :style="canvasStyle" />
</div>
</div>
</div>
</template>
<script>
import Vue from 'vue';
import SketchRule from "vue-sketch-ruler";
const rectWidth = 960;
const rectHeight = 720;
export default Vue.extend({
data() {
return {
scale: 1, //658813476562495, //1,
startX: 0,
startY: 0,
lines: {
h: [100, 200],
v: [100, 200]
},
thick: 20,
width: 500,
height: 400,
lang: "zh-CN", // 中英文
isShowRuler: true, // 显示标尺
isShowReferLine: true // 显示参考线
}
},
components: {
SketchRule
},
computed: {
shadow() {
return {
x: 0,
y: 0,
width: rectWidth,
height: rectHeight
}
},
canvasStyle() {
return {
width: rectWidth + 'px',
height: rectHeight + 'px',
transform: `scale(${this.scale})`
}
}
},
methods: {
handleLine(lines) {
this.lines = lines;
},
handleCornerClick() {
return;
},
handleScroll() {
const screensRect = document
.querySelector("#screens")
.getBoundingClientRect();
const canvasRect = document
.querySelector("#canvas")
.getBoundingClientRect();
// 标尺开始的刻度
const startX = (screensRect.left + this.thick - canvasRect.left) / this.scale;
const startY = (screensRect.top + this.thick - canvasRect.top) / this.scale;
this.startX = startX >> 0;
this.startY = startY >> 0;
},
// 控制缩放值
handleWheel(e) {
if (e.ctrlKey || e.metaKey) {
e.preventDefault();
const nextScale = parseFloat(
Math.max(0.2, this.scale - e.deltaY / 500).toFixed(2)
);
this.scale = nextScale;
}
this.$nextTick(() => {
this.handleScroll();
});
},
initSize() {
const wrapperRect = document
.querySelector("#wrapper")
.getBoundingClientRect();
const borderWidth = 1;
this.width = wrapperRect.width - this.thick - borderWidth;
this.height = wrapperRect.height - this.thick - borderWidth;
}
},
mounted() {
// 滚动居中
this.$refs.screensRef.scrollLeft =
this.$refs.containerRef.getBoundingClientRect().width / 2 - 300; // 300 = #screens.width / 2
this.$nextTick(() => {
this.initSize();
});
}
});
</script>
<style>
body {
margin: 0;
padding: 0;
font-family: sans-serif;
overflow: hidden; }
body * {
box-sizing: border-box;
user-select: none; }
.wrapper {
background-color: #f5f5f5;
position: absolute;
top: 100px;
left: 100px;
width: 900px;
height: 700px;
border: 1px solid #DADADC; }
#screens {
position: absolute;
width: 100%;
height: 100%;
overflow: auto; }
.screen-container {
position: absolute;
width: 5000px;
height: 3000px; }
.scale-value {
position: absolute;
left: 0;
bottom: 100%; }
.button {
position: absolute;
left: 100px;
bottom: 100%; }
.button-ch {
position: absolute;
left: 200px;
bottom: 100%; }
.button-en {
position: absolute;
left: 230px;
bottom: 100%; }
#canvas {
position: absolute;
top: 80px;
left: 50%;
margin-left: -80px;
width: 160px;
height: 200px;
background: lightblue;
transform-origin: 50% 0; }
</style>