📅  最后修改于: 2023-12-03 15:28:33.108000             🧑  作者: Mango
在某些场合下,需要测量网页上面的角度,但是浏览器没有提供相关的工具来达到这个目的。因此,我们可以通过Javascript来实现一个简单的量角器,使用户可以在网页上直接测量角度。
<div id="protractor"></div>
是用于容纳量角器的元素,<img id="protractorImage" src="path/to/protractor.png">
是量角器的图片:<div id="protractor"></div>
<img id="protractorImage" src="path/to/protractor.png">
document.addEventListener('contextmenu', function(e){
var protractor = document.getElementById('protractor');
var protractorImage = document.getElementById('protractorImage');
if(e.target && e.target === protractorImage){
e.preventDefault();
}else{
protractor.innerHTML = '';
protractor.style.display = 'none';
}
});
document.addEventListener('contextmenu', function(e){
var protractor = document.getElementById('protractor');
var protractorImage = document.getElementById('protractorImage');
if(e.target && e.target === protractorImage){
e.preventDefault();
var left = e.pageX;
var top = e.pageY;
protractor.style.left = left + 'px';
protractor.style.top = top + 'px';
protractor.innerHTML = '<img src="path/to/protractor.png">';
protractor.style.display = 'block';
}else{
protractor.innerHTML = '';
protractor.style.display = 'none';
}
});
document.addEventListener('contextmenu', function(e){
var protractor = document.getElementById('protractor');
var protractorImage = document.getElementById('protractorImage');
if(e.target && e.target === protractorImage){
e.preventDefault();
var left = e.pageX;
var top = e.pageY;
var centerX = left + protractorImage.clientWidth / 2;
var centerY = top + protractorImage.clientHeight / 2;
var angle = Math.atan2(centerY - e.clientY, centerX - e.clientX);
angle = angle * 180 / Math.PI;
angle = Math.round(angle);
protractor.style.left = left + 'px';
protractor.style.top = top + 'px';
protractor.innerHTML = '<img src="path/to/protractor.png">' +
'<div id="angleIndicator" style="position:absolute; left:50%; top:50%;' +
'transform: rotate(' + angle + 'deg) translate(-50%, -50%); width: 0; height: 0;' +
'border-bottom: 50px solid red; border-left: 50px solid transparent;"></div>' +
'<div id="angleDisplay" style="position:absolute; left:50%; top:10%;' +
'transform: translate(-50%, 0);' +
'font-size: 30px; font-weight: bold;">' + angle + '°</div>';
protractor.style.display = 'block';
}else{
protractor.innerHTML = '';
protractor.style.display = 'none';
}
});
<style>
#protractor {
position: absolute;
width: 200px;
height: 200px;
pointer-events: none;
}
#protractor img {
width: 100%;
height: 100%;
pointer-events: none;
}
#angleIndicator {
pointer-events: none;
}
#angleDisplay {
pointer-events: none;
color: red;
}
</style>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Protractor</title>
<style>
#protractor {
position: absolute;
width: 200px;
height: 200px;
pointer-events: none;
}
#protractor img {
width: 100%;
height: 100%;
pointer-events: none;
}
#angleIndicator {
pointer-events: none;
}
#angleDisplay {
pointer-events: none;
color: red;
}
</style>
</head>
<body>
<div id="protractor"></div>
<img id="protractorImage" src="path/to/protractor.png">
<script>
document.addEventListener('contextmenu', function(e){
var protractor = document.getElementById('protractor');
var protractorImage = document.getElementById('protractorImage');
if(e.target && e.target === protractorImage){
e.preventDefault();
var left = e.pageX;
var top = e.pageY;
var centerX = left + protractorImage.clientWidth / 2;
var centerY = top + protractorImage.clientHeight / 2;
var angle = Math.atan2(centerY - e.clientY, centerX - e.clientX);
angle = angle * 180 / Math.PI;
angle = Math.round(angle);
protractor.style.left = left + 'px';
protractor.style.top = top + 'px';
protractor.innerHTML = '<img src="path/to/protractor.png">' +
'<div id="angleIndicator" style="position:absolute; left:50%; top:50%;' +
'transform: rotate(' + angle + 'deg) translate(-50%, -50%); width: 0; height: 0;' +
'border-bottom: 50px solid red; border-left: 50px solid transparent;"></div>' +
'<div id="angleDisplay" style="position:absolute; left:50%; top:10%;' +
'transform: translate(-50%, 0);' +
'font-size: 30px; font-weight: bold;">' + angle + '°</div>';
protractor.style.display = 'block';
}else{
protractor.innerHTML = '';
protractor.style.display = 'none';
}
});
</script>
</body>
</html>
以上就是使用Javascript实现量角器右键单击元素的介绍。通过这种方法可以方便地测量网页上角度的大小。