typescript重写canvas --11.文字大小
1.使用 canvas 文字大小
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<canvas id="myCanvas" width="600" height="400">
你的浏览器不支持HTML5
</canvas>
<script type="text/javascript">
var canvas=document.getElementById("myCanvas");
var context=canvas.getContext("2d");
context.beginPath();
//设定文字大小为30px
context.font="30px Arial";
context.fillText("Hello World",100,50);
context.beginPath();
//设定文字大小为50px
context.font="50px Arial";
context.fillText("Hello World",100,150);
context.beginPath();
//设定文字大小为100px
context.font="70px Arial";
context.fillText("Hello World",100,250);
</script>
</body>
</html>
显示效果如下
2.typescript重写canvas --文字大小
html文件
<!DOCTYPE HTML>
<html>
<body>
<canvas id="myCanvas" width="600" height="400">
你的浏览器不支持HTML5
</canvas>
<script src="./js/11.js"></script>
</body>
</html>
ts文件
const canvas=<HTMLCanvasElement>document.getElementById("myCanvas")
const context=canvas.getContext("2d")
if (context){
context.beginPath();
//设定文字大小为30px
context.font="30px Arial";
context.fillText("Hello World",100,50);
context.beginPath();
//设定文字大小为50px
context.font="50px Arial";
context.fillText("Hello World",100,150);
context.beginPath();
//设定文字大小为100px
context.font="70px Arial";
context.fillText("Hello World",100,250);
}
生成的js文件
"use strict";
const canvas = document.getElementById("myCanvas");
const context = canvas.getContext("2d");
if (context) {
context.beginPath();
//设定文字大小为30px
context.font="30px Arial";
context.fillText("Hello World",100,50);
context.beginPath();
//设定文字大小为50px
context.font="50px Arial";
context.fillText("Hello World",100,150);
context.beginPath();
//设定文字大小为100px
context.font="70px Arial";
context.fillText("Hello World",100,250);
}