【问题标题】:Text and animation together on HTML5 canvasHTML5 画布上的文本和动画
【发布时间】:2013-12-07 03:45:48
【问题描述】:

我有一个基于动画的画布,在鼠标悬停时动画雨滴,动画在鼠标悬停时停止。我有提交时应在画布上显示文本的文本框。但是,当我再次移出并鼠标悬停时,此文本会消失。我知道画布在鼠标悬停时会重新绘制,但我无法弄清楚如何使文本保持在原处。谢谢!

我已经根据此处提供的解决方案改编了代码 => Random images falling like rain in canvas (Javascript)

Javascript

var ctx;
var imgBg;
var imgDrops;
var x = 0;
var y = 0;
var noOfDrops = 7;
var fallingDrops = [];
var intV;

imgBg = new Image();
imgBg.src = "image.jpg";
var canvas = document.getElementById('canvasRegn');
ctx = canvas.getContext('2d');
ctx.drawImage(imgBg,0,0,600,450); //Background

function draw() {
    ctx.drawImage(imgBg, 0, 0,600,450); //Background

    for (var i=0; i< noOfDrops; i++)
    {
    ctx.drawImage (fallingDrops[i].image, fallingDrops[i].x, fallingDrops[i].y); //The rain drop

    fallingDrops[i].y += fallingDrops[i].speed;
    fallingDrops[i+4].x += fallingDrops[i].speed-1;//Set the falling speed
    if (fallingDrops[i].y > 450) {  //Repeat the raindrop when it falls out of view
    fallingDrops[i].y = -120; //Account for the image size
    fallingDrops[i].x = Math.random() * 600;    //Make it appear randomly along the width    
    }

    }
}

function setup() {

    intV = setInterval(function(){draw()}, 36);
    for (var i = 0; i < noOfDrops; i++) {
        var fallingDr = new Object();
        fallingDr["image"] =  new Image();
        fallingDr.image.src = "Rain.svg";       
        fallingDr["x"] = Math.random() * 600;
        fallingDr["y"] = Math.random() * 5;
        fallingDr["speed"] = 3 + Math.random() * 5;
        fallingDrops.push(fallingDr);

        }
}
function start(){
setup();
}

function stop(){
    clearInterval(intV);
}

function clicked(){
    var x=document.getElementById("form_val");
    ctx.clearRect(0,0,600,400);
    ctx.font="36px Verdana";
    ctx.fillStyle="yellow";
    ctx.strokeStyle="green";
    ctx.lineWidth=2;
    ctx.strokeText(x.value,200,200);
    ctx.fillText(x.value,200,200);
}

HTML

<canvas id="canvasRegn" width="600" height="450"style="margin:10px;" onmouseover="start()" onmouseout="stop()">
</canvas>
<br>
<input type="text" name="fname" size="50" id="form_val">
<button id="submit" onclick="clicked()">Submit</button>

