Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <canvas id="myCanvas"></canvas>
<form id="controls">
	<p><label><input id="speedSlider" min="-0.2" max="0.2" step="0.001" value="0.01" type="range" />speed</label></p>
</form>
<script>
              
            
!

CSS

              
                body { text-align:center; }
#myCanvas { margin:0; padding:0; z-index:-1000; }
#controls { width:80%; max-width:640px; display:block; margin:25px auto; }
input { width:80%; vertical-align:middle; }
label { width:100%; }
#controls p { width:100%; padding:0.5em 0; }
              
            
!

JS

              
                "use strict";
document.addEventListener("DOMContentLoaded", function(event) {
	let	L = {QP:38, OQ:7.8, OA:15, AB:50, BP:41.5, AC:61.9, PC:39.3, BD:55.8, PD:40.1, CE:36.7, DE:39.4, CF:49, EF:65.7};
	let	O = {x:0,y:0},
		P = {x:L.QP,y:L.OQ},	
		A = {};
	let speed = 0.05; 	//	Speed increment.
	let scale = 2;
	let alpha1 = 0,				// Phase rotation 1
		alpha2 = 2*Math.PI/3,	// Phase rotation 2
		alpha3 = -2*Math.PI/3;;	// Phase rotation 3		
	
	const tau = 2 * Math.PI;
	const canvas = document.getElementById("myCanvas");
	const ctx = canvas.getContext("2d");	
	var requestFrame = null;
	var yGround = 0;	

	// 'Set' speed slider:
	let speedSlider = document.getElementById("speedSlider");
	speedSlider.value = speed;
	speedSlider.oninput = function(e){
		speed = parseFloat(this.value);  
	};
	
	initCanvas();
	function initCanvas() {
		canvas.width = document.body.clientWidth;		
		canvas.height = scale*185;		
		ctx.translate(canvas.width/2, canvas.height/2);
		ctx.scale(scale, scale);
		ctx.lineWidth = 1/scale;
	};	

	// when the window is resized:	
	window.addEventListener("resize", function(){
		initCanvas();
		if (requestFrame) {
			cancelAnimationFrame(requestFrame);
		}		
		requestFrame = requestAnimationFrame(draw);
	}, false);	
	
	requestFrame = requestAnimationFrame(draw);
	function draw() {
		ctx.clearRect(-canvas.width, -canvas.height, 2*canvas.width, 2*canvas.height); // clear the canvas
		ctx.beginPath();
		ctx.strokeStyle = "gray";
		ctx.arc(O.x, O.y, L.OA, 0, tau);
		ctx.stroke();		
		drawIntersectionPoint(L.QP,P.y);
		drawIntersectionPoint(-L.QP,P.y);
		drawIntersectionPoint(O.x,O.y);			
		alpha1 += speed;
		alpha2 += speed;
		alpha3 += speed;		
		drawLinkageLeft(alpha1,'blue');
		drawLinkageLeft(alpha2,'red');
		drawLinkageLeft(alpha3,'green');		
		drawLinkageRight(alpha1,'blue');
		drawLinkageRight(alpha2,'red');
		drawLinkageRight(alpha3,'green');
		ctx.strokeStyle = "gray";
		ctx.setLineDash([1, 1]);
		drawLink(-canvas.width,yGround,canvas.width,yGround);
		ctx.setLineDash([]);
		requestFrame = requestAnimationFrame(draw);
	};
	
	function drawLinkageLeft(alpha,color) {
		ctx.strokeStyle = color;
		P.x = -L.QP;		
		A.x = O.x + L.OA*Math.cos(alpha);
		A.y = O.y + L.OA*Math.sin(alpha);
		drawLink(O.x,O.y, A.x,A.y);
		let B = new IntersectPointsOfTwoCircles(A.x,A.y,L.AB, P.x,P.y,L.BP);
		drawLink(A.x,A.y, B.x_2,B.y_2);
		drawLink(P.x,P.y, B.x_2,B.y_2);
		let C = new IntersectPointsOfTwoCircles(A.x,A.y,L.AC, P.x,P.y,L.PC);	
		drawLink(A.x,A.y, C.x_1,C.y_1);
		drawLink(P.x,P.y, C.x_1,C.y_1);
		let D = new IntersectPointsOfTwoCircles(B.x_2,B.y_2,L.BD, P.x,P.y,L.PD);	
		drawLink(B.x_2,B.y_2, D.x_2,D.y_2);
		drawLink(P.x,P.y, D.x_2,D.y_2);
		let E = new IntersectPointsOfTwoCircles(C.x_1,C.y_1,L.CE, D.x_2,D.y_2,L.DE);	
		drawLink(C.x_1,C.y_1, E.x_1,E.y_1);
		drawLink(D.x_2,D.y_2, E.x_1,E.y_1);
		let F = new IntersectPointsOfTwoCircles(C.x_1,C.y_1,L.CF, E.x_1,E.y_1,L.EF);	
		drawLink(C.x_1,C.y_1, F.x_1,F.y_1);
		drawLink(E.x_1,E.y_1, F.x_1,F.y_1);
		if ( yGround < F.y_1 ) yGround = F.y_1;
	};	
	
	function drawLinkageRight(alpha,color) {
		ctx.strokeStyle = color;		
		P.x = L.QP;		
		A.x = O.x + L.OA*Math.cos(alpha);
		A.y = O.y + L.OA*Math.sin(alpha);
		drawLink(O.x,O.y, A.x,A.y);
		let B = new IntersectPointsOfTwoCircles(A.x,A.y,L.AB, P.x,P.y,L.BP);
		drawLink(A.x,A.y, B.x_1,B.y_1);
		drawLink(P.x,P.y, B.x_1,B.y_1);
		let C = new IntersectPointsOfTwoCircles(A.x,A.y,L.AC, P.x,P.y,L.PC);	
		drawLink(A.x,A.y, C.x_2,C.y_2);
		drawLink(P.x,P.y, C.x_2,C.y_2);
		let D = new IntersectPointsOfTwoCircles(B.x_1,B.y_1,L.BD, P.x,P.y,L.PD);	
		drawLink(B.x_1,B.y_1, D.x_1,D.y_1);
		drawLink(P.x,P.y, D.x_1,D.y_1);
		let E = new IntersectPointsOfTwoCircles(C.x_2,C.y_2,L.CE, D.x_1,D.y_1,L.DE);	
		drawLink(C.x_2,C.y_2, E.x_2,E.y_2);
		drawLink(D.x_1,D.y_1, E.x_2,E.y_2);
		let F = new IntersectPointsOfTwoCircles(C.x_2,C.y_2,L.CF, E.x_2,E.y_2,L.EF);	
		drawLink(C.x_2,C.y_2, F.x_2,F.y_2);
		drawLink(E.x_2,E.y_2, F.x_2,F.y_2);		
	};	

	function drawLink(x1,y1,x2,y2) {
		ctx.beginPath();
		ctx.moveTo(x1, y1);
		ctx.lineTo(x2, y2);
		ctx.stroke();
	};
	
	function drawIntersectionPoint(x,y) {
		if (x != null && y != null) {
			ctx.beginPath();
			ctx.fillStyle = "gray";
			ctx.arc(x, y, 3/scale, 0, tau);
			ctx.fill();
		}
	};
	
	// Constructor of the coordinates of the intersection points of two circles:
	function IntersectPointsOfTwoCircles(x1,y1,r1, x2,y2,r2) {
		this.x_1 = null;	// x coordinate of first intersection point.
		this.y_1 = null;	// y coordinate of first intersection point.
		this.x_2 = null;	// x coordinate of second intersection point.
		this.y_2 = null;	// y coordinate of second intersection point.		
		let dx = x2 - x1,
			dy = y2 - y1,
			r1r1 = r1 * r1,
			r2r2 = r2 * r2;		
		let d = Math.sqrt(dx * dx + dy * dy); // distance between two centers.
		if ( d > r1 + r2 || d < Math.abs(r1 - r2) || ( d == 0 && r1 == r2) ) { return; } // if no intersection or infinite intersections, bail out
		let a = (r1r1 - r2r2 + d*d) / (2*d);
		let h = Math.sqrt(r1r1 - a*a);		
		let x_1a = x1 + (a*dx)/d,
			x_1b = (h*dy)/d;
			this.x_1 = x_1a + x_1b;
			if ( a !== r1 ) this.x_2 = x_1a - x_1b; // if ( a == r1 ) circles are tangent; only one touch point, ie. x2 = x1.
		let y_1a = y1 + (a*dy)/d,
			y_1b = (h*dx)/d;
			this.y_1 = y_1a - y_1b;
			if ( a !== r1 ) this.y_2 = y_1a + y_1b; // if ( a == r1 ) circles are tangent; only one touch point, ie. y2 = y1.	
	};	
});
              
            
!
999px

Console