微信小程序模块化---导入和导出

导出 

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()

猜你喜欢

转载自blog.csdn.net/weixin_41069726/article/details/88769566