【发布时间】:2020-10-17 08:21:59
【问题描述】:
我有一张流线型的风图(请参阅https://earth.nullschool.net/ 了解它的外观)。
我每 10 毫秒对该点进行一次加权插值,它会创建一个 uVector 和一个 vVector,然后计算该速度。根据该速度,为该点分配相应的颜色,如下所示。
if (weightWS < 13) {
color = "#ffa500"
}
if (weightWS < 10) {
color = "#CCCC00"
}
if (weightWS < 7) {
color = "#008000"
}
if (weightWS < 4) {
color = "#0000ff"
}
if (weightWS < 1) {
color = "#800080"
}
颜色然后被发送到动画函数。它在drawData[3]。
function animate(){
//This changes the opacity of the canvas
ctx.beginPath();
ctx.fillStyle = 'rgba(255, 255, 255, 0.4)';
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.closePath();
//This is where the point is created.
for(var pointCount=0;pointCount<pointNum;pointCount++){
let xMid,yMid;
let xPoint = points[pointCount][0];
let yPoint = points[pointCount][1];
if(xPoint < 141 || xPoint >580 || yPoint < 120 || yPoint > 524){
xPoint = getRandomIntInclusive(141,580);
yPoint = getRandomIntInclusive(120,524);
points.push([xPoint,yPoint]);
}
//ctx.fillStyle = "#000000";
var drawData = weightAllData(xPoint,yPoint); //output: speed, uVec, vVec, color
colorArr[pointCount] = drawData[3]; //assign color to array, for QA purposes
ctx.moveTo(xPoint, yPoint);
ctx.lineTo(xPoint+drawData[1],yPoint+drawData[2]);
ctx.strokeStyle = drawData[3]; //assign the color to the strokeStyle
//console.log(ctx.strokeStyle); //prints color
ctx.stroke(); //makes line.
points[pointCount][0] = xPoint+drawData[1];
points[pointCount][1] = yPoint+drawData[2];
//console.log(drawData[0])
}
虽然控制台显示不同的颜色,主要是"#800080"和"#0000ff",但颜色会影响所有的点,速度为1+的点仍然是紫色的("#800080")。
颜色一次应该只影响一个像素,我很肯定应该显示一些蓝点 ("#0000ff")。如果显示一个蓝点,则它同时是所有像素,这不是目标。
无论动画的速度如何(例如 10 毫秒、1000 毫秒、10000 毫秒),都会发生这种情况。有没有人有任何解决方案或暗示问题是什么?
非常感谢您的所有帮助。我非常感谢所花费的时间和精力。
【问题讨论】:
-
你的代码效率有点低。可以从最小开始并使用 if 和 else if 进行清理。
-
在
ctx.moveTo(xPoint, yPoint);行上方的 for 循环中添加调用ctx.beginPath()就像你拥有它一样,你在 for 循环的每次迭代中绘制相同的路径,因此最终颜色是全部颜色线段将是,beginPath 确保您每次迭代都开始一个新路径,而不是添加到现有路径。 -
@Blindman67 成功了!!!哦,非常感谢。如果您想将其添加为答案,我会接受!谢谢!!
-
@epascarello 我知道。我试图确保我什至可以在提高效率之前完成这个项目。谢谢!
标签: javascript html colors html5-canvas line