我有三个点不在同一直线上,本来我想通过这三点,我做到了画一个圆弧。 而Chrome实际上没有真正拉圈,但使用一些贝塞尔曲线来假装它是圆的,因为贝塞尔曲线很便宜。 如果Chrome正在做,作为一个中间人,为什么不是我画的圆样贝塞尔自己(二贝塞尔,从点1到中间点,中间点至3点)? 这将是更清洁,而且价格便宜(2相比,数量不明的贝塞尔曲线浏览器决定的)。 这就是我在哪里卡住了,怎么样? 应该在哪里了“控制点”是什么?
这是在JavaScript中我的老画弧功能
drawArc = function(startPoint, thirdPoint, endPoint){
var ctx = this.ctx;
ctx.lineWidth = this.strokeWidth;
ctx.strokeStyle = this.strokeColor;
var centerObject = circleCenter( new Point(startPoint.x, startPoint.y),
new Point(thirdPoint.x, thirdPoint.y),
new Point(endPoint.x, endPoint.y) );
var centerX = centerObject.x;
var centerY = centerObject.y;
var r = centerObject.r
var angle = Math.atan2(centerX-startPoint.x, centerY-startPoint.y);
// console.log(centerObject);
if (!angle){
ctx.beginPath();
ctx.moveTo(startPoint.x, startPoint.y);
ctx.lineTo(endPoint.x, endPoint.y);
} else {
if( angle > Math.PI/2) {
ctx.beginPath();
ctx.arc(centerX, centerY, r, Math.PI * 1.5-angle, Math.PI * 1.5 + angle, true);
} else {
ctx.beginPath();
ctx.arc(centerX, centerY, r, Math.PI * 1.5-angle, Math.PI * 1.5 + angle, false);
}
}
ctx.globalCompositeOperation = "source-over";
ctx.stroke();
}
var circleCenter = function(startPoint, thirdPoint, endPoint){
var dy1 = thirdPoint.y - startPoint.y;
var dx1 = thirdPoint.x - startPoint.x;
var dy2 = endPoint.y - thirdPoint.y;
var dx2 = endPoint.x - thirdPoint.x;
var aSlope = dy1/dx1;
var bSlope = dy2/dx2;
var centerX = (aSlope*bSlope*(startPoint.y - endPoint.y) + bSlope*(startPoint.x + thirdPoint.x)
- aSlope*(thirdPoint.x+endPoint.x) )/( 2* (bSlope-aSlope) );
var centerY = -1*(centerX - (startPoint.x+thirdPoint.x)/2)/aSlope + (startPoint.y+thirdPoint.y)/2;
var r = dist(centerX, centerY, startPoint.x, startPoint.y)
return {
x: centerX,
y: centerY,
r: r
};
}
谁能帮我改写drawArc功能使用画布bezierCurveTo()方法,而不是ARC()?
我的代码例子是在这里: http://codepen.io/wentin/pen/VYegqq