mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
if cursor did not move, animate only one position.
This commit is contained in:
parent
9daa8461bb
commit
dfd7bec140
@ -21,7 +21,10 @@
|
|||||||
import { getMovementXYatPercent } from './utils/canvas-movement'
|
import { getMovementXYatPercent } from './utils/canvas-movement'
|
||||||
|
|
||||||
const CANVAS_SCALE = 2
|
const CANVAS_SCALE = 2
|
||||||
|
// How often are position data arriving
|
||||||
const POS_INTERVAL_MS = 750
|
const POS_INTERVAL_MS = 750
|
||||||
|
// How many pixel change is considered as movement
|
||||||
|
const POS_THRESHOLD_PX = 20
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
name: 'neko-cursors',
|
name: 'neko-cursors',
|
||||||
@ -85,9 +88,9 @@
|
|||||||
// last points coordinates for each session
|
// last points coordinates for each session
|
||||||
private _last_points!: Record<string, Cursor>
|
private _last_points!: Record<string, Cursor>
|
||||||
|
|
||||||
canvasAnimate(now: number = NaN) {
|
canvasAnimateFrame(now: number = NaN) {
|
||||||
// request another frame
|
// request another frame
|
||||||
if (this._percent <= 1) window.requestAnimationFrame(this.canvasAnimate)
|
if (this._percent <= 1) window.requestAnimationFrame(this.canvasAnimateFrame)
|
||||||
|
|
||||||
// calculate factor
|
// calculate factor
|
||||||
const delta = (now - this._last_animation_time) / POS_INTERVAL_MS
|
const delta = (now - this._last_animation_time) / POS_INTERVAL_MS
|
||||||
@ -113,21 +116,55 @@
|
|||||||
@Watch('hostId')
|
@Watch('hostId')
|
||||||
@Watch('cursors')
|
@Watch('cursors')
|
||||||
canvasUpdateCursors() {
|
canvasUpdateCursors() {
|
||||||
|
// track unchanged cursors
|
||||||
|
let unchanged = 0
|
||||||
|
|
||||||
// create points for animation
|
// create points for animation
|
||||||
this._points = []
|
this._points = []
|
||||||
for (const { id, cursors } of this.cursors) {
|
for (const { id, cursors } of this.cursors) {
|
||||||
// ignore my and host's cursor
|
if (
|
||||||
if (id == this.sessionId || id == this.hostId) continue
|
// if there are no positions
|
||||||
|
cursors.length == 0 ||
|
||||||
|
// ignore own cursor
|
||||||
|
id == this.sessionId ||
|
||||||
|
// ignore host's cursor
|
||||||
|
id == this.hostId
|
||||||
|
) {
|
||||||
|
unchanged++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// get last point
|
||||||
|
const new_last_point = cursors[cursors.length - 1]
|
||||||
|
|
||||||
// add last cursor position to cursors (if available)
|
// add last cursor position to cursors (if available)
|
||||||
let pos = { id } as SessionCursors
|
let pos = { id } as SessionCursors
|
||||||
if (id in this._last_points) {
|
if (id in this._last_points) {
|
||||||
pos.cursors = [this._last_points[id], ...cursors]
|
const last_point = this._last_points[id]
|
||||||
|
|
||||||
|
// if cursor did not move considerably
|
||||||
|
if (
|
||||||
|
Math.abs(last_point.x - new_last_point.x) < POS_THRESHOLD_PX &&
|
||||||
|
Math.abs(last_point.y - new_last_point.y) < POS_THRESHOLD_PX
|
||||||
|
) {
|
||||||
|
// we knew that this cursor did not change, but other
|
||||||
|
// might, so we keep only one point to be drawn
|
||||||
|
pos.cursors = [new_last_point]
|
||||||
|
// and increate unchanged counter
|
||||||
|
unchanged++
|
||||||
|
} else {
|
||||||
|
// if cursor moved, we want to include last point
|
||||||
|
// in the animation, so that movement can be seamless
|
||||||
|
pos.cursors = [last_point, ...cursors]
|
||||||
|
this._last_points[id] = new_last_point
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// if cursor does not have last point, it is not
|
||||||
|
// displayed in canvas and it should be now
|
||||||
pos.cursors = [...cursors]
|
pos.cursors = [...cursors]
|
||||||
|
this._last_points[id] = new_last_point
|
||||||
}
|
}
|
||||||
|
|
||||||
this._last_points[id] = cursors[cursors.length - 1]
|
|
||||||
this._points.push(pos)
|
this._points.push(pos)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,11 +175,16 @@
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if all cursors are unchanged
|
||||||
|
if (unchanged == this.cursors.length) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// start animation if not running
|
// start animation if not running
|
||||||
const percent = this._percent
|
const percent = this._percent
|
||||||
this._percent = 0
|
this._percent = 0
|
||||||
if (percent > 1 || percent == 0) {
|
if (percent > 1 || !percent) {
|
||||||
this.canvasAnimate()
|
this.canvasAnimateFrame()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user