1 Star 0 Fork 0

坐看彼岸花开花败/贝塞尔曲线路径

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
bezier.js 1.91 KB
一键复制 编辑 原始数据 按行查看 历史
坐看彼岸花开花败 提交于 2020-11-27 10:12 . 支持曲线运动
export default class Bezier {
constructor(start, c1, c2, end, style) {
this.start = start;
this.c1 = c1;
this.c2 = c2;
this.end = end;
style = Object.assign({
color: "#ffffff",
lineWidth: 1,
shadowColor: "#ffffff",
shadowBlur: 0,
shadowOffsetX: 0,
shadowOffsetY: 0
}, style);
Object.assign(this, style);
}
draw(ctx, debug) {
const that = this;
ctx.beginPath();
ctx.shadowBlur = this.shadowBlur;
ctx.shadowOffsetX = this.shadowOffsetX;
ctx.shadowOffsetY = this.shadowOffsetY;
ctx.shadowColor = this.shadowColor;
ctx.strokeStyle = this.color;
ctx.lineWidth = this.lineWidth;
ctx.moveTo(that.start[0], that.start[1]);
ctx.bezierCurveTo(that.c1[0], that.c1[1], that.c2[0], that.c2[1], that.end[0], that.end[1]);
ctx.stroke();
ctx.closePath();
ctx.shadowBlur = 0;
if (debug) {
ctx.fillStyle = "#00ff00";
drawPoint(ctx, that.start, 2);
drawPoint(ctx, that.end, 2);
}
}
getPos(t) {
if (t > 1 || t < 0) {
return null;
}
const that = this;
const pow = Math.pow;
const a = pow(1 - t, 3),
b = 3 * t * pow(1 - t, 2),
c = 3 * pow(t, 2) * (1 - t),
d = pow(t, 3);
const x = a * that.start[0] + b * that.c1[0] + c * that.c2[0] + d * that.end[0];
const y = a * that.start[1] + b * that.c1[1] + c * that.c2[1] + d * that.end[1];
return [x, y];
}
}
function drawPoint(ctx, p, size) {
ctx.beginPath();
ctx.arc(p[0], p[1], size, 0, Math.PI * 2);
ctx.fill();
ctx.closePath();
}
function drawLine(ctx, p1, p2) {
ctx.beginPath();
ctx.moveTo(p1[0], p1[1]);
ctx.lineTo(p2[0], p2[1]);
ctx.stroke();
ctx.closePath();
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/sitseebloomfade/bessel-curve-path.git
git@gitee.com:sitseebloomfade/bessel-curve-path.git
sitseebloomfade
bessel-curve-path
贝塞尔曲线路径
master

搜索帮助