|
|
@@ -0,0 +1,83 @@
|
|
|
+<template>
|
|
|
+ <canvas :width="width" :height="height" class="pointer-canvas" ref="pointerCanvas">
|
|
|
+ </canvas>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script>
|
|
|
+export default {
|
|
|
+ name: 'PointerCanvas',
|
|
|
+ data () {
|
|
|
+ return {
|
|
|
+ points: [],
|
|
|
+ width: window.innerWidth,
|
|
|
+ height: window.innerHeight
|
|
|
+ }
|
|
|
+ },
|
|
|
+ mounted () {
|
|
|
+ this._canvas = this.$refs.pointerCanvas
|
|
|
+ this._ctx = this._canvas.getContext('2d')
|
|
|
+
|
|
|
+ window.addEventListener('resize', this.handleResize)
|
|
|
+ this._canvas.addEventListener('mousemove', this.handleMouseMove)
|
|
|
+ this._interval = setInterval(this.shiftPoints, 32)
|
|
|
+ },
|
|
|
+ destroyed () {
|
|
|
+ clearInterval(this._interval)
|
|
|
+ window.removeEventListener('resize', this.handleResize)
|
|
|
+ this._canvas.removeEventListener('mousemove', this.handleMouseMove)
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ handleResize () {
|
|
|
+ this.width = window.innerWidth
|
|
|
+ this.height = window.innerHeight
|
|
|
+ },
|
|
|
+ handleMouseMove (e) {
|
|
|
+ const { x, y } = this.points[this.points.length - 1] ?? { x: 0, y: 0 }
|
|
|
+ console.log(x, e.clientX)
|
|
|
+ if (this.points.length === 0 || Math.abs(x - e.clientX) > 8 || Math.abs(y - e.clientY) > 8) {
|
|
|
+ this.points.unshift({ x: e.clientX, y: e.clientY })
|
|
|
+ if (this.points.length > 20) {
|
|
|
+ this.points.pop()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ shiftPoints () {
|
|
|
+ if (this.points.length > 20) {
|
|
|
+ this.points.pop()
|
|
|
+ }
|
|
|
+ if (this.points.length > 0) {
|
|
|
+ this.points.pop()
|
|
|
+ }
|
|
|
+ this.renderer()
|
|
|
+ },
|
|
|
+ renderer () {
|
|
|
+ this._ctx.lineJoin = 'round'
|
|
|
+ this._ctx.strokeStyle = `rgba(211, 47, 47, 0.3)`
|
|
|
+ this._ctx.fillStyle = `rgba(211, 47, 47, 1)`
|
|
|
+ this._ctx.clearRect(0, 0, 2000, 2000)
|
|
|
+ this._ctx.beginPath()
|
|
|
+ const length = this.points.length
|
|
|
+ this.points.forEach((point, index) => {
|
|
|
+ this._ctx.lineTo(point.x, point.y)
|
|
|
+ this._ctx.lineWidth = (length - index + 6) / 1.5
|
|
|
+ this._ctx.stroke()
|
|
|
+ })
|
|
|
+ this._ctx.closePath()
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+</script>
|
|
|
+
|
|
|
+<style lang="scss" scoped>
|
|
|
+
|
|
|
+.pointer-canvas {
|
|
|
+ position: fixed;
|
|
|
+ top: 0;
|
|
|
+ bottom: 0;
|
|
|
+ left: 0;
|
|
|
+ right: 0;
|
|
|
+ width: 100vw;
|
|
|
+ height: 100vh;
|
|
|
+ z-index: 1;
|
|
|
+}
|
|
|
+</style>
|