Skip to content

Commit 69937e5

Browse files
author
Chris Hurlburt
committed
Merge branch 'jasonlfunk-master'
2 parents ae40e76 + 6b3cbac commit 69937e5

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ props: {
8484
type: Boolean,
8585
default: () => false
8686
},
87+
square: { // should the video be centered vertically in a square container
88+
type: Boolean,
89+
default: () => false
90+
},
8791
controls: { // show video playback controls
8892
type: Boolean,
8993
default: () => false

src/CanvasVideo.js

+22-7
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default {
7777
return {
7878
playing: false,
7979
scrubbing: false,
80-
aspectRatioPercentage: '',
80+
aspectRatio: 1,
8181
duration: 0,
8282
elapsed: 0,
8383
lastTime: 0,
@@ -115,7 +115,7 @@ export default {
115115
const { video } = this.$refs
116116
this.width = video.videoWidth
117117
this.height = video.videoHeight
118-
this.aspectRatioPercentage = `${(this.height / this.width) * 100}%`
118+
this.aspectRatio = this.height / this.width
119119
},
120120
play (e) {
121121
if (e) e.stopPropagation()
@@ -191,9 +191,16 @@ export default {
191191
},
192192
computed: {
193193
computedWrapStyles () {
194-
return (this.cover)
195-
? Object.assign({}, videoWrapInnerStyles, mediaCoveringStyles)
196-
: { paddingBottom: this.aspectRatioPercentage, ...videoWrapInnerStyles }
194+
const base = { paddingBottom: `${this.aspectRatio * 100}%`, ...videoWrapInnerStyles }
195+
const centerSquare = { marginTop: `${(1 - this.aspectRatio) / 2 * 100}%` }
196+
197+
if (this.cover && !this.square) {
198+
return Object.assign({}, videoWrapInnerStyles, mediaCoveringStyles)
199+
} else if (this.square && !this.cover) {
200+
return Object.assign({}, base, centerSquare)
201+
}
202+
203+
return base
197204
},
198205
computedVideoStyles () {
199206
const cover = Object.assign({}, videoStyles, mediaCoveringStyles)
@@ -211,8 +218,12 @@ export default {
211218
}
212219
},
213220
mounted () {
214-
this.init()
215-
this.bind()
221+
if (this.cover && this.square) {
222+
console.error('[vue-canvasvideo]: The cover and square props cannot be used together.')
223+
} else {
224+
this.init()
225+
this.bind()
226+
}
216227
},
217228
props: {
218229
src: {
@@ -231,6 +242,10 @@ export default {
231242
type: Boolean,
232243
default: () => false
233244
},
245+
square: {
246+
type: Boolean,
247+
default: () => false
248+
},
234249
loop: {
235250
type: Boolean,
236251
default: () => false

0 commit comments

Comments
 (0)