Advertisement
Guest User

Star

a guest
Jun 17th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}const randomInRange = (min, max) =>
  2. Math.floor(Math.random() * (max - min)) + min;
  3.  
  4. const VELOCITY_BASE = 1;
  5. const VELOCITY_LIMIT = 2;
  6. const BASE_SIZE = 10;
  7. const BASE_SCALE = 0.1;
  8. const ALPHA_STEP = 0.02;
  9. const ALPHA_LIMIT = 1;
  10. const SCALE_STEP = 0.001;
  11.  
  12. class Star {
  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. constructor() {_defineProperty(this, "canvas", document.createElement('canvas'));_defineProperty(this, "context", this.canvas.getContext('2d'));_defineProperty(this, "STATE", { active: false, characteristics: { alpha: Math.random(), angle: randomInRange(0, 360) * (Math.PI / 180), size: BASE_SIZE, scale: BASE_SCALE } });_defineProperty(this, "activate", () => {this.STATE = { ...this.STATE, active: true };});_defineProperty(this, "reset", start => {const angle = randomInRange(0, 360) * (Math.PI / 180);const alpha = Math.random();const velocity = VELOCITY_BASE;const travelled = randomInRange(velocity === 0 ? window.innerWidth * 0.25 : window.innerWidth * 0.1, start ? start : innerWidth);const x = Math.floor(Math.sin(angle) * travelled) + innerWidth / 2;const y = Math.floor(Math.cos(angle) * travelled) + innerHeight / 2;const active = travelled > window.innerWidth * 0.1 || velocity === 0 ? true : false;this.STATE = { active, characteristics: { ...this.STATE.characteristics, alpha, angle, scale: BASE_SCALE, velocity }, position: { x, y }, distance: { travelled } };});_defineProperty(this, "render", () => {const { alpha, size } = this.STATE.characteristics;this.canvas.height = this.canvas.width = size;this.context.globalAlpha = alpha;this.context.fillStyle = '#eeeeee';this.context.beginPath();this.context.arc(size / 2, size / 2, size / 2, 0, 2 * Math.PI);this.context.fill();});_defineProperty(this, "update", jumping => {const x = Math.floor(Math.sin(this.STATE.characteristics.angle) * this.STATE.distance.travelled) + innerWidth / 2;const y = Math.floor(Math.cos(this.STATE.characteristics.angle) * this.STATE.distance.travelled) + innerHeight / 2;const velocity = jumping ? VELOCITY_LIMIT : this.STATE.characteristics.velocity;this.STATE = { ...this.STATE, characteristics: { ...this.STATE.characteristics, scale: velocity ? this.STATE.characteristics.scale + SCALE_STEP : this.STATE.characteristics.scale, velocity }, position: { x, y }, distance: { ...this.STATE.distance, travelled: this.STATE.distance.travelled + velocity } };});
  97. this.reset();
  98. this.canvas.height = this.canvas.width = this.STATE.characteristics.size;
  99. this.render();
  100. }}
  101.  
  102.  
  103. class HyperSpace {
  104. constructor(options) {_defineProperty(this, "STATE",
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. {
  112. jumping: false,
  113. starPool: [] });_defineProperty(this, "canvas",
  114.  
  115.  
  116. document.createElement('canvas'));_defineProperty(this, "context",
  117. this.canvas.getContext('2d'));_defineProperty(this, "render",
  118. () => {
  119. const { context } = this;
  120. context.save();
  121. if (!this.STATE.jumping && !this.STATE.toBeCleared)
  122. context.clearRect(0, 0, window.innerWidth, window.innerHeight);
  123.  
  124. // Should we activate some items?
  125. if (!this.STATE.jumping && this.STATE.toBeCleared) {
  126. // Do something else instead of updating items
  127. if (this.STATE.blanketOpacity >= ALPHA_LIMIT) {
  128. this.STATE = {
  129. ...this.STATE,
  130. flashed: true,
  131. toBeCleared: false,
  132. jumping: false,
  133. starPool: this.generateStars(this.options.limit) };
  134.  
  135. } else if (!this.STATE.flashed) {
  136. context.fillStyle = `rgba(255, 255, 255, ${this.STATE.blanketOpacity += ALPHA_STEP * 5})`;
  137. context.fillRect(0, 0, innerWidth, innerHeight);
  138. }
  139.  
  140. } else {
  141. if (
  142. Math.random() > 0.75 &&
  143. this.STATE.starPool.filter(s => s.STATE.active === false).length > 0 &&
  144. !this.STATE.jumping)
  145. {
  146. this.STATE.starPool.filter(s => s.STATE.active === false)[0].activate();
  147. }
  148.  
  149. for (const star of this.STATE.starPool) {
  150. // Check if it's within the bounds. If it isn't then it needs to be reset and deactivated
  151. if (
  152. star.STATE.position.x < 0 ||
  153. star.STATE.position.x > innerWidth ||
  154. star.STATE.position.y < 0 ||
  155. star.STATE.position.y > innerHeight && star.STATE.active)
  156. {
  157. star.reset(this.STATE.jumping ? undefined : 50);
  158. } else if (star.STATE.active) {
  159. star.update(this.STATE.jumping);
  160. context.drawImage(
  161. star.canvas,
  162. star.STATE.position.x - star.STATE.characteristics.size / 2,
  163. star.STATE.position.y - star.STATE.characteristics.size / 2,
  164. star.STATE.characteristics.size * star.STATE.characteristics.scale,
  165. star.STATE.characteristics.size * star.STATE.characteristics.scale);
  166.  
  167. }
  168. }
  169. if (this.STATE.blanketOpacity >= 0) {
  170. context.fillStyle = `rgba(0, 0, 0, ${this.STATE.blanketOpacity -= ALPHA_STEP})`;
  171. context.fillRect(0, 0, innerWidth, innerHeight);
  172. }
  173. }
  174. requestAnimationFrame(this.render);
  175. });_defineProperty(this, "jump",
  176. () => {
  177. this.STATE = {
  178. ...this.STATE,
  179. jumping: true,
  180. toBeCleared: true,
  181. flashed: false,
  182. blanketOpacity: 0,
  183. initiated: new Date().getTime() };
  184.  
  185. });_defineProperty(this, "slow",
  186. () => {
  187. // At this stage we need to clear the jets and then reset all the stars to new position
  188. const stop = new Date().getTime();
  189. if (stop - this.STATE.initiated >= 2000) {
  190. this.STATE.jumping = false;
  191. } else {
  192. this.STATE = {
  193. ...this.STATE,
  194. jumping: false,
  195. toBeCleared: false };
  196.  
  197. }
  198. });_defineProperty(this, "bindAction",
  199. () => {
  200. this.canvas.addEventListener('mousedown', this.jump);
  201. this.canvas.addEventListener('touchstart', this.jump);
  202. this.canvas.addEventListener('mouseup', this.slow);
  203. this.canvas.addEventListener('touchend', this.slow);
  204. });_defineProperty(this, "generateStars",
  205. amount => {
  206. const result = [];
  207. for (let s = 0; s < amount; s++) {
  208. result.push(new Star());
  209. }
  210. return result;
  211. });_defineProperty(this, "reset",
  212. () => {
  213. this.STATE = {};
  214. this.setup();
  215. });_defineProperty(this, "setup",
  216. () => {
  217. this.canvas.height = innerHeight;
  218. this.canvas.width = innerWidth;
  219. this.STATE.starPool = this.generateStars(this.options.limit);
  220. });this.options = options;this.setup();document.body.appendChild(this.canvas);this.render();this.bindAction();}}
  221.  
  222. const myHyperspace = new HyperSpace({
  223. limit: 1000 });
  224.  
  225. window.addEventListener(
  226. 'resize',
  227. _.debounce(() => {
  228. myHyperspace.reset();
  229. }, 250));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement