materialImage.es5.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define(['module'], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory(module);
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory(mod);
  11. global.MaterialImage = mod.exports;
  12. }
  13. })(this, function (module) {
  14. 'use strict';
  15. function _toArray(arr) {
  16. return Array.isArray(arr) ? arr : Array.from(arr);
  17. }
  18. function _classCallCheck(instance, Constructor) {
  19. if (!(instance instanceof Constructor)) {
  20. throw new TypeError("Cannot call a class as a function");
  21. }
  22. }
  23. var _createClass = function () {
  24. function defineProperties(target, props) {
  25. for (var i = 0; i < props.length; i++) {
  26. var descriptor = props[i];
  27. descriptor.enumerable = descriptor.enumerable || false;
  28. descriptor.configurable = true;
  29. if ("value" in descriptor) descriptor.writable = true;
  30. Object.defineProperty(target, descriptor.key, descriptor);
  31. }
  32. }
  33. return function (Constructor, protoProps, staticProps) {
  34. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  35. if (staticProps) defineProperties(Constructor, staticProps);
  36. return Constructor;
  37. };
  38. }();
  39. /*!
  40. * yusen
  41. * https://github.com/yscoder/MaterialImage
  42. * MIT LICENSE
  43. */
  44. var colorPlate = [
  45. '#FFEBEE', '#FFCDD2', '#EF9A9A', '#E57373', '#EF5350', '#F44336', '#E53935', '#D32F2F', '#C62828', '#B71C1C', '#FF8A80',
  46. '#FF5252', '#FF1744', '#D50000', '#FCE4EC', '#F8BBD0', '#F48FB1', '#F06292', '#EC407A', '#E91E63', '#D81B60',
  47. '#C2185B', '#AD1457', '#880E4F', '#FF80AB', '#FF4081', '#F50057', '#C51162', '#F3E5F5', '#E1BEE7', '#CE93D8',
  48. '#BA68C8', '#AB47BC', '#9C27B0', '#8E24AA', '#7B1FA2', '#6A1B9A', '#4A148C', '#EA80FC', '#E040FB', '#D500F9',
  49. '#AA00FF', '#EDE7F6', '#D1C4E9', '#B39DDB', '#9575CD', '#7E57C2', '#673AB7', '#5E35B1', '#512DA8', '#4527A0',
  50. '#311B92', '#B388FF', '#7C4DFF', '#651FFF', '#6200EA', '#E8EAF6', '#C5CAE9', '#9FA8DA', '#7986CB', '#5C6BC0',
  51. '#3F51B5', '#3949AB', '#303F9F', '#283593', '#1A237E', '#8C9EFF', '#536DFE', '#3D5AFE', '#304FFE', '#E3F2FD',
  52. '#BBDEFB', '#90CAF9', '#64B5F6', '#42A5F5', '#2196F3', '#1E88E5', '#1976D2', '#1565C0', '#0D47A1', '#82B1FF',
  53. '#448AFF', '#2979FF', '#2962FF', '#E1F5FE', '#B3E5FC', '#81D4FA', '#4FC3F7', '#29B6F6', '#03A9F4', '#039BE5',
  54. '#0288D1', '#0277BD', '#01579B', '#80D8FF', '#40C4FF', '#00B0FF', '#0091EA', '#E0F2F1', '#B2DFDB', '#80CBC4',
  55. '#4DB6AC', '#26A69A', '#009688', '#00897B', '#00796B', '#00695C', '#004D40', '#A7FFEB', '#64FFDA', '#1DE9B6',
  56. '#00BFA5', '#E0F7FA', '#B2EBF2', '#80DEEA', '#4DD0E1', '#26C6DA', '#00BCD4', '#00ACC1', '#0097A7', '#00838F',
  57. '#006064', '#84FFFF', '#18FFFF', '#00E5FF', '#00B8D4', '#E8F5E9', '#C8E6C9', '#A5D6A7', '#81C784', '#66BB6A',
  58. '#4CAF50', '#43A047', '#388E3C', '#2E7D32', '#1B5E20', '#B9F6CA', '#69F0AE', '#00E676', '#00C853', '#F1F8E9', '#DCEDC8',
  59. '#C5E1A5', '#AED581', '#9CCC65', '#8BC34A', '#7CB342', '#689F38', '#558B2F', '#33691E', '#CCFF90', '#B2FF59', '#76FF03', '#64DD17',
  60. '#F9FBE7', '#F0F4C3', '#E6EE9C', '#DCE775', '#D4E157', '#CDDC39', '#C0CA33', '#AFB42B', '#9E9D24', '#827717', '#F4FF81', '#EEFF41',
  61. '#C6FF00', '#AEEA00', '#FFFDE7', '#FFF9C4', '#FFF59D', '#FFF176', '#FFEE58', '#FFEB3B', '#FDD835', '#FBC02D', '#F9A825', '#F57F17',
  62. '#FFFF8D', '#FFFF00', '#FFEA00', '#FFD600', '#FFF8E1', '#FFECB3', '#FFE082', '#FFD54F', '#FFCA28', '#FFC107', '#FFB300', '#FFA000',
  63. '#FF8F00', '#FF6F00', '#FFE57F', '#FFD740', '#FFC400', '#FFAB00', '#FFF3E0', '#FFE0B2', '#FFCC80', '#FFB74D', '#FFA726', '#FF9800',
  64. '#FB8C00', '#F57C00', '#EF6C00', '#E65100', '#FFD180', '#FFAB40', '#FF9100', '#FF6D00', '#FBE9E7', '#FFCCBC', '#FFAB91', '#FF8A65',
  65. '#FF7043', '#FF5722', '#F4511E', '#E64A19', '#D84315', '#BF360C', '#FF9E80', '#FF6E40', '#FF3D00', '#DD2C00', '#FAFAFA', '#F5F5F5', '#EEEEEE',
  66. '#E0E0E0', '#BDBDBD', '#9E9E9E', '#757575', '#616161', '#424242', '#212121', '#ECEFF1', '#CFD8DC', '#B0BEC5', '#90A4AE', '#78909C', '#607D8B',
  67. '#546E7A', '#455A64', '#37474F', '#263238', '#EFEBE9', '#D7CCC8', '#BCAAA4', '#A1887F', '#8D6E63', '#795548', '#6D4C41', '#5D4037', '#4E342E', '#3E2723'];
  68. var random = function random(min, max) {
  69. return Math.floor(Math.random() * (max - min + 1) + min);
  70. };
  71. var getRandomColor = function getRandomColor(colors) {
  72. colors = colors || colorPlate;
  73. return colors[random(0, colors.length - 1)];
  74. };
  75. var getColors = function getColors(count, colorPlate) {
  76. var colors = [];
  77. for (var i = 0; i <= count;) {
  78. var color = getRandomColor(colorPlate);
  79. if (colors.indexOf(color) === -1) {
  80. colors.push(color);
  81. i += 1;
  82. }
  83. }
  84. return colors;
  85. };
  86. var createCanvas = function createCanvas(width, height) {
  87. var canvas = document.createElement('canvas');
  88. canvas.width = width;
  89. canvas.height = height;
  90. return canvas;
  91. };
  92. var getShape = function getShape() {
  93. return ['rect', 'arc'][random(0, 1)];
  94. };
  95. var Drawer = function () {
  96. function Drawer(canvas) {
  97. _classCallCheck(this, Drawer);
  98. this.ctx = canvas.getContext('2d');
  99. }
  100. _createClass(Drawer, [{
  101. key: 'shadow',
  102. value: function shadow(_ref) {
  103. var blur = _ref.blur,
  104. _ref$x = _ref.x,
  105. x = _ref$x === undefined ? 1 : _ref$x,
  106. _ref$y = _ref.y,
  107. y = _ref$y === undefined ? 1 : _ref$y,
  108. _ref$color = _ref.color,
  109. color = _ref$color === undefined ? 'rgba(0, 0, 0, .3)' : _ref$color;
  110. this.ctx.shadowBlur = blur;
  111. this.ctx.shadowOffsetX = x;
  112. this.ctx.shadowOffsetY = y;
  113. this.ctx.shadowColor = color;
  114. }
  115. }, {
  116. key: 'rect',
  117. value: function rect(_ref2) {
  118. var width = _ref2.width,
  119. height = _ref2.height;
  120. var r = random(10, 360) * Math.PI / 180;
  121. var w = random(100, Math.max(width, height)) * (random(-10, 10) || 1);
  122. var h = random(100, Math.min(width, height)) * (random(-10, 10) || 1);
  123. this.ctx.rotate(r);
  124. this.ctx.rect(0, 0, w, h);
  125. this.ctx.rotate(-r);
  126. }
  127. }, {
  128. key: 'arc',
  129. value: function arc(_ref3) {
  130. var width = _ref3.width,
  131. height = _ref3.height;
  132. var r = random(100, Math.min(width, height) / 2);
  133. this.ctx.arc(0, 0, r, 0, 2 * Math.PI);
  134. }
  135. }, {
  136. key: 'draw',
  137. value: function draw(sharp, option) {
  138. var width = option.width,
  139. height = option.height,
  140. color = option.color;
  141. this.ctx.beginPath();
  142. this.shadow({
  143. blur: 20
  144. });
  145. var x = random(0, width);
  146. var y = random(0, height);
  147. this.ctx.translate(x, y);
  148. this[sharp](option);
  149. this.ctx.closePath();
  150. this.ctx.fillStyle = color;
  151. this.ctx.fill();
  152. this.ctx.translate(-x, -y);
  153. }
  154. }, {
  155. key: 'fill',
  156. value: function fill(color, width, height) {
  157. this.ctx.fillStyle = color;
  158. this.ctx.fillRect(0, 0, width, height);
  159. }
  160. }]);
  161. return Drawer;
  162. }();
  163. var MaterialImage = function () {
  164. function MaterialImage() {
  165. var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  166. _classCallCheck(this, MaterialImage);
  167. var _config$el = config.el,
  168. el = _config$el === undefined ? document.querySelector('body') : _config$el,
  169. _config$debug = config.debug,
  170. debug = _config$debug === undefined ? false : _config$debug,
  171. _config$output = config.output,
  172. output = _config$output === undefined ? 'background' : _config$output,
  173. imageType = config.imageType,
  174. quality = config.quality;
  175. var width = el.clientWidth;
  176. var height = el.clientHeight;
  177. this.colors = config.colors || null;
  178. this.debug = debug;
  179. this.element = el;
  180. this.width = width;
  181. this.height = height;
  182. this.outputType = output;
  183. this.outputOption = {
  184. imageType: imageType,
  185. quality: quality
  186. };
  187. this.canvas = createCanvas(width, height);
  188. this.render();
  189. this.protract();
  190. }
  191. _createClass(MaterialImage, [{
  192. key: 'output',
  193. value: function output() {
  194. if (this.outputType === 'canvas') return;
  195. var dataUrl = this.toDataUrl(this.outputOption);
  196. switch (this.outputType) {
  197. case 'background':
  198. this.element.style.backgroundImage = 'url("' + dataUrl + '")';
  199. break;
  200. case 'image':
  201. this.img.src = dataUrl;
  202. break;
  203. default:
  204. break;
  205. }
  206. }
  207. }, {
  208. key: 'protract',
  209. value: function protract() {
  210. var _this = this;
  211. var count = random(0, 8) + 6;
  212. var _getColors = getColors(count, _this.colors),
  213. _getColors2 = _toArray(_getColors),
  214. background = _getColors2[0],
  215. colors = _getColors2.slice(1);
  216. var drawer = new Drawer(this.canvas);
  217. var width = this.width;
  218. var height = this.height;
  219. drawer.fill(background, width, height);
  220. var i = 0;
  221. if (!this.debug) {
  222. while (i < count) {
  223. drawer.draw(getShape(), {
  224. color: colors[i],
  225. width: width,
  226. height: height
  227. });
  228. i += 1;
  229. }
  230. this.output();
  231. } else {
  232. var debugDraw = function debugDraw() {
  233. drawer.draw(getShape(), {
  234. color: colors[i],
  235. width: width,
  236. height: height
  237. });
  238. _this.output();
  239. i += 1;
  240. if (i < count) {
  241. setTimeout(debugDraw, 1000);
  242. }
  243. };
  244. setTimeout(debugDraw, 1000);
  245. }
  246. }
  247. }, {
  248. key: 'adjust',
  249. value: function adjust() {
  250. var canvas = this.canvas;
  251. var element = this.element;
  252. canvas.width = element.clientWidth;
  253. canvas.height = element.clientHeight;
  254. this.protract();
  255. }
  256. }, {
  257. key: 'toDataUrl',
  258. value: function toDataUrl() {
  259. var imageType = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'jpeg';
  260. var quality = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  261. return this.canvas.toDataURL('image/' + imageType, quality);
  262. }
  263. }, {
  264. key: 'render',
  265. value: function render() {
  266. switch (this.outputType) {
  267. case 'canvas':
  268. this.element.appendChild(this.canvas);
  269. break;
  270. case 'background':
  271. this.element.style.cssText += '\n background-repeat: no-repeat;\n background-size: cover;';
  272. break;
  273. case 'image':
  274. this.img = document.createElement('img');
  275. this.img.style.cssText = 'width: 100%; height: 100%';
  276. this.element.appendChild(this.img);
  277. break;
  278. default:
  279. break;
  280. }
  281. }
  282. }, {
  283. key: 'destroy',
  284. value: function destroy() {
  285. switch (this.outputType) {
  286. case 'canvas':
  287. this.canvas.remove();
  288. break;
  289. case 'background':
  290. {
  291. var cssText = this.element.style.cssText;
  292. this.element.style.cssText = cssText.replace(/background[^;]+;/g, '');
  293. break;
  294. }
  295. case 'image':
  296. this.img.remove();
  297. break;
  298. default:
  299. break;
  300. }
  301. }
  302. }]);
  303. return MaterialImage;
  304. }();
  305. module.exports = MaterialImage;
  306. });