给图片添加滤镜前,我们必须先使用cache()
方法将它缓存起来,然后使用filter()
方法添加滤镜。
我们可以使用Konva.Filters.Kaleidoscope
滤镜,给图片创建一个万花筒效果,然后设置kaleidoscopePower
和 kaleidoscopeAngle
的值调整效果。
说明:拖动滑块调整万花筒的角度
查看所有滤镜 Filters Documentation.
Konva Kaleidoscope Image Demoview raw<!DOCTYPE html> <html> <head> <script src="https://unpkg.com/konva@4.0.18/konva.min.js"></script> <meta charset="utf-8" /> <title>Konva Kaleidoscope Image Demo</title> <style> body { margin: 0; padding: 0; overflow: hidden; background-color: #f0f0f0; }
#slider { position: absolute; top: 20px; left: 20px; } </style> </head>
<body> <div id="container"></div> <input id="slider" type="range" min="0" max="360" step="5" value="20" /> <script> function loadImages(sources, callback) { var images = {}; var loadedImages = 0; var numImages = 0; for (var src in sources) { numImages++; } for (var src in sources) { images[src] = new Image(); images[src].onload = function() { if (++loadedImages >= numImages) { callback(images); } }; images[src].src = sources[src]; } } function buildStage(images) { var stage = new Konva.Stage({ container: 'container', width: window.innerWidth, height: window.innerHeight });
var layer = new Konva.Layer();
var lion = new Konva.Image({ image: images.lion, x: 80, y: 30, draggable: true });
lion.cache(); lion.filters([Konva.Filters.Kaleidoscope]); lion.kaleidoscopePower(3); layer.add(lion); stage.add(layer); var slider = document.getElementById('slider'); slider.oninput = function() { lion.kaleidoscopeAngle(slider.value); layer.batchDraw(); }; }
var sources = { lion: '/assets/lion.png' };
loadImages(sources, buildStage); </script> </body> </html>
|