Sof*_*tic 8 javascript math geometry bezier html5-canvas
对于我的一个应用程序,我需要在Html5画布的bezier路径上画一条虚线曲线......它们之间的短划线和间隙应该是可变的...它可以在JavaFx中找到,请看这个链接 ...我想用Html5画布达到同样的效果.我知道如何绘制虚线直线,但沿着贝塞尔曲线不是曲线...
虽然我不是专家,但我知道bezier绘图算法,我用这个算法看到的问题是,它允许你使用时间参数识别贝塞尔坐标,范围从0到1 ......
这是不够的,因为要绘制一个虚线贝塞尔曲线,我需要在主贝塞尔曲线路径上绘制许多具有指定长度参数且在给定间隙距离处的小贝塞尔曲线.必须有一些JavaFx使用的算法.如果有人能帮助我,这将是伟大的.
我认为 JavaFX 正在使用一种通用技术来绘制任何虚线,并且恰好在该示例中的贝塞尔曲线上使用它。
困难的部分是弄清楚从哪里开始和停止每个破折号,这需要知道贝塞尔曲线在沿各个点的 弧长。
有一种分析方法,但我建议如下:
var bezier = function(controlPoints, t) {
/* your code here, I'll presume it returns a 2-element array of x and y. */
};
//just figure out the coordinates of all the points in each dash, don't draw.
//returns an array of arrays, each sub-array will have an even number of nu-
//merical elements, to wit, x and y pairs.
//Argument dashPattern should be an array of alternating dash and space
//lengths, e.g., [10, 10] would be dots, [30, 10] would be dashes,
//[30, 10, 10, 10] would be 30-length dash, 10-length spaces, 10-length dash
// and 10-length space.
var calculateDashedBezier = function(controlPoints, dashPattern) {
var step = 0.001; //this really should be set by an intelligent method,
//rather than using a constant, but it serves as an
//example.
//possibly gratuitous helper functions
var delta = function(p0, p1) {
return [p1[0] - p0[0], p1[1] - p0[1]];
};
var arcLength = function(p0, p1) {
var d = delta(p0, p1);
return Math.sqrt(d[0]*d[0] + d[1] * d[1]);
};
var subPaths = [];
var loc = bezier(controlPoints, 0);
var lastLoc = loc;
var dashIndex = 0;
var length = 0;
var thisPath = [];
for(var t = step; t <= 1; t += step) {
loc = bezier(controlPoints, t);
length += arcLength(lastLoc, loc);
lastLoc = loc;
//detect when we come to the end of a dash or space
if(length >= dashPattern[dashIndex]) {
//if we are on a dash, we need to record the path.
if(dashIndex % 2 == 0)
subPaths.push(thisPath);
//go to the next dash or space in the pattern
dashIndex = (dashIndex + 1) % dashPattern.length;
//clear the arclength and path.
thisPath = [];
length = 0;
}
//if we are on a dash and not a space, add a point to the path.
if(dashIndex % 2 == 0) {
thisPath.push(loc[0], loc[1]);
}
}
if(thisPath.length > 0)
subPaths.push(thisPath);
return subPaths;
};
//take output of the previous function and build an appropriate path
var pathParts = function(ctx, pathParts) {
for(var i = 0; i < pathParts.length; i++) {
var part = pathParts[i];
if(part.length > 0)
ctx.moveTo(part[0], part[1]);
for(var j = 1; j < part.length / 2; j++) {
ctx.lineTo(part[2*j], part[2*j+1]);
}
}
};
//combine the above two functions to actually draw a dashed curve.
var drawDashedBezier = function(ctx, controlPoints, dashPattern) {
var dashes = calculateDashedBezier(controlPoints, dashPattern);
ctx.beginPath();
ctx.strokeStyle = /* ... */
ctx.lineWidth = /* ... */
pathParts(ctx, dashes);
ctx.stroke();
};
Run Code Online (Sandbox Code Playgroud)
这种方法的主要问题是其不智能的粒度。当您的(小)虚线或(大)曲线的步长太大时,步长将无法正常工作,并且虚线边界不会恰好落在您想要的位置。当步长太小时,您可能最终会lineTo()在彼此相距亚像素距离的点上执行s,有时会产生 AA 伪影。过滤掉亚像素距离坐标并不难,但是生成比您真正需要的更多的“顶点”是低效的。提出更好的步长实际上是我考虑更具分析性的攻击。
使用这种方法有一个好处:如果你bezier(controlPoints, t)用其他任何评估为曲线的东西替换,你将绘制虚线! - 再次与上一段中列出的潜在问题相同。但是对于粒度问题的一个真正好的解决方案可能适用于所有“表现良好”的曲线。
将来我们可以使用context.setLineDash(segments):http:
//www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#line-styles