background.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * 游戏背景
  3. */
  4. export default class BackGround {
  5. constructor(ctx, w, h) {
  6. // this.w = w
  7. // this.h = h
  8. this.render(ctx, w, h)
  9. // 重新开始按钮区域方便简易判断按钮点击
  10. this.btnArea = {
  11. startX: 0.04*w,
  12. startY: 50,
  13. endX : 0.04*w + 0.2*w,
  14. endY : 50 + 0.16*w
  15. }
  16. }
  17. /**
  18. * 背景图重绘函数
  19. * 绘制浅色背景图, 大小和屏幕一致
  20. * 绘制三个按钮, 即文字
  21. * 绘制棋盘背景板
  22. */
  23. render(ctx, w, h) {
  24. ctx.clearRect(0, 0, w, h);
  25. ctx.fillStyle = '#F0F0F0'
  26. ctx.fillRect(0, 0, w, h)
  27. ctx.fillStyle = '#F7921E'
  28. ctx.font = "30px 微软雅黑"
  29. // ctx.textBaseline = "top"
  30. ctx.fillText("2048之陈小发", 0.04*w, 0.1*w);
  31. ctx.fillStyle = '#8F7A66'
  32. ctx.fillRect(0.04*w, 50, 0.2*w, 0.16*w)
  33. ctx.fillStyle = '#BAAB9E'
  34. ctx.fillRect(0.28*w, 50, 0.32*w, 0.16*w)
  35. ctx.fillStyle = '#F7921E'
  36. ctx.fillRect(0.64*w, 50, 0.32*w, 0.16*w)
  37. ctx.fillStyle = 'white'
  38. ctx.font = "20px 微软雅黑"
  39. ctx.textAlign = "center"
  40. ctx.textBaseline = "top"
  41. ctx.fillText("重玩", 0.14*w, 50+0.04*w);
  42. ctx.fillText("最高分数", 0.44*w, 50);
  43. ctx.fillText("当前分数", 0.8*w, 50);
  44. // console.log(0.14*w, 50+0.1*w)
  45. // 棋盘背景
  46. ctx.fillStyle = '#BBADA0'
  47. ctx.fillRect(0.04*w, 50+0.3*w, 0.92*w, 0.92*w)
  48. }
  49. }