forked from evancz/elm-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayground.elm
1737 lines (1252 loc) · 36.5 KB
/
Playground.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module Playground exposing
( -- picture
animation
, game
--
, Shape
, circle, oval
, square, rectangle
, triangle, pentagon, hexagon, octagon, polygon
, image, words
, move, moveUp, moveDown, moveLeft, moveRight, moveX, moveY
, scale, rotate
, fade
, group
--
, Number
--
, Time
, spin
, wave
, zigzag
--
, Computer
, Mouse
, Screen
, Keyboard
, TouchState
, toX
, toY
, toXY
--
, Color
, red, orange, yellow, green, blue, purple, brown
, lightRed, lightOrange, lightYellow, lightGreen, lightBlue, lightPurple, lightBrown
, darkRed, darkOrange, darkYellow, darkGreen, darkBlue, darkPurple, darkBrown
, white, lightGrey, grey, darkGrey, lightCharcoal, charcoal, darkCharcoal, black
, rgb
, lightGray, gray, darkGray
)
-- @docs picture, animation, game
{-|
# Playgrounds
@docs animation, game
# Shapes
@docs Shape, circle, oval, square, rectangle, triangle, pentagon, hexagon, octagon, polygon
# Words
@docs words
# Images
@docs image
# Move Shapes
@docs move, moveUp, moveDown, moveLeft, moveRight, moveX, moveY
# Customize Shapes
@docs scale, rotate, fade
# Groups
@docs group
# Time
@docs Time, spin, wave, zigzag
# Computer
@docs Computer, Mouse, Screen, Keyboard, TouchState, toX, toY, toXY
# Colors
@docs Color, rgb, red, orange, yellow, green, blue, purple, brown
### Light Colors
@docs lightRed, lightOrange, lightYellow, lightGreen, lightBlue, lightPurple, lightBrown
### Dark Colors
@docs darkRed, darkOrange, darkYellow, darkGreen, darkBlue, darkPurple, darkBrown
### Shades of Grey
@docs white, lightGrey, grey, darkGrey, lightCharcoal, charcoal, darkCharcoal, black
### Alternate Spellings of Gray
@docs lightGray, gray, darkGray
### Numbers
@docs Number
-}
import Browser
import Browser.Dom as Dom
import Browser.Events as E
import Html.Events.Extra.Touch as Touch
import Html
import Html.Attributes as H
import Svg exposing (..)
import Svg.Attributes exposing (..)
import Json.Decode as D
import Set
import Task
import Time
-- -- PICTURE
-- {-| Make a picture! Here is a picture of a triangle with an eyeball:
-- import Playground exposing (..)
-- main =
-- picture
-- [ triangle green 150
-- , circle white 40
-- , circle black 10
-- ]
-- -}
-- picture : List Shape -> Program () Screen (Int, Int)
-- picture shapes =
-- let
-- init () =
-- (toScreen 600 600, Cmd.none)
-- view screen =
-- { title = "Playground"
-- , body = [ render screen shapes ]
-- }
-- update (width,height) _ =
-- ( toScreen (toFloat width) (toFloat height)
-- , Cmd.none
-- )
-- subscriptions _ =
-- E.onResize Tuple.pair
-- in
-- Browser.document
-- { init = init
-- , view = view
-- , update = update
-- , subscriptions = subscriptions
-- }
-- COMPUTER
{-| When writing a [`game`](#game), you can look up all sorts of information
about your computer:
- [`Mouse`](#Mouse) - Where is the mouse right now?
- [`Keyboard`](#Keyboard) - Are the arrow keys down?
- [`Screen`](#Screen) - How wide is the screen?
- [`Time`](#Time) - What time is it right now?
So you can use expressions like `computer.mouse.x` and `computer.keyboard.enter`
in games where you want some mouse or keyboard interaction.
-}
type alias Computer =
{ mouse : Mouse
, keyboard : Keyboard
, screen : Screen
, time : Time
, touch : TouchState
}
-- TOUCH
{-| Figure out what is going on with the touch screen.
-}
type alias TouchState =
{ maybePoint : Maybe { x : Float, y : Float }
}
-- MOUSE
{-| Figure out what is going on with the mouse.
You could draw a circle around the mouse with a program like this:
import Playground exposing (..)
main =
game view update 0
view computer memory =
[ circle yellow 40
|> moveX computer.mouse.x
|> moveY computer.mouse.y
]
update computer memory =
memory
You could also use `computer.mouse.down` to change the color of the circle
while the mouse button is down.
-}
type alias Mouse =
{ x : Number
, y : Number
, down : Bool
, click : Bool
}
{-| A number like `1` or `3.14` or `-120`.
-}
type alias Number = Float
-- KEYBOARD
{-| Figure out what is going on with the keyboard.
If someone is pressing the UP and RIGHT arrows, you will see a value like this:
{ up = True, down = False, left = False, right = True
, space = False, enter = False, shift = False, backspace = False
, keys = Set.fromList ["ArrowUp","ArrowRight"]
}
So if you want to move a character based on arrows, you could write an update
like this:
update computer y =
if computer.keyboard.up then
y + 1
else
y
Check out [`toX`](#toX) and [`toY`](#toY) which make this even easier!
**Note:** The `keys` set will be filled with the name of all keys which are
down right now. So you will see things like `"a"`, `"b"`, `"c"`, `"1"`, `"2"`,
`"Space"`, and `"Control"` in there. Check out [this list][list] to see the
names used for all the different special keys! From there, you can use
[`Set.member`][member] to check for whichever key you want. E.g.
`Set.member "Control" computer.keyboard.keys`.
[list]: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values
[member]: /packages/elm/core/latest/Set#member
-}
type alias Keyboard =
{ up : Bool
, down : Bool
, left : Bool
, right : Bool
, space : Bool
, enter : Bool
, shift : Bool
, backspace : Bool
, keys : Set.Set String
}
{-| Turn the LEFT and RIGHT arrows into a number.
toX { left = False, right = False, ... } == 0
toX { left = True , right = False, ... } == -1
toX { left = False, right = True , ... } == 1
toX { left = True , right = True , ... } == 0
So to make a square move left and right based on the arrow keys, we could say:
import Playground exposing (..)
main =
game view update 0
view computer x =
[ square green 40
|> moveX x
]
update computer x =
x + toX computer.keyboard
-}
toX : Keyboard -> Number
toX keyboard =
(if keyboard.right then 1 else 0) - (if keyboard.left then 1 else 0)
{-| Turn the UP and DOWN arrows into a number.
toY { up = False, down = False, ... } == 0
toY { up = True , down = False, ... } == 1
toY { up = False, down = True , ... } == -1
toY { up = True , down = True , ... } == 0
This can be used to move characters around in games just like [`toX`](#toX):
import Playground exposing (..)
main =
game view update (0,0)
view computer (x,y) =
[ square blue 40
|> move x y
]
update computer (x,y) =
( x + toX computer.keyboard
, y + toY computer.keyboard
)
-}
toY : Keyboard -> Number
toY keyboard =
(if keyboard.up then 1 else 0) - (if keyboard.down then 1 else 0)
{-| If you just use `toX` and `toY`, you will move diagonal too fast. You will go
right at 1 pixel per update, but you will go up/right at 1.41421 pixels per
update.
So `toXY` turns the arrow keys into an `(x,y)` pair such that the distance is
normalized:
toXY { up = True , down = False, left = False, right = False, ... } == (1, 0)
toXY { up = True , down = False, left = False, right = True , ... } == (0.707, 0.707)
toXY { up = False, down = False, left = False, right = True , ... } == (0, 1)
Now when you go up/right, you are still going 1 pixel per update.
import Playground exposing (..)
main =
game view update (0,0)
view computer (x,y) =
[ square green 40
|> move x y
]
update computer (x,y) =
let
(dx,dy) = toXY computer.keyboard
in
(x + dx, y + dy)
-}
toXY : Keyboard -> (Number, Number)
toXY keyboard =
let
x = toX keyboard
y = toY keyboard
in
if x /= 0 && y /= 0 then
(x / squareRootOfTwo, y / squareRootOfTwo)
else
(x,y)
squareRootOfTwo : Number
squareRootOfTwo =
sqrt 2
-- SCREEN
{-| Get the dimensions of the screen. If the screen is 800 by 600, you will see
a value like this:
{ width = 800
, height = 600
, top = 300
, left = -400
, right = 400
, bottom = -300
}
This can be nice when used with [`moveY`](#moveY) if you want to put something
on the bottom of the screen, no matter the dimensions.
-}
type alias Screen =
{ width : Number
, height : Number
, top : Number
, left : Number
, right : Number
, bottom : Number
}
-- TIME
{-| The current time.
Helpful when making an [`animation`](#animation) with functions like
[`spin`](#spin), [`wave`](#wave), and [`zigzag`](#zigzag).
-}
type Time = Time Time.Posix
{-| Create an angle that cycles from 0 to 360 degrees over time.
Here is an [`animation`](#animation) with a spinning triangle:
import Playground exposing (..)
main =
animation view
view time =
[ triangle orange 50
|> rotate (spin 8 time)
]
It will do a full rotation once every eight seconds. Try changing the `8` to
a `2` to make it do a full rotation every two seconds. It moves a lot faster!
-}
spin : Number -> Time -> Number
spin period time =
360 * toFrac period time
{-| Smoothly wave between two numbers.
Here is an [`animation`](#animation) with a circle that resizes:
import Playground exposing (..)
main =
animation view
view time =
[ circle lightBlue (wave 50 90 7 time)
]
The radius of the circle will cycles between 50 and 90 every seven seconds.
It kind of looks like it is breathing.
-}
wave : Number -> Number -> Number -> Time -> Number
wave lo hi period time =
lo + (hi - lo) * (1 + cos (turns (toFrac period time))) / 2
{-| Zig zag between two numbers.
Here is an [`animation`](#animation) with a rectangle that tips back and forth:
import Playground exposing (..)
main =
animation view
view time =
[ rectangle lightGreen 20 100
|> rotate (zigzag -20 20 4 time)
]
It gets rotated by an angle. The angle cycles from -20 degrees to 20 degrees
every four seconds.
-}
zigzag : Number -> Number -> Number -> Time -> Number
zigzag lo hi period time =
lo + (hi - lo) * abs (2 * toFrac period time - 1)
toFrac : Float -> Time -> Float
toFrac period (Time posix) =
let
ms = Time.posixToMillis posix
p = period * 1000
in
toFloat (modBy (round p) ms) / p
-- ANIMATION
{-| Create an animation!
Once you get comfortable using [`picture`](#picture) to layout shapes, you can
try out an `animation`. Here is square that zigzags back and forth:
import Playground exposing (..)
main =
animation view
view time =
[ square blue 40
|> moveX (zigzag -100 100 2 time)
]
We need to define a `view` to make our animation work.
Within `view` we can use functions like [`spin`](#spin), [`wave`](#wave),
and [`zigzag`](#zigzag) to move and rotate our shapes.
-}
animation : (Time -> List Shape) -> Program () Animation Msg
animation viewFrame =
let
init () =
( Animation E.Visible (toScreen 600 600) (Time (Time.millisToPosix 0))
, Task.perform GotViewport Dom.getViewport
)
view (Animation _ screen time) =
{ title = "Playground"
, body = [ render screen (viewFrame time) ]
}
update msg model =
( animationUpdate msg model
, Cmd.none
)
subscriptions (Animation visibility _ _) =
case visibility of
E.Hidden ->
E.onVisibilityChange VisibilityChanged
E.Visible ->
animationSubscriptions
in
Browser.document
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
type Animation =
Animation E.Visibility Screen Time
animationSubscriptions : Sub Msg
animationSubscriptions =
Sub.batch
[ E.onResize Resized
, E.onAnimationFrame Tick
, E.onVisibilityChange VisibilityChanged
]
animationUpdate : Msg -> Animation -> Animation
animationUpdate msg (Animation v s t as state) =
case msg of
Tick posix -> Animation v s (Time posix)
VisibilityChanged vis -> Animation vis s t
GotViewport {viewport} -> Animation v (toScreen viewport.width viewport.height) t
Resized w h -> Animation v (toScreen (toFloat w) (toFloat h)) t
KeyChanged _ _ -> state
MouseMove _ _ -> state
MouseClick -> state
MouseButton _ -> state
TouchMove _ -> state
-- GAME
{-| Create a game!
Once you get comfortable with [`animation`](#animation), you can try making a
game with the keyboard and mouse. Here is an example of a green square that
just moves to the right:
import Playground exposing (..)
main =
game view update 0
view computer offset =
[ square green 40
|> moveRight offset
]
update computer offset =
offset + 0.03
This shows the three important parts of a game:
1. `memory` - makes it possible to store information. So with our green square,
we save the `offset` in memory. It starts out at `0`.
2. `view` - lets us say which shapes to put on screen. So here we move our
square right by the `offset` saved in memory.
3. `update` - lets us update the memory. We are incrementing the `offset` by
a tiny amount on each frame.
The `update` function is called about 60 times per second, so our little
changes to `offset` start to add up pretty quickly!
This game is not very fun though! Making a `game` also gives you access to the
[`Computer`](#Computer), so you can use information about the [`Mouse`](#Mouse)
and [`Keyboard`](#Keyboard) to make it interactive! So here is a red square that
moves based on the arrow keys:
import Playground exposing (..)
main =
game view update (0,0)
view computer (x,y) =
[ square red 40
|> move x y
]
update computer (x,y) =
( x + toX computer.keyboard
, y + toY computer.keyboard
)
Notice that in the `update` we use information from the keyboard to update the
`x` and `y` values. These building blocks let you make pretty fancy games!
-}
game : (Computer -> memory -> List Shape) -> (Computer -> memory -> memory) -> memory -> Program () (Game memory) Msg
game viewMemory updateMemory initialMemory =
let
init () =
( Game E.Visible initialMemory initialComputer
, Task.perform GotViewport Dom.getViewport
)
view (Game _ memory computer) =
{ title = "Playground"
, body = [ render computer.screen (viewMemory computer memory) ]
}
update msg model =
( gameUpdate updateMemory msg model
, Cmd.none
)
subscriptions (Game visibility _ _) =
case visibility of
E.Hidden ->
E.onVisibilityChange VisibilityChanged
E.Visible ->
gameSubscriptions
in
Browser.document
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
initialComputer : Computer
initialComputer =
{ mouse = Mouse 0 0 False False
, keyboard = emptyKeyboard
, screen = toScreen 600 600
, time = Time (Time.millisToPosix 0)
, touch = TouchState Nothing
}
-- SUBSCRIPTIONS
gameSubscriptions : Sub Msg
gameSubscriptions =
Sub.batch
[ E.onResize Resized
, E.onKeyUp (D.map (KeyChanged False) (D.field "key" D.string))
, E.onKeyDown (D.map (KeyChanged True) (D.field "key" D.string))
, E.onAnimationFrame Tick
, E.onVisibilityChange VisibilityChanged
, E.onClick (D.succeed MouseClick)
, E.onMouseDown (D.succeed (MouseButton True))
, E.onMouseUp (D.succeed (MouseButton False))
, E.onMouseMove (D.map2 MouseMove (D.field "pageX" D.float) (D.field "pageY" D.float))
]
-- GAME HELPERS
type Game memory =
Game E.Visibility memory Computer
type Msg
= KeyChanged Bool String
| Tick Time.Posix
| GotViewport Dom.Viewport
| Resized Int Int
| VisibilityChanged E.Visibility
| MouseMove Float Float
| MouseClick
| MouseButton Bool
| TouchMove (Maybe (Float, Float))
gameUpdate : (Computer -> memory -> memory) -> Msg -> Game memory -> Game memory
gameUpdate updateMemory msg (Game vis memory computer) =
case msg of
Tick time ->
Game vis (updateMemory computer memory) <|
if computer.mouse.click
then { computer | time = Time time, mouse = mouseClick False computer.mouse }
else { computer | time = Time time }
GotViewport {viewport} ->
Game vis memory { computer | screen = toScreen viewport.width viewport.height }
Resized w h ->
Game vis memory { computer | screen = toScreen (toFloat w) (toFloat h) }
KeyChanged isDown key ->
Game vis memory { computer | keyboard = updateKeyboard isDown key computer.keyboard }
MouseMove pageX pageY ->
let
x = computer.screen.left + pageX
y = computer.screen.top - pageY
in
Game vis memory { computer | mouse = mouseMove x y computer.mouse }
MouseClick ->
Game vis memory { computer | mouse = mouseClick True computer.mouse }
MouseButton isDown ->
Game vis memory { computer | mouse = mouseDown isDown computer.mouse }
VisibilityChanged visibility ->
Game visibility memory
{ computer
| keyboard = emptyKeyboard
, mouse = Mouse computer.mouse.x computer.mouse.y False False
}
TouchMove maybeTouch ->
let
touchPosToPos (pageX, pageY) =
{ x = computer.screen.left + pageX
, y = computer.screen.top - pageY
}
maybePos = Maybe.map touchPosToPos maybeTouch
in Game vis memory { computer | touch = TouchState maybePos }
-- SCREEN HELPERS
toScreen : Float -> Float -> Screen
toScreen width height =
{ width = width
, height = height
, top = height / 2
, left = -width / 2
, right = width / 2
, bottom = -height / 2
}
-- MOUSE HELPERS
mouseClick : Bool -> Mouse -> Mouse
mouseClick bool mouse =
{ mouse | click = bool }
mouseDown : Bool -> Mouse -> Mouse
mouseDown bool mouse =
{ mouse | down = bool }
mouseMove : Float -> Float -> Mouse -> Mouse
mouseMove x y mouse =
{ mouse | x = x, y = y }
-- KEYBOARD HELPERS
emptyKeyboard : Keyboard
emptyKeyboard =
{ up = False
, down = False
, left = False
, right = False
, space = False
, enter = False
, shift = False
, backspace = False
, keys = Set.empty
}
updateKeyboard : Bool -> String -> Keyboard -> Keyboard
updateKeyboard isDown key keyboard =
let
keys =
if isDown then
Set.insert key keyboard.keys
else
Set.remove key keyboard.keys
in
case key of
" " -> { keyboard | keys = keys, space = isDown }
"Enter" -> { keyboard | keys = keys, enter = isDown }
"Shift" -> { keyboard | keys = keys, shift = isDown }
"Backspace" -> { keyboard | keys = keys, backspace = isDown }
"ArrowUp" -> { keyboard | keys = keys, up = isDown }
"ArrowDown" -> { keyboard | keys = keys, down = isDown }
"ArrowLeft" -> { keyboard | keys = keys, left = isDown }
"ArrowRight" -> { keyboard | keys = keys, right = isDown }
_ -> { keyboard | keys = keys }
-- SHAPES
{-| Shapes help you make a `picture`, `animation`, or `game`.
Read on to see examples of [`circle`](#circle), [`rectangle`](#rectangle),
[`words`](#words), [`image`](#image), and many more!
-}
type Shape =
Shape
Number -- x
Number -- y
Number -- angle
Number -- scale
Number -- alpha
Form
type Form
= Circle Color Number
| Oval Color Number Number
| Rectangle Color Number Number
| Ngon Color Int Number
| Polygon Color (List (Number, Number))
| Image Number Number String
| Words Color String
| Group (List Shape)
{-| Make circles:
dot = circle red 10
sun = circle yellow 300
You give a color and then the radius. So the higher the number, the larger
the circle.
-}
circle : Color -> Number -> Shape
circle color radius =
Shape 0 0 0 1 1 (Circle color radius)
{-| Make ovals:
football = oval brown 200 100
You give the color, and then the width and height. So our `football` example
is 200 pixels wide and 100 pixels tall.
-}
oval : Color -> Number -> Number -> Shape
oval color width height =
Shape 0 0 0 1 1 (Oval color width height)
{-| Make squares. Here are two squares combined to look like an empty box:
import Playground exposing (..)
main =
picture
[ square purple 80
, square white 60
]
The number you give is the dimension of each side. So that purple square would
be 80 pixels by 80 pixels.
-}
square : Color -> Number -> Shape
square color n =
Shape 0 0 0 1 1 (Rectangle color n n)
{-| Make rectangles. This example makes a red cross:
import Playground exposing (..)
main =
picture
[ rectangle red 20 60
, rectangle red 60 20
]
You give the color, width, and then height. So the first shape is vertical
part of the cross, the thinner and taller part.
-}
rectangle : Color -> Number -> Number -> Shape
rectangle color width height =
Shape 0 0 0 1 1 (Rectangle color width height)
{-| Make triangles. So if you wanted to draw the Egyptian pyramids, you could
do a simple version like this:
import Playground exposing (..)
main =
picture
[ triangle darkYellow 200
]
The number is the "radius", so the distance from the center to each point of
the pyramid is `200`. Pretty big!
-}
triangle : Color -> Number -> Shape
triangle color radius =
Shape 0 0 0 1 1 (Ngon color 3 radius)
{-| Make pentagons:
import Playground exposing (..)
main =
picture
[ pentagon darkGrey 100
]
You give the color and then the radius. So the distance from the center to each
of the five points is 100 pixels.
-}
pentagon : Color -> Number -> Shape
pentagon color radius =
Shape 0 0 0 1 1 (Ngon color 5 radius)
{-| Make hexagons:
import Playground exposing (..)
main =
picture
[ hexagon lightYellow 50
]
The number is the radius, the distance from the center to each point.
If you made more hexagons, you could [`move`](#move) them around to make a
honeycomb pattern!
-}
hexagon : Color -> Number -> Shape
hexagon color radius =
Shape 0 0 0 1 1 (Ngon color 6 radius)
{-| Make octogons:
import Playground exposing (..)
main =
picture
[ octagon red 100
]
You give the color and radius, so each point of this stop sign is 100 pixels
from the center.
-}
octagon : Color -> Number -> Shape
octagon color radius =
Shape 0 0 0 1 1 (Ngon color 8 radius)
{-| Make any shape you want! Here is a very thin triangle:
import Playground exposing (..)
main =
picture
[ polygon black [ (-10,-20), (0,100), (10,-20) ]
]