- Joined
- Jan 2, 2020
- Messages
- 1,414
I seem to have a problem with GetChar().
It only appears sporadically in HTML5 when using AUTO_SUSPEND_ENABLED = False
You can see it by compiling to HTML5 and then active and inactive the browser windows, coming back to it should sometimes work,
and sometimes it would fail to see keyinput until you refresh the webpage.
So far I've tried Linux Firefox and Chrome. It seem to affect both browsers the same but when one works the other can stop working again, until I refresh, but there's no distinct pattern that I've seen so far. So first conclusion was it was Linux keyboard input but it never happens in the browsers otherwise. Neither when AUTOSPEND is not used. Nor in Desktop mode.
What can it be?
EDIT I could replicate the problem in macOS Safari and Chrome.
Here's a simple demo
It only appears sporadically in HTML5 when using AUTO_SUSPEND_ENABLED = False
You can see it by compiling to HTML5 and then active and inactive the browser windows, coming back to it should sometimes work,
and sometimes it would fail to see keyinput until you refresh the webpage.
So far I've tried Linux Firefox and Chrome. It seem to affect both browsers the same but when one works the other can stop working again, until I refresh, but there's no distinct pattern that I've seen so far. So first conclusion was it was Linux keyboard input but it never happens in the browsers otherwise. Neither when AUTOSPEND is not used. Nor in Desktop mode.
What can it be?
EDIT I could replicate the problem in macOS Safari and Chrome.
Here's a simple demo
Code:
#MOJO_AUTO_SUSPEND_ENABLED = False
' On HTML5 (at least both Firefox & Chrome on Linux), GetChar() sometimes stops
' after revisiting webpage when using suspend_enabled (and only then)
Import mojo2
Function Main()
New Game
Return 0
End
Class Game Extends App
Field canvas:Canvas
Field str:String = "", screen:String[100]
Field x:Int = 0, y:Int = 1*12
Field mode:Int = 1 ' 2 = Overwritemode
Field letters:=New List<letter>
Field changeline:Int = 0
Method OnCreate()
canvas = New Canvas()
screen[0] = "Ready."
Return 0
End
Method OnUpdate()
' Update jumping letters
Local g:Float = 9.1*4
changeline = 0 ; If MouseHit(0) Then x = Int(MouseX()/8)*8 ; y = Int(MouseY()/12)*12 ; changeline = 1
Local out:=New List<letter>
For Local letter:=Eachin letters
letter.a = Max(letter.a-.01,0.0)
If letter.a <= 0 Then Continue
letter.yv += 0.1 ; letter.x += letter.xv ; letter.y += letter.yv
If letter.y > 280-12 Then letter.yv = -letter.yv * 0.6 ; letter.y = 280-12
out.AddLast letter
Next
letters=out
Local chr:Int = GetChar()
If chr > 31 And chr < 128 ' WRITING
Local saved:String = Mid(str,x/8+mode,80)
While Len(str) < (x/8)
str = str + " "
Wend
str = Left(str,x/8) ; x = x + 8
str = str + Chr(chr) + saved
Endif
If chr = 8 And x > 0 ' BACKSPACE
Local saved:String = Mid(str,x/8+1,80)
While Len(str) < (x/8)
str = str + " "
Wend
str = Left(str,x/8) ; x = x - 8
Local temp:String = Left(str,Len(str)-0)
chr = Val(Right(temp,1))
str = Left(str,Len(str)-1) + saved
letters.AddLast New letter(x,y,chr)
Endif
Local oldy:Int = y
If chr = 65573 Then x = x - 8 ; x = Max(x,0) ' Left-key
If chr = 65575 Then x = x + 8 ; x = Min(x,640-8) ' Right-key
If chr = 65574 Then y = y - 12 ; y = Max(y,0) ' Up-key
If chr = 65576 Then y = y + 12 ; y = Min(y,480-12) ' Down-key
If chr = 13 Then y = y + 12 ; y = Min(y,480-12) ' Enter-key
If changeline = 1 Then str = screen[Floor(y/12)]
If oldy <> y Then str = screen[Floor(y/12)]
screen[Floor(y/12)] = str
End
Method OnRender()
canvas.Clear
canvas.SetBlendMode BlendMode.Alpha
' Draw text
canvas.SetColor 0.4,0.4,0.4,1
For Local i:Int = 0 To 99
canvas.DrawText screen[i],0,i * 12
Next
' Draw cursor
canvas.SetColor 1,1,1,(Millisecs()/1000.0)
canvas.DrawLine x,y,x,y+12
' canvas.DrawText str,0,y
canvas.SetBlendMode BlendMode.Additive
For Local letter:=Eachin letters
If letter.char <> 0
canvas.SetAlpha 1.0
canvas.DrawText Chr(letter.char),letter.x,letter.y
Endif
Next
canvas.Flush
End
End
Function Len:Int(s:String)
Return s.Length
End
Function Mid:String(s:String,p:Int,n:Int)
p=p-1 ; Return s[(p)..(p+n)]
End
Function Left:String(s:String,n:Int)
Return s[0..n]
End
Function Right:String(s:String,n:Int)
Return s[(s.Length()-n)..(s.Length())]
End
Function Chr:String(n:Int)
Local result:String ; result = result.FromChar(n) ; Return result
End
Function Val:Int(s:String)
Local chrs:Int[] = s.ToChars() ; Return chrs[0]
End
Function Alphabet:Int(s:String,n:Int=0)
Local chrs:Int[] = s.ToChars() ; Return chrs[n] & 31
End
Class rectangle
Field x:Int
Field y:Int
Field w:Int
Field h:Int
End
Class letter
Field x#,y#,xv#,yv#,a#,char:Int
Method New(x#,y#,char:Int)
Local an# = Rnd(360),v# = 0.8
Self.x = x
Self.y = y
Self.xv = Cos(an) * v
Self.yv = Sin(an) * v
Self.a = 1
Self.char = char
End
End
Last edited: