首页 > 解决方案 > Javascript画布,颜色未正确应用

问题描述

我有以下 javascript 画布在屏幕上绘制一堆文本

//Draws the prices fonts
    priceList.map((i, index) => {
        ctx.font = "11px Ariel"
        if(index == 0){
            ctx.fillText(i.toFixed(2), ctx.canvas.width - 42, (heightPoints * index) + 9); //This one is being painted in black
        }else{
            ctx.fillText(i.toFixed(2), ctx.canvas.width - 42, (heightPoints * index) - 5);
        }
        ctx.fillStyle = "lightblue";
    })

生成的画布是以下数字

在此处输入图像描述

如您所见,有一个圆圈,那是因为那个数字的字体是黑色的,我似乎无法改变它的颜色

当 index === 0 时绘制

为什么会这样?为什么不为每个元素应用fillStyle?

编辑:显然它被画了,但我不能把它弄下来

在此处输入图像描述

更新:显然,即使它被记录,最后一个值,我也无法在任何地方绘制它我试图在任意(屏幕中心)位置绘制它并且它不会显示

priceList.map((i: number, index: number) => {
        ctx.font = "11px Arial"
        if(index === 0){ //nowhere to be found
            ctx.fillText("aaaaaaaaaaaaaaaaaaa", 500, 500);
            console.log(i) //64854.00000000001
            console.log(ctx.canvas.height) //885
            console.log(ctx.canvas.width) //1157
        }else{
            ctx.fillText(i.toFixed(2), ctx.canvas.width - 46, (heightPoints * index) - 5);
        }
        ctx.fillStyle = "orange";
    })

标签: javascriptcanvas

解决方案


推荐阅读