123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              cx:<input type="text" id="cx" value=100>
	cy:<input type="text" id="cy" value=100>
	r:<input type="text" id="r" value=1>
	R:<input type="text" id="R" value=30>
	xGap:<input type="text" id="xGap" value=0>
	yGap:<input type="text" id="yGap" value=50>


	<svg id="main" width="800" height="900"></svg>
            
          
!
            
              body {
    background-color: whitesmoke;
    margin: 0;
    padding: 0;
}
.prompt{
    position: absolute;
    top: 50%;
    left: 50%;
    border: 60px solid rgba(18, 61, 199, 0.1);
    border-radius: 50%;
}
.circle {
    cursor: -webkit-grab;
    position: absolute;
    border-radius: 50%;
    -webkit-transform: translate(-50%, -50%);
}
.tail{
    -webkit-transform: translate(-50%, -50%);
}
            
          
!
            
              (function(name, definition) {
  var sux = this.sux = this.sux || {};

  sux[name] = definition();
})('waterdrop', function() {
  var waterdrop = function() {};

  /**
   * 通过入参,计算组成水滴形状的两条贝塞尔曲线控制点.
   *
   * @method getBezierPoints
   *
   * @param  {Number} x 起始点x轴坐标
   * @param  {Number} y 起始点x轴坐标
   * @param  {Number} deltaX 起始点x轴上的间距
   * @param  {Number} deltaY 起始点y轴上的间距
   * @param  {Number} fromR 起始点的半径
   * @param  {Number} toR 目标点的半径
   * @return {Object} 返回包含x1, y1, x2, y2, ... x8, y8十六个参数,分别代表关键点
   */
  waterdrop.prototype.getBezierPoints = function(x, y, deltaX, deltaY, fromR, toR) {
    var x0, y0, // 目标点的坐标
      dX1, dY1, dX2, dY2, dX3, dY3, dX4, dY4, dX5, dY5, dX6, dY6, dX7, dY7, dX8, dY8, // 偏移量
      dis, // 起始点和目标点的距离
      deltaGarmar, // 切线和y轴的夹角
      alpha, // 两点连线和x轴的夹角
      cosAlpha, sinAlpha;

    // 计算圆2的圆点坐标
    x0 = x + deltaX;
    y0 = y + deltaY;

    // 计算距离
    dis = Math.sqrt(Math.pow((deltaX), 2) + Math.pow((deltaY), 2));
    if (dis < Math.max(fromR, toR)) {
      return {
        x1: x0,
        y1: y0,
        x2: x0,
        y2: y0,
        x3: x0,
        y3: y0,
        x4: x0,
        y4: y0,
        x5: x0,
        y5: y0,
        x6: x0,
        y6: y0,
        x7: x0,
        y7: y0,
        x8: x0,
        y8: y0
      };
    }

    // 计算两点连线的夹角
    // tan函数的角度取值范围[-π/2, π/2], 故对alpha值做第二、第三象限处理
    if (deltaX >= 0) {
      alpha = -Math.atan(deltaY / deltaX) + Math.PI / 2;
    } else {
      alpha = -Math.atan(deltaY / deltaX) + Math.PI * 3 / 2;
    }
    sinAlpha = Math.sin(alpha);
    cosAlpha = Math.cos(alpha);

    // 计算切线和y轴的夹角
    deltaGarmar = Math.asin((fromR - toR) / dis);

    // 四个点的偏移量
    dX1 = fromR * Math.cos(deltaGarmar);
    dY1 = fromR * Math.sin(deltaGarmar);
    dX2 = fromR / Math.cos(Math.PI / 4 - deltaGarmar / 2) * Math.cos(Math.PI / 4 + deltaGarmar / 2);
    dY2 = fromR;
    dX3 = toR;
    dY3 = fromR;
    dX4 = toR;
    dY4 = Math.abs(deltaY);
    dX5 = -dX1;
    dY5 = dY1;
    dX6 = -dX2;
    dY6 = dY2;
    dX7 = -dX3;
    dY7 = dY3;
    dX8 = -dX4;
    dY8 = dY4;

    return {
      x1: x + dX1 * cosAlpha + dY1 * sinAlpha,
      y1: y + dY1 * cosAlpha - dX1 * sinAlpha,
      x2: x + dX2 * cosAlpha + dY2 * sinAlpha,
      y2: y + dY2 * cosAlpha - dX2 * sinAlpha,
      x3: x + dX3 * cosAlpha + dY3 * sinAlpha,
      y3: y + dY3 * cosAlpha - dX3 * sinAlpha,
      x4: x + dX4 * cosAlpha + dY4 * sinAlpha,
      y4: y + dY4 * cosAlpha - dX4 * sinAlpha,
      x5: x + dX5 * cosAlpha + dY5 * sinAlpha,
      y5: y + dY5 * cosAlpha - dX5 * sinAlpha,
      x6: x + dX6 * cosAlpha + dY6 * sinAlpha,
      y6: y + dY6 * cosAlpha - dX6 * sinAlpha,
      x7: x + dX7 * cosAlpha + dY7 * sinAlpha,
      y7: y + dY7 * cosAlpha - dX7 * sinAlpha,
      x8: x + dX8 * cosAlpha + dY8 * sinAlpha,
      y8: y + dY8 * cosAlpha - dX8 * sinAlpha
    };
  }

  return new waterdrop;
});


