Skip to main content

Oscillating Blobs

Instructions: Refresh the page to generate new blobs. You can also drag and drop the blobs as they animate.

import Konva from 'konva';

var width = window.innerWidth;
var height = window.innerHeight;

var stage = new Konva.Stage({
  container: 'container',
  width: width,
  height: height,
});
var layer = new Konva.Layer();
var colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple'];
var blobs = [];

// create 6 blobs

for (var n = 0; n < 6; n++) {
  // build array of random points

  var points = [];
  for (var i = 0; i < 5; i++) {
    points.push(stage.width() * Math.random());
    points.push(height * Math.random());
  }

  var blob = new Konva.Line({
    points: points,
    fill: colors[n],
    stroke: 'black',
    strokeWidth: 2,
    tension: 0,
    opacity: Math.random(),
    draggable: true,
    closed: true,
  });

  layer.add(blob);
  blobs.push(blob);
}

stage.add(layer);

var period = 2000;
var centerTension = 0;
var amplitude = 1;

var anim = new Konva.Animation(function (frame) {
  for (var n = 0; n < blobs.length; n++) {
    blobs[n].tension(
      amplitude * Math.sin((frame.time * 2 * Math.PI) / period) +
        centerTension
    );
  }
}, layer);

anim.start();