rotateCanvas.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import {onMounted, ref} from "vue";
  2. import blueImageUrl from '@/assets/login-rotate-blue.png'
  3. import yellowImageUrl from '@/assets/login-rotate-yellow.png'
  4. interface IPoint {
  5. x: number;
  6. y: number;
  7. }
  8. class CircleItem {
  9. ctx: CanvasRenderingContext2D
  10. img: HTMLImageElement
  11. r: number
  12. w: number
  13. h: number
  14. x: number
  15. y: number
  16. speed: number
  17. rectCenterPoint: IPoint
  18. cacheCanvas: HTMLCanvasElement
  19. cacheCtx: CanvasRenderingContext2D
  20. constructor(ctx: CanvasRenderingContext2D, img: HTMLImageElement, r: number, x: number, y: number, speed: number) {
  21. this.ctx = ctx
  22. this.img = img;
  23. this.r = r;
  24. this.w = r * 2;
  25. this.h = r * 2;
  26. this.x = x;
  27. this.y = y;
  28. this.speed = speed;
  29. this.rectCenterPoint = {
  30. x: r,
  31. y: r,
  32. };
  33. this.cacheCanvas = document.createElement("canvas")
  34. this.cacheCtx = this.cacheCanvas.getContext("2d") as CanvasRenderingContext2D
  35. this.cache();
  36. }
  37. draw() {
  38. this.rotate();
  39. this.ctx.scale(1, 0.3);
  40. this.ctx.drawImage(
  41. this.cacheCanvas,
  42. 0,
  43. 0,
  44. this.w,
  45. this.h,
  46. this.x - this.r,
  47. this.y - this.r,
  48. this.r * 2,
  49. this.r * 2
  50. );
  51. this.ctx.scale(1, 1 / 0.3);
  52. }
  53. cache() {
  54. this.cacheCanvas.width = this.w;
  55. this.cacheCanvas.height = this.h;
  56. this.cacheCtx.save();
  57. this.cacheCtx.beginPath();
  58. this.cacheCtx.drawImage(this.img, 0, 0, this.r * 2, this.r * 2);
  59. this.cacheCtx.closePath();
  60. this.cacheCtx.restore();
  61. }
  62. rotate() {
  63. this.cacheCtx.clearRect(0, 0, this.w, this.h);
  64. this.cacheCtx.translate(this.rectCenterPoint.x, this.rectCenterPoint.y);
  65. this.cacheCtx.rotate((this.speed * Math.PI) / 180);
  66. this.cacheCtx.translate(-this.rectCenterPoint.x, -this.rectCenterPoint.y);
  67. this.cacheCtx.drawImage(this.img, 0, 0, this.r * 2, this.r * 2);
  68. }
  69. }
  70. // 旋转底座
  71. export default function useRotateCanvas() {
  72. const CANVAS_W = 1800
  73. const CANVAS_H = 600
  74. const canvasRef = ref<HTMLCanvasElement>();
  75. onMounted(() => {
  76. if (canvasRef.value) {
  77. const canvas = canvasRef.value
  78. const ctx = canvas.getContext("2d") as CanvasRenderingContext2D;
  79. const blueImg = new Image();
  80. blueImg.src = blueImageUrl;
  81. let rotateBlue: CircleItem
  82. blueImg.onload = function () {
  83. rotateBlue = new CircleItem(ctx, blueImg, 750, CANVAS_W / 2, 800, 1);
  84. };
  85. const yellowImg = new Image();
  86. yellowImg.src = yellowImageUrl;
  87. let rotateYellow: CircleItem
  88. yellowImg.onload = function () {
  89. rotateYellow = new CircleItem(ctx, yellowImg, 400, CANVAS_W / 2, 800, 0.6);
  90. };
  91. function animate() {
  92. ctx.clearRect(0, 0, CANVAS_W, CANVAS_H);
  93. rotateBlue && rotateBlue.draw();
  94. rotateYellow && rotateYellow.draw();
  95. requestAnimationFrame(animate);
  96. }
  97. animate();
  98. }
  99. })
  100. return {
  101. canvasRef
  102. }
  103. }