function distance(x1, y1, x2, y2) {
	return Math.sqrt(Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2));
}

function distanceByDeltaValue(deltaX, deltaY) {
	return Math.sqrt(Math.pow((deltaX), 2) + Math.pow((deltaY), 2));
}

// 获取贝塞尔曲线路径
function bezierPath(x, y, deltaX, deltaY, fromR, toR) {
	var x0, y0,
		x1, y1,
		x2, y2,
		x3, y3,
		x4, y4,
		ang1, ang2, ang3, ang4;

	var dis = distance(x, y, x + deltaX, y + deltaY);

	if (dis < (fromR - toR)) {
		return '';
	}

	// 计算两条相交直线的夹脚
	var deltaGarmar = Math.asin((fromR - toR) / dis),
		deltaAlpha = Math.PI / 2 - deltaGarmar,
		deltaTheta = deltaAlpha / 3;

	// 计算4个点的夹脚
	if (deltaY < 0) {
		ang1 = -deltaGarmar;
	} else {
		ang1 = deltaGarmar;
	}

	var dX1 = fromR * Math.cos(ang1),
		dY1 = fromR * Math.sin(ang1),
		dX2 = fromR / Math.cos(Math.PI / 4 - deltaGarmar / 2) * Math.cos(Math.PI / 4 + deltaGarmar / 2),
		dY2 = fromR * deltaY / Math.abs(deltaY),
		dX3 = toR,
		dY3 = fromR * deltaY / Math.abs(deltaY),
		dX4 = toR,
		dY4 = 0;

	// 计算圆2的圆点坐标
	x0 = x + deltaX;
	y0 = y + deltaY;
	// 计算圆1的第一个切点
	x1 = x + dX1;
	y1 = y + dY1;
	// 计算圆1的第二个切点
	x2 = x + dX2;
	y2 = y + dY2;
	// 计算圆2的第一个切点
	x3 = x + dX3;
	y3 = y + dY3;
	// 计算圆2的第二个切点
	x4 = x0 + dX4;
	y4 = y0 + dY4;

	var xx4 = x0 + toR * Math.cos(deltaGarmar),
		yy4 = y0 + toR * Math.sin(deltaGarmar);

	// 计算圆1的第一个切点
	x5 = x - dX1;
	y5 = y + dY1;
	// 计算圆1的第二个切点
	x6 = x - dX2;
	y6 = y + dY2;
	// 计算圆2的第一个切点
	x7 = x - dX3;
	y7 = y + dY3;
	// 计算圆2的第二个切点
	x8 = x0 - dX4;
	y8 = y0 + dY4;

	var svgStr = 'M' + x1 + ',' + y1;
	svgStr += 'C' + x2 + ',' + y2 + ' ' + x3 + ',' + y3 + ' ' + x4 + ',' + y4;
	svgStr += ' L' + x8 + ',' + y8;
	svgStr += 'C' + x7 + ',' + y7 + ' ' + x6 + ',' + y6 + ' ' + x5 + ',' + y5;


	// // 绘制参照点1
	svg.circle(x1, y1, 1).attr({
		fill: 'red'
	});
	// 绘制参照点2
	svg.circle(x2, y2, 1).attr({
		fill: 'green'
	});
	// 绘制参照点1
	svg.circle(x3, y3, 1).attr({
		fill: 'blue'
	});
	// 绘制参照点2
	svg.circle(x4, y4, 1).attr({
		fill: 'yellow'
	});
	// 绘制参照点2
	svg.circle(xx4, yy4, 1).attr({
		fill: 'gray'
	});

	return svgStr;
}

