aboutsummaryrefslogtreecommitdiff
path: root/js/app.js
blob: b943d6993ad16be5d603da34142a9b0f7d0ff55f (plain) (blame)
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
window.onload = ()=>{
    animate()
  }
  
  function animate() {
    animateFrame( "bobAnimate", 70, [
      { // run right
        move: ()=>{ bobAnimate.style.marginLeft = "300px" },
        frames: [ bob_right, bob_right_walk1, bob_right_walk2 ],
        delay: 100,
        limitFrames: 15,
      },
      { // stop blink , blink
        frames: [ bob_right_blink, bob_right, bob_right_blink, bob_right, bob_left ],
        delay: 150,
        limitFrames: 5,
      },
      { // turn left, blink
        frames: [ bob_left_blink, bob_left ],
        delay: 500,
        limitFrames: 2,
      },
      { // run left
        move: ()=>{ bobAnimate.style.marginLeft = "80px" },
        frames: [ bob_left_walk1, bob_left_walk2, bob_left ],
        delay: 100,
        limitFrames: 15
      },
      { // stop blink
        frames: [ bob_left, bob_left_blink, bob_left ],
        delay: 500,
        limitFrames: 3,
      },
      { // turn & blink, blink
        frames: [ bob_right, bob_right_blink, bob_right, bob_right_blink, bob_right ],
        delay: 100,
        limitFrames: 5,
      },
      { // run right
        move: ()=>{ bobAnimate.style.marginLeft = "200px" },
        frames: [ bob_right, bob_right_walk1, bob_right_walk2 ],
        delay: 100,
        limitFrames: 13,
      },
      { // turn, turn, turn
        frames: [ bob_left, bob_right, bob_left ],
        delay: 250,
        limitFrames: 3,
      },
      { // angry, angry
        frames: [ bob_left_angry, bob_left, bob_left_angry, bob_left ],
        delay: 150,
        limitFrames: 4,
      },
      { // run left
        move: ()=>{ bobAnimate.style.marginLeft = "0px" },
        frames: [ bob_left_walk1, bob_left_walk2, bob_left ],
        delay: 100,
        limitFrames: 15
      },
      { // turn slow, blnk slow
        frames: [ bob_right, bob_right_blink, bob_right ],
        delay: 1000,
        limitFrames: 3,
      },
      { // RESTART
        move: ()=>{ animate() }, frames: [], delay: 0, limitFrames: 0
      },
    ])
  }
  
  function buildFrame(elmId, frameWidth, frame) {
    const pW = frameWidth/8
    const elm = document.getElementById(elmId)
    elm.innerHTML = `
      <svg xmlns="http://www.w3.org/2000/svg"
        width="${frameWidth}px"
        height="${frameWidth}px"
        style="background-color: ${frame.bg};"
        viewBox="0 0 522 522">
        <defs>
          <filter id="f1" height="130%" width="130%">
            <feGaussianBlur in="SourceAlpha" stdDeviation="5"></feGaussianBlur>
            <feOffset dx="5" dy="5" result="offsetblur"></feOffset>
            <feComponentTransfer>
              <feFuncA type="linear" slope="0.5"></feFuncA>
            </feComponentTransfer>
            <feMerge>
              <feMergeNode></feMergeNode>
              <feMergeNode in="SourceGraphic"></feMergeNode>
            </feMerge>
          </filter>
        </defs>
        <g filter="url(#f1)" id="g-${elmId}"></g>
      </svg>
    `
    setPixels(elmId, frame)
  }
  
  function setPixels(elmId, frame) {
    const g = document.getElementById("g-"+elmId)
    g.innerHTML = ""
    frame.pixels.map( p => {
      g.innerHTML += /*html*/`
        <path fill-rule="evenodd"
          fill="${p.color}"
          opacity="${p.opacity || 1}"
          d=" M${p.x*64} ${p.y*64}
              L${(p.x*64) + 64} ${p.y*64}
              L${(p.x*64) + 64} ${(p.y*64) + 64}
              L${(p.x*64)} ${(p.y*64) + 64} Z">
        </path>
      `
    })
  }
  
  function animateFrame(elmId, size, anime){
  
    buildFrame("bobAnimate", size, anime[0].frames[0])
    anime[0].frames.push(anime[0].frames[0])
    anime[0].frames.shift()
  
    const g = document.getElementById("g-"+elmId)
  
    function animate(anime) {
  
      const int = setInterval(function(){
        if (anime[0].limitFrames === 0) {
          clearInterval(int)
          anime.shift()
          if (anime.length > 0) {
            if (anime[0].move) anime[0].move()
            animate(anime)
          }
        } else {
          setPixels(elmId, anime[0].frames[0])
          anime[0].frames.push(anime[0].frames[0])
          anime[0].frames.shift()
          anime[0].limitFrames--
        }
      }, anime[0].delay)
  
    }
  
    if (anime[0].move) anime[0].move()
    animate(anime)
  
  }