background.js 647 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import Sprite from '../base/sprite'
  2. const screenWidth = window.innerWidth
  3. const screenHeight = window.innerHeight
  4. const BG_WIDTH = 375
  5. const BG_HEIGHT = 667
  6. /**
  7. * 游戏背景类
  8. * 提供update和render函数实现无限滚动的背景功能
  9. */
  10. export default class BackGround extends Sprite {
  11. constructor(ctx,BG_IMG_SRC) {
  12. super(BG_IMG_SRC, BG_WIDTH, BG_HEIGHT)
  13. this.render(ctx)
  14. }
  15. /**
  16. */
  17. render(ctx,sx=0,sy=0,scalX=1,scalY=1) {
  18. ctx.drawImage(
  19. this.img,
  20. sx,
  21. sy,
  22. this.width*scalX,
  23. this.height*scalY,
  24. 0,
  25. 0,
  26. screenWidth,
  27. screenHeight
  28. )
  29. }
  30. }