Skip to content

Commit 646cb29

Browse files
committed
Basic Elm canvas API
0 parents  commit 646cb29

File tree

5 files changed

+523
-0
lines changed

5 files changed

+523
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/elm-stuff
2+
elm.js

Canvas.elm

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
module Canvas where
2+
3+
import Graphics.Element exposing (Element)
4+
import Basics exposing (pi)
5+
import Color exposing (..)
6+
import Native.Canvas
7+
import Task exposing (Task)
8+
9+
-- Line Styles
10+
11+
type LineCap = ButtCap | RoundCap | SquareCap
12+
type LineJoin = RoundJoin | BevelJoin | MiterJoin
13+
14+
-- Composite Operations
15+
16+
type CompositeOperation
17+
= SourceOver
18+
| SourceIn
19+
| SourceOut
20+
| SourceAtop
21+
| DestinationOver
22+
| DestinationIn
23+
| DestinationOut
24+
| DestinationAtop
25+
| Lighter
26+
| Copy
27+
| Xor
28+
| Multiply
29+
| Screen
30+
| Overlay
31+
| Darken
32+
| Lighten
33+
| ColorDodge
34+
| ColorBurn
35+
| HardLight
36+
| SoftLight
37+
| Difference
38+
| Exclusion
39+
| Hue
40+
| Saturation
41+
| Color
42+
| Luminosity
43+
44+
-- Paths
45+
46+
type alias HasPoint a = { a | x: Float, y: Float }
47+
type alias HasCircle a = HasPoint { a | r: Float }
48+
type alias HasRect a = HasPoint { a | w: Float, h: Float }
49+
type alias Point = HasPoint {}
50+
type alias Circle = HasCircle {}
51+
type alias Rectangle = HasRect {}
52+
53+
type PathMethod
54+
= MoveTo Point
55+
| LineTo Point
56+
| Rect Rectangle
57+
| Arc { x: Float, y: Float, r: Float, startAngle: Float, endAngle: Float, ccw: Bool }
58+
| ArcTo { x1: Float, y1: Float, x2: Float, y2: Float, r: Float }
59+
| ClosePath
60+
61+
type alias Path = List PathMethod
62+
63+
-- Images
64+
65+
type Image = Image
66+
67+
-- Commands
68+
69+
type Command
70+
-- Draw Commands
71+
= Clear Rectangle
72+
| FillPath Path
73+
| StrokePath Path
74+
| FillText String Float Float
75+
| StrokeText String Float Float
76+
| DrawImage Float Float Image
77+
78+
-- State Commands
79+
| FillColor Color
80+
| StrokeColor Color
81+
| FillGrad Gradient
82+
| StrokeGrad Gradient
83+
84+
| LineWidth Float
85+
| LineCapStyle LineCap
86+
| LineJoinStyle LineJoin
87+
| LineMiterLimit Float
88+
89+
| ShadowBlur Float
90+
| ShadowColor Color
91+
| ShadowOffset Float Float
92+
93+
| Translate Float Float
94+
| Rotate Float
95+
| Scale Float Float
96+
97+
| Font String
98+
| Alpha Float
99+
| Composite CompositeOperation
100+
101+
-- Wraps Commands in Save/Restore
102+
| Context (List Command)
103+
104+
-- Draw Commands
105+
106+
clearRect rect = Clear rect
107+
fillRect x y w h = FillPath [Rect (rect x y w h)]
108+
strokeRect x y w h = StrokePath [Rect (rect x y w h)]
109+
110+
fillPath path = FillPath path
111+
strokePath path = StrokePath path
112+
113+
fillCircle circle = FillPath [Arc circle]
114+
strokeCircle circle = StrokePath [Arc circle]
115+
116+
fillText text x y = FillText text x y
117+
strokeText text x y = StrokeText text x y
118+
119+
-- Style Commands
120+
121+
fillColor color = FillColor color
122+
strokeColor color = StrokeColor color
123+
fillGrad grad = FillGrad grad
124+
strokeGrad grad = StrokeGrad grad
125+
126+
lineWidth width = LineWidth width
127+
lineCap cap = LineCapStyle cap
128+
lineJoin join = LineJoinStyle join
129+
lineMiterLimit length = LineMiterLimit length
130+
131+
shadowBlur blurRadius = ShadowBlur blurRadius
132+
shadowColor color = ShadowColor color
133+
shadowOffset offsetX offsetY = ShadowOffset offsetX offsetY
134+
135+
translate x y = Translate x y
136+
rotate angle = Rotate angle
137+
scale scaleX scaleY = Scale scaleX scaleY
138+
139+
font fnt = Font fnt
140+
alpha a = Alpha a
141+
composite compositeOp = Composite compositeOp
142+
143+
context commands = Context commands
144+
145+
-- Path Methods
146+
147+
moveTo x y = MoveTo { x = x, y = y }
148+
lineTo x y = LineTo { x = x, y = y }
149+
150+
rect x y w h = { x = x, y = y, w = w, h = h }
151+
152+
circle x y r = arc x y r 0.0 (2.0 * pi)
153+
154+
arcWithDir x y r startAngle endAngle ccw =
155+
{ x = x, y = y, r = r, startAngle = startAngle, endAngle = endAngle, ccw = ccw }
156+
157+
arc x y r startAngle endAngle = arcWithDir x y r startAngle endAngle False
158+
159+
arcTo x1 y1 x2 y2 r = ArcTo { x1 = x1, y1 = y1, x2 = x2, y2 = y2, r = r }
160+
161+
-- Images
162+
163+
drawImage : Float -> Float -> Image -> Command
164+
drawImage x y img = DrawImage x y img
165+
166+
loadImage : String -> Task String Image
167+
loadImage = Native.Canvas.loadImage
168+
169+
-- Canvas
170+
171+
canvas : (Int, Int) -> List Command -> Element
172+
canvas = Native.Canvas.canvas

