我有一小段代码,里面有 2 个画布和文本。但我无法更改指定画布的字体大小。
if(i==1) {
c.getContext('2d').font = "35pt bold arial";
}
在下面的代码中,我试图更改第二个画布的字体,但它没有被应用。
JSFiddle - http://jsfiddle.net/9n6wD/
var myCanvas = document.createElement('canvas');
myCanvas.width=400;
myCanvas.height=200;
document.body.appendChild(myCanvas);
myCanvasCtx = myCanvas.getContext('2d');
var canvas1 = document.createElement('canvas');
canvas1.width = 150;
canvas1.height = 100;
var myCanvas1Ctx = canvas1.getContext('2d');
myCanvas1Ctx.fillStyle = "#000";
myCanvas1Ctx.font = "25pt arial";
myCanvas1Ctx.fillText("Hello",0,25);
var canvas2 = document.createElement('canvas');
canvas2.width = 150;
canvas2.height = 100;
var myCanvas2Ctx = canvas2.getContext('2d');
myCanvas2Ctx.fillStyle = "#000";
myCanvas2Ctx.font = "25pt arial";
myCanvas2Ctx.fillText("World!",0,25);
var arr = [];
arr.push(new Object({canvas : canvas1, xposition : 50}), new Object({canvas : canvas2, xposition : 170}) );
for(var i in arr) {
var c = arr[i].canvas;
if(i==1) {
c.getContext('2d').font = "35pt bold arial";
}
myCanvasCtx.drawImage(c, arr[i].xposition , 15, c.width, c.height);
}