Giter Site home page Giter Site logo

reflex-dom-canvas's Introduction

CSIRO's Data61 Logo

Reflex HTML5 Canvas

Helper functions for creating and managing a Canvas element and a RenderingContext. Supports both 2d and WebGL.

The main functions for creating the canvas and retrieving the Dynamic containing the requested context are in Reflex.Dom.CanvasDyn. Use either dContext2d or dContextWebgl to build your canvas. You will need to provide a configuration record:

data CanvasConfig (c :: ContextType) t = CanvasConfig
  { -- This is the <canvas> element that you created with Reflex.Dom
    _canvasConfig_El   :: RD.El t
    -- This is a list of stringly arguments that are passed along to the JavaScript 'getContext' function.
  , _canvasConfig_Args :: [Text]
  }

The (c :: ContextType) type variable will be set according to the type of context you are requesting. You should not need to provide it explicitly and is there to prevent you from passing WebGL instructions to a 2D canvas, and vice versa:

exampleFoo = do
  let
    canvasAttrs = Map.fromList
      [ ("height", "480")
      , ("width" , "640")
      , ("id"    , "fruity")
      ]

  -- Create the canvas element, using the backticked el function so Reflex.Dom provides us
  -- with the `El t`, which is the representation of the <canvas> element.
  ( canvasEl, _ ) <- RD.elAttr' "canvas" canvasAttrs RD.blank

  -- Retrieve our CanvasInfo, restricted to 'context2d' because of the types! Yay! :D
  d2D <- dContext2d ( Canvas.CanvasConfig canvasEl [] )
  ...
  ...

This CanvasInfo will be in a Dynamic with the following fields:

data CanvasInfo (c :: ContextType) t = CanvasInfo
  { -- Canvas HTML element
    _canvasInfo_El       :: El t
    -- RenderingContext JS object for the context (2d/webgl) that you can requested.
  , _canvasInfo_context  :: RenderContext c
    -- Function, takes a Key, returns an Event for when that Key is pressed on the Canvas element
  , _canvasInfo_keyEvent :: Key -> Event t ()
  }

The functions:

drawWithCx
  :: ( MonadWidget t m
     , HasRenderFn c ( RenderContext c )
     )
  => Dynamic t ( RenderContext c )
  -> Dynamic t ( RenderContext c -> Double -> JSM a )
  -> Event t ()
  -> m ( Event t a )

and

drawCanvasFree
  :: ( MonadWidget t m
     , HasRenderFn c ( RenderContext c )
     )
  => Dynamic t ( RenderFree c a )
  -> Dynamic t ( RenderContext c )
  -> Event t ()
  -> m (Event t a)

are for applying instructions to the canvas using Dynamic t ( RenderContext c ).

There are two modules Reflex.Dom.Canvas.WebGL and Reflex.Dom.Canvas.Context2D that contain a Free monad implementation of some canvas functions for their respective contexts. These are not complete implementations, but may be useful to someone wanting to experiment with a more FP friendly way of interacting with the Canvas API.

reflex-dom-canvas's People

Contributors

mankykitty avatar enemeth79 avatar

Watchers

James Cloos avatar

Forkers

sh-ft

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.