Warning, /plasma-bigscreen/mycroft-skill-installer/app/qml/CanBackground.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  *   SPDX-FileCopyrightText: 2019-2020 Aditya Mehra <aix.m@outlook.org>
0003  *
0004  *   SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 import QtQuick 2.9
0008 import "code/Rnd.js" as MathFn
0009 
0010 Item {
0011     anchors.fill: parent
0012 
0013     Component.onCompleted: {
0014         delay(2000, function() {
0015             timr.running = true
0016           })
0017         }
0018 
0019         Timer {
0020                id: timer
0021         }
0022 
0023            function delay(delayTime, cb) {
0024                    timer.interval = delayTime;
0025                    timer.repeat = false;
0026                    timer.triggered.connect(cb);
0027                    timer.start();
0028         }
0029 
0030         Timer {
0031                id: timr
0032                interval: 100
0033                repeat: true
0034                running: false
0035 
0036                onTriggered: {
0037                     bgCanvas.requestAnimationFrame(bgCanvas.tick)
0038                 }
0039               }
0040 
0041           Canvas {
0042             id: bgCanvas
0043             anchors.fill: parent
0044             renderStrategy: Canvas.Threaded
0045             property var w: bgCanvas.width
0046             property var h: bgCanvas.height
0047             property var ctx
0048             property var rows
0049             property var cols
0050             property var hexGrid
0051             property var hex
0052             property var nextT
0053             property var fadeT
0054             property var hue
0055             property var count
0056             property var grid: [];
0057             property var watchList: [];
0058             property var radius: 5;
0059 
0060             function init() {
0061               nextT = 0
0062               fadeT = 0
0063               hue = 0
0064               count = 0;
0065               grid = [];
0066               radius = Math.max(12,h/80);
0067               hexGrid = buildGrid(w, h, radius);
0068             }
0069 
0070             function buildGrid(w, h, r) {
0071                var rowh = r * Math.sin(Math.PI / 3);
0072                var colW = r * 2 * 1.5;
0073                rows = Math.ceil(h / rowh) + 1;
0074                cols = Math.ceil(w / colW) + 1;
0075                var grid = [];
0076                for (var row = 0; row < rows; row++) {
0077                  var y = row * rowh;
0078                  for (var col = 0; col < cols; col++) {
0079                    var x = col * colW + (row % 2) * colW / 2;
0080                    var hex = {col: col, row: row, tol: 1 + MathFn.rnd.float(2), pot: 0};
0081                    hex.color = Qt.hsla(MathFn.rnd.float(198, 204), 20, MathFn.rnd.float(75, 80), MathFn.rnd.float(0.01, 0.03));
0082                    hex.x = x;
0083                    hex.y = y;
0084                    grid.push(hex);
0085                  }
0086                }
0087 
0088                // neighbors:
0089                for (var i = 0, l = grid.length; i < l; i++) {
0090                  hex = grid[i];
0091                  var off = hex.row % 2;
0092                  col = hex.col;
0093                  hex.neighbors = [
0094                    grid[i - cols * 2],
0095                    col + off < cols ? grid[i - cols + off] : null,
0096                    col + off < cols ? grid[i + cols + off] : null,
0097                    grid[i + cols * 2],
0098                    col + off > 0 ? grid[i + cols - 1 + off] : null,
0099                    col + off > 0 ? grid[i - cols - 1 + off] : null
0100                  ];
0101                }
0102 
0103                return grid;
0104              }
0105 
0106              function drawGrid(ctx, grid) {
0107                for (var i = 0, l = grid.length; i < l; i++) {
0108                  var hex = grid[i];
0109                  if (!hex.clean) {
0110                    var m = MathFn.rnd(0.6,1);
0111                    drawHex(ctx, hex.x, hex.y, hex.color, m, MathFn.rnd(1,radius/6));
0112                    drawHex(ctx, hex.x, hex.y, hex.color, MathFn.rnd(0.2,m-0.1), MathFn.rnd(1,radius/4));
0113                    hex.clean = true;
0114                  }
0115                }
0116              }
0117 
0118              function tick() {
0119                //bgCanvas.requestPaint()
0120                var delta = (1000 / timr.interval), grid = hexGrid;
0121                var t = getTime();
0122                for (var i = watchList.length - 1; i >= 0; i--) {
0123                var hex = watchList[i];
0124                  if (!hex) { break; }
0125                  if (hex.t < t) {
0126                    removeFromWatch(hex);
0127                    trigger(hex, t);
0128                  }
0129                }
0130 
0131                if (fadeT < t) {
0132                  ctx.globalCompositeOperation = "destination-in";
0133                  ctx.fillStyle = Qt.hsla(0, 0, 100, 0.95);
0134                  ctx.fillRect(0, 0, w, h);
0135                  ctx.globalCompositeOperation = "lighter";
0136                  fadeT = t + 250;
0137                }
0138 
0139                if (t > nextT) {
0140                  hex = hexGrid[MathFn.rnd.integer(cols) - cols + cols * rows]
0141                  hex.t = getTime() + MathFn.rnd.float(100, 200);
0142                  hex.from = 4;//Math.random(6);
0143                  hex.hue = MathFn.rnd(360);
0144                  hex.lm = 0.7;
0145                  hex.chance = 1.2;
0146                  watchList.push(hex);
0147                  nextT = getTime() + 200;
0148                }
0149                drawGrid(ctx, hexGrid);
0150              }
0151 
0152              function trigger(hex, t) {
0153                hex.val = MathFn.rnd(400,900);
0154                hex.t = t + hex.val;
0155                hex.clean = false;
0156                var chance = hex.chance;
0157                while (MathFn.rnd.boolean(chance)) {
0158                  var turn = MathFn.rnd.bit(2.5) * MathFn.rnd.sign();
0159                  var dir = (hex.from + 3 + turn + 6) % 6;
0160                  var neighbor = hex.neighbors[dir];
0161                  chance *= 0.25;
0162                  if (!neighbor || neighbor.t >= t) {
0163                    continue;
0164                  }
0165                  neighbor.val = MathFn.rnd.float(0,1)*MathFn.rnd.float(0,1)*50+5;
0166                  neighbor.t = t;// + neighbor.val;
0167                  neighbor.from = (dir + 3) % 6;
0168                  neighbor.chance = hex.chance * 0.999;
0169                  neighbor.hue = hex.hue // + Math.random(-3,3)+(dir-3);
0170                  neighbor.color = Qt.hsla(neighbor.hue, 50, MathFn.rnd(10,35));
0171                  neighbor.lm = hex.lm;
0172                  watchList.push(neighbor);
0173                }
0174              }
0175 
0176              function removeFromWatch(hex) {
0177                // this could be improved by switching to a double linked list
0178                for (var i = watchList.length - 1; i >= 0; i--) {
0179                  if (hex === watchList[i]) {
0180                    watchList.splice(i,1);
0181                  }
0182                }
0183              }
0184 
0185              function getTime() {
0186                return (new Date()).getTime();
0187              }
0188 
0189              function drawHex(ctx, x, y, fill, m, s) {
0190                var r = radius-1-s/2;
0191                r *= m||1;
0192                r = Math.ceil(r);
0193                ctx.beginPath();
0194                var p = Math.PI / 6 * 2;
0195                for (var i = 0; i < 6; i++) {
0196                  var x1 = x + Math.cos(p * i) * r, y1 = y + Math.sin(p * i) * r;
0197                  if (i == 0) {
0198                    ctx.moveTo(x1, y1);
0199                  }
0200                  else {
0201                    ctx.lineTo(x1, y1);
0202                  }
0203                }
0204                ctx.closePath();
0205                ctx.strokeStyle = fill;
0206                ctx.lineWidth = s||1;
0207                ctx.stroke();
0208                return ctx;
0209              }
0210 
0211             onPaint: {
0212                  //console.log("here")
0213                  ctx = getContext("2d")
0214                  init()
0215                }
0216             onPainted: {
0217                 bgCanvas.tick()
0218                 }
0219             }
0220         }