@@ -25,13 +25,43 @@ export function sprite(options) {
2525 this . texture
2626 this . size = options . size
2727
28- this . imReady = function ( ) {
29- sprite . ready = true
30- sprite . render ( )
31- console . log ( options . texture + " ready, rendering children" )
32- for ( const child of sprite . children ) {
33- child . render ( )
28+ this . render = function ( ) {
29+ if ( ! sprite . parent ) {
30+ console . log ( sprite . texture + " tried rendering but couldnt" )
31+ return
32+ } // Stop if theres no parent
33+ if ( ! sprite . ready || ! sprite . parent . ready ) { return } // Stop if texture or parent's texture isnt ready
34+ // Sets the center of the element to where it's top left corner was
35+ // Then sets its center to the center of its parent
36+ this . body . style . position = "absolute"
37+ this . body . style . left = - this . size . x / 2 + nopx ( this . parent . body . style . width ) / 2 + "px"
38+ this . body . style . top = - this . size . y / 2 + nopx ( this . parent . body . style . height ) / 2 + "px"
39+
40+ this . body . style . transform = ""
41+ if ( this . rot . p ) {
42+ this . body . style . transformStyle = "preserve-3d"
43+ this . body . style . transform += "perspective(" + this . rot . p + "px) "
44+ }
45+ this . body . style . transform += "translate(" + this . pos . x + "px, " + this . pos . y + "px) " // X Y
46+ this . body . style . transform += "rotateX(" + this . rot . x + "deg) " // Rotation X
47+ this . body . style . transform += "rotateY(" + this . rot . y + "deg) " // Rotation Y
48+ this . body . style . transform += "rotateZ(" + this . rot . z + "deg) " // Rotation Z (2D)
49+ this . body . style . transformStyle = ""
50+ this . body . style . transform += "scale(" + this . scale + ") " // Scale
51+ //this.body.style.transformOrigin = "bottom"
52+
53+ this . body . style . width = this . size . x + "px" // Width
54+ this . body . style . height = this . size . y + "px" // Height
55+ if ( this . texture != null ) {
56+ this . body . style . background = "url(" + this . texture . src + ")"
3457 }
58+
59+ this . body . style . transform += "translate(" + this . offset . x + "px, " + this . offset . y + "px) " // Offset
60+ //this.body.style.transform += "translateZ(0) "
61+ this . body . style . transformOrigin = ( - this . offset . x + this . body . style . width / 2 ) + "px " + ( - this . offset . y + this . body . style . height / 2 ) + "px"
62+ this . body . style . imageRendering = "pixelated"
63+
64+ this . body . style . outline = this . debug ? "1px solid yellow" : "none"
3565 }
3666
3767 this . setTexture = function ( url ) {
@@ -51,21 +81,32 @@ export function sprite(options) {
5181 } else {
5282 sprite . texture = null
5383 if ( sprite . size == null ) { sprite . size = { x :0 , y :0 } }
84+ sprite . imReady ( )
5485 }
5586 }
5687
5788 this . append = function ( child ) {
5889 sprite . children . push ( child )
5990 child . setParent ( sprite )
91+ child . render ( )
6092 }
6193 this . setParent = function ( parent ) {
6294 if ( sprite . parent ) {
6395 sprite . parent . children = sprite . parent . children . filter ( element => element != sprite )
6496 }
6597 sprite . parent = parent
6698 parent . body . appendChild ( this . body )
99+ sprite . render ( )
100+ }
101+ this . imReady = function ( ) {
102+ sprite . ready = true
103+ sprite . render ( )
104+ console . log ( options . texture + " ready, rendering children:" )
105+ for ( const child of sprite . children ) {
106+ child . render ( )
107+ console . log ( "- " + child . texture )
108+ }
67109 }
68-
69110
70111 if ( options . texture != "canvas" ) { // Normal behaviour
71112 this . setTexture ( options . texture )
@@ -74,7 +115,7 @@ export function sprite(options) {
74115 this . body = document . createElement ( "canvas" )
75116 this . body . width = this . size . x
76117 this . body . height = this . size . y
77- this . ready = true
118+ sprite . imReady ( )
78119 }
79120
80121 this . body . addEventListener ( "click" , ( event ) => {
@@ -112,43 +153,5 @@ export function sprite(options) {
112153 this . render ( )
113154 }
114155
115- this . render = function ( ) {
116- if ( ! sprite . ready || ! sprite . parent . ready ) { return }
117- // Sets the center of the element to where it's top left corner was
118- // Then sets its center to the center of its parent
119- this . body . style . position = "absolute"
120- this . body . style . left = - this . size . x / 2 + nopx ( this . parent . body . style . width ) / 2 + "px"
121- this . body . style . top = - this . size . y / 2 + nopx ( this . parent . body . style . height ) / 2 + "px"
122-
123- this . body . style . transform = ""
124- if ( this . rot . p ) {
125- this . body . style . transformStyle = "preserve-3d"
126- this . body . style . transform += "perspective(" + this . rot . p + "px) "
127- }
128- this . body . style . transform += "translate(" + this . pos . x + "px, " + this . pos . y + "px) " // X Y
129- this . body . style . transform += "rotateX(" + this . rot . x + "deg) " // Rotation X
130- this . body . style . transform += "rotateY(" + this . rot . y + "deg) " // Rotation Y
131- this . body . style . transform += "rotateZ(" + this . rot . z + "deg) " // Rotation Z (2D)
132- this . body . style . transformStyle = ""
133- this . body . style . transform += "scale(" + this . scale + ") " // Scale
134- //this.body.style.transformOrigin = "bottom"
135-
136- this . body . style . width = this . size . x + "px" // Width
137- this . body . style . height = this . size . y + "px" // Height
138- if ( this . texture != null ) {
139- this . body . style . background = "url(" + this . texture . src + ")"
140- }
141-
142- this . body . style . transform += "translate(" + this . offset . x + "px, " + this . offset . y + "px) " // Offset
143- //this.body.style.transform += "translateZ(0) "
144- this . body . style . transformOrigin = ( - this . offset . x + this . body . style . width / 2 ) + "px " + ( - this . offset . y + this . body . style . height / 2 ) + "px"
145- this . body . style . imageRendering = "pixelated"
146-
147- this . body . style . filter = "drop-shadow(5px 5px 5px #222)"
148- this . body . style . filter = "none"
149- //console.log(this.body.style)
150-
151- this . body . style . border = this . debug ? "1px solid yellow" : "none"
152- }
153156 //this.render()
154157}
0 commit comments