Skip to content

Commit

Permalink
[previews] Clean annotation finding
Browse files Browse the repository at this point in the history
The current time used can be problematic because it doesn't exactly
matched a given frame due to float precision.

This commit makes current time computation closer to their related frame
and make annotation loading loosier about the time of the annotation to
grab.
  • Loading branch information
frankrousseau committed Dec 12, 2023
1 parent 9f498d2 commit b199cb8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
15 changes: 9 additions & 6 deletions src/components/mixins/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export const annotationMixin = {
},

methods: {
findAnnotation(list, time) {
return list.find(a => a.time < time + 0.0001 && a.time > time - 0.0001)
},

// Objects

/*
Expand Down Expand Up @@ -259,7 +263,7 @@ export const annotationMixin = {
addToAdditions(obj) {
this.markLastAnnotationTime()
const currentTime = this.getCurrentTime()
const additions = this.additions.find(a => a.time === currentTime)
const additions = this.findAnnotation(this.additions, currentTime)
if (additions) {
additions.drawing.objects.push(obj.serialize())
} else {
Expand All @@ -283,7 +287,7 @@ export const annotationMixin = {
*/
removeFromAdditions(obj) {
const currentTime = this.getCurrentTime()
const additions = this.additions.find(a => a.time === currentTime)
const additions = this.findAnnotation(this.additions, currentTime)
if (additions) {
additions.drawing.objects = additions.drawing.objects.filter(
o => o.id !== obj.id
Expand All @@ -297,7 +301,7 @@ export const annotationMixin = {
addToDeletions(obj) {
this.markLastAnnotationTime()
const currentTime = this.getCurrentTime()
const deletion = this.deletions.find(d => d.time === currentTime)
const deletion = this.findAnnotation(this.deletions, currentTime)
if (deletion) {
deletion.objects.push(obj.id)
} else {
Expand All @@ -309,7 +313,6 @@ export const annotationMixin = {
if (!obj.serialize) {
this.addSerialization(obj)
}

this.postAnnotationDeletion(currentTime, obj.serialize())
},

Expand All @@ -325,7 +328,7 @@ export const annotationMixin = {
*/
removeFromDeletions(obj) {
const currentTime = this.getCurrentTime()
const deletions = this.deletions.find(a => a.time === currentTime)
const deletions = this.findAnnotation(this.deletions, currentTime)
if (deletions) {
deletions.objects = deletions.objects.filter(oId => oId !== obj.id)
}
Expand All @@ -345,7 +348,7 @@ export const annotationMixin = {
addToUpdatesSerializedObject(obj) {
this.markLastAnnotationTime()
const currentTime = this.getCurrentTime()
const updates = this.updates.find(a => a.time === currentTime)
const updates = this.findAnnotation(this.updates, currentTime)
if (updates) {
updates.drawing.objects = updates.drawing.objects.filter(
o => o.id !== obj.id
Expand Down
3 changes: 2 additions & 1 deletion src/components/mixins/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,8 @@ export const playerMixin = {
},

getCurrentTime() {
return roundToFrame(this.currentTimeRaw, this.fps) || 0
const time = roundToFrame(this.currentTimeRaw, this.fps) || 0
return Number(time.toPrecision(4))
},

setCurrentTimeRaw(time) {
Expand Down
10 changes: 8 additions & 2 deletions src/components/previews/PreviewPlayer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,7 @@ export default {
},
setVideoFrameContext(frame) {
frame = Math.min(frame, this.nbFrames - 1)
if (this.currentFrame !== frame) {
const time = frame * this.frameDuration
this.currentFrame = frame
Expand Down Expand Up @@ -1066,7 +1067,8 @@ export default {
getCurrentTime() {
if (!this.isMovie) return 0
return this.currentTimeRaw
const time = roundToFrame(this.currentTimeRaw, this.fps)
return Number(time.toPrecision(4))
},
play() {
Expand Down Expand Up @@ -1457,7 +1459,10 @@ export default {
if (this.isMovie) {
time = roundToFrame(time, this.fps)
return this.annotations.find(annotation => {
return roundToFrame(annotation.time, this.fps) === time
return (
roundToFrame(annotation.time, this.fps) < time + 0.0001 &&
roundToFrame(annotation.time, this.fps) > time - 0.0001
)
})
} else if (this.isPicture) {
return this.annotations.find(annotation => annotation.time === 0)
Expand All @@ -1477,6 +1482,7 @@ export default {
if (this.isMovie) {
currentTime = this.currentFrame * this.frameDuration
currentTime = roundToFrame(currentTime, this.fps)
currentTime = Number(currentTime.toPrecision(4))
}
const annotation = this.getAnnotation(currentTime)
const annotations = this.getNewAnnotations(currentTime, annotation)
Expand Down
5 changes: 4 additions & 1 deletion src/components/previews/VideoViewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,10 @@ export default {
} else {
currentTimeRaw = 0
}
return Math.ceil(currentTimeRaw / this.frameDuration) + 1
let frame = Math.ceil(currentTimeRaw / this.frameDuration) + 1
frame = Number(frame.toPrecision(4))
frame = Math.min(frame, this.nbFrames)
return frame
},
play() {
Expand Down

0 comments on commit b199cb8

Please sign in to comment.