示例代码:
<html>
<head>
<style>
canvas {
border: 1px greenyellow solid;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
var canvas = document.getElementById("canvas");
canvas.width = 800;
canvas.height = 600;
var context = canvas.getContext("2d");
// Canvas居中写字,参数(context对象,要写的字,字体,颜色,绘制的高度)
function canvas_text(_paint, _text, _fontSzie, _color, _height) {
_paint.font = _fontSzie;
_paint.fillStyle = _color;
_paint.textAlign = "center";
_paint.textBaseline = "middle";
_paint.fillText(_text, canvas.width / 2, _height);
}
// 绘制中文字示例,参数(context对象,要写的字,字体,颜色,绘制的高度)
canvas_text(context, "要写的文字", "18px bold 黑体", "#000", 300);
</script>
</body>
</html>
参数疑问: