第一次加载时绘制到画布失败.刷新后就可以工作了

drawing to canvas fails on first load. Refresh and it works

本文关键字:刷新 失败 就可以 工作 加载 绘制 布失败 第一次      更新时间:2023-09-26

这是我的函数。

drawButtons: function() {
    gameController.ctx.strokeStyle='pink';
    gameController.ctx.fillStyle='rgba(128,128,128,1)';
    gameController.ctx.lineWidth=2;
    this.roundedRect(gameController.ctx,105,162,160,50,10,true,true);
    this.roundedRect(gameController.ctx,105,240,160,50,10,true,true);
    gameController.ctx.fillStyle = 'rgba(153,051,000,1)';
    gameController.ctx.font = "35px AR DARLING";
    gameController.ctx.fillText("1 Player",120,200);
    gameController.ctx.fillText("2 Player",120,278);
    }

在第一次加载网页时,没有一个被绘制出来。如果有人希望看到,我可以添加代码来显示我在哪里调用该方法。也许我必须添加.onload(),但我只知道如何在画布上添加图像,而不是fillText等。谢谢。

window.onload在我的drawButtons方法中就是解决方案。

drawButtons: function() {
    window.onload = (function(){
    gameController.ctx.strokeStyle='pink';
    gameController.ctx.fillStyle='rgba(128,128,128,1)';
    gameController.ctx.lineWidth=2;
    nonGameContent.roundedRect(gameController.ctx,105,162,160,50,10,true,true);
    nonGameContent.roundedRect(gameController.ctx,105,240,160,50,10,true,true);
    gameController.ctx.fillStyle = 'rgba(153,051,000,1)';
    gameController.ctx.font = "35px AR DARLING";
    gameController.ctx.fillText("1 Player",120,200);
    gameController.ctx.fillText("2 Player",120,278);
    })},