简体   繁体   English

刷新HTML画布而不刷新页面

[英]Refresh html canvas without refreshing page

I have a bouncing ball simulation that is coded with javascript and html. 我有一个用javascript和html编码的弹跳球模拟。 I need to update some of the variables (Gravity and Bounce factor) by inputting the values in a text box then click on a button to refresh the canvas and bounce the ball with new parameters. 我需要通过在文本框中输入值来更新一些变量(重力和反弹系数),然后单击按钮刷新画布并使用新参数反弹球。 Heres my code. 这是我的代码。

 var canvas = document.getElementById("canvas"), ctx = canvas.getContext("2d"); var W = 1300, H = 450; canvas.height = H; canvas.width = W; var ball = {}, gravity = 0.2, bounceFactor = 0.7; $('#update').click(function() { gravity = document.getElementById('grav').value; bounceFactor = document.getElementById('bounce').value; document.getElementById('setgrav').value = gravity; document.getElementById('setbounce').value = bounceFactor; update(); setInterval(update, 1000 / 60); }); ball = { x: 0, y: 0, radius: 15, color: "red", // Velocity components vx: 2, vy: 1, draw: function() { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false); ctx.fillStyle = this.color; ctx.fill(); ctx.closePath(); } }; function clearCanvas() { ctx.clearRect(0, 0, W, H); } function update() { clearCanvas(); ball.draw(); ball.y += ball.vy; ball.x += ball.vx; ball.vy += gravity; if (ball.y + ball.radius > H) { ball.y = H - ball.radius; ball.vy *= -bounceFactor; } } setInterval(update, 1000 / 60); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> Gravity <input type="text" name="grav" id="grav" value="" /> <p> Bounce <input type="text" name="bounce" id="bounce" value="" /> <p> Set Gravity <input type="text" name="setgrav" id="setgrav" value="" /> <p> Set Bounce <input type="text" name="setbounce" id="setbounce" value="" /> <p> </form> <button id="update">Update</button> <canvas id="canvas" style="display:block; border: 1px solid blue; margin:50px auto;"> </canvas> 

Its not working. 它不起作用。 When i click update. 当我单击更新。 the Canvas goes blank. 画布为空白。 What im i doing wrong? 我在做什么错? Thanks 谢谢

UPDATED QUESTION: 更新的问题:

What if i needed to use an image for the ball instead of drawing the ball using Javascript? 如果我需要为球使用图像而不是使用Javascript绘制球怎么办?

Instead of assigning the actual value which is string, assign the value parsed as float into the variables. 而不是分配实际的字符串值,而是将解析为float的值分配给变量。

gravity = parseFloat(document.getElementById('setgrav').value);
bounceFactor = parseFloat(document.getElementById('setbounce').value);

Now, whenever you click update, the values will be updated. 现在,每当您单击更新时,值将被更新。 I am assuming that the elements having set in their ids are the ones used to set the values of gravity and bounce. 我假设已在其id中set的元素是用于设置引力和反弹值的元素。

Thanks For the help @Akash, your solution helped out. 感谢@Akash的帮助,您的解决方案获得了帮助。 It updated the values but i also had to reset the balls attributes for it to start bouncing from the beginning once i click update. 它更新了值,但我还必须重新设置balls属性,以使其在单击更新后从头开始弹起。 Heres the Javascript code for the update function 这是更新功能的Javascript代码

$('#update').click(function(){

gravity = parseFloat(document.getElementById('grav').value);
bounceFactor = parseFloat(document.getElementById('bounce').value);

document.getElementById('setgrav').value = gravity;
document.getElementById('setbounce').value = bounceFactor;

ball = {
    x: 0,
    y: 0,

    radius: 15,
    color: "red",

    // Velocity components
    vx: 2,
    vy: 1,

    draw: function() {

        ctx.beginPath();
        ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2, false);
        ctx.fillStyle = this.color;
        ctx.fill();
        ctx.closePath();
    }
};


update();

});

The controls of the form have no value (=== undefined) at the start, so when you update, you let one/several undefined values leak through your code. 表单的控件在开始时就没有值(=== undefined),因此在更新时,您会让一个/几个未定义的值通过代码泄漏。 As you might know, this won't break the program, it will just cease the display in this case. 如您所知,这不会破坏程序,在这种情况下只会停止显示。

Two fixes : 两个修复:
• Initialize your controls so they have a valid value. •初始化控件,使其具有有效值。 It will also help the user to know about the order of magnitude of the values. 它还将帮助用户了解值的数量级。
• Update the variables values only after making some basic validation. •仅在进行一些基本验证后才更新变量值。

function getValue(ctrlId) {
   return document.getElementById(ctrlId).value;
}

function isValidNumber ( val ) { return !isNaN(+val); }

function update() {
   var newGravity = getValue( 'grav' );
   if (isValidNumber ( newGravity ) ) gravity = newGravity ;
   //... same for other values.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM