github.com/rajveermalviya/gamen@v0.1.2-0.20220930195403-9be15877c1aa/internal/web/util.go (about) 1 //go:build js 2 3 package web 4 5 import ( 6 "strconv" 7 "syscall/js" 8 9 "github.com/rajveermalviya/gamen/dpi" 10 ) 11 12 func scaleFactor() float64 { 13 return js.Global().Get("devicePixelRatio").Float() 14 } 15 16 func setCanvasSize(canvas js.Value, size dpi.Size[float64]) { 17 scaleFactor := scaleFactor() 18 19 physicalSize := size.ToPhysical(scaleFactor) 20 logicalSize := size.ToLogical(scaleFactor) 21 22 canvas.Set("width", physicalSize.Width) 23 canvas.Set("height", physicalSize.Height) 24 25 canvas.Get("style").Call( 26 "setProperty", 27 "width", 28 strconv.FormatFloat(logicalSize.Width, 'f', 6, 64)+"px", 29 ) 30 canvas.Get("style").Call( 31 "setProperty", 32 "height", 33 strconv.FormatFloat(logicalSize.Height, 'f', 6, 64)+"px", 34 ) 35 } 36 37 func isASCII(s string) bool { 38 for i := 0; i < len(s); i++ { 39 if s[i] > 127 { 40 return false 41 } 42 } 43 return true 44 }