first semi-working example

This commit is contained in:
Tomáš Mládek 2018-01-23 23:02:16 +01:00
parent dfab37b3e0
commit 27ad99bf26
3 changed files with 38 additions and 4 deletions

View file

@ -32,7 +32,6 @@
}, },
methods: { methods: {
loadImage: function (imageUrl) { loadImage: function (imageUrl) {
console.log(imageUrl + '!!!')
this.image = new Image() this.image = new Image()
this.image.onload = () => { this.image.onload = () => {
this.imageLoaded = true this.imageLoaded = true

View file

@ -3,6 +3,10 @@
<div id="player-canvas-container"> <div id="player-canvas-container">
<canvas ref="canvas" id="player-canvas" :height="height" :width="width"></canvas> <canvas ref="canvas" id="player-canvas" :height="height" :width="width"></canvas>
</div> </div>
<div>
<label for="posSlider">POS</label>
<input type="range" min="0" :max="frames - 1" value="0" class="slider" id="posSlider" v-model="position">
</div>
<button @click="playPause">{{buttonLabel}}</button> <button @click="playPause">{{buttonLabel}}</button>
</div> </div>
</template> </template>
@ -18,7 +22,17 @@
return { return {
image: null, image: null,
buttonLabel: 'PLAY', buttonLabel: 'PLAY',
playing: false playing: false,
position: 0,
tmp_ctx: null,
animation_id: null
}
},
computed: {
frames: function () {
if (this.image === null) return 0
return Math.ceil(this.image.width / this.width) *
Math.ceil(this.image.height / this.height)
} }
}, },
mounted: function () { mounted: function () {
@ -37,14 +51,22 @@
setTimeout(() => { setTimeout(() => {
this.clear('red') this.clear('red')
}, 0) }, 0)
},
position: function () {
if (this.image === null) return
this.tmp_ctx = this.$refs.canvas.getContext('2d')
this.$render()
} }
}, },
methods: { methods: {
playPause: function () { playPause: function () {
this.playing = !this.playing this.playing = !this.playing
if (this.playing) { if (this.playing) {
this.tmp_ctx = this.$refs.canvas.getContext('2d')
this.animation_id = requestAnimationFrame(this.$render_advance)
this.buttonLabel = 'STOP' this.buttonLabel = 'STOP'
} else { } else {
cancelAnimationFrame(this.animation_id)
this.buttonLabel = 'PLAY' this.buttonLabel = 'PLAY'
} }
}, },
@ -54,7 +76,20 @@
ctx.fillRect(0, 0, parseInt(this.width), parseInt(this.height)) ctx.fillRect(0, 0, parseInt(this.width), parseInt(this.height))
}, },
$render: function () { $render: function () {
let wb = Math.ceil(this.image.width / this.width)
let x = (this.position % wb) * this.width
let w = x + this.width < this.image.width ? this.width : this.image.width - x
let y = Math.floor(this.position / wb) * this.height
let h = y + this.height < this.image.height ? this.height : this.image.height - y
if (w !== this.width || h !== this.height) this.clear('black')
this.tmp_ctx.drawImage(this.image, x, y, w, h, 0, 0, w, h)
},
$render_advance: function () {
this.$render()
if (this.position < this.frames) {
this.position += 1
requestAnimationFrame(this.$render_advance)
}
} }
} }
} }

View file

@ -35,7 +35,7 @@
</div> </div>
<div> <div>
<label for="fps">FPS: </label> <label for="fps">FPS: </label>
<input class="spinBox" id="fps" v-model="fps" type="number" min="1" max="60" step="1" value="60"> <input class="spinBox" id="fps" v-model="fps" type="number" min="1" max="60" step="1" value="60" disabled>
</div> </div>
<div> <div>
<label>Est. length: {{length}}</label> <label>Est. length: {{length}}</label>