导出
backgroud.js
export default function(ctx) {
var bg = new Image();
bg.src = "./images/bg.jpg";
bg.width = 512;
bg.height = 512;
var width = window.innerWidth; //全局对象
var height = window.innerHeight;
var top = 0;
move();
function move() {
top++;
requestAnimationFrame(function() {
ctx.clearRect(0, 0, width, height);
ctx.drawImage(bg, 0, 0, bg.width, bg.height, 0, top, width, height);
ctx.drawImage(
bg,
0,
0,
bg.width,
bg.height,
0,
-height + top,
width,
height
);
if (top == height) {
top = 0;
}
move();
});
}
}
导出 audio.js
export default function() {
var bgm = new Audio();
bgm.volume = 0.02;
bgm.src = "./audio/bgm.mp3";
bgm.play();
}
导入 background.js 和audio.js
import "./js/component/weapp-adapter.js"
import Audio from "./js/component/audio"
import Background from "./js/component/background.js"
var ctx = canvas.getContext("2d");
Background(ctx)
Audio()