Javascript 在html5画布上移动对象

Javascript 在html5画布上移动对象,javascript,html,canvas,Javascript,Html,Canvas,我使用fillText选项在html5画布对象上放置了一个文本,问题是我需要移动文本位置或更改已渲染文本的颜色 很快,我需要知道如何操作画布元素的特定子对象,我认为画布后面没有对象模型,因此您无法访问“文本对象”之类的“子对象”并对其进行更改。 你可以做的是用不同的颜色再次绘制文本,覆盖画布的“像素”。 如果要移动文本,首先必须清除画布或使用背景/透明颜色重新绘制文本,以去除前一位置的文本。然后你可以在新的位置上绘制文本。希望它可以被允许为某人的项目做广告 看看这里,你可以从中获得灵感。 它是类

我使用fillText选项在html5画布对象上放置了一个文本,问题是我需要移动文本位置或更改已渲染文本的颜色


很快,我需要知道如何操作画布元素的特定子对象,我认为画布后面没有对象模型,因此您无法访问“文本对象”之类的“子对象”并对其进行更改。 你可以做的是用不同的颜色再次绘制文本,覆盖画布的“像素”。
如果要移动文本,首先必须清除画布或使用背景/透明颜色重新绘制文本,以去除前一位置的文本。然后你可以在新的位置上绘制文本。

希望它可以被允许为某人的项目做广告

看看这里,你可以从中获得灵感。
它是类似对象的画布库。允许您处理事件、制作动画等。

我从未尝试过,但我认为这将是一种方法

var canvas = document.getElementById("canvas"); //get the canvas dom object
var ctx = canvas.getContext("2d"); //get the context
var c = {  //create an object to draw
  x:0,  //x value
  y:0,  //y value
  r:5; //radius
}
var redraw = function(){  // this function redraws the c object every frame (FPS)
  ctx.clearRect(0, 0, canvas.width, canvas.height); // clear the canvas
  ctx.beginPath();  //start the path
  ctx.arc(c.x, c.y, c.r, 0, Math.PI*2); //draw the circle
  ctx.closePath(); //close the circle path
  ctx.fill(); //fill the circle
  requestAnimationFrame(redraw);//schedule this function to be run on the next frame
}
function move(){  // this function modifies the object
  var decimal = Math.random() // this returns a float between 0.0 and 1.0
  c.x = decimal * canvas.width; // mulitple the random decimal by the canvas width and height to get a random pixel in the canvas;
  c.y = decimal * canvas.height;
}
redraw(); //start the animation
setInterval(move, 1000); // run the move function every second (1000 milliseconds)
这是一把小提琴。


如果您想要轻松和翻译,请相应地更改
移动
方法。

这将在画布上移动一个小圆圈

var can=document.getElementById('canvas');
罐高=1000;罐宽=1300;
var ctx=can.getContext('2d');
变量x=10,y=100;
ctx.fillStyle=“黑色”;
ctx.fillRect(700100100100);
函数绘图(){
ctx.beginPath();
弧(x,y,20,0,2*Math.PI);
ctx.fillStyle='rgba(250,0,0,0.4)';
ctx.fill();
x+=2;
ctx.fillStyle=“rgba(34,45,23,0.4)”;
ctx.fillRect(0,0,罐宽,罐高);
请求动画帧(绘制);
//ctx.clearRect(0,0,罐宽,罐高);
}
draw()

帆布考试
var dom=document.getElementById(“我的画布”);
var ctx=dom.getContext(“2d”);
var x1=设置间隔(处理程序,1);
var x=50;
变量y=50;
r=40;
函数处理程序()
{
ctx.clearRect(0,0500);
r1=(数学PI/180)*0;
r2=(数学PI/180)*360;
ctx.beginPath();
//x=x*Math.random();
x=x+2;
r=r+10*Math.random();
弧(x,y,r,r1,r2);
ctx.closePath();
ctx.fillStyle=“蓝色”;
ctx.fill();
ctx.stroke();
如果(x>400)
{
x=50;
y=y+10;
}
r=40;
}

以下是上述内容的一部分,以防其他人需要:
<html>
<head>
<title>Canvas Exam</title>
</head>
<body>
<canvas id="my_canvas" height="500" width="500" style="border:1px solid black">
</canvas>
<script>
 var dom=document.getElementById("my_canvas");
 var ctx=dom.getContext("2d");
 var x1=setInterval(handler,1);
 var x=50;
 var y=50;
 r=40;
 function handler()
{
  ctx.clearRect(0,0,500,500);
  r1=(Math.PI/180)*0;
  r2=(Math.PI/180)*360;
  ctx.beginPath();

  //x=x*Math.random();
  x=x+2;
  r=r+10*Math.random();
  ctx.arc(x,y,r,r1,r2);
  ctx.closePath();
  ctx.fillStyle="blue";
  ctx.fill();
  ctx.stroke();

  if(x>400)
 {
  x=50;
  y=y+10;
 }
  r=40;
}
</script>
</body>
</html>