[Haskell] ANN: shine and shine-varying: Lightweight declarative 2D graphics à la gloss using GHCJS (and a FRP interface)

Francesco Gazzetta francygazz at gmail.com
Fri Apr 22 15:08:51 UTC 2016


Shine wraps JavaScript’s drawing functions in a declarative API, hiding the
boilerplate canvas code.

The API mimics the one provided by gloss (Picture, animate, play…), but
some types are different to better adapt to js code (ex. images, fonts,
inputs).

Example:

main = runWebGUI $ \ webView -> do
    ctx <- fixedSizeCanvas webView 800 600
    let concentricCircles = foldMap Circle [1,10..100]
    draw ctx concentricCircles -- one-shot drawing

The only direct dependency is ghcjs-dom, so the resulting jsexe should be
relatively lightweight.

I also wrote shine-varying, a FRP interface to shine in terms of Vars plus
some utility Vars.

Example (translation of the resize-yogi Elm example):

resizeImage img (x',y') = Translate (x/2) (y/2) -- Pictures are
centered on (0,0), so we need to move it
                        $ Image (Stretched x y) img -- Scale the
picture to the given position
  where
    x = fromIntegral x' -- mousePosition is Integral
    y = fromIntegral y'

main = runWebGUI $ \ webView -> do
    ctx <- fixedSizeCanvas webView 1024 768
    Just doc <- webViewGetDomDocument webView
    narwhal <- makeImage
"https://wiki.haskell.org/wikiupload/8/85/NarleyYeeaaahh.jpg"
    let resizedNarwhal = resizeImage narwhal <$> mousePosition
    playVarying ctx doc 30 resizedNarwhal

I plan to write/port a few simple games with it soon.

Francesco
​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell/attachments/20160422/bbeec16b/attachment.html>


More information about the Haskell mailing list