UNPKG

15.1 kBJavaScriptView Raw
1var creaturejs = (function () {
2var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
3
4function commonjsRequire () {
5 throw new Error('Dynamic requires are not currently supported by rollup-plugin-commonjs');
6}
7
8
9
10function createCommonjsModule(fn, module) {
11 return module = { exports: {} }, fn(module, module.exports), module.exports;
12}
13
14var cacheLru = createCommonjsModule(function (module, exports) {
15/*
16** Cache-LRU -- In-Memory Cache with O(1) Operations and LRU Purging Strategy
17** Copyright (c) 2015-2016 Ralf S. Engelschall <rse@engelschall.com>
18**
19** Permission is hereby granted, free of charge, to any person obtaining
20** a copy of this software and associated documentation files (the
21** "Software"), to deal in the Software without restriction, including
22** without limitation the rights to use, copy, modify, merge, publish,
23** distribute, sublicense, and/or sell copies of the Software, and to
24** permit persons to whom the Software is furnished to do so, subject to
25** the following conditions:
26**
27** The above copyright notice and this permission notice shall be included
28** in all copies or substantial portions of the Software.
29**
30** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
33** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
34** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
35** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
36** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37*/
38
39(function(f){{module.exports=f();}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof commonjsRequire=="function"&&commonjsRequire;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 commonjsRequire=="function"&&commonjsRequire;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
40"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var i=0;i<t.length;i++){var n=t[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n);}}return function(t,i,n){return i&&e(t.prototype,i),n&&e(t,n),t}}(),CacheLRU=function(){function e(){return _classCallCheck(this,e),this._index={},this._LRU={newer:null},this._MRU={older:null},this._LRU.newer=this._MRU,this._MRU.older=this._LRU,this._cur=0,this._max=1/0,this._dispose=function(){},this}return _createClass(e,[{key:"limit",value:function(e){var t=this._max;return arguments.length>0&&(this._max=e,this._purge()),t}},{key:"dispose",value:function(e){return this._dispose=e,this}},{key:"length",value:function(){return this._cur}},{key:"keys",value:function(){return this.each(function(e,t){this.push(t);},[])}},{key:"values",value:function(){return this.each(function(e){this.push(e);},[])}},{key:"each",value:function(e,t){arguments<2&&(t=this);for(var i=0,n=this._MRU.older;n!==this._LRU;)e.call(t,n.val,n.key,i++),n=n.older;return t}},{key:"has",value:function(e){return void 0!==this._index[e]}},{key:"peek",value:function(e){var t=this._index[e];if(void 0!==t)return t.expires<Date.now()?void this.del(t.key):t.val}},{key:"touch",value:function(e){var t=this._index[e];if(void 0===t)throw new Error("touch: no such item");return this._promote(t),this}},{key:"get",value:function(e){var t=this._index[e];if(void 0!==t)return t.expires<Date.now()?void this.del(t.key):(this._promote(t),t.val)}},{key:"set",value:function(e,t,i){arguments.length<3&&(i=1/0),i+=Date.now();var n=this._index[e];if(void 0===n)n={older:null,newer:null,key:e,val:t,expires:i},this._index[e]=n,this._attach(n),this._cur++,this._purge();else{var r=n.val;n.val=t,this._promote(n),this._dispose.call(void 0,n.key,r,"set");}return this}},{key:"del",value:function(e){var t=this._index[e];if(void 0===t)throw new Error("del: no such item");return delete this._index[e],this._detach(t),this._cur--,this._dispose.call(void 0,e,t.val,"del"),this}},{key:"clear",value:function(){for(;this._cur>0;)this.del(this._LRU.newer.key);return this}},{key:"_purge",value:function(){for(;this._cur>this._max;)this.del(this._LRU.newer.key);}},{key:"_promote",value:function(e){this._detach(e),this._attach(e);}},{key:"_detach",value:function(e){e.older.newer=e.newer,e.newer.older=e.older,e.older=null,e.newer=null;}},{key:"_attach",value:function(e){e.older=this._MRU.older,e.newer=this._MRU,e.newer.older=e,e.older.newer=e;}}]),e}();module.exports=CacheLRU;
41},{}]},{},[1])(1)
42});
43
44
45
46});
47
48var alea = createCommonjsModule(function (module, exports) {
49(function (root, factory) {
50 {
51 module.exports = factory();
52 }
53}(commonjsGlobal, function () {
54
55 'use strict';
56
57 // From http://baagoe.com/en/RandomMusings/javascript/
58
59 // importState to sync generator states
60 Alea.importState = function(i){
61 var random = new Alea();
62 random.importState(i);
63 return random;
64 };
65
66 return Alea;
67
68 function Alea() {
69 return (function(args) {
70 // Johannes Baagøe <baagoe@baagoe.com>, 2010
71 var s0 = 0;
72 var s1 = 0;
73 var s2 = 0;
74 var c = 1;
75
76 if (args.length == 0) {
77 args = [+new Date];
78 }
79 var mash = Mash();
80 s0 = mash(' ');
81 s1 = mash(' ');
82 s2 = mash(' ');
83
84 for (var i = 0; i < args.length; i++) {
85 s0 -= mash(args[i]);
86 if (s0 < 0) {
87 s0 += 1;
88 }
89 s1 -= mash(args[i]);
90 if (s1 < 0) {
91 s1 += 1;
92 }
93 s2 -= mash(args[i]);
94 if (s2 < 0) {
95 s2 += 1;
96 }
97 }
98 mash = null;
99
100 var random = function() {
101 var t = 2091639 * s0 + c * 2.3283064365386963e-10; // 2^-32
102 s0 = s1;
103 s1 = s2;
104 return s2 = t - (c = t | 0);
105 };
106 random.uint32 = function() {
107 return random() * 0x100000000; // 2^32
108 };
109 random.fract53 = function() {
110 return random() +
111 (random() * 0x200000 | 0) * 1.1102230246251565e-16; // 2^-53
112 };
113 random.version = 'Alea 0.9';
114 random.args = args;
115
116 // my own additions to sync state between two generators
117 random.exportState = function(){
118 return [s0, s1, s2, c];
119 };
120 random.importState = function(i){
121 s0 = +i[0] || 0;
122 s1 = +i[1] || 0;
123 s2 = +i[2] || 0;
124 c = +i[3] || 0;
125 };
126
127 return random;
128
129 } (Array.prototype.slice.call(arguments)));
130 }
131
132 function Mash() {
133 var n = 0xefc8249d;
134
135 var mash = function(data) {
136 data = data.toString();
137 for (var i = 0; i < data.length; i++) {
138 n += data.charCodeAt(i);
139 var h = 0.02519603282416938 * n;
140 n = h >>> 0;
141 h -= n;
142 h *= n;
143 n = h >>> 0;
144 h -= n;
145 n += h * 0x100000000; // 2^32
146 }
147 return (n >>> 0) * 2.3283064365386963e-10; // 2^-32
148 };
149
150 mash.version = 'Mash 0.9';
151 return mash;
152 }
153}));
154});
155
156const cache = new cacheLru({
157 max: 128,
158});
159
160const SIZE = 12;
161const _2_32 = Math.pow(2, 32);
162
163function _cloneCanvas(canvas) {
164 const result = document.createElement('canvas');
165 result.width = canvas.width;
166 result.height = canvas.height;
167
168 const ctx = result.getContext('2d');
169 ctx.drawImage(canvas, 0, 0);
170
171 return result;
172}
173function _cloneEntry(entry) {
174 if (typeof entry === 'string') {
175 return entry;
176 } else {
177 return entry.map(_cloneCanvas);
178 }
179}
180
181function makeCreature(seed, {single = false}) {
182 seed = seed || String(Math.random());
183
184 const key = seed + ':' + single;
185 const entry = cache.get(key);
186 if (entry) {
187 return _cloneEntry(entry);
188 } else {
189 const entry = (() => {
190 const rng = new alea(seed);
191
192 function _setPixel(ctx, x, y, c) {
193 let {pixelImageData} = ctx;
194 if (!pixelImageData) {
195 pixelImageData = ctx.createImageData(1,1);
196 ctx.pixelImageData = pixelImageData;
197 }
198 const {data} = pixelImageData;
199 data[0] = (c >> (8 * 2)) & 0xFF;
200 data[1] = (c >> (8 * 1)) & 0xFF;
201 data[2] = (c >> (8 * 0)) & 0xFF;
202 data[3] = c ? 255 : 0;
203 ctx.putImageData(pixelImageData, x, y);
204 }
205
206 function _getPixel(ctx, x, y) {
207 const pixelImageData = ctx.getImageData(x, y, 1, 1);
208 const {data} = pixelImageData;
209 const r = data[0];
210 const g = data[1];
211 const b = data[2];
212 const a = data[3];
213 return (a << (8 * 3)) | (r << (8 * 2)) | (g << (8 * 1)) | (b << (8 * 0));
214 }
215
216 function getColor32(alpha, red, green, blue) {
217 return alpha << 24 | red << 16 | green << 8 | blue;
218 }
219
220 function getRGB(color) {
221 const alpha = color >>> 24;
222 const red = color >> 16 & 255;
223 const green = color >> 8 & 255;
224 const blue = color & 255;
225 return {
226 alpha,
227 red,
228 green,
229 blue,
230 };
231 }
232
233 function HSVtoRGB(h, s, v, alpha = 255) {
234 let result = 0;
235 if (s === 0) {
236 result = getColor32(alpha,v * 255,v * 255,v * 255);
237 } else {
238 h = h / 60;
239
240 const intH = Math.floor(h);
241 let f = h - intH;
242 let p = v * (1 - s);
243 let q = v * (1 - s * f);
244 let t = v * (1 - s * (1 - f));
245 switch (intH) {
246 case 0:
247 result = getColor32(alpha,v * 255,t * 255,p * 255);
248 break;
249 case 1:
250 result = getColor32(alpha,q * 255,v * 255,p * 255);
251 break;
252 case 2:
253 result = getColor32(alpha,p * 255,v * 255,t * 255);
254 break;
255 case 3:
256 result = getColor32(alpha,p * 255,q * 255,v * 255);
257 break;
258 case 4:
259 result = getColor32(alpha,t * 255,p * 255,v * 255);
260 break;
261 case 5:
262 result = getColor32(alpha,v * 255,p * 255,q * 255);
263 break;
264 default:
265 throw new Error('FlxColor Error: HSVtoRGB : Unknown color');
266 }
267 }
268 return result;
269 }
270
271 function RGBtoHSV(color) {
272 let hue = NaN;
273 let saturation = NaN;
274
275 const rgb = getRGB(color);
276 const red = rgb.red / 255;
277 const green = rgb.green / 255;
278 const blue = rgb.blue / 255;
279
280 const min = Math.min(red,green,blue);
281 const max = Math.max(red,green,blue);
282 const delta = max - min;
283 const lightness = (max + min) / 2;
284
285 if (delta === 0) {
286 hue = 0;
287 saturation = 0;
288 } else {
289 if(lightness < 0.5) {
290 saturation = delta / (max + min);
291 } else {
292 saturation = delta / (2 - max - min);
293 }
294 let delta_r = ((max - red) / 6 + delta / 2) / delta;
295 let delta_g = ((max - green) / 6 + delta / 2) / delta;
296 let delta_b = ((max - blue) / 6 + delta / 2) / delta;
297 if (red === max) {
298 hue = delta_b - delta_g;
299 } else if (green === max) {
300 hue = 1 / 3 + delta_r - delta_b;
301 } else if (blue === max) {
302 hue = 2 / 3 + delta_g - delta_r;
303 }
304 if (hue < 0) {
305 hue = hue + 1;
306 }
307 if (hue > 1) {
308 hue = hue - 1;
309 }
310 }
311 hue = hue * 360;
312 hue = Math.round(hue);
313
314 return {
315 hue,
316 saturation,
317 lightness,
318 value: lightness,
319 };
320 }
321
322 function mirror(ctx) {
323 const w = SIZE;
324 const h = SIZE;
325 for(let iY = 0; iY < h; iY++) {
326 for(let iX = w / 2; iX < w; iX++) {
327 _setPixel(ctx, iX, iY, _getPixel(ctx, w - 1 - iX, iY));
328 }
329 }
330 }
331
332 function renderMainFrame(ctx) {
333 const w = SIZE;
334 const h = SIZE;
335 const color = Math.floor(rng() * _2_32);
336
337 let show = color;
338 const halfw = (w - 1) / 2;
339 const halfh = (h - 1) / 2;
340 const radius = Math.min(Math.sqrt(Math.pow(halfw,2)),Math.sqrt(Math.pow(halfh,2)));
341 const c = RGBtoHSV(show);
342
343 for (let i = 0; i <= halfw; i++) {
344 for (let j = 0; j < h; j++) {
345 let dist = Math.min(1,Math.max(0,Math.sqrt(Math.pow(i - halfw,2) + Math.pow(j - halfh,2)) / radius));
346 c.hue = Math.max(0,Math.min(359,c.hue + Math.round((rng() * 2 - 1) * 359 * 0.1)));
347 c.saturation = Math.max(0,Math.min(1,c.saturation + (rng() * 2 - 1) * 0.1));
348 c.value = 1 - dist;
349 show = HSVtoRGB(c.hue,c.saturation,c.value);
350 if (rng() >= dist) {
351 _setPixel(ctx, i, j, show);
352 }
353 }
354 }
355
356 mirror(ctx);
357 }
358
359 function renderAltFrame(ctx) {
360 let animChance = 1;
361
362 const w = SIZE;
363 const h = SIZE;
364 const halfw = (w - 1) / 2;
365
366 for (let i = 1; i < halfw; i++) {
367 for (let j = 1; j < h - 1; j++) {
368 if (rng() <= animChance && _getPixel(ctx, i, j) !== _getPixel(ctx, i - 1, j)) {
369 const centerPixel = _getPixel(ctx, i, j);
370 const leftPixel = _getPixel(ctx, i - 1, j);
371 _setPixel(ctx, i - 1, j, centerPixel);
372 _setPixel(ctx, i, j, leftPixel);
373 i++;
374 j++;
375 } else if (rng() <= animChance && _getPixel(ctx, i, j) !== _getPixel(ctx, i, j - 1)) {
376 const centerPixel = _getPixel(ctx, i, j);
377 const topPixel = _getPixel(ctx, i, j - 1);
378 _setPixel(ctx, i, j - 1, centerPixel);
379 _setPixel(ctx, i, j, topPixel);
380 i++;
381 j++;
382 }
383 }
384 }
385
386 mirror(ctx);
387 }
388
389 const canvas = document.createElement('canvas');
390 canvas.width = SIZE;
391 canvas.height = SIZE;
392 canvas.style.width = 64;
393 canvas.style.height = 64;
394 canvas.style.imageRendering = 'pixelated';
395 const ctx = canvas.getContext('2d');
396
397 renderMainFrame(ctx);
398 const mainFrame = _cloneCanvas(canvas);
399
400 if (!single) {
401 renderAltFrame(ctx);
402 const altFrame = canvas;
403
404 return [mainFrame, altFrame];
405 } else {
406 return canvas.toDataURL('image/png');
407 }
408 })();
409
410 cache.set(key, entry);
411
412 return _cloneEntry(entry);
413 }
414}
415const makeAnimatedCreature = seed => makeCreature(seed, {single: false});
416const makeStaticCreature = seed => makeCreature(seed, {single: true});
417
418var index = {
419 makeAnimatedCreature,
420 makeStaticCreature,
421};
422
423return index;
424
425}());