Zu Beginn eines Brick Breaker Spiel zielt der Spieler den Ball mit der Maus. Wenn Sie mit der linken Maustaste klicken, bewegt sich der Ball in diese Richtung. Wie erstelle ich eine Linie, die anzeigt, wohin der Ball zielt? Ich habe versucht, Beispiele zu finden, wie es geht, konnte aber nichts finden.Wie ziele ich meinen Ball in Javascript
Wenn ich setzen ctx.lineTo(cursorX, cursorY);
nichts zeigt, aber wenn ich ctx.lineTo(100, 100);
setzen es zieht die Linie. Benötige ich eine Animationsfunktion, damit die Linie an der Stelle am neuen cursorX
und cursorY
gezeichnet wird?
Mein Code:
<html>
<script type="text/javascript">
window.onload = init;
function init() {
if (window.Event) {
document.captureEvents(Event.MOUSEMOVE);
}
document.onmousemove = getCursorXY;
drawLine();
}
function getCursorXY(e) {
cursorX = document.getElementById('cursorX').value = (window.Event)
? e.pageX
: event.clientX + (document.documentElement.scrollLeft
? document.documentElement.scrollLeft
: document.body.scrollLeft);
cursorY = document.getElementById('cursorY').value = (window.Event)
? e.pageY
: event.clientY + (document.documentElement.scrollTop
? document.documentElement.scrollTop
: document.body.scrollTop);
}
function drawLine() {
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.beginPath();
ctx.moveTo(0,0);
ctx.lineTo(cursorX, cursorY);
ctx.stroke();
}
</script>
<body>
<canvas id="myCanvas" width="578" height="200"></canvas>
<input type="text" id="cursorX" size="3"> X-position of the mouse cursor
<br /><br />
<input type="text" id="cursorY" size="3"> Y-position of the mouse cursor
</body>
</html>
welchen Teil Sie eine Linie verwirrt zu schaffen? Wie bekomme ich die Endpunkte? oder wie man das verdammte Ding wirklich zeichnet? –