JSDM

HTML

 
1
<!--
2
3
   Gears by @liabru
4
5
   See the full Fractal Gears experiment:
6
   http://brm.io/gears/
7
8
   More of the source at:
9
   https://github.com/liabru/gears-d3-js
10
11
   Uses the awesome d3.js by @mbostock
12
13
-->
14
15
<div class="container gears-d3-canvas"></div>

CSS

xxxxxxxxxx
21
 
1
body { 
2
  background: #033649;
3
  padding: 0;
4
  margin: 0;
5
}
6
7
.container {
8
  max-width: 1280px;
9
  margin: 0 auto;
10
}
11
12
.gears-d3-canvas svg {
13
  max-width: 100%;
14
  max-height: 768px;
15
}
16
17
.style-0 path { fill: #00A0B0; }
18
.style-1 path { fill: #6A4A3C; }
19
.style-2 path { fill: #CC333F; }
20
.style-3 path { fill: #EB6841; }
21
.style-4 path { fill: #EDC951; }
? ?
? ?
必须是有效的URL
+ 添加另一个资源

JS

xxxxxxxxxx
122
 
1
/**
2
* gear-toy.js
3
* http://brm.io/gears-d3-js/
4
* License: MIT
5
*/
6
7
var _svg,
8
    _allGears = [],
9
    _randomiseInterval,
10
    _canvasWidth = 1280,
11
    _canvasHeight = 768,
12
    _xOffset = _canvasWidth * 0.5,
13
    _yOffset = _canvasHeight * 0.4,
14
    _gearFactors = [64, 64, 32, 48, 48, 96, 112, 256],
15
    _gearStyles = ['style-0', 'style-1', 'style-2', 'style-3', 'style-4'],
16
    _autoShuffle = true;
17
18
var _options = {
19
  radius: 16,
20
  holeRadius: 0.4,
21
  transition: true,
22
  speed: 0.01,
23
  autoShuffle: true,
24
  number: 20,
25
  addendum: 8,
26
  dedendum: 3,
27
  thickness: 0.7,
28
  profileSlope: 0.5
29
};
30
31
var main = function() {
32
33
  // set up our d3 svg element
34
  _svg = d3.select('.gears-d3-canvas')
35
  .append('svg')
36
  .attr('viewBox', '0 0 ' + _canvasWidth + ' ' + _canvasHeight)
37
  .attr('preserveAspectRatio', 'xMinYMin slice');
38
39
  // generate and randomise scene
40
  _generateScene(_options);
41
  _randomiseScene(false);
42
43
  // start a timer to randomise every few secs
44
  _randomiseInterval = setInterval(function() {
45
    if (_autoShuffle)
46
      _randomiseScene(true);
47
  }, 4000);
48
  
49
  setTimeout(function() {
50
    _randomiseScene(true);
51
  }, 100);
52
53
  // start the d3 animation timer
54
  d3.timer(function () {
55
    _svg.selectAll('.gear-path')
56
    .attr('transform', function (d) {
57
      d.angle += d.speed;
58
      return 'rotate(' + d.angle * (180 / Math.PI) + ')';
59
    });
60
  });
61
};
62
63
var _generateScene = function(options) {
64
  var holeRadius,
65
      teeth,
66
      radius,
67
      factor,
68
      newGear,
69
      innerRadius;
70
71
  _gearStyles = Gear.Utility.arrayShuffle(_gearStyles);
72
73
  for (var i = 0; i < options.number; i++) {
74
    factor = _gearFactors[i % _gearFactors.length];
75
    radius = factor / 2;
76
    teeth = radius / 4;
77
    innerRadius = radius - options.addendum - options.dedendum;
78
    holeRadius = factor > 96 ? innerRadius * 0.5 + innerRadius * 0.5 * options.holeRadius : innerRadius * options.holeRadius;
79
80
    _allGears.push(newGear = Gear.create(_svg, { 
81
      radius: radius, 
82
      teeth: teeth, 
83
      x: 0, 
84
      y: 0, 
85
      holeRadius: holeRadius,
86
      addendum: options.addendum,
87
      dedendum: options.dedendum,
88
      thickness: options.thickness,
89
      profileSlope: options.profileSlope
90
    }));
91
92
    newGear.classed(_gearStyles[i % _gearStyles.length], true);
93
  }
94
};
95
96
var _randomiseScene = function(transition) {
97
  _allGears = Gear.Utility.arrayShuffle(_allGears);
98
  Gear.randomArrange(_allGears, _xOffset, _yOffset);
99
  Gear.setPower(_allGears[0], 0.01);
100
  Gear.updateGears(_allGears);
101
102
  _svg.selectAll('.gear')
103
  .each(function(d, i) {
104
    if (transition) {
105
      d3.select(this)
106
      .transition()
107
      .ease('elastic')
108
      .delay(i * 80 + Math.random() * 80)
109
      .duration(1500)
110
      .attr('transform', function(d) {
111
        return 'translate(' + [ d.x, d.y ] + ')';
112
      });
113
    } else {
114
      d3.select(this)
115
      .attr('transform', function(d) {
116
        return 'translate(' + [ d.x, d.y ] + ')';
117
      });
118
    }
119
  });
120
};
121
122
main();
必须是有效的URL
请输入有效的URL
+ 添加另一个资源
Close

文件管理 点击文件查看URL

图片

  1. 暂无文件

CSS

  1. 暂无文件

JavaScript

  1. 暂无文件

其他

  1. 暂无文件
拖动文件到上面的区域或者:
加载中 ..................