CanvasCurve.vue 4.02 KB
Newer Older
hucy's avatar
hucy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
<!--
 * canvas 曲线
  -->
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { drawGrid, rotateOriginOffset, toArc } from '../utils';

const center = {
  x: 200,
  y: 300,
};
const step = ref(0);
const translateX = ref(0);
const translateXstep = ref(0);
const isShowGrid = ref(false);
const state = reactive({
  gridDom: null as any,
  canvasDom: null as any,
  curveDom: null as any,
  r: 100,
  r2: 40,
});
onMounted(() => {
  state.canvasDom = document.getElementById('canvas');
  state.gridDom = document.getElementById('canvas-grid');
  state.curveDom = document.getElementById('canvas-curve');

  initCanvas();
});

function initCanvas() {
  const { r } = state;
  let ctx = state.canvasDom.getContext('2d');
  let pen = state.curveDom.getContext('2d');

  translateXstep.value = (r - Math.cos(toArc(30)) * r) / 60;

  window.requestAnimationFrame(() => {
    draw(ctx, r, pen);
  });
}

function draw(ctx: any, r: number, pen: any) {
  // 基础圆
  ctx.save();
  ctx.clearRect(0, 0, 1200, 600);
  ctx.beginPath();
  ctx.arc(center.x, center.y, r, 0, 2 * Math.PI);
  ctx.lineWidth = 1;
  ctx.strokeStyle = '#78C0A8';
  ctx.stroke();
  ctx.restore();

  ctx.save();
  // 每次走0.5度,1秒钟走30度,
  const arc = toArc(360 - step.value * 0.5);
  // const arc = toArc(30);
  const offset1 = rotateOriginOffset(1200, center, arc);
  ctx.translate(offset1.x, offset1.y); // 改变旋转中心
  ctx.rotate(arc);
  ctx.beginPath();
  ctx.moveTo(center.x, center.y);
  ctx.lineTo(center.x + r, center.y);
  ctx.lineWidth = 2;
  ctx.strokeStyle = '#78C0A8';
  ctx.stroke();

  // 第二个圆
  const centerX2 = center.x + r; // 第2个圆的圆心
  const centerY2 = center.y;
  ctx.beginPath();
  ctx.arc(centerX2, centerY2, 4, 0, Math.PI * 2);
  ctx.strokeStyle = '#FC4422';
  ctx.stroke();
  ctx.restore();

  // 圆弧上点的坐标
  const a = Math.sin(arc) * r;
  const b = Math.cos(arc) * r;
  const x0 = center.x + b;
  const y0 = center.y + a;
  const lastX = center.x + r + 100;
  ctx.save();
  ctx.beginPath();
  ctx.moveTo(x0, y0);
  ctx.lineTo(lastX, y0);
  ctx.strokeStyle = '#78C0A8';
  ctx.stroke();
  ctx.restore();

  // 曲线画布
  translateX.value += translateXstep.value; // 每次平移的值
  pen.save();
  pen.beginPath();
  pen.arc(lastX - translateX.value, y0, 2, 0, Math.PI * 2);
  pen.fillStyle = '#78C0A8';
  pen.fill();
  pen.restore();

  step.value++;

  // 回调函数执行次数通常是每秒 60 次
  // if (step.value < 60) {
  //   window.requestAnimationFrame(() => {
  //     draw(ctx, r, pen);
  //   });
  // }

  if (translateX.value > 400) {
    translateX.value = 0;
    pen.clearRect(0, 0, 1200, 600);
  }
  if (step.value === 60 * 12) {
    step.value = 0;
  }
  window.requestAnimationFrame(() => {
    draw(ctx, r, pen);
  });
}

function showGrid() {
  let ctx = state.gridDom.getContext('2d');
  isShowGrid.value = !isShowGrid.value;
  if (isShowGrid.value) {
    drawGrid('red', 1200, 600, ctx);
  } else {
    ctx.clearRect(0, 0, 1200, 600);
  }
}
</script>
<template>
  <div class="fit">
    <div class="title">canvas 曲线</div>
    <div class="btns">
      <q-btn label="网格" color="primary" @click="showGrid" />
    </div>
    <div class="relative-position">
      <canvas
        id="canvas"
        width="1200"
        height="600"
        style="border: 1px solid red; position: absolute; top: 0; left: 0"
      ></canvas>
      <canvas
        id="canvas-curve"
        width="1200"
        height="600"
        :style="{
          border: '1px solid transparent',
          position: 'absolute',
          top: 0,
          left: 0,
          transform: `translate(${translateX}px, 0)`,
        }"
      ></canvas>
      <canvas
        id="canvas-grid"
        width="1200"
        height="600"
        style="
          border: 1px solid transparent;
          position: absolute;
          top: 0;
          left: 0;
        "
      ></canvas>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.title {
  margin-bottom: $padding-md;
}
.btns {
  margin-bottom: $padding-md;
}
</style>