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

              
                <!DOCTYPE html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Quadratic bezier through three points</title>
</head>
<body style="background-color: #DDD;">
<h1>Quadratic bezier throw 3 points</h1>
<div>
  
  Also take a look the the math paper <a target="_blank" href="https://microbians.com/mathcode">Quadratic bezier through three points! →</a> <br/><br/>
  Gabriel Suchowolski (<a href="https://microbians.com" target="_blank">microbians</a>), December, 2012
</div>
<div id="little">Thanks to 艾蔓草 xhhjin for the code (that I fork) implementing my math paper.</div>
  <br/>
</div>
<input type="checkbox" id="cbThrough" name="through"/>Primitive quadratic Bezier (as control points)</input><br/><br/>
<canvas id="canvas" height="500" width="800" class="through" style="cursor: default; background-color: #FFF;"></canvas>
</body>
</html>
              
            
!

CSS

              
                html, body { font-family: sans-serif; height: 100%; margin:0;
padding:10px;
}
canvas { display:block;}
#btnControl { font-size:1em; position: absolute; top: 10px; left: 10px; }
#btnSplit { font-size:1em; position: absolute; top: 35px; left: 10px; }
#text { position: absolute; top: 75px; left: 10px; }
a {
  text-decoration: none;
  font-weight:700;
  color: #2f94e2;
}
#little { font-size:.7em; color:#a0a0a0; position: absolute; top: 775px; left: 10px; }
              
            
!

