-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
142 lines (127 loc) · 4.71 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.left-container {
float: left;
position: relative;
width: 600px;
padding: 40px;
}
.form-group {
height: 42px;
line-height: 42px;
margin-bottom: 10px;
}
.form-group label {
width: 158px;
display: inline-block;
text-align: right;
padding-right: 20px;
}
.form-group input {
border: 1px solid #ddd;
color: #34495e;
font-family: Lato,sans-serif;
font-size: 14px;
padding: 8px 5px 7px;
width: 248px;
height: 22px;
border-radius: 3px;
outline: 0;
}
.right-container {
float: right;
width: 100%;
height: 600px;
margin-left: -680px;
}
.canvas-box {
margin-left: 680px;
padding: 40px;
}
</style>
<title>钻头模拟图生成工具</title>
</head>
<body>
<div class="main">
<div class="left-container">
<div class="form-group">
<label>比例</label>
<input id="ratio" type="text" value="1:6:3">
</div>
<div class="form-group">
<label>顶角角度</label>
<input id="angle" type="text" value="60">
</div>
<div class="form-group">
<label>底角角度</label>
<input id="downAngle" type="text" value="120">
</div>
<div class="form-group">
<label></label>
<button id="createImage">生成图片</button>
</div>
</div>
<div class="right-container">
<div class="canvas-box"><canvas id="myCanvas" width="1200" height="1200"></canvas></div>
</div>
</div>
<script>
var oCreateImageBtn = document.querySelector('#createImage');
var oAngleIpt = document.querySelector('#angle');
var oDownAngleIpt = document.querySelector('#downAngle');
var oCanvas = document.querySelector('#myCanvas');
var nFirstHeight = 30;
var aPointA = [600, 20];
_fMain();
oCreateImageBtn.addEventListener('click', _fMain);
function _fMain() {
const aRatio = _fGetRatio();
const nRad = (+oAngleIpt.value / 2) * Math.PI / 180;
const nLine = Math.tan(nRad) * nFirstHeight;
const aPointB = [aPointA[0] - nLine, aPointA[1] + nFirstHeight];
const aPointC = [aPointA[0] + nLine, aPointA[1] + nFirstHeight];
const nSecondHeight = _fGetSecondHeight();
const nSecondLine = Math.tan(nRad) * nSecondHeight;
const aPointD = [aPointA[0] - nSecondLine, aPointA[1] + nSecondHeight];
const aPointE = [aPointA[0] + nSecondLine, aPointA[1] + nSecondHeight];
const nThirdSquare = nLine * nFirstHeight * (aRatio[2] / aRatio[0]);
const nDownRad = (180 - oDownAngleIpt.value) * Math.PI / 180;
const nThirdLine = Math.sqrt(Math.pow(nSecondLine, 2) - nThirdSquare / Math.tan(nDownRad));
const nThirdHeight = Math.tan(nDownRad) * (nSecondLine - nThirdLine);
const aPointF = [aPointD[0] + (nSecondLine - nThirdLine), aPointD[1] + nThirdHeight];
const aPointG = [aPointF[0] + 2 * nThirdLine, aPointD[1] + nThirdHeight];
const oCtx = oCanvas.getContext('2d');
oCtx.clearRect(0, 0, 4000, 4000);
oCtx.beginPath();
oCtx.moveTo(aPointA[0], aPointA[1]);
oCtx.lineTo(aPointD[0], aPointD[1]);
oCtx.lineTo(aPointE[0], aPointE[1]);
oCtx.lineTo(aPointA[0], aPointA[1]);
oCtx.moveTo(aPointB[0], aPointB[1]);
oCtx.lineTo(aPointC[0], aPointC[1]);
oCtx.moveTo(aPointD[0], aPointD[1]);
oCtx.lineTo(aPointF[0], aPointF[1]);
oCtx.lineTo(aPointG[0], aPointG[1]);
oCtx.lineTo(aPointE[0], aPointE[1]);
oCtx.stroke();
}
function _fGetSecondHeight() {
const aRatio = _fGetRatio();
const nSecondHeight = Math.sqrt((aRatio[0] + aRatio[1]) / aRatio[0]) * nFirstHeight;
return nSecondHeight;
}
function _fGetRatio() {
const oRatioIpt = document.querySelector('#ratio');
const sRatio = oRatioIpt.value;
let aRatio = sRatio.split(/[::]/);
aRatio = aRatio.map(sItem => {
return +sItem.trim() || 0;
});
return aRatio;
}
</script>
</body>
</html>