Ajax Loader
×

Snap.svg animation test Open & Close & Smash

SRC: https://dribbble.com/shots/1625088-Open-Close-Smash

HTML
script(src="http://cdnjs.cloudflare.com/ajax/libs/snap.svg/0.3.0/snap.svg-min.js")
1
script(src="http://cdnjs.cloudflare.com/ajax/libs/snap.svg/0.3.0/snap.svg-min.js")
 
CSS
*
1
*
2
  box-sizing border-box
3
 
4
body
5
  background-image url(http://cs621825.vk.me/v621825414/12654/s6st9fid6I0.jpg)
6
  background-size cover
7
  background-attachment fixed
8
  margin 0
9
  padding 0
10
  overflow hidden
11
svg
12
  border 2px dashed #fff
13
  margin auto
14
  g
15
    cursor pointer
 
JavaScript
var TestAnim, animSpan, anm, colors, i, ii, j, len, s, text, x;
1
var TestAnim, animSpan, anm, colors, i, ii, j, len, s, text, x;
2
 
3
  s = Snap(window.innerWidth, window.innerHeight);
4
 
5
  TestAnim = (function() {
6
    function TestAnim(Snap1, anim, color, speed) {
7
      var darken, i;
8
      this.Snap = Snap1;
9
      this.anim = anim;
10
      this.color = color;
11
      if (speed) {
12
        this.speed = speed;
13
      } else {
14
        this.speed = 500;
15
      }
16
      i = Snap.color(this.color);
17
      darken = Snap.rgb(i.r - (i.r * 1 / 4), i.g - (i.g * 1 / 4), i.b - (i.b * 1 / 4));
18
      this.shd = this.Snap.filter(Snap.filter.shadow(3, 3, 3, darken));
19
      this.pathStyle = {
20
        stroke: '#fff',
21
        fill: 'none',
22
        strokeWidth: 5,
23
        filter: this.shd
24
      };
25
      return this;
26
    }
27
 
28
    TestAnim.prototype.draw = function(x, y) {
29
      var c, g, long, pahtCenter, pahtTop, pathBottom, r, sBottom, sTop, size, toggle, xBottom, xTop;
30
      r = this.Snap.rect(x - 30, y - 35, 100, 100).attr({
31
        fill: this.color
32
      });
33
      c = this.Snap.circle(x + 20, y + 15, 35).attr(this.pathStyle).transform("S .6 0");
34
      size = 1;
35
      long = 4;
36
      sTop = "M " + x + " " + y + " l" + (long * 10 / 2) + " 0 l" + (long * 10 / 2) + " 0";
37
      sBottom = "M " + x + " " + (y + 30) + " l" + (long * 10 / 2) + " 0 l" + (long * 10 / 2) + " 0";
38
      pahtTop = this.Snap.path(sTop);
39
      pahtCenter = this.Snap.path("M " + x + " " + (y + 15) + " h" + (long * 10));
40
      pathBottom = this.Snap.path(sBottom);
41
      pahtTop.attr(this.pathStyle);
42
      pahtCenter.attr(this.pathStyle);
43
      pathBottom.attr(this.pathStyle);
44
      xTop = "M " + (x + 5) + " " + y + " l" + (long * 10 / 2 - 5) + " 15 l" + (long * 10 / 2 - 5) + " -15";
45
      xBottom = "M " + (x + 5) + " " + (y + 30) + " l" + (long * 10 / 2 - 5) + " -15 l" + (long * 10 / 2 - 5) + " 15";
46
      toggle = 0;
47
      g = this.Snap.g(r, c, pahtTop, pahtCenter, pathBottom).click(function() {
48
        if (toggle = !toggle) {
49
          pahtCenter.stop().animate({
50
            opacity: 0
51
          }, this.speed * 0.3, this.anim);
52
          c.stop().animate({
53
            transform: "S 1 1"
54
          }, this.speed, this.anim);
55
          pahtTop.stop().animate({
56
            d: xTop
57
          }, this.speed, this.anim);
58
          return pathBottom.stop().animate({
59
            d: xBottom
60
          }, this.speed, this.anim);
61
        } else {
62
          c.stop().animate({
63
            transform: "S .6 0"
64
          }, this.speed * 0.7, this.anim, function() {
65
            return pahtCenter.attr({
66
              opacity: 1
67
            });
68
          });
69
          pahtTop.stop().animate({
70
            d: sTop
71
          }, this.speed, this.anim);
72
          return pathBottom.stop().animate({
73
            d: sBottom
74
          }, this.speed, this.anim);
75
        }
76
      });
77
      g.speed = this.speed;
78
      g.anim = this.anim;
79
      return this;
80
    };
81
 
82
    return TestAnim;
83
 
84
  })();
85
 
86
  colors = ['#3FC2C9', '#33D533', '#6975E0', '#E820CA', '#FF090F', '#D9EE1A', '#A4A4A4', '#4A393D'];
87
 
88
  text = ['linear', 'easeout', 'easein', 'easeinout', 'backin', 'backout', 'elastic', 'bounce'];
89
 
90
  animSpan = [mina.linear, mina.easeout, mina.easein, mina.easeinout, mina.backin, mina.backout, mina.elastic, mina.bounce];
91
 
92
  x = 1;
93
 
94
  for (i = j = 0, len = animSpan.length; j < len; i = ++j) {
95
    anm = animSpan[i];
96
    if (i >= 4) {
97
      x = 2;
98
      ii = i - 3;
99
    } else {
100
      ii = i + 1;
101
    }
102
    new TestAnim(s, anm, colors[i]).draw(110 * ii, 110 * x);
103
    s.text(110 * ii, 110 * x - 22, text[i]);
104
  }
105
 
 

Snap.svg animation test Open & Close & Smash

CSSDeck G+