@@ -6,81 +6,77 @@ const spriteImage = new ex.ImageSource('sprite.png');
66const loader = new ex . Loader ( [ spriteImage ] ) ;
77
88export class Scene extends ex . Scene {
9- onInitialize ( ) {
10- this . sprite = spriteImage . toSprite ( ) ;
11- }
12-
9+ particles = [ ] ;
1310 onActivate ( ctx ) {
14- this . engine = ctx . data . engine ;
11+ this . benchmarkEngine = ctx . data . engine ;
1512
1613 this . clear ( ) ;
1714
1815 // Particle creation
19- const particles = Array . from ( { length : this . engine . count } ) ;
16+ this . particles = new Array ( this . benchmarkEngine . count ) ;
2017 const rnd = [ 1 , - 1 ] ;
18+ const spriteImage = new ex . ImageSource ( 'sprite.png' ) ;
19+ spriteImage . load ( ) ;
20+ const sprite = ex . Sprite . from ( spriteImage ) ;
2121
22- const graphicsGroup = new ex . GraphicsGroup ( { members : [ ] } ) ;
22+ const random = new ex . Random ( ) ;
23+ let randomCircles = [ ] ;
2324
24- for ( let i = 0 ; i < this . engine . count ; i ++ ) {
25+ for ( let i = 0 ; i < 80 ; i ++ ) {
26+ const size = 10 + Math . random ( ) * 80 ;
27+ const x = Math . random ( ) * this . benchmarkEngine . width ;
28+ const y = Math . random ( ) * ( this . benchmarkEngine . height - size ) ;
29+
30+ const circle = new ex . Circle ( {
31+ x : x ,
32+ y : y ,
33+ radius : size ,
34+ color :
35+ this . benchmarkEngine . type === 'stroke'
36+ ? ex . Color . Transparent
37+ : ex . Color . fromRGB ( 255 , 255 , 255 ) ,
38+ strokeColor :
39+ this . benchmarkEngine . type === 'stroke'
40+ ? ex . Color . fromRGB ( 255 , 255 , 255 )
41+ : ex . Color . fromRGB ( 0 , 0 , 0 ) ,
42+ strokeWidth : this . benchmarkEngine . type === 'stroke' ? 1 : undefined ,
43+ } ) ;
44+ randomCircles . push ( circle ) ;
45+ }
46+ for ( let i = 0 ; i < this . benchmarkEngine . count ; i ++ ) {
2547 const size = 10 + Math . random ( ) * 80 ;
26- const x = Math . random ( ) * this . engine . width ;
27- const y = Math . random ( ) * ( this . engine . height - size ) ;
48+ const x = Math . random ( ) * this . benchmarkEngine . width ;
49+ const y = Math . random ( ) * ( this . benchmarkEngine . height - size ) ;
2850 const [ dx , dy ] = [
2951 3 * Math . random ( ) * rnd [ Math . floor ( Math . random ( ) * 2 ) ] ,
3052 3 * Math . random ( ) * rnd [ Math . floor ( Math . random ( ) * 2 ) ] ,
3153 ] ;
3254
33- if ( this . engine . type === 'sprite' ) {
34- graphicsGroup . members . push ( {
35- graphic : this . sprite ,
36- offset : ex . vec ( x , y ) ,
37- } ) ;
55+ this . particles [ i ] = { x, y, size : size , dx, dy, graphics : null } ;
56+ if ( this . benchmarkEngine . type === 'sprite' ) {
57+ this . particles [ i ] . graphics = sprite ;
3858 } else {
39- const circle = new ex . Circle ( {
40- x : x ,
41- y : y ,
42- radius : size ,
43- color :
44- this . engine . type === 'stroke'
45- ? ex . Color . Transparent
46- : ex . Color . fromRGB ( 255 , 255 , 255 ) ,
47- strokeColor :
48- this . engine . type === 'stroke'
49- ? ex . Color . fromRGB ( 255 , 255 , 255 )
50- : ex . Color . fromRGB ( 0 , 0 , 0 ) ,
51- strokeWidth : this . engine . type === 'stroke' ? 1 : undefined ,
52- } ) ;
53- graphicsGroup . members . push ( {
54- graphic : circle ,
55- offset : ex . vec ( x , y ) ,
56- } ) ;
59+ this . particles [ i ] . graphics = random . pickOne ( randomCircles ) ;
5760 }
58-
59- particles [ i ] = { x, y, size : size , dx, dy, el : graphicsGroup . members [ i ] } ;
6061 }
61-
62- const screenElement = new ex . ScreenElement ( ) ;
63-
64- screenElement . graphics . use ( graphicsGroup ) ;
65-
66- this . add ( screenElement ) ;
67-
68- this . particles = particles ;
6962 }
70-
71- onPostUpdate ( engine ) {
72- for ( let i = 0 ; i < this . engine . count ; i ++ ) {
63+ onPostUpdate ( ) {
64+ for ( let i = 0 ; i < this . benchmarkEngine . count ; i ++ ) {
7365 const r = this . particles [ i ] ;
7466 r . x -= r . dx ;
7567 r . y -= r . dy ;
7668 if ( r . x + r . size < 0 ) r . dx *= - 1 ;
7769 else if ( r . y + r . size < 0 ) r . dy *= - 1 ;
78- if ( r . x > engine . screen . drawWidth ) r . dx *= - 1 ;
79- else if ( r . y > engine . screen . drawHeight ) r . dy *= - 1 ;
80- r . el . offset . setTo ( r . x , r . y ) ;
70+ if ( r . x > this . engine . screen . drawWidth ) r . dx *= - 1 ;
71+ else if ( r . y > this . engine . screen . drawHeight ) r . dy *= - 1 ;
72+ }
73+ this . benchmarkEngine . fpsmeter . tick ( ) ;
74+ }
75+ onPostDraw ( ctx ) {
76+ for ( let i = 0 ; i < this . benchmarkEngine . count ; i ++ ) {
77+ const particle = this . particles [ i ] ;
78+ particle . graphics . draw ( ctx , particle . x , particle . y ) ;
8179 }
82-
83- this . engine . fpsmeter . tick ( ) ;
8480 }
8581}
8682
@@ -103,9 +99,9 @@ class ExcaliburEngine extends Engine {
10399 width : this . width ,
104100 height : this . height ,
105101 canvasElement : this . canvas ,
102+ physics : false , // this benchmark is only doing drawing
106103 backgroundColor : ex . Color . fromRGB ( 26 , 26 , 26 ) ,
107104 suppressPlayButton : true ,
108- physics : { enabled : false } ,
109105 scenes : { scene : Scene } ,
110106 } ) ;
111107 this . game = game ;
0 commit comments