每次鼠标移动时,我都需要帮助旋转矩形以指向屏幕上的鼠标。我一直试图使用onmousemove事件和计算度/角度,然后转换为弧度,但出于某种原因,当我将弧度变量rad放在rotate();内时,矩形根本不会旋转。
<!DOCTYPE html>
<html>
<head>
<title>Rotate Rectangle Project</title>
<style>
canvas {
background:#f05424; display: block; margin: 0 auto;
}
body {
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>
<canvas id="myCanvas" width=500 height=500 top=10px></canvas>
<p id='coord'>0</p>
<p id='degree'>0</p>
<p id='radian'>0</p>
<script>
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext("2d");
var x = 100;
var y = 100;
var w = 100;
var h = 30;
var rX, rY, mX, mY, degrees, rad, coords;
document.onmousemove = function(e){
rX =533;
rY =100;
mX = e.clientX;
mY = e.clientY;
coords = mX + ',' + mY;
degrees = mY - rY + mX - rX;
rad = Math.atan2(mY - rY, mX - rX)* Math.PI/180;
draw();
document.getElementById('coord').innerHTML = coords;
document.getElementById('degree').innerHTML = degrees;
document.getElementById('radian').innerHTML = rad;
};
function rectangle(){
ctx.beginPath();
ctx.translate(x, y);
ctx.rotate(rad);
ctx.translate(-x, -y);
ctx.rect(x, y, w, h);
ctx.fillStyle = '#f8c778';
ctx.fill();
ctx.closePath();
}
function draw(){
ctx.clearRect(x,y,canvas.width,canvas.height);
rectangle();
}
</script>
</body>
</html>
答案 0 :(得分:0)
我已经编辑了您的代码,基本上您需要在$objPHPExcel->getActiveSheet()
->setCellValue(
'B10',
'=SUM(A10:B9)'
);
上调用rectangle
并清除画布。
onmousemove
&#13;
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext("2d");
var x = 100;
var y = 100;
var w = 100;
var h = 30;
var mouseX, mouseY, degrees, rad, coords;
document.onmousemove = function(e) {
mouseX = e.clientX;
mouseY = e.clientY;
coords = mouseX + ',' + mouseY;
degrees = mouseY - y + mouseX - x;
rad = Math.atan2(mouseY - y, mouseX - x) * (180 / Math.PI);
rectangle();
};
function rectangle() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.translate(x, y);
ctx.rotate(rad);
ctx.translate(-x, -y);
ctx.rect(x, y, w, h);
ctx.fillStyle = '#f8c778';
ctx.fill();
ctx.closePath();
}
&#13;
body {
margin: 0px;
padding: 0px;
}
canvas {
background: #f05424;
display: block;
margin: 0 auto;
}
&#13;