【问题讨论】:

    标签: javascript html canvas html5-canvas html5-animation


    【解决方案1】:

    每次重绘画布时,您都需要重绘文本框,我会亲自重命名“clicked()”并从“draw()”内部调用它(根据您是否希望它出现在 drop 之前或之后高于或低于)

    您还必须从“clicked()”中删除 ctx.clearRect(),否则它会覆盖下雨(如果您将其放在顶部)

    然后您需要编辑它的调用方式,clicked() 函数可以设置一个布尔变量,该变量在 draw 函数内部进行检查(如果为 true,则绘制文本框)

    伪代码示例:

    var text = false
    draw(){
        drawRain()
        if(text == true){drawText()}
    }
    clicked(){
        text = true
    }
    

    然后,如果您希望文本框可编辑,您可以在 drawText() 中使用变量而不是固定值,例如

    在drawText()之外

    fontVar = "36px Verdana";
    fillColour = "yellow";
    strokeColour = "green";
    

    drawText() 内部

    ctx.font=fontVar;
    ctx.fillStyle=fillColour;
    ctx.strokeStyle=strokeColour;
    

    【讨论】:

    • 我想到了这一点,但是当您更改消息(即提交第二条消息)时,它会与第一条消息混淆。这是唯一的问题。
    • 啊,所以你想要多个文本框可见?你的意思是当你不希望它们重叠时它们会相互重叠?
    • 是的,当我提交第二条消息时,第一条消息应该被删除而不是重叠。
    • 我建议的方式不应该让它们重叠,如果你用相同的函数绘制它们并且只编辑该函数中使用的变量,它将绘制新的文本框。稍等一下,我将在答案中添加一些伪代码。
    • 我们越来越近了。 1 个问题是 clicked() 设置了 text=true 所以当我输入另一个文本时。当我将鼠标悬停在画布上时,无需单击提交按钮,新文本将替换前一个文本,因为文本为“真”。
    【解决方案2】:

    这个问题的答案在于铺设两个画布层。第一个 Canvas 层将具有背景图像和动画效果。它上面的第二层将绘制文本。 注意:感谢@DBS 找到解决方案。

    JavaScript

    script type="text/javascript">
    var ctx;
    var ctx2
    var imgBg;
    var imgDrops;
    var x = 0;
    var y = 0;
    var noOfDrops = 20;
    var fallingDrops = [];
    var intV;
    fontVar ="36px Verdana";
    fillColour="yellow";
    strokeColour="green";
    
    imgBg = new Image();
    imgBg.src = "image.jpg";
    var canvas = document.getElementById('myCanvas');
    ctx = canvas.getContext('2d');
    ctx.drawImage(imgBg,0,0,600,400); //Background
    
    var canvas2 = document.getElementById('drawText');
    ctx2 = canvas2.getContext('2d');
    
    function drawing(){
    ctx.drawImage(imgBg,0,0,600,400); //Background
    }
    
    function draw() {
        ctx.drawImage(imgBg, 0, 0,600,400); //Background    
        for (var i=0; i< noOfDrops; i++)
        {
    
        ctx.drawImage (fallingDrops[i].image, fallingDrops[i].x, fallingDrops[i].y,35,35); //The rain drop
    
        fallingDrops[i].y += fallingDrops[i].speed;
        fallingDrops[i+7].x += fallingDrops[i].speed-1;//Set the falling speed
        if (fallingDrops[i].y > 400) {  //Repeat the raindrop when it falls out of view
            fallingDrops[i].y = -120; //Account for the image size
            fallingDrops[i].x = Math.random() * 600;    //Make it appear randomly along the width    
            }
        }       
    }
    
    
    function setup() {
    
        intV = setInterval(function(){draw()}, 36);
        for (var i = 0; i < noOfDrops; i++) {
            var fallingDr = new Object();
            fallingDr["image"] =  new Image();
            fallingDr.image.src = "Rain.svg";       
            fallingDr["x"] = Math.random() * 600;
            fallingDr["y"] = Math.random() * 5;
            fallingDr["speed"] = 3 + Math.random() * 5;
            fallingDrops.push(fallingDr);
    
            }
    }
    function start(){
    setup();
    }
    
    function stop(){
        clearInterval(intV);
    }
    
    function clicked(){
        z=document.getElementById("form_val");
        ctx2.clearRect(0,0,600,400);
        ctx2.font=fontVar;
        ctx2.fillStyle=fillColour;
        ctx2.strokeStyle=strokeColour;
        ctx2.lineWidth=2;
        ctx2.strokeText(z.value,200,200);
        ctx2.fillText(z.value,200,200);
    }
    
    </script>
    

    HTML

    <div class="wrapper">
    <canvas id="myCanvas" width="600" height="400" style="margin:1px;"></canvas>
    <canvas id="drawText" width="600" height="400" onmouseover="start()" onmouseout="stop()</canvas>
    </div>
    <br>
    Greeting Message: <input type="text" name="fname" size="50" id="form_val">
    <button id="submit" onclick="clicked()">Add this message</button>
    </div>
    

    CSS

    How can I stack two same-sized canvas on top of each other?

    【讨论】:

      猜你喜欢
      • 2012-07-18
      • 1970-01-01
      • 2015-05-25
      • 2016-06-19
      • 1970-01-01
      • 2013-07-31
      • 2011-10-24
      • 1970-01-01
      • 2012-05-15
      相关资源
      最近更新 更多