CanvasStudy.vue 9.14 KB
Newer Older
hucy's avatar
hucy committed
1
<!--
hucy's avatar
hucy committed
2
 * canvas 时钟
hucy's avatar
hucy committed
3 4
  -->
<script setup lang="ts">
hucy's avatar
hucy committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
import { onMounted, ref, reactive, onBeforeUnmount } from 'vue';

const isShowGrid = ref(false);
const state = reactive({
  center: {
    x: 300,
    y: 300,
  }, // 圆心
  r: 200, // 半径,
  timer: null as any,
  canvasSeconds: null as any,
});

onBeforeUnmount(() => {
  if (state.timer) {
    clearInterval(state.timer);
    state.timer = null;
  }
});
hucy's avatar
hucy committed
24 25

onMounted(() => {
hucy's avatar
hucy committed
26
  state.canvasSeconds = document.getElementById('canvas-seconds');
hucy's avatar
hucy committed
27 28
  let canvas: any = document.getElementById('canvas');
  let pen = canvas.getContext('2d');
hucy's avatar
hucy committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  const { center, r } = state;

  let img = new Image();
  img.src = require('./imgs/clock-bg.jpg');
  img.onload = function () {
    // 背景色
    pen.save();
    pen.fillStyle = '#2d2d2d';
    pen.fillRect(0, 0, 600, 600);
    pen.restore();

    // 绘制图片
    pen.save();
    pen.beginPath();
    pen.arc(center.x, center.y, r, 0, 2 * Math.PI);
    // pen.fill();
    // 将上面的区域作为剪辑区域
    pen.clip();
    pen.drawImage(img, 241, 68, 300, 300, 100, 100, 2 * r, 2 * r);
    // pen.drawImage(img, -90, 80);
    pen.restore();
hucy's avatar
hucy committed
50

hucy's avatar
hucy committed
51 52 53
    pen.save();
    // 大刻度
    for (let i = 0; i < 12; i++) {
hucy's avatar
hucy committed
54
      pen.beginPath();
hucy's avatar
hucy committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
      const a1 = Math.sin(toArc(i * 30)) * (r - 10); // (r - 10) 10是刻度与圆弧边线的间隙
      const b1 = Math.cos(toArc(i * 30)) * (r - 10);
      const x0 = center.x + a1;
      const y0 = center.y - b1;
      const a2 = Math.sin(toArc(i * 30)) * 8; // 8是大刻度线的长度
      const b2 = Math.cos(toArc(i * 30)) * 8;
      const x1 = x0 - a2;
      const y1 = y0 + b2;
      pen.moveTo(x0, y0);
      pen.lineTo(x1, y1);
      pen.lineWidth = 4;
      pen.lineCap = 'round';
      pen.strokeStyle = '#5CD8F9';

      // pen.shadowColor = 'rgba(0, 0, 0, 0.35)';
      // pen.shadowBlur = 10;
      // pen.shadowOffsetX = -8;
      // pen.shadowOffsetY = 10;

hucy's avatar
hucy committed
74 75
      pen.stroke();
    }
hucy's avatar
hucy committed
76 77 78 79 80
    pen.restore();

    // 小刻度
    pen.save();
    for (let i = 0; i < 60; i++) {
hucy's avatar
hucy committed
81
      pen.beginPath();
hucy's avatar
hucy committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95
      const a1 = Math.sin(toArc(i * 6)) * (r - 10); // (r - 10) 10是刻度与圆弧边线的间隙
      const b1 = Math.cos(toArc(i * 6)) * (r - 10);
      const x0 = center.x + a1;
      const y0 = center.y - b1;
      const a2 = Math.sin(toArc(i * 6)) * 4; // 4是大刻度线的长度
      const b2 = Math.cos(toArc(i * 6)) * 4;
      const x1 = x0 - a2;
      const y1 = y0 + b2;
      pen.moveTo(x0, y0);
      pen.lineTo(x1, y1);
      pen.lineWidth = 1;
      pen.lineCap = 'round';
      pen.strokeStyle = '#48E6FE';

hucy's avatar
hucy committed
96 97
      pen.stroke();
    }
hucy's avatar
hucy committed
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
    pen.restore();

    // 绘制文本
    pen.save();
    for (let i = 0; i < 12; i++) {
      pen.beginPath();
      const a1 = Math.sin(toArc(i * 30)) * (r - 40);
      const b1 = Math.cos(toArc(i * 30)) * (r - 40);

      const num = i || 12;
      const x0 = center.x + a1;
      const y0 = center.y - b1;

      pen.font = '26px sans-serif';
      pen.textAlign = 'center';
      pen.textBaseline = 'middle';

      pen.fillStyle = '#5CD8F9';
      pen.fillText(`${num}`, x0, y0);
    }
    pen.restore();

    // 外圆
    pen.save();
    pen.beginPath();
    pen.arc(center.x, center.y, r, 0, 2 * Math.PI);
    pen.lineWidth = 10;
    pen.strokeStyle = '#0E4A6F';

    // 阴影
    pen.shadowColor = 'rgba(0, 0, 0, 0.35)';
    pen.shadowBlur = 20;
    pen.shadowOffsetX = -12;
    pen.shadowOffsetY = 20;
    pen.stroke();
    pen.restore();

    // 圆心
    pen.save();
    pen.beginPath();
    pen.arc(center.x, center.y, 16, 0, 2 * Math.PI);
    pen.fillStyle = '#204969';
    pen.shadowColor = 'rgba(0, 0, 0, 0.35)';
    pen.shadowBlur = 5;
    pen.shadowOffsetX = -10;
    pen.shadowOffsetY = 10;
    pen.fill();
    pen.restore();

    state.timer = setInterval(() => {
      drawSeconds();
    }, 1000);
  };
});