function draw(x, y, deltaX, deltaY, fromR, toR) {
	var x0, y0,
		x1, y1,
		x2, y2,
		x3, y3,
		x4, y4,
		ang1, ang2, ang3, ang4;

	var dis = distance(x, y, x + deltaX, y + deltaY),
		dis2 = distance(x, y, x + toR, y + deltaY);

	// 计算两条相交直线的夹脚
	var deltaGarmar = Math.atan(toR / dis),
		deltaAlpha = Math.asin(fromR / dis2),
		deltaTheta = Math.PI / 2 - deltaGarmar - deltaAlpha;

	console.log(dis, dis2);

	x0 = x + deltaX;
	y0 = y + deltaY;

	x1 = x + fromR * Math.cos(deltaTheta);
	y1 = y + fromR * Math.sin(deltaTheta);

	x2 = 0;
	y2 = 0;

	x3 = 0;
	y3 = 0;

	x4 = 0;
	y4 = 0;

	// 绘制参照点1
	svg.circle(x0, y0, 1).attr({
		fill: 'red'
	});

	// 绘制参照点1
	svg.circle(x1, y1, 1).attr({
		fill: 'red'
	});
	// 绘制参照点2
	svg.circle(x2, y2, 1).attr({
		fill: 'green'
	});
	// 绘制参照点1
	svg.circle(x3, y3, 1).attr({
		fill: 'blue'
	});
	// 绘制参照点2
	svg.circle(x4, y4, 1).attr({
		fill: 'yellow'
	});
}


var R = $('#R'),
	r = $('#r'),
	cx = $('#cx'),
	cy = $('#cy'),
	xGap = $('#xGap'),
	yGap = $('#yGap');

var svg = Snap('#main');

function redraw() {
	svg.clear();

	var RV = parseInt(R.val()),
		rV = parseInt(r.val()),
		cxV = parseInt(cx.val()),
		cyV = parseInt(cy.val()),
		xGapV = parseInt(xGap.val()),
		yGapV = parseInt(yGap.val());

	// 圆main
	var c2 = svg.circle(cxV + xGapV, cyV + yGapV, rV);
	c2.attr({
		fill: 'rgba(0,0,0,0.2)'
	});

	// 圆nxt
	var c1 = svg.circle(cxV, cyV, RV);
	c1.attr({
		fill: 'rgba(0,0,0,0.2)'
	});

	// 圆nxt与圆main的衔接
	var res = sux.waterdrop.getBezierPoints(cxV, cyV, xGapV, yGapV, RV, rV);

	var svgStr = 'M' + res.x1 + ',' + res.y1;
	svgStr += 'C' + res.x2 + ',' + res.y2 + ' ' + res.x3 + ',' + res.y3 + ' ' + res.x4 + ',' + res.y4;
	svgStr += ' L' + res.x8 + ',' + res.y8;
	svgStr += 'C' + res.x7 + ',' + res.y7 + ' ' + res.x6 + ',' + res.y6 + ' ' + res.x5 + ',' + res.y5;

	var c3 = svg.path(svgStr);
	c3.attr({
		fill: 'rgba(0,0,0,0.4)'
	});
}

R.change(redraw);
r.change(redraw);
cx.change(redraw);
cy.change(redraw);
xGap.change(redraw);
yGap.change(redraw);

redraw();
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console