0
我試圖弄亂html5 canvas並淡入淡出文本。當我做這件事時,它從不保留畫布上的其他東西可見。我想淡入淡出的文字卻沒有出現。我想我可以使用ctx.save()和ctx.restore()來解決這個問題,但無濟於事我無能爲力。HTML5 Canvas淡入淡出文本,不起作用
這是我使用的代碼。
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
var canvas, ctx, step = 50, steps = 255;
var delay = 20;
var rgbstep = 50;
function init() {
canvas = document.getElementById("myCanvas");
ctx = canvas.getContext("2d");
// Fire text
ctx.font = "Bold 160pt Trebuchet MS";
ctx.fillStyle = "#8DC63F";
ctx.fillText('F', 350, 195);
ctx.font = "Bold 120pt Trebuchet MS";
ctx.fillStyle = "#8DC63F";
ctx.fillText('IRE', 469, 195);
//Fly text
ctx.font = "Bold 160pt Trebuchet MS";
ctx.fillStyle = "#A7A9AC";
ctx.fillText('F', 705, 195);
ctx.font = "Bold 120pt Trebuchet MS";
ctx.fillStyle = "#A7A9AC";
ctx.fillText('LY', 825, 195);
ctx.save();
var img = new Image();
img.src = 'logo/firefly.png';
img.onload = function() {
ctx.drawImage(img, 0, 0);
};
Textfadeup();
}
function Textfadeup() {
rgbstep++;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.font = "43pt Tahoma";
ctx.fillStyle = "rgb(" + rgbstep + "," + rgbstep + "," + rgbstep + ")";
ctx.fillText('innovative technologies', 379, 255);
ctx.restore();
if (rgbstep < 255) {
var t = setTimeout('Textfadeup()', 10);
}
if (rgbstep == 255) {
Textfadedown();
}
}
function Textfadedown() {
rgbstep = rgbstep-1;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.font = "43pt Tahoma";
ctx.fillStyle = "rgb(" + rgbstep + "," + rgbstep + "," + rgbstep + ")";
ctx.fillText('innovative technologies', 379, 255);
ctx.restore();
if (rgbstep > 80) {
var t = setTimeout('Textfadedown()', 10);
}
if (rgbstep == 80) {
Textfadeup();
}
}
</script>
</head>
<body onload="init();">
<canvas id="myCanvas" width="1500" height="500">
</canvas>
</body>
</html>
任何代碼片段,我可以制定出來,或指出我犯我的錯誤將有所幫助。
jsfiddle ??????????? – gsiradze
你需要先定義你的淡入淡出功能,看看我提供的答案 – Canvas