html5画线条输出坐标点位,如何使用javascript HTML5画布通过N个点绘制平滑曲线?...

有点晚了,但是为了记录。

您可以通过使用基数样条线(也称为规范样条线)绘制穿过点的平滑曲线来实现平滑线条。

我为画布制作了这个功能 - 它分为三个功能,以增加多功能性。主包装函数如下所示:function drawCurve(ctx, ptsa, tension, isClosed, numOfSegments, showPoints) {

showPoints  = showPoints ? showPoints : false;

ctx.beginPath();

drawLines(ctx, getCurvePoints(ptsa, tension, isClosed, numOfSegments));

if (showPoints) {

ctx.stroke();

ctx.beginPath();

for(var i=0;i

ctx.rect(ptsa[i] - 2, ptsa[i+1] - 2, 4, 4);

}}

绘制曲线的数组中包含x,y点的顺序:x1,y1, x2,y2, ...xn,yn。

像这样使用它:var myPoints = [10,10, 40,30, 100,10]; //minimum two pointsvar tension = 1;drawCurve(ctx, myPoints); //default tension=0.5drawCurve(ctx, myPoints, tension);

上面的函数调用两个子函数,一个用于计算平滑点。这将返回一个带有新点的数组 - 这是计算平滑点的核心函数:function getCurvePoints(pts, tension, isClosed, numOfSegments) {

// use input value if provided, or use a default value

tension = (typeof tension != 'undefined') ? tension : 0.5;

isClosed = isClosed ? isClosed : false;

numOfSegments = numOfSegments ? numOfSegments : 16;

var _pts = [], res = [],    // clone array

x, y,           // our x,y coords

t1x, t2x, t1y, t2y, // tension vectors

c1, c2, c3, c4,     // cardinal points

st, t, i;       // steps based on num. of segments

// clone array so we don't change the original

//

_pts = pts.slice(0);

// The algorithm require a previous and next point to the actual point array.

// Check if we will draw closed or open curve.

// If closed, copy end points to beginning and first points to end

// If open, duplicate first points to befinning, end points to end

if (isClosed) {

_pts.unshift(pts[pts.length - 1]);

_pts.unshift(pts[pts.length - 2]);

_pts.unshift(pts[pts.length - 1]);

_pts.unshift(pts[pts.length - 2]);

_pts.push(pts[0]);

_pts.push(pts[1]);

}

else {

_pts.unshift(pts[1]);   //copy 1. point and insert at beginning

_pts.unshift(pts[0]);

_pts.push(pts[pts.length - 2]); //copy last point and append

_pts.push(pts[pts.length - 1]);

}

// ok, lets start..

// 1. loop goes through point array

// 2. loop goes through each segment between the 2 pts + 1e point before and after

for (i=2; i 

for (t=0; t <= numOfSegments; t++) {

// calc tension vectors

t1x = (_pts[i+2] - _pts[i-2]) * tension;

t2x = (_pts[i+4] - _pts[i]) * tension;

t1y = (_pts[i+3] - _pts[i-1]) * tension;

t2y = (_pts[i+5] - _pts[i+1]) * tension;

// calc step

st = t / numOfSegments;

// calc cardinals

c1 =   2 * Math.pow(st, 3)  - 3 * Math.pow(st, 2) + 1;

c2 = -(2 * Math.pow(st, 3)) + 3 * Math.pow(st, 2);

c3 =       Math.pow(st, 3)  - 2 * Math.pow(st, 2) + st;

c4 =       Math.pow(st, 3)  -     Math.pow(st, 2);

// calc x and y cords with common control vectors

x = c1 * _pts[i]    + c2 * _pts[i+2] + c3 * t1x + c4 * t2x;

y = c1 * _pts[i+1]  + c2 * _pts[i+3] + c3 * t1y + c4 * t2y;

//store points in array

res.push(x);

res.push(y);

}

}

return res;}

实际绘制点作为平滑曲线(或任何其他分段线,只要你有一个x,y数组):function drawLines(ctx, pts) {

ctx.moveTo(pts[0], pts[1]);

for(i=2;i

ctx.beginPath();

drawLines(ctx, getCurvePoints(ptsa, tension, isClosed, numOfSegments));

if (showPoints) {

ctx.beginPath();

for(var i=0;i

ctx.rect(ptsa[i] - 2, ptsa[i+1] - 2, 4, 4);

}

ctx.stroke();}var myPoints = [10,10, 40,30, 100,10, 200, 100, 200, 50, 250, 120]; //minimum two pointsvar tension = 1;drawCurve(ctx, myPoints); //default tension=0.5drawCurve(ctx, myPoints, tension);function getCurvePoints(pts, tension, isClosed, numOfSegments) {

// use input value if provided, or use a default value

tension = (typeof tension != 'undefined') ? tension : 0.5;

isClosed = isClosed ? isClosed : false;

numOfSegments = numOfSegments ? numOfSegments : 16;

var _pts = [], res = [],// clone array

x, y,// our x,y coords

t1x, t2x, t1y, t2y,// tension vectors

c1, c2, c3, c4,// cardinal points

st, t, i;// steps based on num. of segments

// clone array so we don't change the original

//

_pts = pts.slice(0);

// The algorithm require a previous and next point to the actual point array.

// Check if we will draw closed or open curve.

// If closed, copy end points to beginning and first points to end

// If open, duplicate first points to befinning, end points to end

if (isClosed) {

_pts.unshift(pts[pts.length - 1]);

_pts.unshift(pts[pts.length - 2]);

_pts.unshift(pts[pts.length - 1]);

_pts.unshift(pts[pts.length - 2]);

_pts.push(pts[0]);

_pts.push(pts[1]);

}

else {

_pts.unshift(pts[1]);//copy 1. point and insert at beginning

_pts.unshift(pts[0]);

_pts.push(pts[pts.length - 2]);//copy last point and append

_pts.push(pts[pts.length - 1]);

}

// ok, lets start..

// 1. loop goes through point array

// 2. loop goes through each segment between the 2 pts + 1e point before and after

for (i=2; i 

for (t=0; t <= numOfSegments; t++) {

// calc tension vectors

t1x = (_pts[i+2] - _pts[i-2]) * tension;

t2x = (_pts[i+4] - _pts[i]) * tension;

t1y = (_pts[i+3] - _pts[i-1]) * tension;

t2y = (_pts[i+5] - _pts[i+1]) * tension;

// calc step

st = t / numOfSegments;

// calc cardinals

c1 =   2 * Math.pow(st, 3) - 3 * Math.pow(st, 2) + 1;

c2 = -(2 * Math.pow(st, 3)) + 3 * Math.pow(st, 2);

c3 =    Math.pow(st, 3)- 2 * Math.pow(st, 2) + st;

c4 =    Math.pow(st, 3)-   Math.pow(st, 2);

// calc x and y cords with common control vectors

x = c1 * _pts[i]+ c2 * _pts[i+2] + c3 * t1x + c4 * t2x;

y = c1 * _pts[i+1]+ c2 * _pts[i+3] + c3 * t1y + c4 * t2y;

//store points in array

res.push(x);

res.push(y);

}

}

return res;}function drawLines(ctx, pts) {

ctx.moveTo(pts[0], pts[1]);

for(i=2;i

This results in this:

You can easily extend the canvas so you can call it like this instead:ctx.drawCurve(myPoints);

Add the following to the javascript:if (CanvasRenderingContext2D != 'undefined') {

CanvasRenderingContext2D.prototype.drawCurve =

function(pts, tension, isClosed, numOfSegments, showPoints) {

drawCurve(this, pts, tension, isClosed, numOfSegments, showPoints)}}

You can find a more optimized version of this on NPM (npm i cardinal-spline-js) or on GitLab.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值