1- import { init , GameLoop } from 'kontra' ;
1+ import { init , GameLoop , Sprite } from 'kontra' ;
22import Engine from './engine.js' ;
33
44class KontraEngine extends Engine {
@@ -22,6 +22,8 @@ class KontraEngine extends Engine {
2222 this . ctx . strokeStyle = 'black' ;
2323 this . ctx . fillStyle = 'white' ;
2424 }
25+ const image = new Image ( ) ;
26+ image . src = 'sprite.png' ;
2527
2628 // Particle creation
2729 const particles = new Array ( this . count ) ;
@@ -34,7 +36,16 @@ class KontraEngine extends Engine {
3436 3 * Math . random ( ) * rnd [ Math . floor ( Math . random ( ) * 2 ) ] ,
3537 3 * Math . random ( ) * rnd [ Math . floor ( Math . random ( ) * 2 ) ] ,
3638 ] ;
37- particles [ i ] = { x, y, size : size , dx, dy } ;
39+ let sprite ;
40+ if ( this . type === 'sprite' ) {
41+ sprite = Sprite ( {
42+ x : x ,
43+ y : y ,
44+ anchor : { x : 0.5 , y : 0.5 } ,
45+ image : image ,
46+ } ) ;
47+ }
48+ particles [ i ] = { x, y, size : size , dx, dy, el : sprite } ;
3849 }
3950 this . particles = particles ;
4051 }
@@ -51,16 +62,24 @@ class KontraEngine extends Engine {
5162 else if ( r . y + r . size < 0 ) r . dy *= - 1 ;
5263 if ( r . x > this . width ) r . dx *= - 1 ;
5364 else if ( r . y > this . height ) r . dy *= - 1 ;
65+ if ( r . el ) {
66+ r . el . x = r . x ;
67+ r . el . y = r . y ;
68+ }
5469 }
5570 this . fpsmeter . tick ( ) ;
5671 } ,
5772 render : ( ) => {
5873 for ( let i = 0 ; i < this . count ; i ++ ) {
5974 const r = this . particles [ i ] ;
60- this . ctx . beginPath ( ) ;
61- this . ctx . arc ( r . x , r . y , r . size , 0 , 2 * Math . PI ) ;
62- if ( this . type === 'fill' ) this . ctx . fill ( ) ;
63- this . ctx . stroke ( ) ;
75+ if ( this . type === 'sprite' ) {
76+ r . el . render ( ) ;
77+ } else {
78+ this . ctx . beginPath ( ) ;
79+ this . ctx . arc ( r . x , r . y , r . size , 0 , 2 * Math . PI ) ;
80+ if ( this . type === 'fill' ) this . ctx . fill ( ) ;
81+ this . ctx . stroke ( ) ;
82+ }
6483 }
6584 } ,
6685 } ) ;
0 commit comments