Rotate a line around a circle


jack blank

I have two arcs with strokes and I want an animated line between them. The animation of the line should be perpendicular to the points of the inner circle.

This is what I want, almost what I want.

The problem is:

  • The length of the wire is not the length between the 2 turns because it rotates around the inner turn.
  • Sometimes the line is not perpendicular to the point of the inner circle. For example, when it gets to the rounded corners, it's tilted a little bit.

I'm having trouble understanding how to use the trig function lineTo. Probably because that changes the length of the line and I don't know how to get the x and y coordinates of the outer circle to get the end point of the line. I'm used to it math.cos * lengthand this gives me a slash. I have nothing, I just want the coordinates of the outer circle.

window.onload = function(){
	var canvas = document.getElementById("canvas");
	var context = canvas.getContext("2d");
	function lineAtAngle(startX, startY, angleDeg, length, startX2, startY2, angleDeg2, length2){
			var angle = angleDeg * (Math.PI / 180);
			var angle2 = angleDeg2 * (Math.PI / 180);
			// context.moveTo(startX, startY);
			context.beginPath();
			context.moveTo(
				Math.cos(angle) * length + startX,
				Math.sin(angle) * length + startY
				)
			 context.lineTo(
				Math.cos(angle2) *(length2 )+ startX2,
				Math.sin(angle2)  *(length2) + startY2
				)
			// context.lineTo(canvas.width / 2 + 60, canvas.height / 2, angle2, length2)
			
			context.lineWidth = 10;
			context.stroke();
			context.closePath();
			console.log("startX2: " + startX2 + " startY2: " + startY2 )
			console.log(Math.sin(angle2) + startY2)
			console.log(length)
	}
	function myLineTo(startX, startY, angleDeg, length){

	}
	var length1 = canvas.width / 2 + 60 - canvas.width / 2 -30
	var length2 = canvas.width / 2 ;
	// var length2 = 1;
	console.log(length2)
	var angle1 = 0;
	var angle2 = 0;
	(function animate(){
		context.clearRect(0,0, canvas.width, canvas.height);
		window.requestAnimationFrame(animate);	
		context.beginPath()
		context.arc(canvas.width / 2, canvas.height / 2, 30, 0, 2 * Math.PI, true)
		context.lineWidth = 1;
		context.stroke()

		context.beginPath();
		context.arc(canvas.width / 2, canvas.height / 2, 60, 0, 2 * Math.PI, true);
		context.stroke();
		context.closePath()

		context.beginPath();
		context.arc(canvas.width / 2, canvas.height / 2, 3, 0, 2 * Math.PI, true)
		context.fill()
		context.closePath();
		angle1++
		angle2++
		// lineAtAngle(canvas.width / 2 , canvas.height / 2 , angle1, length1, canvas.width / 2 + 60, canvas.height / 2, angle2, length2 )
		lineAtAngle(canvas.width / 2 , canvas.height / 2 , angle1, length1, canvas.width / 2 + 60, canvas.height / 2, angle2, length2 )




	}())

}
		canvas{
			background: #aaa;
		}
<canvas id="canvas" width="400" height="400"></canvas>

Steven Hansen

The idea of ​​drawing a line is that you need to provide a start point ( moveTo) and an end point ( lineTo). Your current code complicates the whole thing with multiple angles and lengths. You want to imagine that what you are doing is starting from the center of the circle and adding an offset to place the starting point on the edge of the inner circle. Using trig here is great. The length of the line will simply be the outer radius minus the inner radius, which is the same direction as the offset (only one angle is needed).

Without fundamentally changing your approach, the code below shows the changes you can use to try this out. Let the linefunction take a start point (x,y) and offset and angle and length. The starting point is the center of the circle, and the offset is the radius of the inner circle. The length (again) is the outer radius minus the inner radius.

