计算必须使用的两个向量之间的角度Math.acos(编辑:事实证明不必使用Math.acos,因为有Math.atan2-way),它只接受 范围内的值[-1..1]。但是 ifvector1.x==vector2.x和vector1.y==vector2.yAND 由于 JS 的性质,会导致0.1+0.2>0.3有时Math.acos得到某些东西>1并且毫不奇怪地返回的情况NaN。
我在所有计算之前用 if 检查来解决它,if (v1.x==v2.x&&v1.y==v2.y)我只是return 0
And if (v1.x==-v2.x&&v1.y==-v2.y) return Math.PI。
我这样做了(编辑接受的答案有更好的版本):
function angle(origin, p1, p2, sign=false){
if (p1.x==p2.x && p1.y==p2.y) return 0
if (p1.x==-p2.x && p1.y==-p2.y) return Math.PI
const a = {x: p1.x-origin.x, y: p1.y-origin.y}
const b = {x: p2.x-origin.x, y: p2.y-origin.y}
sign = sign && …Run Code Online (Sandbox Code Playgroud) 问题是我的想法.有人能帮我吗?在<script>我的html文件的标签中,我有这个:
window.ondragover = function(e){return false;}
window.ondragenter = function(e){return false;}
window.ondrop = function(e){
var files = e.target.files || e.dataTransfer.files;
for (var i = 0, file; file = files[i];i++){
var img = document.createElement('img');
img.height = 200;
img.width = 200;
img.style.background = 'grey';
document.body.appendChild(img);
var reader = new FileReader();
reader.onload = function(){
img.src = reader.result;
}
reader.readAsDataURL(file);
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
但是当我在浏览器上删除多个图像文件时,只加载最后一个图像文件并显示在最后一个img元素中,其他图像文件保持灰色.