-
Notifications
You must be signed in to change notification settings - Fork 0
/
Warp.elm
722 lines (571 loc) · 19.6 KB
/
Warp.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
port module Warp exposing (main)
import Browser
import Browser.Events
import Html exposing (..)
import Html.Attributes
import Html.Events
import Json.Decode as D
import Math.Vector2 exposing (Vec2, vec2)
import Math.Vector3 exposing (Vec3, vec3)
import Svg exposing (Svg)
import Svg.Attributes exposing (viewBox)
import Task
import WebGL exposing (Mesh, Shader)
import WebGL.Texture exposing (Texture)
-- ELM ARCHITECTURE
type alias Model =
{ time : Float
, error : Bool
, bitmoji : Maybe Texture
, userId : String
, dance : Dance { comicId : String }
, editMode : Maybe EditMode
, toCustomization : D.Decoder Customization
}
type EditMode
= MovingAnchors
| EditingTiming
main : Program () Model Msg
main =
Browser.element
{ init = init
, view = view
, subscriptions = subscriptions
, update = update
}
init : () -> ( Model, Cmd Msg )
init () =
let
kofi =
"4b014b97-f9a9-480e-8e7f-3c74def6e9f6"
in
load
{ time = 0
, error = False
, bitmoji = Nothing
, userId = kofi
, dance = lean
, editMode = Nothing
, toCustomization = D.fail ""
}
load : Model -> ( Model, Cmd Msg )
load model =
let
url =
baseUrl
++ model.dance.comicId
++ "-"
++ model.userId
++ "-v1.png?transparent=1&palette=1"
in
WebGL.Texture.loadWith WebGL.Texture.nonPowerOfTwoOptions url
|> Task.attempt GotBitmoji
|> Tuple.pair model
type Msg
= Diff Float
| GotBitmoji (Result WebGL.Texture.Error Texture)
| NewBitmoji (Result D.Error ( String, String ))
| SetDance (Dance { comicId : String })
| SetEditMode (Maybe EditMode)
| StartCustomization (D.Decoder Customization)
| Customize Customization
| StopCustomization
update : Msg -> Model -> ( Model, Cmd Msg )
update msg ({ dance } as model) =
case msg of
Diff time ->
pure { model | time = model.time + time }
GotBitmoji (Err _) ->
pure { model | error = True }
GotBitmoji (Ok bitmoji) ->
pure { model | error = False, bitmoji = Just bitmoji }
NewBitmoji (Err _) ->
pure { model | error = True }
NewBitmoji (Ok ( userId, comicId )) ->
load { model | userId = userId, dance = { dance | comicId = comicId } }
SetDance dance_ ->
load { model | dance = dance_ }
SetEditMode editMode ->
pure { model | editMode = editMode }
StartCustomization toCustomization ->
pure { model | toCustomization = toCustomization }
Customize customization ->
pure { model | dance = customize customization dance }
StopCustomization ->
pure { model | toCustomization = D.fail "" }
pure : a -> ( a, Cmd msg )
pure a =
( a, Cmd.none )
port imageDrop : (D.Value -> msg) -> Sub msg
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ imageDrop (NewBitmoji << D.decodeValue parseIds)
, Browser.Events.onAnimationFrameDelta Diff
, Browser.Events.onMouseUp (D.succeed StopCustomization)
]
view : Model -> Html Msg
view model =
main_ [ Html.Attributes.style "max-width" (px bitmojiSize) ]
[ div [ Html.Attributes.style "position" "relative" ]
[ WebGL.toHtml
[ Html.Attributes.width bitmojiSize
, Html.Attributes.height bitmojiSize
]
(viewCanvas model)
, case model.editMode of
Nothing ->
text ""
Just editMode ->
Svg.svg
[ Html.Attributes.style "position" "absolute"
, Html.Attributes.style "top" "0"
, Html.Attributes.style "left" "0"
, Html.Attributes.width bitmojiSize
, Html.Attributes.height bitmojiSize
, Svg.Attributes.viewBox "0 0 1 1"
, Html.Events.on "mousemove" (D.map Customize model.toCustomization)
]
[ viewAnchor editMode
A
model.dance.aTimeMultiplier
model.dance.aPhase
model.dance.aTarget
model.dance.aMovement
, viewAnchor editMode
B
model.dance.bTimeMultiplier
model.dance.bPhase
model.dance.bTarget
model.dance.bMovement
, viewAnchor editMode
C
model.dance.cTimeMultiplier
model.dance.cPhase
model.dance.cTarget
model.dance.cMovement
, viewAnchor editMode
D
model.dance.dTimeMultiplier
model.dance.dPhase
model.dance.dTarget
model.dance.dMovement
]
]
, fieldset []
[ radio "presets" SetDance model.dance <|
[ ( lean, "The Lean" )
, ( disco, "Disco Wave" )
]
, hr [] []
, span []
[ strong [] [ text "Customize" ]
, em [] [ text " (requires mouse)" ]
]
, showIf model.error <|
div [ Html.Attributes.class "warning" ]
[ b [] [ Html.text "That doesn't seem like a Bitmoji..." ] ]
, ol []
[ li []
[ text "Install the "
, a [ Html.Attributes.href chromeExtensionUrl, Html.Attributes.target "_blank" ]
[ text "official Bitmoji Chrome extension" ]
]
, li [] [ text "Drag-and-drop your Bitmoji here" ]
]
, radio "edit" SetEditMode model.editMode <|
[ ( Nothing, "Hide controls" )
, ( Just MovingAnchors, "Move anchors" )
, ( Just EditingTiming, "Edit timing" )
]
]
]
viewCanvas : Model -> List WebGL.Entity
viewCanvas model =
case model.bitmoji of
Nothing ->
[]
Just bitmoji ->
[ WebGL.entity vertexShader fragmentShader mesh <|
{ time = model.time / 1000
, bitmoji = bitmoji
, aTimeMultiplier = model.dance.aTimeMultiplier
, aPhase = model.dance.aPhase
, aTarget = model.dance.aTarget
, aMovement = model.dance.aMovement
, bTimeMultiplier = model.dance.bTimeMultiplier
, bPhase = model.dance.bPhase
, bTarget = model.dance.bTarget
, bMovement = model.dance.bMovement
, cTimeMultiplier = model.dance.cTimeMultiplier
, cPhase = model.dance.cPhase
, cTarget = model.dance.cTarget
, cMovement = model.dance.cMovement
, dTimeMultiplier = model.dance.dTimeMultiplier
, dPhase = model.dance.dPhase
, dTarget = model.dance.dTarget
, dMovement = model.dance.dMovement
}
]
viewAnchor : EditMode -> Anchor -> Float -> Float -> Vec2 -> Vec2 -> Svg Msg
viewAnchor editMode anchor timeMultiplier phase target movement =
let
x =
Math.Vector2.getX target
y =
1 - Math.Vector2.getY target
width =
Math.Vector2.getX movement
height =
Math.Vector2.getY movement
in
Svg.g [] <|
case editMode of
MovingAnchors ->
[ crossPath x y width height <|
[ Svg.Attributes.style "cursor:move"
, customizeOnMouseDown <|
D.map (Target anchor) (D.map2 vec2 offsetX offsetYInverse)
]
, verticalAxis x y height <|
customizeOnMouseDown <|
D.map (MovementY anchor << distance y) offsetY
, horizontalAxis x y width <|
customizeOnMouseDown <|
D.map (MovementX anchor << distance x) offsetX
]
EditingTiming ->
[ crossPath x y phase timeMultiplier []
, verticalAxis x y timeMultiplier <|
customizeOnMouseDown <|
D.map (TimeMultiplier anchor << distance y) offsetY
, horizontalAxis x y phase <|
customizeOnMouseDown <|
D.map (Phase anchor << distance x) offsetX
]
crossPath : Float -> Float -> Float -> Float -> List (Attribute msg) -> Svg msg
crossPath x y width height attrs =
let
d =
Svg.Attributes.d <|
String.join " " <|
List.map (String.join " ") <|
[ [ "M", String.fromFloat x, String.fromFloat y ]
, [ "v", String.fromFloat (-height / 2) ]
, [ "v", String.fromFloat height ]
, [ "v", String.fromFloat (-height / 2) ]
, [ "h", String.fromFloat (-width / 2) ]
, [ "h", String.fromFloat width ]
]
in
Svg.path (d :: attrs) []
verticalAxis : Float -> Float -> Float -> Attribute msg -> Svg msg
verticalAxis centerX centerY value attr =
Svg.circle
[ Svg.Attributes.style "cursor:ns-resize"
, Svg.Attributes.r "0.01"
, Svg.Attributes.cx <| String.fromFloat centerX
, Svg.Attributes.cy <| String.fromFloat (centerY + value / 2)
, attr
]
[]
horizontalAxis : Float -> Float -> Float -> Attribute msg -> Svg msg
horizontalAxis centerX centerY value attr =
Svg.circle
[ Svg.Attributes.style "cursor:ew-resize"
, Svg.Attributes.r "0.01"
, Svg.Attributes.cx <| String.fromFloat (centerX + value / 2)
, Svg.Attributes.cy <| String.fromFloat centerY
, attr
]
[]
radio : String -> (a -> msg) -> a -> List ( a, String ) -> Html msg
radio name toMsg value options =
let
radio_ ( thisValue, thisLabel ) =
label []
[ input
[ Html.Attributes.name name
, Html.Attributes.type_ "radio"
, Html.Attributes.value thisLabel
, Html.Attributes.checked (thisValue == value)
, onChange thisLabel (toMsg thisValue)
]
[]
, text " "
, text thisLabel
]
in
div [] <| List.map radio_ options
onChange : String -> msg -> Attribute msg
onChange this msg =
Html.Events.on "change" <|
D.andThen
(\x ->
if x == this then
D.succeed msg
else
D.fail ""
)
Html.Events.targetValue
customizeOnMouseDown : D.Decoder Customization -> Attribute Msg
customizeOnMouseDown decoder =
Html.Events.onMouseDown <| StartCustomization decoder
px : Float -> String
px value =
String.fromFloat value ++ "px"
showIf : Bool -> Html msg -> Html msg
showIf check el =
if check then
el
else
text ""
offsetX : D.Decoder Float
offsetX =
D.map (\x -> x / bitmojiSize) (D.field "offsetX" D.float)
offsetY : D.Decoder Float
offsetY =
D.map (\y -> y / bitmojiSize) (D.field "offsetY" D.float)
offsetYInverse : D.Decoder Float
offsetYInverse =
D.map (\y -> 1 - y) offsetY
distance : Float -> Float -> Float
distance center offset =
(offset - center) * 2
-- MESH
mesh : Mesh { position : Vec3 }
mesh =
WebGL.triangles
[ ( { position = vec3 -1 1 0 }
, { position = vec3 1 1 0 }
, { position = vec3 -1 -1 0 }
)
, ( { position = vec3 -1 -1 0 }
, { position = vec3 1 1 0 }
, { position = vec3 1 -1 0 }
)
]
-- SHADERS
type alias Uniforms =
Dance { time : Float, bitmoji : Texture }
vertexShader : Shader { position : Vec3 } Uniforms { vFragCoord : Vec2 }
vertexShader =
[glsl|
precision mediump float;
attribute vec3 position;
varying vec2 vFragCoord;
void main () {
gl_Position = vec4(position, 1.0);
vFragCoord = (position.xy + 1.0) / 2.0;
}
|]
fragmentShader : Shader {} Uniforms { vFragCoord : Vec2 }
fragmentShader =
[glsl|
precision mediump float;
varying vec2 vFragCoord;
uniform float time;
uniform sampler2D bitmoji;
const int animSteps = 60;
const float animDist = 0.003;
const float pi = 3.141592653589793;
uniform float aTimeMultiplier;
uniform float aPhase;
uniform vec2 aTarget;
uniform vec2 aMovement;
uniform float bTimeMultiplier;
uniform float bPhase;
uniform vec2 bTarget;
uniform vec2 bMovement;
uniform float cTimeMultiplier;
uniform float cPhase;
uniform vec2 cTarget;
uniform vec2 cMovement;
uniform float dTimeMultiplier;
uniform float dPhase;
uniform vec2 dTarget;
uniform vec2 dMovement;
vec2 dance(float time, vec2 uv, vec2 target, vec2 movement) {
vec2 diff = abs(uv - target);
vec2 value = vec2(0.);
for(int step = 0; step < animSteps; step++) {
if (length(diff) < length(float(step)*animDist)) {
value += (movement/1000.)*time;
}
}
return value;
}
void main(void) {
vec2 uv = vFragCoord;
vec2 img = vec2(uv);
img += dance(sin(time*aTimeMultiplier*100. + aPhase*pi), uv, aTarget, aMovement);
img += dance(sin(time*bTimeMultiplier*100. + bPhase*pi), uv, bTarget, bMovement);
img += dance(sin(time*cTimeMultiplier*100. + cPhase*pi), uv, cTarget, cMovement);
img += dance(sin(time*dTimeMultiplier*100. + dPhase*pi), uv, dTarget, dMovement);
gl_FragColor = texture2D(bitmoji, img);
}
|]
-- BITMOJI "API"
bitmojiSize : number
bitmojiSize =
398
parseIds : D.Decoder ( String, String )
parseIds =
D.andThen parseIds_ D.string
parseIds_ : String -> D.Decoder ( String, String )
parseIds_ raw =
if not <| String.startsWith baseUrl raw then
D.fail ""
else
case dropUntilUserId (String.split "-" raw) of
Nothing ->
D.fail ""
Just userId ->
String.dropLeft (String.length baseUrl) raw
|> String.split ("-" ++ userId)
|> List.head
|> Maybe.map (D.succeed << Tuple.pair userId)
|> Maybe.withDefault (D.fail "")
dropUntilUserId : List String -> Maybe String
dropUntilUserId segments =
case segments of
[] ->
Nothing
[ a, b, c, d, e, _ ] ->
Just (String.join "-" [ a, b, c, d, e ])
_ :: rest ->
dropUntilUserId rest
baseUrl : String
baseUrl =
"https://render.bitstrips.com/v2/cpanel/"
chromeExtensionUrl : String
chromeExtensionUrl =
"https://chrome.google.com/webstore/detail/bitmoji/bfgdeiadkckfbkeigkoncpdieiiefpig?hl=en"
{-| DANCE
Choose which 4 points (a, b, c, d) to animate in the Bitmoji image!
TimeMultiplier: rate of movement. Bigger means faster.
Phase: radians by which to offset movement. Bigger means more delayed.
Target: coordinates [0, 1]. Point to target.
Movement: distance in target coordinate system [0, 1]. How far to animate.
-}
type alias Dance a =
{ a
| aTimeMultiplier : Float
, aPhase : Float
, aTarget : Vec2
, aMovement : Vec2
, bTimeMultiplier : Float
, bPhase : Float
, bTarget : Vec2
, bMovement : Vec2
, cTimeMultiplier : Float
, cPhase : Float
, cTarget : Vec2
, cMovement : Vec2
, dTimeMultiplier : Float
, dPhase : Float
, dTarget : Vec2
, dMovement : Vec2
}
lean : Dance { comicId : String }
lean =
{ comicId = "49490f4e-eabb-4cab-bcb6-69f361d66706"
-- HEAD
, aTimeMultiplier = 0.16
, aPhase = -1 / 2
, aTarget = vec2 0.5 0.7
, aMovement = vec2 0 0.2
-- HIPS
, bTimeMultiplier = 0.08
, bPhase = 0
, bTarget = vec2 0.5 0.3
, bMovement = vec2 0.6 0
-- FEET
, cTimeMultiplier = 0.16
, cPhase = -1 / 2
, cTarget = vec2 0.5 0.1
, cMovement = vec2 0 0.1
-- ???
, dTimeMultiplier = 0.1
, dPhase = 0.1
, dTarget = vec2 0.2 0.2
, dMovement = vec2 0.1 0.1
}
disco : Dance { comicId : String }
disco =
{ comicId = "5ee3832d-7743-43c8-b6d7-ea47f11a1798"
-- LEFT ARM
, aTimeMultiplier = 0.08
, aPhase = 0
, aTarget = vec2 0.1 0.6
, aMovement = vec2 0.2 0.3
-- HIPS
, bTimeMultiplier = 0.08
, bPhase = -1 / 2
, bTarget = vec2 0.5 0.3
, bMovement = vec2 0.5 -0.2
-- RIGHT ARM
, cTimeMultiplier = 0.08
, cPhase = -1
, cTarget = vec2 0.7 0.4
, cMovement = vec2 0.3 0.5
-- HEAD
, dTimeMultiplier = 0.08
, dPhase = -1 / 2
, dTarget = vec2 0.45 0.7
, dMovement = vec2 0.05 -0.15
}
type Customization
= TimeMultiplier Anchor Float
| Phase Anchor Float
| Target Anchor Vec2
| MovementX Anchor Float
| MovementY Anchor Float
type Anchor
= A
| B
| C
| D
customize : Customization -> Dance a -> Dance a
customize customization dance =
case customization of
TimeMultiplier A value ->
{ dance | aTimeMultiplier = value }
Phase A value ->
{ dance | aPhase = value }
Target A value ->
{ dance | aTarget = value }
MovementX A value ->
{ dance | aMovement = Math.Vector2.setX value dance.aMovement }
MovementY A value ->
{ dance | aMovement = Math.Vector2.setY value dance.aMovement }
TimeMultiplier B value ->
{ dance | bTimeMultiplier = value }
Phase B value ->
{ dance | bPhase = value }
Target B value ->
{ dance | bTarget = value }
MovementX B value ->
{ dance | bMovement = Math.Vector2.setX value dance.bMovement }
MovementY B value ->
{ dance | bMovement = Math.Vector2.setY value dance.bMovement }
TimeMultiplier C value ->
{ dance | cTimeMultiplier = value }
Phase C value ->
{ dance | cPhase = value }
Target C value ->
{ dance | cTarget = value }
MovementX C value ->
{ dance | cMovement = Math.Vector2.setX value dance.cMovement }
MovementY C value ->
{ dance | cMovement = Math.Vector2.setY value dance.cMovement }
TimeMultiplier D value ->
{ dance | dTimeMultiplier = value }
Phase D value ->
{ dance | dPhase = value }
Target D value ->
{ dance | dTarget = value }
MovementX D value ->
{ dance | dMovement = Math.Vector2.setX value dance.dMovement }
MovementY D value ->
{ dance | dMovement = Math.Vector2.setY value dance.dMovement }