window.onload = function(){
   var innerCircleRadius = 30;
   var outerCircleRadius = 60;
   var canvas = document.getElementById("canvas");
   var context = canvas.getContext("2d");
   var angle1 = 0;
   function lineAtAngle(startX, startY, angleDeg, offset, length) {
      var angle = angleDeg * (Math.PI / 180); // Convert to radians.
      var cosAngle = Math.cos(angle); // Only need cos(angle) once.
      var sinAngle = Math.sin(angle); // Only need sin(angle) once.
      var startXPos = cosAngle * offset + startX; 
      var startYPos = sinAngle * offset + startY; 
      var endXPos = cosAngle * length + startXPos;
      var endYPos = sinAngle * length + startYPos;
      context.beginPath();
            
      context.moveTo(startXPos, startYPos);
      context.lineTo(endXPos, endYPos); 
			
      context.lineWidth = 10;
      context.stroke();
      context.closePath();
   }
   (function animate() {
	  context.clearRect(0,0, canvas.width, canvas.height);
	  window.requestAnimationFrame(animate);	
	  context.beginPath()
	  context.arc(canvas.width / 2, canvas.height / 2, innerCircleRadius, 0, 2 * Math.PI, true)
	  context.lineWidth = 1;
	  context.stroke()

	  context.beginPath();
	  context.arc(canvas.width / 2, canvas.height / 2, outerCircleRadius, 0, 2 * Math.PI, true);
	  context.stroke();
	  context.closePath()

	  context.beginPath();
	  context.arc(canvas.width / 2, canvas.height / 2, 3, 0, 2 * Math.PI, true)
	  context.fill()
	  context.closePath();
	  angle1++
	  lineAtAngle(canvas.width / 2 , canvas.height / 2 , angle1, innerCircleRadius, outerCircleRadius - innerCircleRadius);
   }())
}
canvas {
   background: #aaa;
}
<canvas id="canvas" width="400" height="400"></canvas>

Related


Rotate a line around a circle

jack blank I have two arcs with strokes and I want an animated line between them. The animation of the line should be perpendicular to the points of the inner circle. This is what I want, almost what I want. The problem is: The length of the wire is not the le

Rotate a line around a circle

jack blank I have two arcs with strokes and I want an animated line between them. The animation of the line should be perpendicular to the points of the inner circle. This is what I want, almost what I want. The problem is: The length of the wire is not the le

Rotate a line around a circle

jack blank I have two arcs with strokes and I want an animated line between them. The animation of the line should be perpendicular to the points of the inner circle. This is what I want, almost what I want. The problem is: The length of the wire is not the le

Rotate a line around a circle

jack blank I have two arcs with strokes and I want an animated line between them. The animation of the line should be perpendicular to the points of the inner circle. This is what I want, almost what I want. The problem is: The length of the wire is not the le

Rotate a line around a circle

jack blank I have two arcs with strokes and I want an animated line between them. The animation of the line should be perpendicular to the points of the inner circle. This is what I want, almost what I want. The problem is: The length of the wire is not the le

Rotate line around circle in three.js

