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
| "use strict";
function _toConsumableArray(e) { return _arrayWithoutHoles(e) || _iterableToArray(e) || _unsupportedIterableToArray(e) || _nonIterableSpread() }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.") }
function _unsupportedIterableToArray(e, r) { if (e) { if ("string" == typeof e) return _arrayLikeToArray(e, r); var t = Object.prototype.toString.call(e).slice(8, -1); return "Object" === t && e.constructor && (t = e.constructor.name), "Map" === t || "Set" === t ? Array.from(e) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(e, r) : void 0 } }
function _iterableToArray(e) { if ("undefined" != typeof Symbol && null != e[Symbol.iterator] || null != e["@@iterator"]) return Array.from(e) }
function _arrayWithoutHoles(e) { if (Array.isArray(e)) return _arrayLikeToArray(e) }
function _arrayLikeToArray(e, r) { (null == r || r > e.length) && (r = e.length); for (var t = 0, a = new Array(r); t < r; t++) a[t] = e[t]; return a }
function _classCallCheck(e, r) { if (!(e instanceof r)) throw new TypeError("Cannot call a class as a function") }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var a = r[t]; a.enumerable = a.enumerable || !1, a.configurable = !0, "value" in a && (a.writable = !0), Object.defineProperty(e, a.key, a) } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), e } var config = { src: "https://f.zhheo.com/Guli/others/open-peeps-sheet.png", rows: 15, cols: 7 }, randomRange = function (e, r) { return e + Math.random() * (r - e) }, randomIndex = function (e) { return 0 | randomRange(0, e.length) }, removeFromArray = function (e, r) { return e.splice(r, 1)[0] }, removeItemFromArray = function (e, r) { return removeFromArray(e, e.indexOf(r)) }, removeRandomFromArray = function (e) { return removeFromArray(e, randomIndex(e)) }, getRandomFromArray = function (e) { return e[0 | randomIndex(e)] }, resetPeep = function (e) { var r, t, a = e.stage, n = e.peep, o = .5 < Math.random() ? 1 : -1, i = 100 - 250 * gsap.parseEase("power2.in")(Math.random()), s = a.height - n.height + i; return 1 == o ? (r = -n.width, t = a.width, n.scaleX = 1) : (r = a.width + n.width, t = 0, n.scaleX = -1), n.x = r, n.y = s, { startX: r, startY: n.anchorY = s, endX: t } }, normalWalk = function (e) { var r = e.peep, t = e.props, a = (t.startX, t.startY), n = t.endX, o = gsap.timeline(); return o.timeScale(randomRange(.5, 1.5)), o.to(r, { duration: 10, x: n, ease: "none" }, 0), o.to(r, { duration: .25, repeat: 40, yoyo: !0, y: a - 10 }, 0), o }, walks = [normalWalk], Peep = function () { function a(e) { var r = e.image, t = e.rect; _classCallCheck(this, a), this.image = r, this.setRect(t), this.x = 0, this.y = 0, this.anchorY = 0, this.scaleX = 1, this.walk = null } return _createClass(a, [{ key: "setRect", value: function (e) { this.rect = e, this.width = e[2], this.height = e[3], this.drawArgs = [this.image].concat(_toConsumableArray(e), [0, 0, this.width, this.height]) } }, { key: "render", value: function (e) { e.save(), e.translate(this.x, this.y), e.scale(this.scaleX, 1), e.draw/Image.apply(e, _toConsumableArray(this.drawArgs)), e.restore() } }]), a }(), img = document.createElement("img"); img.onload = init, img.src = config.src; var canvas = document.querySelector("#canvas"), ctx = canvas.getContext("2d"), stage = { width: 0, height: 0 }, allPeeps = [], availablePeeps = [], crowd = [];
function init() { createPeeps(), resize(), gsap.ticker.add(render), window.addEventListener("resize", resize) }
function createPeeps() { for (var e = config.rows, r = config.cols, t = e * r, a = img.naturalWidth / e, n = img.naturalHeight / r, o = 0; o < t; o++) allPeeps.push(new Peep({ /image: img, rect: [o % e * a, (o / e | 0) * n, a, n] })) }
function resize() { stage.width = canvas.clientWidth, stage.height = canvas.clientHeight, canvas.width = stage.width * devicePixelRatio, canvas.height = stage.height * devicePixelRatio, crowd.forEach(function (e) { e.walk.kill() }), crowd.length = 0, availablePeeps.length = 0, availablePeeps.push.apply(availablePeeps, allPeeps), initCrowd() }
function initCrowd() { for (; availablePeeps.length;) addPeepToCrowd().walk.progress(Math.random()) }
function addPeepToCrowd() { var e = removeRandomFromArray(availablePeeps), r = getRandomFromArray(walks)({ peep: e, props: resetPeep({ peep: e, stage: stage }) }).eventCallback("onComplete", function () { removePeepFromCrowd(e), addPeepToCrowd() }); return e.walk = r, crowd.push(e), crowd.sort(function (e, r) { return e.anchorY - r.anchorY }), e }
function removePeepFromCrowd(e) { removeItemFromArray(crowd, e), availablePeeps.push(e) }
function render() { canvas.width = canvas.width, ctx.save(), ctx.scale(devicePixelRatio, devicePixelRatio), crowd.forEach(function (e) { e.render(ctx) }), ctx.restore() }
|