doulu4203 2013-12-30 13:09
浏览 433
已采纳

更新canvas元素而不刷新页面

I have a question and I have been trying everything but just cant crack it. .I have set my HTML5 canvas up to send coordinates, objects etc to a database and then when that canvas is reloaded it (page refresh) it reloads all of the objects with the correct coordinates. .however what I'm trying to achieve is the canvas to update without page refresh every x seconds to show changes etc for example someone else adds something to the canvas everyone will see the change without page refresh so all users are seeing the same thing?

Thanks in advance for any help?

Okay im adding to this post, here is a simple code example of my problem, this all works fine and the var even updated fine when showing in the alert that I have put in however the canvas does now show the changes down below? all the getvars.php file is doing is echo 'Im coming from getvars scripts!';

`<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body{ margin:10px; background:#666; }
#my_canvas{ background:#FFF; border:#000 1px solid; }
</style>
<script src='http://code.jquery.com/jquery-latest.js'></script>
<script>

$(function() {getStatus();});
function getStatus() {  
greg = $.ajax({
url: "getvars.php",
async: false
}).responseText;
setTimeout("getStatus()",10000);

function draw(){
var ctx = document.getElementById('my_canvas').getContext('2d');
ctx.fillStyle = 'black';
ctx.font = 'italic bold 60px Arial, sans-serif';
ctx.fillText(greg, 50, 50, 300);}   
window.alert(greg) 
window.onload = draw;}
</script>
</head>
<body>
<canvas id="my_canvas" width="500" height="350"></canvas>
</body>  
</html>`

is there something I need to add to make the canvas update as the alert is showing the var is updating from the getvars.php when I change it just the canvas stays the same? argh its driving me mad! Thanks in advance for any help you might be able to give?

  • 写回答

2条回答

  • donglao6169 2013-12-30 14:04
    关注

    Put your drawing code inside a .done handler on your jquery ajax call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集