Free to live var lineGeometry = new THREE.Geometry(); var lineMaterial = new THREE.LineBasicMaterial({ color: 0x000000 }); lineGeometry.vertices.push( new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 10, 0), ); var line = new THREE.Line(lineGeomet

Rotate line around circle in three.js

Free to live var lineGeometry = new THREE.Geometry(); var lineMaterial = new THREE.LineBasicMaterial({ color: 0x000000 }); lineGeometry.vertices.push( new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 10, 0), ); var line = new THREE.Line(lineGeomet

Rotate line around circle in three.js

Free to live var lineGeometry = new THREE.Geometry(); var lineMaterial = new THREE.LineBasicMaterial({ color: 0x000000 }); lineGeometry.vertices.push( new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 10, 0), ); var line = new THREE.Line(lineGeomet

Rotate line around circle in three.js

Free to live var lineGeometry = new THREE.Geometry(); var lineMaterial = new THREE.LineBasicMaterial({ color: 0x000000 }); lineGeometry.vertices.push( new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 10, 0), ); var line = new THREE.Line(lineGeomet

Rotate line around circle in three.js

Free to live var lineGeometry = new THREE.Geometry(); var lineMaterial = new THREE.LineBasicMaterial({ color: 0x000000 }); lineGeometry.vertices.push( new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 10, 0), ); var line = new THREE.Line(lineGeomet

Rotate text around a circle

dbj44 I want to rotate some text (numbers) positioned around a circle so that it looks like this: In order to apply the rotation, I've tried this: .attr("transform", function(d, i) { return "rotate(" + (-90 + ((360 / dial.length) * i)) + ", 135, 135)"; });but

Rotate the box around a circle

DannieCoderBoi In the process of learning Javascript/jQuery - I've been trying to figure out how to do it, and I think I've completely screwed up JSFiddle. Fiddle : http://jsfiddle.net/y7qWw/ _ What I want to do is rotate the div around a circle so that it is

Rotate text around a circle

dbj44 I want to rotate some text (numbers) positioned around a circle so that it looks like this: In order to apply the rotation, I've tried this: .attr("transform", function(d, i) { return "rotate(" + (-90 + ((360 / dial.length) * i)) + ", 135, 135)"; });but

Rotate the box around a circle

DannieCoderBoi In the process of learning Javascript/jQuery - I've been trying to figure out how to do it, and I think I've completely screwed up JSFiddle. Fiddle : http://jsfiddle.net/y7qWw/ _ What I want to do is rotate the div around a circle so that it is

Rotate circle around triangle canvas

KX0 I want to rotate a circle around a triangle using canvas. Had this code earlier, but here is the circle in the middle, and a rotated rectangle, I want the circle to rotate and have a triangle in the middle. Can someone help? Here is the JS code I have: var

Rotate svg path around circle

Usama nadeem I want to rotate a polygon around a circle. I want to fix the starting point of the polygon to the center of the circle like this http://www.enspiregroup.com . I tried a lot but got stuck. See code below. CSS and HTML5 .circle-segment {

Rotate circle around triangle canvas

KX0 I want to rotate a circle around a triangle using canvas. Had this code earlier, but here is the circle in the middle, and a rotated rectangle, I want the circle to rotate and have a triangle in the middle. Can someone help? Here is the JS code I have: var

Rotate svg path around circle

Usama nadeem I want to rotate a polygon around a circle. I want to fix the starting point of the polygon to the center of the circle like this http://www.enspiregroup.com . I tried a lot but got stuck. See code below. CSS and HTML5 .circle-segment {

Rotate svg path around circle

Usama nadeem I want to rotate a polygon around a circle. I want to fix the starting point of the polygon to the center of the circle like this http://www.enspiregroup.com . I tried a lot but got stuck. See code below. CSS and HTML5 .circle-segment {

Rotate svg path around circle

Usama nadeem I want to rotate a polygon around a circle. I want to fix the starting point of the polygon to the center of the circle like this http://www.enspiregroup.com . I tried a lot but got stuck. See code below. CSS and HTML5 .circle-segment {

Rotate svg path around circle

Usama nadeem I want to rotate a polygon around a circle. I want to fix the starting point of the polygon to the center of the circle like this http://www.enspiregroup.com . I tried a lot but got stuck. See code below. CSS and HTML5 .circle-segment {

Rotate circle around triangle canvas

KX0 I want to rotate a circle around a triangle using canvas. Had this code earlier, but here is the circle in the middle, and a rotated rectangle, I want the circle to rotate and have a triangle in the middle. Can someone help? Here is the JS code I have: var

Rotate svg path around circle

Usama nadeem I want to rotate a polygon around a circle. I want to fix the starting point of the polygon to the center of the circle like this http://www.enspiregroup.com . I tried a lot but got stuck. See code below. CSS and HTML5 .circle-segment {

Rotate svg path around circle

Usama nadeem I want to rotate a polygon around a circle. I want to fix the starting point of the polygon to the center of the circle like this http://www.enspiregroup.com . I tried a lot but got stuck. See code below. CSS and HTML5 .circle-segment {

Rotate svg path around circle

Usama nadeem I want to rotate a polygon around a circle. I want to fix the starting point of the polygon to the center of the circle like this http://www.enspiregroup.com . I tried a lot but got stuck. See code below. CSS and HTML5 .circle-segment {

Rotate a circle around another rotation circle

Suhas Batu I have a circle in an svg that rotates around a given point. I added another circle. I want to rotate the second circle around the first rotation circle. How can I do this? So far I am able to rotate the first circle around a point. The second circl

Rotate an image with CSS without moving around a circle

you can: I have an image of a wind turbine that I want to rotate. The code below will do the job, but with the wind turbine spinning in one circle. How to make it not rotate around the circle? any suggestion <!DOCTYPE html> <html> <head> <style> .rotate

Rotate an object around a circle using CSS?

Andrew Leonardi I am trying to rotate three objects around a circle. So far I have been able to make an object rotate around a circle. If I don't mess with the code, I won't get anywhere. Can anyone suggest the best way to do this? Here is part of the code and

Rotate rectangle around circumference of circle on canvas

Mussmanson I'm trying to create a circular "equalizer" effect using JavaScript and HTML canvas for a small project I'm working on, and it works great, except for one small thing. It's just a series of rectangular bars moving to the mp3 over time, nothing fancy