Skip to content

Instantly share code, notes, and snippets.

@tatocaster
Last active September 23, 2015 21:29
Show Gist options
  • Save tatocaster/1acb3f4c1ded437cde9e to your computer and use it in GitHub Desktop.
Save tatocaster/1acb3f4c1ded437cde9e to your computer and use it in GitHub Desktop.
Rotating Arc on canvas https://tatocaster.me/arc.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Rotating Arc on canvas</title>
</head>
<body>
<canvas id="canvas" width="400" height="150"></canvas>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
/**
* @author tatocaster <kutaliatato@gmail.com>
*/
(function(window){
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
context.strokeStyle = "red";
context.lineWidth = "5";
// ცენტრის მიმართ კუთხის გამოსათვლელად
var canvasOffsetX = canvas.width/2;
var canvasOffsetY = canvas.height/2;
var radiusX = canvas.width/2;
var radiusY = canvas.height/2;
var radius = 50;
function drawArc(mouseMoveEvent){
var e = mouseMoveEvent;
context.clearRect(0, 0, canvas.width, canvas.height);
// კუთხის გამოთვლა რადიანებში.
var rotateAngle = Math.atan2(parseInt(canvasOffsetX - e.clientX), parseInt(e.clientY - canvasOffsetY));
context.beginPath();
context.arc(radiusX, radiusY, radius, rotateAngle, rotateAngle + Math.PI);
context.stroke();
}
canvas.onmousemove=function(e){drawArc(e);};
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment