basics of player component, global imageLoad bus

This commit is contained in:
Tomáš Mládek 2018-01-18 18:21:22 +01:00
parent f37051633b
commit dfab37b3e0
5 changed files with 82 additions and 9 deletions

View file

@ -4,7 +4,7 @@
<Sidebar ref="sidebar" @loadImage="loadImage" @guideSize="guideSize"/> <Sidebar ref="sidebar" @loadImage="loadImage" @guideSize="guideSize"/>
</div> </div>
<div id="canvas-wrap"> <div id="canvas-wrap">
<Canvas ref="canvas" @imageLoaded="updateImageSize"/> <Canvas ref="canvas"/>
</div> </div>
</div> </div>
</template> </template>
@ -24,9 +24,6 @@
}, },
guideSize: function (size) { guideSize: function (size) {
this.$refs.canvas.slice = size this.$refs.canvas.slice = size
},
updateImageSize: function (size) {
this.$refs.sidebar.imageSize = size
} }
} }
} }

View file

@ -1,6 +1,6 @@
<template> <template>
<div id="canvas-container"> <div id="canvas-container">
<canvas id="canvas"></canvas> <canvas ref="canvas" id="canvas"></canvas>
</div> </div>
</template> </template>
@ -36,7 +36,7 @@
this.image = new Image() this.image = new Image()
this.image.onload = () => { this.image.onload = () => {
this.imageLoaded = true this.imageLoaded = true
this.$emit('imageLoaded', [this.image.width, this.image.height]) this.$bus.$emit('imageLoaded', this.image)
this.refresh() this.refresh()
} }
this.image.src = imageUrl this.image.src = imageUrl
@ -44,7 +44,7 @@
refresh: function () { refresh: function () {
if (!this.imageLoaded) return if (!this.imageLoaded) return
let image = this.image let image = this.image
let canvas = document.getElementById('canvas') let canvas = this.$refs.canvas
let ctx = canvas.getContext('2d') let ctx = canvas.getContext('2d')
ctx.clearRect(0, 0, canvas.width, canvas.height) ctx.clearRect(0, 0, canvas.width, canvas.height)

65
src/components/Player.vue Normal file
View file

@ -0,0 +1,65 @@
<template>
<div id="player">
<div id="player-canvas-container">
<canvas ref="canvas" id="player-canvas" :height="height" :width="width"></canvas>
</div>
<button @click="playPause">{{buttonLabel}}</button>
</div>
</template>
<script>
export default {
name: 'player',
props: {
width: String,
height: String
},
data: function () {
return {
image: null,
buttonLabel: 'PLAY',
playing: false
}
},
mounted: function () {
this.$bus.$on('imageLoaded', (image) => {
this.image = image
})
this.clear('red')
},
watch: {
width: function () {
setTimeout(() => {
this.clear('red')
}, 0)
},
height: function () {
setTimeout(() => {
this.clear('red')
}, 0)
}
},
methods: {
playPause: function () {
this.playing = !this.playing
if (this.playing) {
this.buttonLabel = 'STOP'
} else {
this.buttonLabel = 'PLAY'
}
},
clear: function (style) {
let ctx = this.$refs.canvas.getContext('2d')
ctx.fillStyle = style
ctx.fillRect(0, 0, parseInt(this.width), parseInt(this.height))
},
$render: function () {
}
}
}
</script>
<style scoped>
</style>

View file

@ -41,18 +41,24 @@
<label>Est. length: {{length}}</label> <label>Est. length: {{length}}</label>
</div> </div>
<hr> <hr>
<player :width="guideSizeX" :height="guideSizeY"/>
</div> </div>
</template> </template>
<!--suppress JSSuspiciousNameCombination --> <!--suppress JSSuspiciousNameCombination -->
<script> <script>
import Player from '@/components/Player'
export default { export default {
name: 'Sidebar', name: 'Sidebar',
components: {
player: Player
},
data: function () { data: function () {
return { return {
imageSize: [-1, -1], imageSize: [-1, -1],
guideSizeX: 64, guideSizeX: '64',
guideSizeY: 64, guideSizeY: '64',
lastGuideSizeX: 64, lastGuideSizeX: 64,
lastGuideSizeY: 64, lastGuideSizeY: 64,
lockSize: true, lockSize: true,
@ -151,6 +157,9 @@
} }
}, },
mounted: function () { mounted: function () {
this.$bus.$on('imageLoaded', (image) => {
this.imageSize = [image.width, image.height]
})
this.$emit('guideSize', [this.guideSizeX, this.guideSizeY]) this.$emit('guideSize', [this.guideSizeX, this.guideSizeY])
} }
} }

View file

@ -5,6 +5,8 @@ import App from './App'
Vue.config.productionTip = false Vue.config.productionTip = false
Vue.prototype.$bus = new Vue({})
/* eslint-disable no-new */ /* eslint-disable no-new */
new Vue({ new Vue({
el: '#app', el: '#app',