使用javascript和HTML5画布的游戏应用程序的背景

使用javascript和HTML5画布的游戏应用程序的背景,html,Html,我正在创建一个游戏应用程序。我必须经常使用 ctx.fillStyle = "blue"; ctx.fillRect(0, 0, width, height); 但我想在背景中拍摄动人的电影,比如风景、山峦、道路、路过的树木。如何实现这一点?我建议您在背景层运行一个视频,参考html5的视频元素,然后在画布中使用destination out and source over属性。找到下面的代码 // Fill canvas using 'destination-out' and alpha a

我正在创建一个游戏应用程序。我必须经常使用

ctx.fillStyle = "blue";
ctx.fillRect(0, 0, width, height);

但我想在背景中拍摄动人的电影,比如风景、山峦、道路、路过的树木。如何实现这一点?

我建议您在背景层运行一个视频,参考html5的视频元素,然后在画布中使用destination out and source over属性。找到下面的代码

// Fill canvas using 'destination-out' and alpha at 0.05
ctx.globalCompositeOperation = 'destination-out';
ctx.fillStyle = "rgba(255, 255, 255, 0.05)";
ctx.beginPath();
ctx.fillRect(0, 0,W,H);
ctx.fill();
// Set the default mode.
ctx.globalCompositeOperation = 'source-over';
这样,您的背景将独立于游戏运行。当然,您可以使用javascript中的eventlistners等暂停和恢复它