1
この例の四角形が表示されているのはなぜですか?私は間違っているの?キャンバスにアルファの不透明な四角形を描く正しい方法は何ですか?
例:私は理解してどのように
var ctx = document.getElementById("ctx").getContext("2d");
ctx.canvas.width = 500;
ctx.canvas.height = 500;
ctx.fillStyle = "rgba(0, 0, 20, 1)";
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.fillStyle = "rgba(255, 255, 255, 1)";
ctx.fillRect(100, 100, 300, 300);
for(let i = 0; i < 10; i++)
{
\t ctx.fillStyle = "rgba(0, 0, 20, 0.2)";
\t ctx.fillRect(100, 100, 300, 300);
}
<canvas id="ctx"></canvas>
、サイクルの5 iterraations後、正方形は消えるする必要がありますが、これは正しいことで不透明度を計算する方法10の後に起こるありません方法?
新しい例があります:あなたは不透明度を下回る見ることができるように
var ctx = document.getElementById("ctx").getContext("2d");
var background = "#000236";
ctx.canvas.width = 500;
ctx.canvas.height = 500;
ctx.fillStyle = background;
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
var i = 0;
var inter = setInterval(function()
{
for (let j = 0; j < 14; j++)
{
\t ctx.fillStyle = "rgba(255, 255, 255, 1)";
\t ctx.fillRect(10 + j * 40, i * 10, 20, 20);
}
ctx.save();
ctx.globalAlpha = 0.2;
ctx.fillStyle = background;
ctx.fillRect(0, 0, 500, 500);
ctx.restore();
i++;
\t
if (i >= 50)
{
i = 0;
}
}, 50);
<canvas id="ctx"></canvas>
私はあなたが何を意味するかを理解し、しかし、あなたのソリューションです私は何らかのアニメーションを作りたいと思うので、不適切ではありません。私は永遠に満たされたくありません。この例をよく見てみましょう:https://jsfiddle.net/profesor08/vmp22seq/ fillStyleの不透明度を設定する代わりにグローバル不透明度を使用しますが、結果は同じです。 – Profesor08