-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Web graphic applications with Processing. -- -- Processing is a visual design programming language. -- Processing.js is the sister project of Processing designed for -- the web. The Haskell processing package is a web animation -- library with Processing.js as backend. -- -- What is this for? -- -- With this library you are able to write scripts that, once executed in -- a browser, will execute interactive visual programs. -- -- Where can I see a running example? -- -- An example output can be reached at -- http://liibe.com/experimental/rocket.html. Also, take a look at -- http://liibe.com/experimental/mill.html. The code of the latter -- is included in the source distribution (/examples/mill.hs). -- -- How do I learn to use it? -- -- The API reference of the library includes guidance and is complemented -- with code examples. Look also to the examples directory -- included in the source distribution. It contains some fully working -- examples. -- -- The library provides different APIs (interfaces). Each one with a -- different philosophy. -- --
-- v <- newVar 10 -- ten <- readVar v -- writeVar v 20 -- point (10,ten) ---- -- will draw a point at (10,20). readVar :: (Monad (m c), ProcMonad m, ProcType a) => Var a -> m c a -- | Write a value to a variable. writeVar :: (ProcMonad m, ProcType a) => Var a -> a -> m c () -- | Variables, commands and functions. The interface to the -- processing.js API (http://processingjs.org/reference), with -- some additions, deletions and modifications. module Graphics.Web.Processing.Core.Interface -- | Width of the canvas. screenWidth :: Proc_Int -- | Height of the canvas. screenHeight :: Proc_Int -- | Noise random function. noise :: (ProcMonad m, Monad (m c)) => Proc_Point -> m c Proc_Float -- | Class of contexts where the user can draw pictures in the screen. -- Instances: -- --
-- execProcM = snd . runProcM --execProcM :: ProcM c a -> ProcCode c -- | Processing scripting, mid interface. Unlike the basic -- interface (see Graphics.Web.Processing.Basic) the script is -- more guided by the types. However, the output is less predictable, -- since it does some tricks in order to obtain semantics that are more -- coherent with Haskell. The difference is small, but let's say that -- this module has more freedom writing the output code. It also applies -- code optimizations, so the output code may look different (see -- execScriptM and Graphics.Web.Processing.Optimize). -- -- How to work with it? -- -- Everything is done within the ScriptM monad, a state monad that -- controls the entire script, including the preamble, draw loop, setup, -- etc. The interaction with the different parts of the script is done -- via events (see EventM). For example, the Draw -- event controls the draw loop. -- --
-- mouse :: ScriptM Preamble () -- mouse = do -- on Setup $ do -- size screenWidth screenHeight -- fill $ Color 255 255 255 255 -- on Draw $ do -- background $ Color 0 0 0 255 -- p <- getMousePoint -- circle p 10 ---- -- Note that to make it work, the context of the script must be -- Preamble. -- -- Interaction with variables is done via the ProcVarMonad class. -- This class defines methods to interact with variables in both the -- ScriptM monad and the EventM monad. To store custom -- types in variables, see the -- Graphics.Web.Processing.Mid.CustomVar module. -- -- Once your script is complete, use execScriptM to get the result -- code. module Graphics.Web.Processing.Mid -- | Context of an event. The context determines which functions can be -- used. Preamble is not an instance of Context to avoid -- using Preamble as an event (see on). class Context c -- | Monad of events. Use on to insert an event in a script -- (ScriptM). To write the event code, use the functions in -- Graphics.Web.Processing.Core.Interface, since EventM is -- an instance of ProcMonad. data EventM c a -- | Scripter monad. This monad is where Processing code is written. -- Because of some implementation details, ScriptM has a context -- c. However, this context is always Preamble. data ScriptM c a -- | Set an event. Different events are specified by the instances of the -- Context class. -- -- For example, the following code sets the fill pattern in the -- setup event (the event that is called once at the beginning of the -- execution). -- --
-- on Setup $ fill $ Color 0 0 0 255 --on :: Context c => c -> EventM c () -> ScriptM Preamble () -- | Execute the scripter monad to get the full Processing script. Use -- renderScript or renderFile to render it. -- -- After generating the script, the output code is optimized using -- optimizeBySubstitution. execScriptM :: ScriptM Preamble () -> ProcScript -- | Type of variables. data Var a -- | Get the name of a variable. varName :: Var a -> Text -- | Class of monads where variables can be set in a natural way (similar -- to IORef). Instances of this class behave in a proper way, -- without the weird behavior of the original readVar. class ProcMonad m => ProcVarMonad m newVar :: (ProcVarMonad m, ProcType a) => a -> m Preamble (Var a) readVar :: (ProcVarMonad m, ProcType a) => Var a -> m c a writeVar :: (ProcVarMonad m, ProcType a) => Var a -> a -> m c () instance ProcVarMonad EventM instance ProcVarMonad ScriptM instance Context KeyPressed instance Context MouseReleased instance Context MouseClicked instance Context Draw instance Context Setup instance ProcMonad ScriptM instance Monad (ScriptM c) instance Applicative (ScriptM c) instance Functor (ScriptM c) instance ProcMonad EventM instance Monad (EventM c) instance Applicative (EventM c) instance Functor (EventM c) -- | This module re-exports the mid interface to processing. module Graphics.Web.Processing -- | This module implements variables which may contain values from types -- different from the native types (Proc_* types). -- -- To make a type available to custom variables, it needs to be -- instantiated in the CustomValue class, which is subclass of the -- VarLength class. These instances are derivables using the -- DeriveGeneric extension. Things you need are: enable the -- DeriveGeneric language extension, import GHC.Generics, -- derive a Generic instance of your type and then write the -- following instances (where Foo is any type of interest): -- --
-- instance VarLength Foo -- instance CustomValue Foo ---- -- Note that Foo must be made from other types that are -- instances of CustomValue. Also, note that instances of -- VarLength or CustomValue can not be recursive or -- sum types. An example: -- --
-- {-# LANGUAGE DeriveGeneric #-}
--
-- import Graphics.Web.Processing.Mid
-- import Graphics.Web.Processing.Mid.CustomVar
-- import GHC.Generics
--
-- data Point = Point Proc_Float Proc_Float
-- deriving Generic
--
-- instance VarLength Point
-- instance CustomValue Point
--
--
-- Types instance of the CustomValue class can be contained by a
-- special type of variables, called CustomVar (Custom Variable).
-- Functions for custom variables are equal to the function for regular
-- variables, except that they all end in C. For example,
-- newVar is called newVarC for custom variables.
--
-- The dependency of this module in several language extensions was the
-- reason to make it separate from the rest of the mid interface
-- where it belongs to. Somehow, it forces the user to use
-- DeriveGeneric and import GHC.Generics to do something
-- useful with it (more than use custom variables for tuples).
module Graphics.Web.Processing.Mid.CustomVar
-- | Variable with custom values.
data CustomVar a
-- | Typeclass of values that can be stored in several native variables
-- (Var).
class VarLength a where varLength _ = 1
varLength :: VarLength a => a -> Int
-- | Typeclass of custom values, which can be stored in custom variables
-- (CustomVar).
class VarLength a => CustomValue a where newVarC x = liftM castCVar $ gnewVarC (from x) readVarC v = liftM to $ greadVarC (castCVar v) writeVarC v x = gwriteVarC (castCVar v) (from x)
newVarC :: (CustomValue a, Monad (m Preamble), ProcVarMonad m) => a -> m Preamble (CustomVar a)
readVarC :: (CustomValue a, Monad (m c), ProcVarMonad m) => CustomVar a -> m c a
writeVarC :: (CustomValue a, Monad (m c), ProcVarMonad m) => CustomVar a -> a -> m c ()
instance Generic (CustomVar a)
instance Datatype D1CustomVar
instance Constructor C1_0CustomVar
instance (CustomValue a, CustomValue b, CustomValue c) => CustomValue (a, b, c)
instance (VarLength a, VarLength b, VarLength c) => VarLength (a, b, c)
instance (CustomValue a, CustomValue b) => CustomValue (a, b)
instance (VarLength a, VarLength b) => VarLength (a, b)
instance CustomValue a => GCustomValue (K1 i a)
instance GCustomValue a => GCustomValue (M1 i c a)
instance GCustomValue (a :+: b)
instance (GVarLength a, GCustomValue a, GCustomValue b) => GCustomValue (a :*: b)
instance VarLength a => GVarLength (K1 i a)
instance GVarLength a => GVarLength (M1 i c a)
instance GVarLength (a :+: b)
instance (GVarLength a, GVarLength b) => GVarLength (a :*: b)
instance GVarLength U1
instance VarLength Proc_Char
instance VarLength Proc_Image
instance VarLength Proc_Text
instance VarLength Proc_Float
instance VarLength Proc_Int
instance VarLength Proc_Bool
instance CustomValue Proc_Char
instance CustomValue Proc_Image
instance CustomValue Proc_Text
instance CustomValue Proc_Float
instance CustomValue Proc_Int
instance CustomValue Proc_Bool
-- | A Monoid models figures in the plane. Then, figures are
-- displayed or animated using a Processing script.
--
-- For example, this expression represents a circle of radius 10 centered
-- at the origin:
--
-- -- Circle (0,0) 10 ---- -- The origin will be represented at the center of the screen. As opposed -- to the other modules, y-coordinates increase to the top, while -- x-coordinates still increase to the right. -- -- This is a red rectangle with top-left corner at the origin, 10 points -- height and 10 points width: -- --
-- FillColor (Color 255 0 0 255) $ Rectangle (0,0) 10 10 ---- -- To display several figures together, use the Monoid instance: -- --
-- Circle (0,0) 10 <> Circle (0,20) 10 ---- -- If you just want to display this figure in the target canvas, use -- displayFigure. If you want to animate it, use -- animateFigure. Animations depend on the number of frames since -- the beginning of the execution, instead of in the time spent. -- -- Once you have created a processing script (a value of type -- ProcScript), use renderFile to write it to a file. See -- also the Graphics.Web.Processing.Html module. -- -- The default filling color and line color are white and black -- respectively. Use FillColor and LineColor to change -- these colors. Colors are in RGBA format, meaning that they may -- be transparent (with an alpha value of 0), opaque (with an alpha value -- of 255) or something in between. Use a fully transparent color to -- indicate that a Figure should not be filled. -- -- You can apply transformations like translation, rotation and scaling. -- If p is a point and f a figure, Translate p -- f will draw f with p as the origin of -- coordinates. Rotations and scalings are always done in respect to the -- origin, but note that you can modify where the origin is using -- Translate. module Graphics.Web.Processing.Simple -- | RGBA colors. Values must be between 0 and 255, including in the alpha -- channel. data Color Color :: Proc_Int -> Proc_Int -> Proc_Int -> Proc_Int -> Color -- | Red channel. redc :: Color -> Proc_Int -- | Blue channel. bluec :: Color -> Proc_Int -- | Green channel. greenc :: Color -> Proc_Int -- | Alpha channel (opacity). 0 means transparent, and 255 opaque. alphac :: Color -> Proc_Int -- | A point as a pair of floating point numbers. type Proc_Point = (Proc_Float, Proc_Float) -- | A path is just a list of points. type Path = [Proc_Point] -- | The monoid of plane figures. data Figure -- | Line joining a list of points. Line :: Path -> Figure -- | Polygon given a list of vertex. Polygon :: Path -> Figure -- | Ellipse centered at the given point, with width and height also -- specified. Ellipse :: Proc_Point -> Proc_Float -> Proc_Float -> Figure -- | Circle centered at the given point and with the specified radius. Circle :: Proc_Point -> Proc_Float -> Figure -- | Arc. The arc is drawn following the line of an ellipse between two -- angles. The first argument is the center of the ellipse. The next two -- arguments are the width and height of the ellipse. The last two -- arguments are the initial and end angles of the arc. Arc :: Proc_Point -> Proc_Float -> Proc_Float -> Proc_Float -> Proc_Float -> Figure -- | Rectangle such that the top-left corner is at the specified point, and -- its width and height are specified by the other two arguments. Rectangle :: Proc_Point -> Proc_Float -> Proc_Float -> Figure -- | Bezier curve. First and last arguments are the initial and end points -- of the curve. The other points are control points. Bezier :: Proc_Point -> Proc_Point -> Proc_Point -> Proc_Point -> Figure -- | Text. Text :: Proc_Point -> Proc_Text -> Figure -- | Set the line color of a figure. LineColor :: Color -> Figure -> Figure -- | Set the filling color of a figure. FillColor :: Color -> Figure -> Figure -- | Translate a figure in the direction of a vector. Translate :: Proc_Point -> Figure -> Figure -- | Rotate a figure by the given angle in radians. Rotate :: Proc_Float -> Figure -> Figure -- | Scale a figure by the given x and y factors. Scale :: Proc_Float -> Proc_Float -> Figure -> Figure -- | List of figures. Figures :: [Figure] -> Figure -- | Display a figure using a Processing script. displayFigure :: Maybe Int -> Maybe Int -> Color -> Figure -> ProcScript -- | Create a Processing animation from a Figure-valued function. animateFigure :: Maybe Int -> Maybe Int -> Int -> Color -> (Proc_Int -> Figure) -> ProcScript -- | Framework to create interactive scripts. -- -- Note that is required for the state to be an instance of -- CustomValue. More info on how to instantiate a type in the -- CustomValue class in the -- Graphics.Web.Processing.Mid.CustomVar module. interactiveFigure :: CustomValue w => Maybe Int -> Maybe Int -> Int -> w -> (w -> Figure) -> (w -> Color) -> (Proc_Int -> w -> w) -> (Proc_Point -> w -> w) -> [(Key, w -> w)] -> ProcScript -- | Keyboard keys recognized by Processing. data Key CharKey :: Char -> Key SpecialKey :: SpecialKey -> Key ArrowKey :: ArrowKey -> Key ModKey :: KeyModifier -> Key -> Key -- | Arrow keys. data ArrowKey UP :: ArrowKey DOWN :: ArrowKey LEFT :: ArrowKey RIGHT :: ArrowKey -- | Key modifiers. data KeyModifier ALT :: KeyModifier CONTROL :: KeyModifier SHIFT :: KeyModifier -- | Special keys. data SpecialKey BACKSPACE :: SpecialKey TAB :: SpecialKey ENTER :: SpecialKey RETURN :: SpecialKey ESC :: SpecialKey instance Monoid Figure -- | Once created, processing scripts can be included in HTML canvas. To be -- able to reproduce the animation, you must import the -- processing.js library, downloadable from -- http://processingjs.org/download (do not import it from the -- original link, download it and use your own copy). To import -- processing.js, use a script tag. -- --
-- <script src="processing.js"></script> ---- -- See importScript. -- -- Note from the author: I didn't manage to run a processing animation -- locally, so you may have the same issue. Once I uploaded them -- to my server, they worked just fine. module Graphics.Web.Processing.Html -- | Create a canvas element which contain a Processing animation. The -- output is of the following form: -- --
-- <canvas data-processing-sources="specified path"></canvas> --procCanvas :: FilePath -> Html -- | Create the following HTML element: -- --
-- <script src="specified path"></script> ---- -- Use it to import the processing.js script, inside the -- head tag. importScript :: FilePath -> Html -- | Default template for visualizing Processing scripts in HTML. defaultHtml :: FilePath -> FilePath -> Text -> Html -- | Write a Processing script and the HTML default template for it to -- files, using renderFile and defaultHtml. writeHtml :: FilePath -> FilePath -> Text -> FilePath -> ProcScript -> IO ()