【问题标题】:How can I dynamically generate a polygon shape that moves upward in Canvas?如何动态生成在 Canvas 中向上移动的多边形形状?
【发布时间】:2017-01-04 20:08:34
【问题描述】:

现在,我所拥有的内容显示在这个小提琴中:https://jsfiddle.net/p0o5fvdm/

我想要的是多边形平滑地向上移动并用一些黑色填充它下面的部分,该黑色跨越与多边形对齐的随机宽度并像顶部一样平滑地向上移动。我尝试使用以下代码,但它没有保留原始多边形的形状,并且新多边形被绘制在旧多边形上,导致闪烁。

这是我的动画功能:

function animate(myShape, canvas, context, startTime) {

    var time = (new Date()).getTime() - startTime;

    myShape.y1 -= 1;
    myShape.y2 -= 1;
    myShape.y3 -= 1;


    context.clearRect(0, 0, canvas.width, canvas.height);

    drawShape(myShape, context);


    requestAnimFrame(function() {
      animate(myShape, canvas, context, startTime);
    });
}

我添加了以下代码以使用context.clearRect上方的以下代码动态设置底部边缘的宽度。

if(myShape.y2 < 400) {
    myShape.y2 = 400;
    myShape.x2 = Math.random()*300;
    myShape.y3 = 400;
}

这是myShape的初始值:

var myShape = {
    x1: 200,
    y1: 0,
    x2: 120,
    y2: 400,
    x3: 0,
    y3: 400
};

我的画布高度为 400 像素。这就是为什么我在这里使用它作为参考。如果我需要添加更多详细信息,请告诉我。

【问题讨论】:

    标签: javascript html canvas requestanimationframe


    【解决方案1】:

    下面是一个让你的画布无限向上滚动的具有随机宽度的多边形的计划:

    • 保留一组随机 X、递增 Y 坐标,这些坐标构成您的多边形。
    • 第一个元素array[0].y 应该小于零。
    • 最后一个元素array[array.length-1].y 应该大于画布高度。
    • 在每个动画循环期间通过将每个 array.y 减 1 来制作动画。
    • 当array[1].y 减小到零以下时,shiftarray[0] 会从数组的开头离开。
    • 当最后一个 array.y 动画到可视画布上时,将一个新元素推送到 array.y 大于画布高度的数组中。

    示例代码和演示:

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");
    var cw=canvas.width;
    var ch=canvas.height;
    
    var a=[
        {x:rx(),y:-Math.random()*100},  // y above canvas
        {x:rx(),y:ry()}                 // y below canvas
    ];
    
    // redraw
    draw(a);
    
    requestAnimationFrame(animate);
    
    function animate(){
        // update - move all y's upward
        for(var i=0;i<a.length;i++){ a[i].y-=3; }
        // test if a[1] is off top of canvas
        if(a[1].y<0){a.shift();}
        // test if last a[] is on canvas
        if(a.length<2 || a[a.length-1].y<canvas.height){
            a.push({x:rx(),y:ry()});
        }
        // redraw
        draw(a);
        // request another animation loop
        requestAnimationFrame(animate);
    }
    //
    function draw(a){
        ctx.clearRect(0,0,cw,ch);
        ctx.beginPath();
        ctx.moveTo(0,a[0].y);
        for(var i=0;i<a.length;i++){
            ctx.lineTo(a[i].x,a[i].y);
        }
        ctx.lineTo(0,a[a.length-1].y);
        ctx.fill();
    }
    //
    function rx(){ return(canvas.width/2+Math.random()*200); }
    function ry(){ return(canvas.height*1.25+Math.random()*canvas.height/2); }
    body{ background-color:white; }
    #canvas{border:1px solid red; }
    &lt;canvas id="canvas" width=512 height=512&gt;&lt;/canvas&gt;

    【讨论】:

    • 谢谢。你的算法几乎解决了我的问题。只有一个问题。我需要用该多边形完全填充画布的左侧,为此,我需要将最后一个数组元素的 x 值设置为零。但随后移位导致所有值最终变为零。将 x 的最终值设置为随机值会导致多边形不是凸面的,并且可能会在其左侧留下空白。
    • 感谢演示。我觉得自己无法正确实施它有点愚蠢。 :)
    • 不客气。没有感觉愚蠢......我怀疑你最终会得到它。 :-)
    • 我有一个疑问。既然数组只有两个元素,那a[1]和a[a.length-1]不一样吗?
    • 我现在明白了。 animate 函数会在数组长度小于 2 以及最后一个元素高于画布底部时不断添加数组值。感谢您的回答标记:)
    猜你喜欢
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    相关资源
    最近更新 更多