1
+ // This file is licenced under the GNU Affero General Public License and the Resonate Supplemental Terms. (See file LICENSE and LICENSE-SUPPLEMENT or <https://github.com/98ahni/Resonate>)
2
+ // <Copyright (C) 2024 98ahni> Original file author
3
+
4
+ const default_console_log = console . log ;
5
+ const default_console_warn = console . warn ;
6
+ const default_console_error = console . error ;
7
+ ( ( ) => {
8
+ self . onerror = function ( event , source , lineno , colno , error ) {
9
+ console . error ( "onerror: " + event . name + ": " + event . message +
10
+ "\n\t/bin/public/" + source . split ( '/' ) . slice ( - 1 ) + ":" + lineno + ":" + colno ) ;
11
+ console . error ( error . stack ) ;
12
+ //if(window.matchMedia('(any-pointer: coarse)').matches) // Enable if alert should be for touch only.
13
+ alert ( "OnError: \n" + event . name + ": " + event . message ) ;
14
+ } ;
15
+ fetch ( "/console" , {
16
+ method : "POST" ,
17
+ headers : { 'Content-Type' : 'application/json' } ,
18
+ body : JSON . stringify ( { type : 'log' , data : [ 'Log server open' ] } )
19
+ } ) . then (
20
+ /*resolve*/ ( response ) => {
21
+ if ( response . status == 405 ) {
22
+ return ;
23
+ }
24
+ console . log = ( ...data ) => {
25
+ fetch ( "/console" , {
26
+ method : "POST" ,
27
+ headers : { 'Content-Type' : 'application/json' } ,
28
+ body : JSON . stringify ( { type : 'log' , data} )
29
+ } ) . then (
30
+ /*resolve*/ ( ) => { } ,
31
+ /*reject*/ ( ) => {
32
+ // No server, restore functions
33
+ console . log = default_console_log ;
34
+ console . warn = default_console_warn ;
35
+ console . error = default_console_error ;
36
+ } ) ;
37
+ default_console_log . apply ( console , data ) ;
38
+ } ;
39
+ console . warn = ( ...data ) => {
40
+ fetch ( "/console" , {
41
+ method : "POST" ,
42
+ headers : { 'Content-Type' : 'application/json' } ,
43
+ body : JSON . stringify ( { type : 'warn' , data} )
44
+ } ) . then (
45
+ /*resolve*/ ( ) => { } ,
46
+ /*reject*/ ( ) => {
47
+ // No server, restore functions
48
+ console . log = default_console_log ;
49
+ console . warn = default_console_warn ;
50
+ console . error = default_console_error ;
51
+ } ) ;
52
+ default_console_warn . apply ( console , data ) ;
53
+ } ;
54
+ console . error = ( ...data ) => {
55
+ fetch ( "/console" , {
56
+ method : "POST" ,
57
+ headers : { 'Content-Type' : 'application/json' } ,
58
+ body : JSON . stringify ( { type : 'error' , data} )
59
+ } ) . then (
60
+ /*resolve*/ ( ) => { } ,
61
+ /*reject*/ ( ) => {
62
+ // No server, restore functions
63
+ console . log = default_console_log ;
64
+ console . warn = default_console_warn ;
65
+ console . error = default_console_error ;
66
+ } ) ;
67
+ default_console_error . apply ( console , data ) ;
68
+ } ;
69
+ } ) ;
70
+ } ) ( ) ;
71
+ function remap ( value , l1 , h1 , l2 , h2 ) {
72
+ return l2 + ( h2 - l2 ) * ( value - l1 ) / ( h1 - l1 ) ;
73
+ }
74
+
75
+ const animMaxDuration = 250 ;
76
+
77
+ var canvas = null ;
78
+ var context ;
79
+ var bgColor = 0 ;
80
+ var decorColor = 0 ;
81
+ var isShowing = false ;
82
+ var animIn = false ;
83
+ var animOut = false ;
84
+ var animStartTime = 0 ;
85
+ function render ( time ) {
86
+ if ( ! isShowing ) {
87
+ requestAnimationFrame ( render ) ;
88
+ return ;
89
+ }
90
+ let width = canvas . width ;
91
+ let height = canvas . height ;
92
+ let size = ( width < height ? width : height ) * .4 ;
93
+ let sizeMult = ( ( Math . sin ( time * .001 ) + 1 ) * .25 ) + .5 ;
94
+ let alphaMult = ( 1 - sizeMult ) + .5 ;
95
+
96
+ context . clearRect ( 0 , 0 , width , height ) ;
97
+ if ( animIn ) {
98
+ if ( animStartTime == 0 ) { animStartTime = time ; }
99
+ let duration = time - animStartTime ;
100
+ if ( duration >= animMaxDuration ) {
101
+ duration = animMaxDuration ;
102
+ animIn = false ;
103
+ animStartTime = 0 ;
104
+ }
105
+ sizeMult = remap ( Math . sqrt ( Math . sqrt ( duration ) ) , 0 , Math . sqrt ( Math . sqrt ( animMaxDuration ) ) , 5 , sizeMult ) ;
106
+ alphaMult = remap ( duration , 0 , animMaxDuration , 0 , alphaMult ) ;
107
+ context . globalAlpha = remap ( duration , 0 , animMaxDuration , 0 , 1 ) ;
108
+ }
109
+ if ( animOut ) {
110
+ if ( animStartTime == 0 ) { animStartTime = time ; console . log ( "animOut start" ) ; }
111
+ let duration = time - animStartTime ;
112
+ if ( duration >= animMaxDuration ) {
113
+ duration = animMaxDuration ;
114
+ animOut = false ;
115
+ isShowing = false ;
116
+ animStartTime = 0 ;
117
+ console . log ( "animOut end" ) ;
118
+ this . postMessage ( { cmd : 'resetZ' } ) ;
119
+ }
120
+ sizeMult = remap ( duration * duration * duration , 0 , animMaxDuration * animMaxDuration * animMaxDuration , sizeMult , 5 ) ;
121
+ alphaMult = remap ( duration , 0 , animMaxDuration , alphaMult , 0 ) ;
122
+ context . globalAlpha = remap ( duration , 0 , animMaxDuration , 1 , 0 ) ;
123
+ }
124
+
125
+ context . fillStyle = `rgba(
126
+ ${ bgColor & 0xff } ,
127
+ ${ ( bgColor >>> 8 ) & 0xff } ,
128
+ ${ ( bgColor >>> 16 ) & 0xff } ,
129
+ ${ ( bgColor >>> 24 ) / 0xff }
130
+ )` ;
131
+ context . fillRect ( 0 , 0 , width , height ) ;
132
+
133
+ context . fillStyle = "white" ;
134
+ context . fillStyle = `rgba(
135
+ ${ decorColor & 0xff } ,
136
+ ${ ( decorColor >>> 8 ) & 0xff } ,
137
+ ${ ( decorColor >>> 16 ) & 0xff } ,
138
+ 1
139
+ )` ;
140
+ context . globalAlpha = .1 * alphaMult ;
141
+ context . beginPath ( ) ;
142
+ context . arc ( width * .5 , height * .5 , size * sizeMult , 0 , Math . PI * 2 , true ) ;
143
+ context . fill ( ) ;
144
+ context . globalAlpha = .3 * alphaMult ;
145
+ context . beginPath ( ) ;
146
+ context . arc ( width * .5 , height * .5 , size * .8 * sizeMult , 0 , Math . PI * 2 , true ) ;
147
+ context . fill ( ) ;
148
+ context . globalAlpha = .5 * alphaMult ;
149
+ context . beginPath ( ) ;
150
+ context . arc ( width * .5 , height * .5 , size * .6 * sizeMult , 0 , Math . PI * 2 , true ) ;
151
+ context . fill ( ) ;
152
+ context . globalAlpha = .7 * alphaMult ;
153
+ context . beginPath ( ) ;
154
+ context . arc ( width * .5 , height * .5 , size * .4 * sizeMult , 0 , Math . PI * 2 , true ) ;
155
+ context . fill ( ) ;
156
+ context . globalAlpha = .9 * alphaMult ;
157
+ context . beginPath ( ) ;
158
+ context . arc ( width * .5 , height * .5 , size * .2 * sizeMult , 0 , Math . PI * 2 , true ) ;
159
+ context . fill ( ) ;
160
+ context . globalAlpha = 1 ;
161
+ requestAnimationFrame ( render ) ;
162
+ }
163
+
164
+ onmessage = async function ( msg )
165
+ {
166
+ if ( msg . data . cmd == 'init' ) {
167
+ canvas = msg . data . canvas ;
168
+ context = canvas . getContext ( '2d' ) ;
169
+ requestAnimationFrame ( render ) ;
170
+ }
171
+ else if ( msg . data . cmd == 'show' ) {
172
+ bgColor = msg . data . bgColor ;
173
+ decorColor = msg . data . decorColor ;
174
+ canvas . width = msg . data . width ;
175
+ canvas . height = msg . data . height ;
176
+ animIn = msg . data . animIn ;
177
+ isShowing = true ;
178
+ }
179
+ else if ( msg . data . cmd == 'hide' ) {
180
+ canvas . width = msg . data . width ;
181
+ canvas . height = msg . data . height ;
182
+ animOut = true ;
183
+ }
184
+ }
0 commit comments