|
| 1 | +(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ |
| 2 | +"use strict"; |
| 3 | + |
| 4 | +var _core = _interopRequireDefault(require("@keyframes/core")); |
| 5 | + |
| 6 | +var _keyframes = _interopRequireDefault(require("../src/keyframes.spritesheet")); |
| 7 | + |
| 8 | +function _interopRequireDefault(obj) { |
| 9 | + return obj && obj.__esModule ? obj : { |
| 10 | + default: obj |
| 11 | + }; |
| 12 | +} |
| 13 | + |
| 14 | +_core.default.plugin(_keyframes.default); |
| 15 | + |
| 16 | +window.onload = function () { |
| 17 | + var ss = _core.default.spriteSheet({ |
| 18 | + name: 'gem', |
| 19 | + rows: 6, |
| 20 | + cols: 7, |
| 21 | + width: 210, |
| 22 | + height: 180, |
| 23 | + offsetX: 0, |
| 24 | + offsetY: 0, |
| 25 | + count: 39 |
| 26 | + }); |
| 27 | + |
| 28 | + var container = new _core.default(document.querySelectorAll('.spriteContainer')[0]); |
| 29 | + container.playSpriteSheet('gem', '3s', -1, ss); |
| 30 | +}; |
| 31 | + |
| 32 | +},{"../src/keyframes.spritesheet":3,"@keyframes/core":2}],2:[function(require,module,exports){ |
| 33 | +"use strict"; |
| 34 | + |
| 35 | +Object.defineProperty(exports, "__esModule", { |
| 36 | + value: true |
| 37 | +}); |
| 38 | +exports.default = void 0; |
| 39 | + |
| 40 | +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } |
| 41 | + |
| 42 | +function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } |
| 43 | + |
| 44 | +function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } |
| 45 | + |
| 46 | +var Keyframes = |
| 47 | +/*#__PURE__*/ |
| 48 | +function () { |
| 49 | + function Keyframes(elem) { |
| 50 | + _classCallCheck(this, Keyframes); |
| 51 | + |
| 52 | + this.elem = elem; |
| 53 | + } |
| 54 | + |
| 55 | + _createClass(Keyframes, [{ |
| 56 | + key: "isSupported", |
| 57 | + value: function isSupported() { |
| 58 | + return document.body.style.animationName !== undefined; |
| 59 | + } |
| 60 | + }, { |
| 61 | + key: "reset", |
| 62 | + value: function reset(callback) { |
| 63 | + this.elem.style.animationPlayState = 'running'; |
| 64 | + this.elem.style.animation = 'none'; |
| 65 | + |
| 66 | + if (callback) { |
| 67 | + setTimeout(callback, 0); |
| 68 | + } |
| 69 | + } |
| 70 | + }, { |
| 71 | + key: "pause", |
| 72 | + value: function pause() { |
| 73 | + this.elem.style.animationPlayState = 'paused'; |
| 74 | + } |
| 75 | + }, { |
| 76 | + key: "resume", |
| 77 | + value: function resume() { |
| 78 | + this.elem.style.animationPlayState = 'running'; |
| 79 | + } |
| 80 | + }, { |
| 81 | + key: "play", |
| 82 | + value: function play(frameOptions, callback) { |
| 83 | + var _this = this; |
| 84 | + |
| 85 | + var animObjToStr = function animObjToStr(obj) { |
| 86 | + var newObj = Object.assign({}, { |
| 87 | + duration: '0s', |
| 88 | + timingFunction: 'ease', |
| 89 | + delay: '0s', |
| 90 | + iterationCount: 1, |
| 91 | + direction: 'normal', |
| 92 | + fillMode: 'forwards' |
| 93 | + }, obj); |
| 94 | + return [newObj.name, newObj.duration, newObj.timingFunction, newObj.delay, newObj.iterationCount, newObj.direction, newObj.fillMode].join(' '); |
| 95 | + }; |
| 96 | + |
| 97 | + var animationcss = ''; |
| 98 | + |
| 99 | + if (frameOptions.constructor === Array) { |
| 100 | + var frameOptionsStrings = []; |
| 101 | + |
| 102 | + for (var i = 0; i < frameOptions.length; i += 1) { |
| 103 | + frameOptionsStrings.push(typeof frameOptions[i] === 'string' ? frameOptions[i] : animObjToStr(frameOptions[i])); |
| 104 | + } |
| 105 | + |
| 106 | + animationcss = frameOptionsStrings.join(', '); |
| 107 | + } else if (typeof frameOptions === 'string') { |
| 108 | + animationcss = frameOptions; |
| 109 | + } else { |
| 110 | + animationcss = animObjToStr(frameOptions); |
| 111 | + } |
| 112 | + |
| 113 | + var addEvent = function addEvent(type, eventCallback) { |
| 114 | + var listenerName = "".concat(type, "Listener"); |
| 115 | + |
| 116 | + _this.elem.removeEventListener(type, _this[listenerName]); |
| 117 | + |
| 118 | + _this[listenerName] = eventCallback; |
| 119 | + |
| 120 | + _this.elem.addEventListener(type, _this[listenerName]); |
| 121 | + }; |
| 122 | + |
| 123 | + this.elem.style.animationPlayState = 'running'; |
| 124 | + this.elem.style.animation = animationcss; |
| 125 | + this.frameOptions = frameOptions; |
| 126 | + addEvent('animationiteration', callback || frameOptions.complete); |
| 127 | + addEvent('animationend', callback || frameOptions.complete); |
| 128 | + } |
| 129 | + }], [{ |
| 130 | + key: "createKeyframeTag", |
| 131 | + value: function createKeyframeTag(id, css) { |
| 132 | + var elem = document.createElement('style'); |
| 133 | + elem.innerHTML = css; |
| 134 | + elem.setAttribute('class', 'keyframe-style'); |
| 135 | + elem.setAttribute('id', id); |
| 136 | + elem.setAttribute('type', 'text/css'); |
| 137 | + document.getElementsByTagName('head')[0].appendChild(elem); |
| 138 | + } |
| 139 | + }, { |
| 140 | + key: "generate", |
| 141 | + value: function generate(frameData) { |
| 142 | + var frameName = frameData.name || ''; |
| 143 | + var css = "@keyframes ".concat(frameName, " {"); |
| 144 | + |
| 145 | + for (var key in frameData) { |
| 146 | + if (key !== 'name' && key !== 'media' && key !== 'complete') { |
| 147 | + css += "".concat(key, " {"); |
| 148 | + |
| 149 | + for (var property in frameData[key]) { |
| 150 | + css += "".concat(property, ":").concat(frameData[key][property], ";"); |
| 151 | + } |
| 152 | + |
| 153 | + css += '}'; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + if (frameData.media) { |
| 158 | + css = "@media ".concat(frameData.media, "{").concat(css, "}"); |
| 159 | + } |
| 160 | + |
| 161 | + var frameStyle = document.getElementById(frameName); |
| 162 | + |
| 163 | + if (frameStyle) { |
| 164 | + frameStyle.innerHTML = css; |
| 165 | + } else { |
| 166 | + Keyframes.createKeyframeTag(frameName, css); |
| 167 | + } |
| 168 | + } |
| 169 | + }, { |
| 170 | + key: "define", |
| 171 | + value: function define(frameData) { |
| 172 | + if (frameData.length) { |
| 173 | + for (var i = 0; i < frameData.length; i += 1) { |
| 174 | + this.generate(frameData[i]); |
| 175 | + } |
| 176 | + } else { |
| 177 | + this.generate(frameData); |
| 178 | + } |
| 179 | + } |
| 180 | + }, { |
| 181 | + key: "plugin", |
| 182 | + value: function plugin(pluginFunc) { |
| 183 | + pluginFunc(Keyframes); |
| 184 | + } |
| 185 | + }]); |
| 186 | + |
| 187 | + return Keyframes; |
| 188 | +}(); |
| 189 | + |
| 190 | +exports.default = Keyframes; |
| 191 | + |
| 192 | +},{}],3:[function(require,module,exports){ |
| 193 | +"use strict"; |
| 194 | + |
| 195 | +Object.defineProperty(exports, "__esModule", { |
| 196 | + value: true |
| 197 | +}); |
| 198 | +exports.default = void 0; |
| 199 | + |
| 200 | +var _default = function _default(Keyframes) { |
| 201 | + Keyframes.spriteSheets = {}; |
| 202 | + |
| 203 | + Keyframes.spriteSheet = function (opts) { |
| 204 | + var defaults = { |
| 205 | + name: '', |
| 206 | + rows: 1, |
| 207 | + cols: 1, |
| 208 | + height: 0, |
| 209 | + width: 0, |
| 210 | + offsetX: 0, |
| 211 | + offsetY: 0, |
| 212 | + count: opts.rows * opts.cols, |
| 213 | + spriteWidth: opts.width / opts.cols, |
| 214 | + spriteHeight: opts.height / opts.rows, |
| 215 | + loop: true |
| 216 | + }; |
| 217 | + opts = Object.assign({}, defaults, opts); |
| 218 | + Keyframes.spriteSheets[opts.name] = opts; |
| 219 | + var spriteStep = 100 / opts.count; |
| 220 | + var spriteFrames = {}; |
| 221 | + var x = opts.offsetX; |
| 222 | + var y = opts.offsetY; |
| 223 | + |
| 224 | + for (var i = 0; i < opts.count; i += 1) { |
| 225 | + spriteFrames["".concat(Math.round(spriteStep * i), "%")] = { |
| 226 | + 'background-position': "-".concat(x, "px -").concat(y, "px") |
| 227 | + }; |
| 228 | + |
| 229 | + if (x >= opts.width - opts.spriteWidth) { |
| 230 | + y += opts.spriteHeight; |
| 231 | + x = opts.offsetX; |
| 232 | + } else { |
| 233 | + x += opts.spriteWidth; |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + return Object.assign({}, { |
| 238 | + name: opts.name |
| 239 | + }, spriteFrames); |
| 240 | + }; |
| 241 | + |
| 242 | + Keyframes.prototype.playSpriteSheet = function (name, time, loops, keyframes) { |
| 243 | + if (keyframes) { |
| 244 | + Keyframes.define(keyframes); |
| 245 | + } |
| 246 | + |
| 247 | + if (loops) { |
| 248 | + if (loops < 0) { |
| 249 | + loops = 'infinite'; |
| 250 | + } |
| 251 | + } else { |
| 252 | + loops = 'infinite'; |
| 253 | + } |
| 254 | + |
| 255 | + var animate = "".concat(name, " ").concat(time, " steps(1) ").concat(loops); |
| 256 | + var existingAnimation = this.elem.style.animation; |
| 257 | + |
| 258 | + if (existingAnimation && existingAnimation.split(' ')[0] !== 'none') { |
| 259 | + animate = "".concat(existingAnimation, ", ").concat(animate); |
| 260 | + } |
| 261 | + |
| 262 | + this.elem.style.animation = animate; |
| 263 | + return this; |
| 264 | + }; |
| 265 | +}; |
| 266 | + |
| 267 | +exports.default = _default; |
| 268 | + |
| 269 | +},{}]},{},[1]); |
0 commit comments