私はmyCanvasをmyCanvas1に向かわせるためにこのコードを書いています。私はMath.atan2()メソッドを使ってこれを試みました。しかし、それは動作しません。何か案は? JQueryを使用しないでください。キャンバスを別のキャンバスに移動しようとしています - JAVASCRIPT
HTML:
<canvas id="myCanvas"></canvas>
<canvas id="myCanvas1"></canvas>
JS:
var follower = document.getElementById('myCanvas');
var flw = follower.getContext('2d');
var runner = document.getElementById('myCanvas1');
var rnr = runner.getContext('2d');
document.addEventListener('keydown', moveShot);
//Cordinates of sPositions 1 and two
var sPosition0 = [700, 700];
var sPosition1 = [400, 400];
var xPosition0 = sPosition0[0], yPosition0 = sPosition0[1];
var xPosition1 = sPosition1[0], yPosition1 = sPosition1[1];
//This should be the arctan between sPosition0 and sPosition1
var angleRadians0 = Math.atan2(sPosition0[0] - sPosition1[0], sPosition0[1] - Position1[1]);
/*The speed of the object is 4. To get it to move diagonally towards sPosition1 I need to divide dy with the angle arctan between the two objects */
var dx = 4;
var dy = 4/angleRadians0;
function moveShot(){
// Deleting the "old" square
flw.clearRect(0, 0, 700, 700);
//Drawing the square at its appropriate position
flw.fillRect(xPosition0, yPosition0, 100, 100);
//Adding the movement after every frame
xPosition0 += dx;
yPosition0 += dy;
setTimeout(moveShot, 20);
}
CSS:
#myCanvas1{
height: 100px;
width: 100px;
background-color: '#ff0000';
}
ありがとう!
編集: 実際に何が起こったかについては、私は非常に混乱しています。何も起こらない、私はそれが午前3時であると私は言っていないと私は誰かが非常に明白な間違いを指摘したと思って、すべてが意味を作るだろうと思った。だから、何が起こるのはうまく、何もない、なぜ私は理解できません。それから、もう一度、その午前3時と私はどこかで台無しにしたかもしれませんが、私はどこに見えません。
ここでフィドルhttps://jsfiddle.net/Snubben/15tf0svd/3/
あなたは何が起こると予想しているのか、実際に何が起こるのかは述べていません。人々がより少ない労力で助けを提供できるように、詳細を追加できますか? –
あなたは正確に何を動こうとしていますか?あなたは複数のキャンバスを持っています。そして、あなたのmoveShot関数は、最初のキャンバスの内側にある四角形を動かしているように見えます。 ... –