- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我终于找到了如何动画绘制贝塞尔曲线。我见过其他使用二次曲线来做到这一点的解决方案,但我所做的需要 4 个点,而且 b 样条很难找到随机图,而且这正是我想要做的;与贝塞尔曲线。
我的问题是,如果看不到点或线,我无法找到良好、快速的速度。我一定是错过了什么。有人可以指出我的错误或更有效的方法来在任何时间/速度上顺利进行吗?我需要它比下面的例子更稳定和更快,但如果我再这样做,差距会越来越大......
修改代码:https://jsfiddle.net/qzsy8aL7/
//B(t) = (1 - t)^3P0 + 3t(1 - t)^2 P1 + 3t^2(1 - t)P2 + t^3P3
function animatedBSpline(context, points, t) {
// Draw curve segment
context.beginPath();
context.moveTo(
Math.pow(1 - t, 3) * points[0].x +
3 * t * Math.pow(1 - t, 2) * points[1].x +
3 * Math.pow(t, 2) * (1 - t) * points[2].x +
Math.pow(t, 3) * points[3].x,
Math.pow(1 - t, 3) * points[0].y +
3 * t * Math.pow(1 - t, 2) * points[1].y +
3 * Math.pow(t, 2) * (1 - t) * points[2].y +
Math.pow(t, 3) * points[3].y
);
// Draw spline segemnts
context.lineTo(
Math.pow((1 - t) + 0.001, 3) * points[0].x +
3 * (t + 0.001) * Math.pow((1 - t) + 0.001, 2) * points[1].x +
3 * Math.pow(t + 0.001, 2) * (1 - (t + 0.001)) * points[2].x +
Math.pow(t + 0.001, 3) * points[3].x,
Math.pow((1 - t) + 0.001, 3) * points[0].y +
3 * (t + 0.001) * Math.pow((1 - t) + 0.001, 2) * points[1].y +
3 * Math.pow(t + 0.001, 2) * (1 - (t + 0.001)) * points[2].y +
Math.pow(t + 0.001, 3) * points[3].y
);
//33d4ff
context.strokeStyle="#35bb23";
context.lineJoin="round";
context.lineWidth=2;
context.fillStyle = "black";
context.stroke();
context.fill();
// Keep going until t = 1
if (t < 1) requestAnimationFrame(function() {
animatedBSpline(context, points, t + 0.01);
});
else
context.closePath();
}
如果需要更多信息,请告诉我。我整天都在做这个。
添加:如果我只是直接用这些图画出来,而不是动画它正在完成,它看起来很好,显然,但只是想指出这一点。这与我制作动画的方式有关,我只是不知道。
最佳答案
这是带有绿色贝塞尔曲线动画的完整更新代码:
(function() {
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
$(function() {
var canvas = $('#drawings')[0];
var context = canvas.getContext('2d');
var lineLength = 0;
var lineLengthMax = 7;
var timer;
// Define points
var points = [[{
x: 600,
y: 200
}, {
x: 550,
y: 100
}, {
x: 350,
y: 100
}, {
x: 300,
y: 250
}],
[{
x: 350,
y: 250
}, {
x: 75,
y: 225
}, {
x: 30,
y: 400
}, {
x: 120,
y: 450
}],
[{
x: 200,
y: 450
}, {
x: 5,
y: 380
}, {
x: 25,
y: 750
}, {
x: 175,
y: 610
}],
[{
x: 200,
y: 520
}, {
x: 150,
y: 560
}, {
x: 175,
y: 750
}, {
x: 325,
y: 605
}],
[{
x: 400,
y: 395
}, {
x: 275,
y: 450
}, {
x: 250,
y: 750
}, {
x: 565,
y: 655
}],
[{
x: 515,
y: 540
}, {
x: 500,
y: 695
}, {
x: 660,
y: 675
}, {
x: 675,
y: 560
}],
[{
x: 600,
y: 400
}, {
x: 790,
y: 315
}, {
x: 1005,
y: 500
}, {
x: 675,
y: 585
}],
[{
x: 500,
y: 250
}, {
x: 700,
y: 100
}, {
x: 775,
y: 350
}, {
x: 700,
y: 380
}]];
//33d4ff
context.strokeStyle="#35bb23";
context.lineJoin="round";
context.lineWidth=2;
doLineDraw();
//animatedBSpline(context, points, 0);
function doLineDraw() {
if (lineLength <= lineLengthMax) {
clearTimeout(timer);
// Kick things off at t = 0
context.beginPath();
animatedBSpline(context, points[lineLength], 0);
//animatedBSpline(context, eval('points'+(lineLength)), 0);
lineLength++;
if (lineLength <= lineLengthMax)
timer = setTimeout(doLineDraw, 2000);
}
}
//B(t) = (1 - t)^3P0 + 3t(1 - t)^2 P1 + 3t^2(1 - t)P2 + t^3P3
function animatedBSpline(context, points, t) {
// Draw curve segment
if (t == 0)
context.moveTo(
Math.pow(1 - t, 3) * points[0].x +
3 * t * Math.pow(1 - t, 2) * points[1].x +
3 * Math.pow(t, 2) * (1 - t) * points[2].x +
Math.pow(t, 3) * points[3].x,
Math.pow(1 - t, 3) * points[0].y +
3 * t * Math.pow(1 - t, 2) * points[1].y +
3 * Math.pow(t, 2) * (1 - t) * points[2].y +
Math.pow(t, 3) * points[3].y
);
// Draw spline segemnts
context.lineTo(
Math.pow((1 - t) + 0.001, 3) * points[0].x +
3 * (t + 0.001) * Math.pow((1 - t) + 0.001, 2) * points[1].x +
3 * Math.pow(t + 0.001, 2) * (1 - (t + 0.001)) * points[2].x +
Math.pow(t + 0.001, 3) * points[3].x,
Math.pow((1 - t) + 0.001, 3) * points[0].y +
3 * (t + 0.001) * Math.pow((1 - t) + 0.001, 2) * points[1].y +
3 * Math.pow(t + 0.001, 2) * (1 - (t + 0.001)) * points[2].y +
Math.pow(t + 0.001, 3) * points[3].y
);
//context.fillStyle = "black";
context.stroke();
//context.fill();
// Keep going until t = 1
if (t < 1) requestAnimationFrame(function() {
animatedBSpline(context, points, t + 0.01);
});
}
});
}());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
<article>
<canvas id="drawings" width="1000" height="1000" />
</article>
</section>
几个关键点,其中一些与问题无关。
eval
来引用后置数字的变量名。只需使用数组即可。context.beginPath()
和 context.moveTo()
,这导致 context.stroke()
和 context.fill()
“忘记”之前的指令。我将 context.beginPath()
移到 animatedBSpline()
之外,并指定 context.moveTo()
在 处运行t==0
在该函数中,这样就没有不相交的点。希望对您有所帮助。
关于javascript - 如何在html5 canvas中绘制动画贝塞尔曲线时保持流畅的线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38809021/
我正在尝试创建 treasury yield curve 的图表比较两个不同日期的汇率。我很难将两条曲线组合起来并创建一个干净的图形。 我的问题:如何将两条 yield 曲线绘制在一起, yield
我在 R 平台中使用 randomForest 包进行分类任务。 rf_object<-randomForest(data_matrix, label_factor, cutoff=c(k,1-k))
我的设计师给我设计了这个设计,但我不知道如何最好地处理图像上方和下方的曲线。 我考虑过 clip-path 但不知道如何 flex 它。如果可以的话,我不想使用图像。 最佳答案 您可以使用 borde
我正在使用 Canvas 中的笔触和路径来制作两条线,我希望它们像波浪效果一样弯曲。而不是在 Photoshop 中创建实际图像来实现此目的。 谁能帮忙得到如下图所示的曲线? 我还想在末端实现圆 An
我正在尝试开发一种可以处理图像骨架的路径/曲线的代码。我想要一个来自两点之间骨架的点 vector 。 这段代码加了点就结束了,没找到解决办法。 #include "opencv2/highgui/
现在需要帮助。我可以用MKPolyline和MKPolylineView画线,但是如何在MKMapView上的两个坐标之间画弧线或曲线呢?非常感谢。 最佳答案 在回答问题之前,重要的是要提到 MKOv
我正在尝试应用 sklearn 的想法 ROC extension to multiclass到我的数据集。我的每类 ROC 曲线看起来都找到了一条直线,取消显示曲线波动的 sklearn 示例。 我
我有以下概念问题,我无法理解。 以下是调查数据示例,其中我有一个时间列,指示某人需要多长时间才能回答某个问题。 现在,我感兴趣的是清洁量将如何根据此阈值发生变化,即如果我增加阈值会发生什么,如果我降低
如何为使用视频的对象检测应用绘制每个窗口的误报率与未命中率(或误报概率)和 ROC(接收器操作曲线)的图表?如何确定误报和命中的数量?一个例子是很有用。 最佳答案 它很简单。将所有真正 (H0) 值存
我正在尝试绘制随机森林分类的 ROC 曲线。绘图有效,但我认为我绘制了错误的数据,因为生成的绘图只有一个点(准确性)。 这是我使用的代码: set.seed(55) data.controls <
我有如下两个模型: library(mlbench) data(Sonar) library(caret) set.seed(998) my_data <- Sonar fitControl <-
是否可以仅通过查看其 ROC 曲线来了解分类器是否过度拟合?我看到如果它的 AUC 太高(例如 98%)可能会过度拟合,但这也可能意味着分类器非常好。有没有办法区分这两种情况? 最佳答案 简短的回答:
我正在 JavaFX 中创建一个图形,它应该由有向边连接。最好是双三次曲线。有谁知道如何添加箭头? 箭头当然应该根据曲线的末端进行旋转。 这是一个没有箭头的简单示例: import javafx.ap
我需要对我正在尝试的技术进行一些说明。我正在尝试将一个实体从 A 点移动到 B 点,但我不希望该实体沿直线移动。 例如,如果实体位于 x: 0, y:0 并且我想到达点 x:50, y: 0,我希望实
我试图在曲线下方绘制阴影区域,但阴影区域位于曲线上方。谁能告诉我我的代码有什么问题? x=seq(0,30) y1=exp(-0.1*x) plot(x,y1,type="l",lwd=2,col="
我需要对我正在尝试的技术进行一些说明。我正在尝试将一个实体从 A 点移动到 B 点,但我不希望该实体沿直线移动。 例如,如果实体位于 x: 0, y:0 并且我想到达点 x:50, y: 0,我希望实
我有一个如下所示的模型: library(mlbench) data(Sonar) library(caret) set.seed(998) my_data <- Sonar fitControl <
有没有办法从pyspark中的Spark ML获取ROC曲线上的点?在文档中,我看到了一个 Scala 的例子,但不是 python:https://spark.apache.org/docs/2.1
我正在尝试使用Local Outlier Factor (LOF)算法,并想绘制 ROC 曲线。问题是,scikit-learn 提供的库不会为每个预测生成分数。 那么,有什么办法可以解决这个问题吗?
我目前正在使用 GDI+ 绘制折线图,并使用 Graphics.DrawCurve 来平滑线条。问题是曲线并不总是与我输入的点匹配,这使得曲线在某些点上超出了图形框架,如下所示(红色是 Graph
我是一名优秀的程序员,十分优秀!