-1

I have an Angular 7 app with a home page containing a large coloured block (enough to fill the page) at the top with a header and some images. I want to put some lava effect animations into the background similar to this

code in case link is removed:

HTML:

<canvas id="lamp-anim" class="lamp-anim" width="1034" height="613"></canvas>

CSS:

body {
  background: #f857a6;  /* fallback for old browsers */
  background: -webkit-linear-gradient(to top, #ff5858, #f857a6);  /* Chrome 
  10-25, Safari 5.1-6 */
  background: linear-gradient(to top, #ff5858, #f857a6); /* W3C, IE 10+/ 
  Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
}

JS:

window.lavaAnimation = function() {
  "use strict";
  var t, i = {
      screen: {
        elem: null,
        callback: null,
        ctx: null,
        width: 0,
        height: 0,
        left: 0,
        top: 0,
        init: function(t, i, s) {
          return this.elem = document.getElementById(t), this.callback = i || null, "CANVAS" == this.elem.tagName && (this.ctx = this.elem.getContext("2d")), window.addEventListener("resize", function() {
            this.resize()
          }.bind(this), !1), this.elem.onselectstart = function() {
            return !1
          }, this.elem.ondrag = function() {
            return !1
          }, s && this.resize(), this
        },
        resize: function() {
          var t = this.elem;
          for (this.width = t.offsetWidth, this.height = t.offsetHeight, this.left = 0, this.top = 0; null != t; t = t.offsetParent) this.left += t.offsetLeft, this.top += t.offsetTop;
          this.ctx && (this.elem.width = this.width, this.elem.height = this.height), this.callback && this.callback()
        }
      }
    },
    s = function(t, i) {
      this.x = t, this.y = i, this.magnitude = t * t + i * i, this.computed = 0, this.force = 0
    };
  s.prototype.add = function(t) {
    return new s(this.x + t.x, this.y + t.y)
  };
  var h = function(t) {
    var i = .1,
      h = 1.5;
    this.vel = new s((Math.random() > .5 ? 1 : -1) * (.2 + .25 * Math.random()), (Math.random() > .5 ? 1 : -1) * (.2 + Math.random())), this.pos = new s(.2 * t.width + Math.random() * t.width * .6, .2 * t.height + Math.random() * t.height * .6), this.size = t.wh / 15 + (Math.random() * (h - i) + i) * (t.wh / 15), this.width = t.width, this.height = t.height
  };
  h.prototype.move = function() {
    this.pos.x >= this.width - this.size ? (this.vel.x > 0 && (this.vel.x = -this.vel.x), this.pos.x = this.width - this.size) : this.pos.x <= this.size && (this.vel.x < 0 && (this.vel.x = -this.vel.x), this.pos.x = this.size), this.pos.y >= this.height - this.size ? (this.vel.y > 0 && (this.vel.y = -this.vel.y), this.pos.y = this.height - this.size) : this.pos.y <= this.size && (this.vel.y < 0 && (this.vel.y = -this.vel.y), this.pos.y = this.size), this.pos = this.pos.add(this.vel)
  };
  var e = function(t, i, e, n, a) {
    this.step = 5, this.width = t, this.height = i, this.wh = Math.min(t, i), this.sx = Math.floor(this.width / this.step), this.sy = Math.floor(this.height / this.step), this.paint = !1, this.metaFill = r(t, i, t, n, a), this.plx = [0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0], this.ply = [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1], this.mscases = [0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 0, 2, 1, 1, 0], this.ix = [1, 0, -1, 0, 0, 1, 0, -1, -1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1], this.grid = [], this.balls = [], this.iter = 0, this.sign = 1;
    for (var o = 0; o < (this.sx + 2) * (this.sy + 2); o++) this.grid[o] = new s(o % (this.sx + 2) * this.step, Math.floor(o / (this.sx + 2)) * this.step);
    for (var l = 0; e > l; l++) this.balls[l] = new h(this)
  };
  e.prototype.computeForce = function(t, i, s) {
    var h, e = s || t + i * (this.sx + 2);
    if (0 === t || 0 === i || t === this.sx || i === this.sy) h = .6 * this.sign;
    else {
      h = 0;
      for (var r, n = this.grid[e], a = 0; r = this.balls[a++];) h += r.size * r.size / (-2 * n.x * r.pos.x - 2 * n.y * r.pos.y + r.pos.magnitude + n.magnitude);
      h *= this.sign
    }
    return this.grid[e].force = h, h
  }, e.prototype.marchingSquares = function(t) {
    var i = t[0],
      s = t[1],
      h = t[2],
      e = i + s * (this.sx + 2);
    if (this.grid[e].computed === this.iter) return !1;
    for (var r, n = 0, a = 0; 4 > a; a++) {
      var l = i + this.ix[a + 12] + (s + this.ix[a + 16]) * (this.sx + 2),
        d = this.grid[l].force;
      (d > 0 && this.sign < 0 || 0 > d && this.sign > 0 || !d) && (d = this.computeForce(i + this.ix[a + 12], s + this.ix[a + 16], l)), Math.abs(d) > 1 && (n += Math.pow(2, a))
    }
    if (15 === n) return [i, s - 1, !1];
    5 === n ? r = 2 === h ? 3 : 1 : 10 === n ? r = 3 === h ? 0 : 2 : (r = this.mscases[n], this.grid[e].computed = this.iter);
    var p = this.step / (Math.abs(Math.abs(this.grid[i + this.plx[4 * r + 2] + (s + this.ply[4 * r + 2]) * (this.sx + 2)].force) - 1) / Math.abs(Math.abs(this.grid[i + this.plx[4 * r + 3] + (s + this.ply[4 * r + 3]) * (this.sx + 2)].force) - 1) + 1);
    return o.lineTo(this.grid[i + this.plx[4 * r] + (s + this.ply[4 * r]) * (this.sx + 2)].x + this.ix[r] * p, this.grid[i + this.plx[4 * r + 1] + (s + this.ply[4 * r + 1]) * (this.sx + 2)].y + this.ix[r + 4] * p), this.paint = !0, [i + this.ix[r + 4], s + this.ix[r + 8], r]
  }, e.prototype.renderMetaballs = function() {
    for (var t, i = 0; t = this.balls[i++];) t.move();
    for (this.iter++, this.sign = -this.sign, this.paint = !1, o.fillStyle = this.metaFill, o.beginPath(), i = 0; t = this.balls[i++];) {
      var s = [Math.round(t.pos.x / this.step), Math.round(t.pos.y / this.step), !1];
      do s = this.marchingSquares(s); while (s);
      this.paint && (o.fill(), o.closePath(), o.beginPath(), this.paint = !1)
    }
  };
  var r = function(t, i, s, h, e) {
    var r = o.createRadialGradient(t / 1, i / 1, 0, t / 1, i / 1, s);
    return r.addColorStop(0, h), r.addColorStop(1, e), r
  };
  if (document.getElementById("lamp-anim")) {
    var n = function() {
        requestAnimationFrame(n), o.clearRect(0, 0, a.width, a.height), t.renderMetaballs()
      },
      a = i.screen.init("lamp-anim", null, !0),
      o = a.ctx;
    a.resize(), t = new e(a.width, a.height, 6, "#3494E6", "#EC6EAD")
  }
  return {
    run: n
  }
}();


if (document.getElementById('lamp-anim')) {
  lavaAnimation.run();
}
setTimeout(function() {
  $('.js-works-d-list').addClass('is-loaded');
}, 150);

Is it possible to convert/do this in angular animations? Are they flexible enough to do this sort of (what id call advanced) animation?

Steve Fitzsimons
  • 3,754
  • 7
  • 27
  • 66

1 Answers1

0

I think the question of 'can I convert this to Angular' is a bit off because Angular runs on Typescript, which is a language built from javascript. So, yes you can do all this in Angular or rather using Typescript within an Angular app.

We're always here to help once you get some code written in an Angular app! But in general, we are here to help you were you get stuck in code and help you solve the problem. It's a bit more challenging to say 'yes it will work' without seeing how you implement it in your project and can't really guide or help you until we see how your angular components are written.

Short answer: Yeah, I think it can work. But it also depends how you implement this code into your Angular app.

Rinktacular
  • 1,116
  • 3
  • 19
  • 45
  • Thanks for taking the time to post a thought out answer. Maybe I didn't phrase the question well enough to satisfy those how keep voting to close it but you took the time to start conversation rather. So thanks – Steve Fitzsimons Jan 18 '19 at 09:48
  • @SteveFitzsimons any time, people on SO are very quick to close questions that are generally 'how do I do this thing' before trying it out. In most cases, people want to see code you've tried and we can help you solve errors rather than starting you off without any basis :) – Rinktacular Jan 18 '19 at 19:14