Main.elm

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
module Main where
2+
3+
import Graphics.Element exposing (..)
4+
import Time exposing (..)
5+
import Canvas exposing (..)
6+
import Mouse
7+
import Basics exposing (pi)
8+
import Color exposing (..)
9+
import Task exposing (Task, andThen, toResult)
10+
11+
w = 1024
12+
h = 768
13+
14+
c = circle 512 384 32
15+
16+
center = (512, 384)
17+
18+
styleCmds =
19+
[ strokeColor red
20+
, shadowBlur 10
21+
, shadowColor blue
22+
, shadowOffset 5 5
23+
, lineWidth 2
24+
, lineCap ButtCap
25+
, lineJoin RoundJoin
26+
]
27+
28+
linGrad = linear (0, 0) (w, h)
29+
[ (0, green)
30+
, (1, lightBlue)
31+
]
32+
33+
radGrad = radial (75, 50) 5 (90, 60) 100
34+
[ (0, red), (1, white) ]
35+
36+
drawCmds =
37+
[ fillGrad linGrad
38+
, fillRect 0 0 w h
39+
, fillColor orange
40+
, strokePath
41+
[ moveTo 20 20
42+
, lineTo 100 20
43+
, arcTo 150 20 150 70 50
44+
, lineTo 150 120
45+
]
46+
, fillCircle c
47+
, strokeCircle c
48+
, context
49+
[ composite HardLight
50+
, translate -100 -100
51+
, fillCircle c
52+
]
53+
, strokeColor purple
54+
55+
, font "50px Arial"
56+
, fillText "Elm-Canvas" 450 100
57+
]
58+
59+
commands = styleCmds ++ drawCmds
60+
61+
type alias Model =
62+
{ w : Int
63+
, h : Int
64+
, commands : List Command
65+
}
66+
67+
model = Signal.constant
68+
{ w = 1024
69+
, h = 768
70+
, commands = commands
71+
}
72+
73+
view : Model -> Result String Image -> Element
74+
view model result =
75+
let
76+
drawImg = case result of
77+
Err _ -> []
78+
Ok img -> [drawImage 512 384 img]
79+
in
80+
canvas (model.w, model.h) (model.commands ++ drawImg)
81+
82+
main : Signal Element
83+
main = Signal.map2 view model results.signal
84+
85+
results : Signal.Mailbox (Result String Image)
86+
results = Signal.mailbox (Err "Image not found")
87+
88+
imageSrc = "http://inkarnate.com/images/map-builder/skins/darkfantasy-world/objects/compass.png"
89+
90+
port updateImage : Task String ()
91+
port updateImage =
92+
(loadImage imageSrc |> Task.toResult) `andThen` Signal.send results.address

elm-package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "1.0.0",
3+
"summary": "helpful summary of your project, less than 80 characters",
4+
"repository": "https://github.com/USER/PROJECT.git",
5+
"license": "BSD3",
6+
"source-directories": [
7+
"."
8+
],
9+
"exposed-modules": [],
10+
"native-modules": true,
11+
"dependencies": {
12+
"elm-lang/core": "2.1.0 <= v < 3.0.0"
13+
},
14+
"elm-version": "0.15.1 <= v < 0.16.0"
15+
}

0 commit comments

Comments
 (0)