pass canvas size to overlay.

This commit is contained in:
Miroslav Šedivý 2021-02-12 17:11:28 +01:00
parent 873e9250cc
commit a1d755d6eb
2 changed files with 41 additions and 14 deletions

View File

@ -5,8 +5,8 @@
<neko-overlay <neko-overlay
:webrtc="webrtc" :webrtc="webrtc"
:control="state.control" :control="state.control"
:screenWidth="state.screen.size.width" :screenSize="state.screen.size"
:screenHeight="state.screen.size.height" :canvasSize="canvasSize"
:isControling="controlling && watching" :isControling="controlling && watching"
:implicitControl="state.control.implicit_hosting && state.members[state.member_id].profile.can_host" :implicitControl="state.control.implicit_hosting && state.members[state.member_id].profile.can_host"
@implicit-control-request="websocket.send('control/request')" @implicit-control-request="websocket.send('control/request')"
@ -74,6 +74,10 @@
websocket = new NekoWebSocket() websocket = new NekoWebSocket()
webrtc = new NekoWebRTC() webrtc = new NekoWebRTC()
observer = new ResizeObserver(this.onResize.bind(this)) observer = new ResizeObserver(this.onResize.bind(this))
canvasSize: { width: number; height: number } = {
width: 0,
height: 0,
}
@Prop({ type: Boolean }) @Prop({ type: Boolean })
private readonly autoplay!: boolean private readonly autoplay!: boolean
@ -505,6 +509,11 @@
this._container.style.height = `${vertical}px` this._container.style.height = `${vertical}px`
this._container.style.marginTop = `${(offsetHeight - vertical) / 2}px` this._container.style.marginTop = `${(offsetHeight - vertical) / 2}px`
this._container.style.marginLeft = `0px` this._container.style.marginLeft = `0px`
Vue.set(this, 'canvasSize', {
width: offsetWidth,
height: vertical,
})
} }
// horizontal centering // horizontal centering
else if (screen_ratio < canvas_ratio) { else if (screen_ratio < canvas_ratio) {
@ -513,6 +522,11 @@
this._container.style.height = `${offsetHeight}px` this._container.style.height = `${offsetHeight}px`
this._container.style.marginTop = `0px` this._container.style.marginTop = `0px`
this._container.style.marginLeft = `${(offsetWidth - horizontal) / 2}px` this._container.style.marginLeft = `${(offsetWidth - horizontal) / 2}px`
Vue.set(this, 'canvasSize', {
width: horizontal,
height: offsetHeight,
})
} }
// no centering // no centering
else { else {
@ -520,6 +534,11 @@
this._container.style.height = `${offsetHeight}px` this._container.style.height = `${offsetHeight}px`
this._container.style.marginTop = `0px` this._container.style.marginTop = `0px`
this._container.style.marginLeft = `0px` this._container.style.marginLeft = `0px`
Vue.set(this, 'canvasSize', {
width: offsetWidth,
height: offsetHeight,
})
} }
} }

View File

@ -74,10 +74,16 @@
private readonly control!: Control private readonly control!: Control
@Prop() @Prop()
private readonly screenWidth!: number private readonly screenSize: { width: number; height: number } = {
width: 0,
height: 0,
}
@Prop() @Prop()
private readonly screenHeight!: number private readonly canvasSize: { width: number; height: number } = {
width: 0,
height: 0,
}
@Prop() @Prop()
private readonly isControling!: boolean private readonly isControling!: boolean
@ -141,8 +147,8 @@
const rect = this._overlay.getBoundingClientRect() const rect = this._overlay.getBoundingClientRect()
return { return {
x: Math.round((this.screenWidth / rect.width) * (clientX - rect.left)), x: Math.round((this.screenSize.width / rect.width) * (clientX - rect.left)),
y: Math.round((this.screenHeight / rect.height) * (clientY - rect.top)), y: Math.round((this.screenSize.height / rect.height) * (clientY - rect.top)),
} }
} }
@ -255,6 +261,12 @@
} }
} }
@Watch('canvasSize')
onCanvasSizeChange({ width, height }: { width: number; height: number }) {
this._overlay.width = width
this._overlay.height = height
}
private cursorElem: HTMLImageElement = new Image() private cursorElem: HTMLImageElement = new Image()
@Watch('control.cursor.image') @Watch('control.cursor.image')
onCursorImageChange({ uri }: { uri: string }) { onCursorImageChange({ uri }: { uri: string }) {
@ -265,19 +277,15 @@
onCursorPositionChange({ x, y }: { x: number; y: number }) { onCursorPositionChange({ x, y }: { x: number; y: number }) {
if (this.isControling || this.control.cursor.image == null) return if (this.isControling || this.control.cursor.image == null) return
// synchronize intrinsic with extrinsic dimensions // get intrinsic dimensions
const { width, height } = this._overlay.getBoundingClientRect() const { width, height } = this._overlay
if (this._overlay.width != width || this._overlay.height != height) {
this._overlay.width = width
this._overlay.height = height
}
// redraw cursor // redraw cursor
this._ctx.clearRect(0, 0, width, height) this._ctx.clearRect(0, 0, width, height)
this._ctx.drawImage( this._ctx.drawImage(
this.cursorElem, this.cursorElem,
(x / this.screenWidth) * width - this.control.cursor.image.x, (x / this.screenSize.width) * width - this.control.cursor.image.x,
(y / this.screenHeight) * height - this.control.cursor.image.y, (y / this.screenSize.height) * height - this.control.cursor.image.y,
this.control.cursor.image.width, this.control.cursor.image.width,
this.control.cursor.image.height, this.control.cursor.image.height,
) )