2011-06-01 92 views
10

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

不久,我需要知道如何操纵canvas元素的特定子

回答

12

就像Tz说的那样,你必须在自己身上建立所有的持久性。但这决不意味着难以做到。我已经写了一对夫妇短片tutorials,如果你要完成这项任务,这将使你开始。

+0

@基山是不是你应该给+1的唯一原因?你有没有给过一个不太好的答案+1?哈。 AIGF – DutGRIFF 2015-05-17 13:54:06

+1

Canvas是否没有在其上绘制“对象”的内部矢量状态?它仅仅是一个像素的“斑点”? – 2015-12-04 13:46:46

+0

这是正确的。这只是一个位图,不知道如何绘制任何东西,除非您自己录制它。 – 2015-12-04 18:21:16

5

我认为这是画布后面没有对象模型,所以你不能访问“子对象”就像一个“文本对象”改变。 你可以做的是用不同的颜色重写文本,覆盖画布的“像素”。 如果要移动文本,首先必须清除画布或使用背景/透明颜色重新绘制文本以摆脱先前位置中的文本。然后,您可以在新位置绘制文字。

4

希望允许广告某人的项目。

看看http://ocanvas.org/你可以在那里获得灵感。 像画布库那样的对象。允许你处理事件,制作动画等。

3

我从来没有尝试过,但我认为这将是实现它的方式。

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) 

这是它的小提琴。 http://jsfiddle.net/r4JPG/2/

如果要缓存和翻译,请相应地更改move方法。

3

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

<html> 
<head> 
</head> 
<body> 
    <canvas id="canvas" style="background:rgba(34,45,23,0.4)"></canvas> 
    <script> 
     var can = document.getElementById('canvas'); 
     can.height = 1000; can.width = 1300; 
     var ctx = can.getContext('2d'); 
     var x = 10, y = 100; 
     ctx.fillStyle = "black"; 
     ctx.fillRect(700, 100, 100, 100); 

     function draw() { 
      ctx.beginPath(); 
      ctx.arc(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, can.width, can.height); 
      requestAnimationFrame(draw); 
      //ctx.clearRect(0,0,can.width,can.height); 
     } 
     draw(); 

    </script> 
</body> 
</html> 

查看示例here

+1

这是上面的JSFiddle,以防其他人想要它:http://jsfiddle.net/4fWt5/ – 2014-03-27 14:56:11

0
<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>