function drawSeconds() {
  let pen = state.canvasSeconds.getContext('2d');

  const { center, r } = state;

  const date = getDate();
  // console.log('---', date);

  const secondsDeg = date.seconds * 6 + 270;
  const minDeg = date.minutes * 6 + 270 + date.seconds * 0.1;
  const hoursDeg =
    date.hours * 30 + 270 + date.minutes * 0.5 + date.seconds * (1 / 120);
  pen.clearRect(0, 0, 600, 600);

  // 时间显示
hucy's avatar
hucy committed
168
  pen.save();
hucy's avatar
hucy committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
  pen.font = '26px sans-serif';
  pen.textAlign = 'center';
  pen.textBaseline = 'middle';
  pen.fillStyle = '#5CD8F9';
  const hStr = date.hours < 10 ? `0${date.hours}` : date.hours;
  const mStr = date.minutes < 10 ? `0${date.minutes}` : date.minutes;
  const sStr = date.seconds < 10 ? `0${date.seconds}` : date.seconds;

  const timeStr = `${hStr}:${mStr}:${sStr}`;
  pen.fillText(timeStr, center.x, center.y - (2 / 5) * r);
  pen.restore();

  // 时针
  pen.save();
  const offseHour = rotateOriginOffset(600, center, toArc(hoursDeg));
  pen.translate(offseHour.x, offseHour.y); // 改变旋转中心
  pen.rotate(toArc(hoursDeg));
  pen.beginPath();
  pen.moveTo(center.x - 20, center.y);
  pen.lineTo(center.x + r - 110, center.y);
  pen.lineWidth = 9;
  pen.lineCap = 'round';
  pen.strokeStyle = '#1E88D2';

  pen.shadowColor = 'rgba(0, 0, 0, 0.35)';
  pen.shadowBlur = 5;
  pen.shadowOffsetX = -10;
  pen.shadowOffsetY = 10;
  pen.stroke();
hucy's avatar
hucy committed
198 199 200 201
  pen.restore();

  pen.save();
  pen.beginPath();
hucy's avatar
hucy committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
  pen.arc(center.x, center.y, 12, 0, 2 * Math.PI);
  pen.fillStyle = '#71C6F7';
  pen.shadowColor = 'rgba(0, 0, 0, 0.35)';
  pen.shadowBlur = 2;
  pen.shadowOffsetX = -2;
  pen.shadowOffsetY = 2;
  pen.fill();
  pen.restore();

  // 分针
  pen.save();
  const offsetMin = rotateOriginOffset(600, center, toArc(minDeg));
  pen.translate(offsetMin.x, offsetMin.y); // 改变旋转中心
  pen.rotate(toArc(minDeg));
  pen.beginPath();
  pen.moveTo(center.x - 20, center.y);
  pen.lineTo(center.x + r - 80, center.y);
  pen.lineWidth = 5;
  pen.lineCap = 'round';
  pen.strokeStyle = '#71C6F7';

  pen.shadowColor = 'rgba(0, 0, 0, 0.35)';
  pen.shadowBlur = 5;
  pen.shadowOffsetX = -10;
  pen.shadowOffsetY = 10;

hucy's avatar
hucy committed
228
  pen.stroke();
hucy's avatar
hucy committed
229 230 231 232 233 234 235
  pen.restore();

  // 秒针
  pen.save();
  const offset = rotateOriginOffset(600, center, toArc(secondsDeg));
  pen.translate(offset.x, offset.y); // 改变旋转中心
  pen.rotate(toArc(secondsDeg));
hucy's avatar
hucy committed
236
  pen.beginPath();
hucy's avatar
hucy committed
237 238
  pen.moveTo(center.x - 40, center.y);
  pen.lineTo(center.x + r - 10, center.y);
hucy's avatar
hucy committed
239
  pen.lineWidth = 2;
hucy's avatar
hucy committed
240 241 242 243 244 245 246 247
  pen.lineCap = 'round';
  pen.strokeStyle = '#71C6F7';

  pen.shadowColor = 'rgba(0, 0, 0, 0.35)';
  pen.shadowBlur = 5;
  pen.shadowOffsetX = -10;
  pen.shadowOffsetY = 10;

hucy's avatar
hucy committed
248 249 250
  pen.stroke();
  pen.restore();

hucy's avatar
hucy committed
251 252 253 254 255 256 257 258 259 260 261
  pen.save();
  pen.beginPath();
  pen.arc(center.x, center.y, 5, 0, 2 * Math.PI);
  pen.fillStyle = '#204969';
  pen.shadowColor = 'rgba(0, 0, 0, 0.35)';
  pen.shadowBlur = 2;
  pen.shadowOffsetX = -2;
  pen.shadowOffsetY = 2;
  pen.fill();
  pen.restore();
}
hucy's avatar
hucy committed
262

