2012-12-09 37 views
0

我對canvas fillText函數有問題。我有這樣的代碼:如何將文本添加到畫布中

 scene.shapes['shop1'] = new Shape(); 
     var ps1 = scene.shapes['shop1'].points; // for convenience 

     ps1[0] = new Point(1024, 66, 10); // left bottom 
     ps1[1] = new Point(694, 66, 10); // right bottom 
     ps1[2] = new Point(694, 466, 10); // right top 
     ps1[3] = new Point(1024, 466, 10); // left top  


     // Background 
     scene.shapes['shop1'].polygons.push(new Polygon(
      [ps1[0], ps1[1], ps1[2], ps1[3] ], 
      new Point(0, 0, 1), 
      true /* double-sided */, 
      Polygon.SOLID, 
      [177, 216, 249] 
     )); 

        scene.shapes['shop1Header'] = new Shape(); 
     var ps1h = scene.shapes['shop1Header'].points; // for convenience 

     ps1h[0] = new Point(1024, 400, 11); // left bottom 
     ps1h[1] = new Point(694, 400, 11); // right bottom 
     ps1h[2] = new Point(694, 466, 11); // right top 
     ps1h[3] = new Point(1024, 466, 11); // left top  


     // Background 
     scene.shapes['shop1Header'].polygons.push(new Polygon(
      [ps1h[0], ps1h[1], ps1h[2], ps1h[3] ], 
      new Point(0, 0, 1), 
      true /* double-sided */, 
      Polygon.SOLID, 
      [175, 175, 175] 
     ));  

          var x = -1024; 
          var y = -500; 

          ctx.font = '60pt Calibri'; 
          ctx.lineWidth = 3; 
          // fill color 
          ctx.fillStyle = 'blue'; 
          ctx.fillText('Hello World!', x, y); 

但它不創建文本,只顯示商店。你知道嗎,如何解決這個問題?非常感謝你。

回答

2

x和y的值是負值,因此您正在繪製在畫布外部。如果將x設置爲0並將y設置爲0 +字體大小,它將在左上角繪製文本。

編輯:您需要將字體大小添加到y。

ctx.font = "60px Calibri"; // Use pixels instead of points 
ctx.fillText("Hello World", 0, 60); 
+0

這個負值不是問題,因爲我有攝像機z positoin = 2048,它必須顯示。我試圖改變爲O,O但仍然沒有。 – user1870556

+0

對不起。我糾正了我的答案。 2d畫布上沒有z位置。你如何分配ctx? – ZippyV

+0

這裏http://pastebin.com/56GFR2Vi完成代碼。 我如何分配ctx: canvas = document.getElementById(「cv」); ctx = canvas.getContext(「2d」); 並且存在z位置的相機定位: scene.camera.x = 0; scene.camera.y = 0; scene.camera.z = 2048; – user1870556

相關問題