openreplay/frontend/app/player/web/addons/MouseTrail.ts
Delirium 35461acaf3
[WIP] Mobile replayer (#1452)
* fix(ui): fix up mobile recordings display

* fix(ui): some messages

* fix(ui): some messages

* fix(player): fix msg generation for ios messages

* feat(player): add generic mmanager interface for ios player impl

* feat(player): mobile player and message manager; touch manager; videoplayer

* feat(player): add iphone shells, add log panel,

* feat(player): detect ios sessions and inject correct player

* feat(player): move screen mapper to utils

* feat(player): events panel for mobile, map shell sizes to device type data,

* feat(player): added network tab to mobile player; unify network message (ios and web)

* feat(player): resize canvas up to phone screen size, fix capitalize util

* feat(player): some general changes to support mobile events and network entries

* feat(player): remove swipes from timeline

* feat(player): more stuff for list walker

* fix(ui): performance tab, mobile project typings and form

* fix(ui):some ui touches for ios replayer shell

* fix(ui): more fixes for ui, new onboarding screen for mobile projects

* feat(ui): mobile overview panel (xray)

* feat(ui): fixes for phone shell and tap events

* fix(tracker): change phone shells and sizes

* fix(tracker): fix border on replay screen

* feat(ui): use crashes from db to show in session

* feat(ui): use event name for xray

* feat(ui): some overall ui fixes

* feat(ui): IOS -> iOS

* feat(ui): change tags to ant d

* fix(ui): fast fix

* fix(ui): fix for capitalizer

* fix(ui): fix for browser display

* fix(ui): fix for note popup

* fix(ui): change exceptions display

* fix(ui): add click rage to ios xray

* fix(ui): some icons and resizing

* fix(ui): fix ios context menu overlay, fix console logs creation for ios

* feat(ui): added icons

* feat(ui): performance warnings

* feat(ui): performance warnings

* feat(ui): different styles

* feat(ui): rm debug true

* feat(ui): fix warnings display

* feat(ui): some styles for animation

* feat(ui): add some animations to warnings

* feat(ui): move perf warnings to performance graph

* feat(ui): hide/show warns dynamically

* feat(ui): new mobile touch animation

* fix(tracker): update msg for ios

* fix(ui): taprage fixes

* fix(ui): regenerate icons and messages

* fix(ui): fix msgs

* fix(backend): fix events gen

* fix(backend): fix userid msg
2023-10-27 12:12:09 +02:00

178 lines
No EOL
4.3 KiB
TypeScript

/**
* Inspired by Bryan C (@bryjch at codepen)
* */
const LINE_DURATION = 3.5;
const LINE_WIDTH_START = 5;
export type SwipeEvent = { x: number; y: number; direction: 'up' | 'down' | 'left' | 'right' }
export default class MouseTrail {
public isActive = true;
public context: CanvasRenderingContext2D;
private dimensions = { width: 0, height: 0 };
/**
* 1 - every frame,
* 2 - every 2nd frame
* and so on, doesn't always work properly
* but 1 doesnt affect performance so we're fine
* */
private drawOnFrame = 1;
private currentFrame = 0;
private lineDuration = LINE_DURATION;
private points: Point[] = [];
private swipePoints: Point[] = []
constructor(private readonly canvas: HTMLCanvasElement, isNativeMobile: boolean = false) {
// @ts-ignore patching window
window.requestAnimFrame =
window.requestAnimationFrame ||
// @ts-ignore
window.webkitRequestAnimationFrame ||
// @ts-ignore
window.mozRequestAnimationFrame ||
// @ts-ignore
window.oRequestAnimationFrame ||
// @ts-ignore
window.msRequestAnimationFrame ||
function (callback: any) {
window.setTimeout(callback, 1000 / 60);
};
if (isNativeMobile) {
this.lineDuration = 5
}
}
resizeCanvas = (w: number, h: number) => {
if (this.context !== undefined) {
this.context.canvas.width = w;
this.context.canvas.height = h;
this.canvas.width = w;
this.canvas.height = h;
this.dimensions.width = w;
this.dimensions.height = h;
}
};
createContext = () => {
if (this.canvas) {
this.context = this.canvas.getContext('2d')!;
this.init();
} else {
console.error('Canvas element not found');
}
};
leaveTrail = (x: number, y: number) => {
if (this.currentFrame === this.drawOnFrame) {
this.addPoint(x + 7, y + 7);
this.currentFrame = 0;
}
this.currentFrame++;
};
init = () => {
if (this.isActive) {
this.animatePoints();
// @ts-ignore patched
window.requestAnimFrame(this.init);
}
};
createSwipeTrail = ({ x, y, direction }: SwipeEvent) => {
const startCoords = this.calculateTrail({ x, y, direction });
this.addPoint(startCoords.x, startCoords.y);
this.addPoint(x, y);
}
calculateTrail = ({ x, y, direction }: SwipeEvent) => {
switch (direction) {
case 'up':
return { x, y: y - 20 };
case 'down':
return { x, y: y + 20 };
case 'left':
return { x: x - 20, y };
case 'right':
return { x: x + 20, y };
default:
return { x, y };
}
}
animatePoints = () => {
this.context.clearRect(0, 0, this.context.canvas.width, this.context.canvas.height);
const duration = (this.lineDuration * 1000) / 60;
let point, lastPoint;
for (let i = 0; i < this.points.length; i++) {
point = this.points[i];
if (this.points[i - 1] !== undefined) {
lastPoint = this.points[i - 1];
} else {
lastPoint = this.points[i];
}
point.lifetime! += 1;
if (point.lifetime! > duration) {
this.points.splice(i, 1);
continue;
}
const inc = point.lifetime! / duration; // 0 to 1 over lineDuration
const dec = 1 - inc;
const spreadRate = LINE_WIDTH_START * (1 - inc);
this.context.lineJoin = 'round';
this.context.lineWidth = spreadRate;
this.context.strokeStyle = `rgba(60, 170, 170, ${dec})`
this.context.beginPath();
this.context.moveTo(lastPoint.x, lastPoint.y);
this.context.lineTo(point.x, point.y);
this.context.stroke();
this.context.closePath();
}
};
addPoint = (x: number, y: number) => {
const point = new Point(x, y, 0);
this.points.push(point);
};
}
type Coords = { x: number; y: number };
class Point {
constructor(public x: number, public y: number, public lifetime?: number) {}
static distance(a: Coords, b: Coords) {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.sqrt(dx * dx + dy * dy);
}
static midPoint(a: Coords, b: Coords) {
const mx = a.x + (b.x - a.x) * 0.5;
const my = a.y + (b.y - a.y) * 0.5;
return new Point(mx, my);
}
static angle(a: Coords, b: Coords) {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.atan2(dy, dx);
}
get pos() {
return this.x + ',' + this.y;
}
}