我有这段代码来创建我的 html 画布标题:
<script type="text/javascript">
(function () {
var canvas = document.getElementById('header-canvas'),
context = canvas.getContext('2d');
// resize the canvas to fill browser window dynamically
window.addEventListener('resize', resizeCanvas, false);
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = 120;
drawStuff();
}
resizeCanvas();
function drawStuff() {
var c = document.getElementById("header-canvas"),
ctx = c.getContext("2d"),
grd = ctx.createLinearGradient(0, 0, 0, c.height),
x = c.width / 2;
y = c.height / 2;
grd.addColorStop(0, "#0ac5f4");
grd.addColorStop(1, "#7ae0fa");
ctx.fillStyle = grd;
ctx.fillRect(0, 0, c.width, c.height);
ctx.font = '30px LatoHL';
ctx.textAlign = 'center';
ctx.fillStyle = 'white';
ctx.fillText('MACKENZIE GRAY', x, y);
}
})();
</script>
但是我的文字只有在我重新调整窗口大小时才会出现......这个功能有什么问题?
编辑:我应该补充一点,渐变确实出现了。