hucy's avatar
hucy committed
263 264 265 266 267 268 269 270 271
// 绘制网格
function drawGrid(color: string, w: number, h: number, pen: any) {
  const step = 100;
  const w_l = w / step;
  const h_l = h / step;

  pen.save();
  // 横着的线
  for (let i = 0; i <= h_l; i++) {
hucy's avatar
hucy committed
272
    pen.beginPath();
hucy's avatar
hucy committed
273 274 275
    pen.strokeStyle = color;
    pen.moveTo(0, i * step);
    pen.lineTo(w, i * step);
hucy's avatar
hucy committed
276
    pen.stroke();
hucy's avatar
hucy committed
277 278 279 280 281 282 283 284 285 286
  }
  // 竖着的线
  for (let i = 0; i <= w_l; i++) {
    pen.beginPath();
    pen.moveTo(i * step, 0);
    pen.lineTo(i * step, h);
    pen.stroke();
  }
  pen.restore();
}
hucy's avatar
hucy committed
287

hucy's avatar
hucy committed
288 289 290 291 292 293 294 295
function showGrid() {
  let canvas: any = document.getElementById('canvas-grid');
  let pen = canvas.getContext('2d');
  isShowGrid.value = !isShowGrid.value;
  if (isShowGrid.value) {
    drawGrid('#FD7013', 600, 600, pen); // 网格
  } else {
    pen.clearRect(0, 0, 600, 600);
hucy's avatar
hucy committed
296
  }
hucy's avatar
hucy committed
297
}
hucy's avatar
hucy committed
298

hucy's avatar
hucy committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
function toArc(degree: number) {
  return (degree * Math.PI) / 180;
}

function rotateOriginOffset(
  width: number,
  center: { x: number; y: number },
  arc: any
) {
  const r1 = width - center.x;
  const xRes1 = Math.cos(arc) * r1;
  const yRes1 = Math.sin(arc) * r1;
  const x1 = center.x + xRes1;
  const y1 = center.y + yRes1;

  const x0 = width;
  const y0 = center.y;
  const c0 = Math.sqrt(Math.pow(x0, 2) + Math.pow(y0, 2));
  const arc0 = Math.atan2(y0, x0);
  const arc_0 = arc0 + arc;
  const y2 = Math.sin(arc_0) * c0;
  const x2 = y2 / Math.tan(arc_0);

  const xLength = x1 - x2;
  const yLength = y1 - y2;
  return {
    x: xLength,
    y: yLength,
  };
}

function getDate() {
  const myDate = new Date();
  return {
    hours: myDate.getHours(),
    minutes: myDate.getMinutes(),
    seconds: myDate.getSeconds(),
  };
}
hucy's avatar
hucy committed
338 339 340 341 342
</script>
<template>
  <div class="fit">
    <div>canvas</div>

hucy's avatar
hucy committed
343 344 345
    <div>
      <q-btn label="显示网格" color="primary" @click="showGrid" />
    </div>
hucy's avatar
hucy committed
346 347 348
    <div class="relative-position">
      <canvas
        id="canvas"
hucy's avatar
hucy committed
349
        width="600"
hucy's avatar
hucy committed
350 351 352 353
        height="600"
        style="border: 1px solid red; position: absolute; top: 0; left: 0"
      ></canvas>
      <canvas
hucy's avatar
hucy committed
354 355 356 357 358 359 360 361 362 363 364 365 366
        id="canvas-seconds"
        width="600"
        height="600"
        style="
          border: 1px solid transparent;
          position: absolute;
          top: 0;
          left: 0;
        "
      ></canvas>
      <canvas
        id="canvas-grid"
        width="600"
hucy's avatar
hucy committed
367
        height="600"
hucy's avatar
hucy committed
368 369 370 371 372 373
        style="
          border: 1px solid transparent;
          position: absolute;
          top: 0;
          left: 0;
        "
hucy's avatar
hucy committed
374 375 376 377 378
      ></canvas>
    </div>
  </div>
</template>

hucy's avatar
hucy committed
379
<style lang="scss" scoped></style>