module Graphics.Blank.Utils where
import Data.ByteString.Base64
import qualified Data.ByteString as B
import Data.Monoid
import Data.Text(Text)
import qualified Data.Text as Text
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
import Graphics.Blank.Canvas
import Graphics.Blank.Generated
import Graphics.Blank.JavaScript
clearCanvas :: Canvas ()
clearCanvas = do
setTransform (1, 0, 0, 1, 0, 0)
me <- myCanvasContext
clearRect (0,0,width me,height me)
saveRestore :: Canvas a -> Canvas a
saveRestore m = do
save ()
r <- m
restore ()
return r
infixr 0 #
(#) :: a -> (a -> Canvas b) -> Canvas b
(#) obj act = act obj
readDataURL :: Text -> FilePath -> IO Text
readDataURL mime_type filePath = do
dat <- B.readFile filePath
return $ "data:" <> mime_type <> ";base64," <> decodeUtf8 (encode dat)
dataURLMimeType :: Text -> Text
dataURLMimeType txt
| dat /= "data" = error "dataURLMimeType: no 'data:'"
| not (Text.null rest0) && not (Text.null rest2) = mime_type
| otherwise = error "dataURLMimeType: bad parse"
where
(dat,rest0) = Text.span (/= ':') txt
Just (_,rest1) = Text.uncons rest0
(mime_type,rest2) = Text.span (/= ';') rest1
writeDataURL :: FilePath -> Text -> IO ()
writeDataURL fileName
= B.writeFile fileName
. decodeLenient
. encodeUtf8
. Text.tail
. Text.dropWhile (/= ',')
drawImageAt :: Image image => (image, Double, Double) -> Canvas ()
drawImageAt (img, dx, dy) = Method $ DrawImage (img, [dx, dy])
drawImageSize :: Image image => (image, Double, Double, Double, Double) -> Canvas ()
drawImageSize (img, dx, dy, dw, dh) = Method $ DrawImage (img, [dx, dy, dw, dh])
drawImageCrop :: Image image => (image, Double, Double, Double, Double, Double, Double, Double, Double) -> Canvas ()
drawImageCrop (img, sx, sy, sw, sh, dx, dy, dw, dh)
= Method $ DrawImage (img, [sx, sy, sw, sh, dx, dy, dw, dh])
putImageDataAt :: (ImageData, Double, Double) -> Canvas ()
putImageDataAt (imgData, dx, dy) = Method $ PutImageData (imgData, [dx, dy])
putImageDataDirty :: (ImageData, Double, Double, Double, Double, Double, Double) -> Canvas ()
putImageDataDirty (imgData, dx, dy, dirtyX, dirtyY, dirtyWidth, dirtyHeight)
= Method $ PutImageData (imgData, [dx, dy, dirtyX, dirtyY, dirtyWidth, dirtyHeight])