Skip to content

Instantly share code, notes, and snippets.

@sharkdp
Created March 22, 2016 18:47
Show Gist options
  • Save sharkdp/bdb4548abb6103be3e2a to your computer and use it in GitHub Desktop.
Save sharkdp/bdb4548abb6103be3e2a to your computer and use it in GitHub Desktop.
Rendering with additional effects
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="controls"></div>
<canvas id="canvas" width="600" height="600"></canvas>
</body>
</html>
module Main where
import Prelude
import Data.Int
import Data.Array
import Data.Maybe
import Data.Foldable
import Control.Monad.Eff
import Control.Monad.Eff.Random
import Graphics.Canvas
import Graphics.Drawing
import Flare
width :: Number
width = 600.0
height :: Number
height = 600.0
randomPoint :: forall eff. Eff (random :: RANDOM | eff) Point
randomPoint = do
x <- randomRange 0.0 width
y <- randomRange 0.0 height
return { x: x, y: y}
-- Generate a drawing consisting of n random points
randomPoints :: forall eff. Int -> Eff (random :: RANDOM | eff) Drawing
randomPoints n = (foldMap makeCircle) <$> points
where
makeCircle p = filled (fillColor black) $ circle p.x p.y 10.0
points = replicateM n randomPoint
-- A custom rendering function
drawRandomPoints :: forall eff. Context2D -> Int -> Eff (random :: RANDOM, canvas :: Canvas | eff) Unit
drawRandomPoints ctx n = do
clearRect ctx { x: 0.0, y: 0.0, w: width, h: height }
drawing <- randomPoints n
render ctx drawing
main = do
Just canvas <- getCanvasElementById "canvas"
ctx <- getContext2D canvas
let ui = intRange "numPoints" 1 100 10
runFlareWith "controls" (drawRandomPoints ctx) ui
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment