JSFiddle - React, Tailwind, and code Playground
HTML
<div id="content" >
<div id="start" class="button">
Start
</div>
<div id="stop" class="button">
Stop
</div>
<div id="clear" class="button">
Clear
</div>
<p id="infoBar">
test
</p>
<canvas id="CanvasCurve" width="800px" height="600px" >
</canvas>
</div>
CSS
#CanvasCurve {
background-color:black
}
.button {
border: 2px solid #CCC;
background-color: #FFF;
width: auto;
margin: 0px;
padding-left: 20px;
margin: 3px;
width: 50px;
}
.button:hover
{
cursor: pointer;
background-color:yellow;
}
JavaScript
var canvas;
var context;
var left, right, direction, x, y, angel;
var interval;
function init() {
context.fillStyle = "magenta";
angel = 0;
x = 400, y = 300;
left = 37, right = 39;
$("#infoBar").html("Direction: " + direction + " angel: " + angel);
}
canvas = document.getElementById("CanvasCurve");
context = canvas.getContext("2d");
init();
window.onkeydown = keydownControl;
window.onkeyup = keyupControl;
$('#start').click(function() {
jQuery.fn.timer = function() {
move(direction);
}
interval = window.setInterval(function() {
$("#infoBar").timer();
}, 100);
});
$('#stop').click(function() {
clearInterval(interval);
});
$('#clear').click(function() {
clearInterval(interval);
canvas.width = canvas.width;
init();
});
function move(direction) {
if(direction == left) {
angel = (angel - 5) % 360;
if(angel < 0) angel += 360;
} else if (direction == right) {
angel = (angel + 5) % 360;
}
x = (x + Math.round(Math.cos(angel*0.0174532925)*5)) % 800;
if (x < 0) x += 800;
y = (y + Math.round(Math.sin(angel*0.0174532925)*5)) % 600;
if (y < 0) y += 600;
$("#infoBar").html("Direction: " + direction + " angel: " + angel + " x,y:" + x + "," + y);
drawPoint(x,y);
}
function drawPoint(x, y) {
context.beginPath();
context.arc(x,y,5,0,Math.PI*2,true);
context.closePath();
context.fill();
}
function keydownControl(e) {
direction = e.keyCode;
}
function keyupControl(e) {
direction = "none";
}