JS

              
                (function() {

	var canvas, ctx, point, style, drag = null, dPoint;

	// define initial points
	function Init() {
		point = {
			p1: { x:200, y:350 },
			p2: { x:600, y:350 }
		};
		point.cp1 = { x: 500, y: 200 };
		
		// default styles
		style = {
			curve:	{ width: 2, color: "#333" },
			cpline:	{ width: 1, color: "#C00" },
			curve1:	{ width: 1, color: "#2f94e2" },
			curve2: { width: 1, color: "#2f94e2" },
			point: { radius: 10, width: 2, color: "#2f94e2", fill: "rgba(200,200,200,0.5)", arc1: 0, arc2: 2 * Math.PI }
		}
		
		// line style defaults
		ctx.lineCap = "round";
		ctx.lineJoin = "round";

		// event handlers
		canvas.onmousedown = DragStart;
		canvas.onmousemove = Dragging;
		canvas.onmouseup = canvas.onmouseout = DragEnd;
		
		DrawCanvas();
	}
	
	
	// draw canvas
	function DrawCanvas() {
		ctx.clearRect(0, 0, canvas.width, canvas.height);
		
		// control lines
		ctx.lineWidth = style.cpline.width;
		ctx.strokeStyle = style.cpline.color;
		ctx.beginPath();
		ctx.moveTo(point.p1.x, point.p1.y);
		ctx.lineTo(point.cp1.x, point.cp1.y);
		ctx.lineTo(point.p2.x, point.p2.y);
		ctx.stroke();
		
		// curve
		ctx.lineWidth = style.curve.width;
		ctx.strokeStyle = style.curve.color;
		ctx.beginPath();
		ctx.moveTo(point.p1.x, point.p1.y);

		through = !document.getElementById("cbThrough").checked;
		if(through)
		{
			tmpx1 = point.p1.x-point.cp1.x;
			tmpx2 = point.p2.x-point.cp1.x;
			tmpy1 = point.p1.y-point.cp1.y;
			tmpy2 = point.p2.y-point.cp1.y;
			dist1 = Math.sqrt(tmpx1*tmpx1+tmpy1*tmpy1);
			dist2 = Math.sqrt(tmpx2*tmpx2+tmpy2*tmpy2);
			tmpx = point.cp1.x-Math.sqrt(dist1*dist2)*(tmpx1/dist1+tmpx2/dist2)/2;
			tmpy = point.cp1.y-Math.sqrt(dist1*dist2)*(tmpy1/dist1+tmpy2/dist2)/2;
			ctx.quadraticCurveTo(tmpx, tmpy, point.p2.x, point.p2.y);
		}
		else
		{
			ctx.quadraticCurveTo(point.cp1.x, point.cp1.y, point.p2.x, point.p2.y);
		}
		
		ctx.stroke();
		
		//new, t range is [0, 1]
		ctx.beginPath();
		ctx.lineWidth = style.curve1.width;
		ctx.strokeStyle = style.curve1.color;
		
		ctx.moveTo(point.p1.x, point.p1.y);
		
		tmpx1 = point.cp1.x-point.p1.x;
		tmpx2 = point.p2.x-point.cp1.x;
		tmpx3 = point.p2.x-point.p1.x;
		tmpy1 = point.cp1.y-point.p1.y;
		tmpy2 = point.p2.y-point.cp1.y;
		tmpy3 = point.p2.y-point.p1.y;
		//dist1 = Math.sqrt(tmpx1*tmpx1+tmpy1*tmpy1);
		//dist2 = Math.sqrt(tmpx2*tmpx2+tmpy2*tmpy2);
		dist1 = Math.sqrt(Math.sqrt(tmpx1*tmpx1+tmpy1*tmpy1));
		dist2 = Math.sqrt(Math.sqrt(tmpx2*tmpx2+tmpy2*tmpy2));
		var t1 = dist1/(dist1+dist2);	//0.5  dist1/(dist1+dist2)
		var fAX = point.p1.x;
		var fBX = (tmpx1-t1*t1*tmpx3)/(t1-t1*t1);
		var fCX = tmpx3-fBX;
		var fAY = point.p1.y;
		var fBY = (tmpy1-t1*t1*tmpy3)/(t1-t1*t1);
		var fCY = tmpy3-fBY;
		var step = 0.01
		for(var i = 0; i < (1+step); i+=step) {
			// 计算两个动点的坐标
			var t = i;
			var bx  = fAX + fBX * t + fCX * t * t;
			var by  = fAY + fBY * t + fCY * t * t;
			
			ctx.lineTo(bx, by);
		}
		ctx.stroke();
		
		//new2, t range is extended to [-1, 2]
		ctx.beginPath();
		ctx.lineWidth = style.curve2.width;
		ctx.strokeStyle = style.curve2.color;
		
		tmpx1 = point.cp1.x-point.p1.x;
		tmpx2 = point.p2.x-point.cp1.x;
		tmpx3 = point.p2.x-point.p1.x;
		tmpy1 = point.cp1.y-point.p1.y;
		tmpy2 = point.p2.y-point.cp1.y;
		tmpy3 = point.p2.y-point.p1.y;
		dist1 = Math.sqrt(Math.sqrt(tmpx1*tmpx1+tmpy1*tmpy1));
		dist2 = Math.sqrt(Math.sqrt(tmpx2*tmpx2+tmpy2*tmpy2));
		t1 = dist1/(dist1+dist2);
		fCX = (9*tmpx1-9*t1*tmpx3)/(t1*t1-t1);
		fBX = 3*tmpx3-fCX;
		fAX = point.p1.x-fBX/3-fCX/9;
		fCY = (9*tmpy1-9*t1*tmpy3)/(t1*t1-t1);
		fBY = 3*tmpy3-fCY;
		fAY = point.p1.y-fBY/3-fCY/9;
		step = 0.01;
		var a = -0.5;	// No less than -1
		var b = 1.5;	// No more than 2
		for(i = a; i < (b+step); i+=step) {
			var t = (i+1)/3;	// i-(-1)/(2-(-1))
			var bx  = fAX + fBX * t + fCX * t * t;
			var by  = fAY + fBY * t + fCY * t * t;
			
			if(i==a)
				ctx.moveTo(bx, by);
			else
				ctx.lineTo(bx, by);
		}
		ctx.stroke();

		// control points
		for (var p in point) {
			ctx.lineWidth = style.point.width;
			ctx.strokeStyle = style.point.color;
			ctx.fillStyle = style.point.fill;
			ctx.beginPath();
			ctx.arc(point[p].x, point[p].y, style.point.radius, style.point.arc1, style.point.arc2, true);
			ctx.fill();
			ctx.stroke();
		}
	}
	
	// start dragging
	function DragStart(e) {
		e = MousePos(e);
		var dx, dy;
		for (var p in point) {
			dx = point[p].x - e.x;
			dy = point[p].y - e.y;
			if ((dx * dx) + (dy * dy) < style.point.radius * style.point.radius) {
				drag = p;
				dPoint = e;
				canvas.style.cursor = "move";
				return;
			}
		}
	}
	
	
	// dragging
	function Dragging(e) {
		if (drag) {
			e = MousePos(e);
			point[drag].x += e.x - dPoint.x;
			point[drag].y += e.y - dPoint.y;
			dPoint = e;
			DrawCanvas();
		}
	}
	
	
	// end dragging
	function DragEnd(e) {
		drag = null;
		canvas.style.cursor = "default";
		DrawCanvas();
	}

	
	// event parser
	function MousePos(event) {
		event = (event ? event : window.event);
		return {
			x: event.pageX - canvas.offsetLeft,
			y: event.pageY - canvas.offsetTop
		}
	}
	
	
	// start
	canvas = document.getElementById("canvas");
	if (canvas.getContext) {
		ctx = canvas.getContext("2d");
		Init();
	}
	
})();

 





              
            
!
999px

Console