77<script >
88export default {
99 name: ' sprite' ,
10- props: {
10+ props: {
1111 spriteSrc: {
1212 type: String ,
1313 default: ' '
@@ -16,55 +16,54 @@ export default {
1616 type: String ,
1717 default: ' sprite'
1818 },
19- spriteW: {
20- type: Number ,
21- default: 1
19+ spriteW: {
20+ type: Number ,
21+ default: 1
2222 },
23- spriteH: {
24- type: Number ,
25- default: 1
23+ spriteH: {
24+ type: Number ,
25+ default: 1
2626 },
27- spriteSpeed: {
28- type: Number ,
29- default: 1
27+ spriteSpeed: {
28+ type: Number ,
29+ default: 1
3030 }
3131 },
32- data () {
33- return {
34- visible: true ,
35- frameIndex : 0 ,
36- tickCount: 0 ,
37- frameLength: 0 ,
38- ticksPerFrame: 0 ,
32+ data () {
33+ return {
34+ visible: true ,
35+ frameIndex: 0 ,
36+ tickCount: 0 ,
37+ frameLength: 0 ,
38+ ticksPerFrame: 0 ,
3939 numberOfFrames: 0 ,
4040 frameRate: 20 ,
41- ctx: ' ' ,
42- canvas : ' ' ,
43- mySprite: ' ' ,
44- animationFrameId: - 1
41+ ctx: ' ' ,
42+ canvas: ' ' ,
43+ mySprite: ' ' ,
44+ animationFrameId: - 1
4545 }
4646 },
47- mounted () {
47+ mounted () {
4848 let vm = this
4949 this .$nextTick (() => {
5050 vm .mySprite = new Image ()
51- vm .mySprite .onload = ( e ) => {
51+ vm .mySprite .onload = e => {
5252 vm .spriteInit (e .target )
5353 }
5454 vm .mySprite .src = vm .spriteSrc
5555 })
5656 },
57- methods: {
58- spriteInit (img ){
57+ methods: {
58+ spriteInit (img ) {
5959 this .canvas = this .$el .querySelector (` #${ this .spriteId } ` )
6060 this .ctx = this .canvas .getContext (' 2d' )
6161 this .ticksPerFrame = this .spriteSpeed
6262 this .frameLength = img .width
6363 this .numberOfFrames = img .width / this .spriteW
6464 this .spriteLoop ()
65-
6665 },
67- spriteUpdate () {
66+ spriteUpdate () {
6867 this .tickCount ++
6968 if (this .tickCount > this .ticksPerFrame ) {
7069 this .tickCount = 0
@@ -77,30 +76,25 @@ export default {
7776 }
7877 }
7978 },
80- spriteRender (){
81- this .ctx .clearRect (0 , 0 , this .spriteW , this .spriteH );
82- // Draw the animation
83- var toDraw = this .frameIndex * this .spriteW
84- this .ctx .drawImage (
85- this .mySprite ,
86- toDraw ,
87- 0 ,
88- this .spriteW ,
89- this .spriteH ,
90- 0 ,
91- 0 ,
92- this .spriteW ,
93- this .spriteH )
79+ spriteRender () {
80+ this .ctx .clearRect (0 , 0 , this .spriteW , this .spriteH )
81+ // Draw the animation
82+ const toDraw = this .frameIndex * this .spriteW
83+ this .ctx .drawImage (
84+ this .mySprite ,
85+ toDraw, 0 , this .spriteW , this .spriteH ,
86+ 0 , 0 , this .spriteW , this .spriteH
87+ )
9488 },
95- spriteLoop () {
89+ spriteLoop () {
9690 this .animationFrameId = window .requestAnimationFrame (this .spriteLoop )
9791 this .spriteUpdate ()
9892 this .spriteRender ()
9993 },
100- stop () {
94+ stop () {
10195 window .cancelAnimationFrame (this .animationFrameId )
10296 },
103- play () {
97+ play () {
10498 this .spriteLoop ()
10599 }
106100 }
0 commit comments