跳到主要内容

振荡 Blob 图形

操作说明:刷新页面以生成新的 Blob 图形。还可以在 Blob 图形播放动画时拖放它们。

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();