targetj 1.0.259 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Export.js +16 -1
- package/README.md +18 -25
- package/build/AnimationManager.js +1 -1
- package/build/AnimationUtil.js +1 -8
- package/build/App.js +13 -4
- package/build/BaseModel.js +39 -14
- package/build/Bracket.js +7 -1
- package/build/Child.js +504 -0
- package/build/EventListener.js +10 -1
- package/build/LocationManager.js +161 -40
- package/build/PageManager.js +35 -134
- package/build/ParticleChild.js +575 -0
- package/build/ParticleRenderer.js +818 -0
- package/build/ParticleRuntime.js +1513 -0
- package/build/ParticleTModel.js +1423 -0
- package/build/ParticleUtil.js +265 -0
- package/build/RunScheduler.js +10 -3
- package/build/ScheduleUtil.js +2 -2
- package/build/StateManager.js +235 -31
- package/build/StateUtil.js +14 -15
- package/build/TModel.js +62 -28
- package/build/TModelFactory.js +48 -0
- package/build/TModelManager.js +146 -98
- package/build/TModelUtil.js +11 -8
- package/build/TUtil.js +57 -47
- package/build/TargetData.js +7 -1
- package/build/TargetExecutor.js +63 -9
- package/build/TargetManager.js +39 -23
- package/build/TargetParser.js +38 -2
- package/build/TargetUtil.js +26 -5
- package/build/Viewport.js +2 -2
- package/build/VisibilityUtil.js +4 -2
- package/build/index.js +6 -1
- package/dist/targetjs.js +1 -1
- package/dist/targetjs.js.gz +0 -0
- package/package.json +1 -1
|
@@ -0,0 +1,818 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
|
|
3
|
+
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
|
|
4
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
5
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
6
|
+
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
|
7
|
+
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
|
|
8
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
9
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
10
|
+
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
|
|
11
|
+
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
|
|
12
|
+
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
13
|
+
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
|
14
|
+
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
15
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
16
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
17
|
+
import { ColorUtil } from "./ColorUtil.js";
|
|
18
|
+
import { TUtil } from "./TUtil.js";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* It renders GPU-backed children using WebGPU.
|
|
22
|
+
*/
|
|
23
|
+
var ParticleRenderer = /*#__PURE__*/function () {
|
|
24
|
+
function ParticleRenderer(tmodel) {
|
|
25
|
+
_classCallCheck(this, ParticleRenderer);
|
|
26
|
+
this.tmodel = tmodel;
|
|
27
|
+
this.canvas = undefined;
|
|
28
|
+
this.device = undefined;
|
|
29
|
+
this.context = undefined;
|
|
30
|
+
this.format = undefined;
|
|
31
|
+
this.pipeline = undefined;
|
|
32
|
+
this.bindGroup = undefined;
|
|
33
|
+
this.particleBuffer = undefined;
|
|
34
|
+
this.targetParticleBuffer = undefined;
|
|
35
|
+
this.uniformBuffer = undefined;
|
|
36
|
+
this.transitionStep = 0;
|
|
37
|
+
this.transitionActive = false;
|
|
38
|
+
this.particleCount = 0;
|
|
39
|
+
this.canvasLayer = undefined;
|
|
40
|
+
this.canvasX = 0;
|
|
41
|
+
this.canvasY = 0;
|
|
42
|
+
this.initializationPromise = undefined;
|
|
43
|
+
}
|
|
44
|
+
return _createClass(ParticleRenderer, [{
|
|
45
|
+
key: "init",
|
|
46
|
+
value: function () {
|
|
47
|
+
var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
48
|
+
var _this = this;
|
|
49
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
50
|
+
while (1) switch (_context.prev = _context.next) {
|
|
51
|
+
case 0:
|
|
52
|
+
if (!this.device) {
|
|
53
|
+
_context.next = 2;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
return _context.abrupt("return", true);
|
|
57
|
+
case 2:
|
|
58
|
+
if (this.tmodel.hasDom()) {
|
|
59
|
+
_context.next = 4;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
return _context.abrupt("return", false);
|
|
63
|
+
case 4:
|
|
64
|
+
if (!this.initializationPromise) {
|
|
65
|
+
this.initializationPromise = this.initialize().catch(function (error) {
|
|
66
|
+
_this.initializationPromise = undefined;
|
|
67
|
+
throw error;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
_context.next = 7;
|
|
71
|
+
return this.initializationPromise;
|
|
72
|
+
case 7:
|
|
73
|
+
return _context.abrupt("return", !!this.device);
|
|
74
|
+
case 8:
|
|
75
|
+
case "end":
|
|
76
|
+
return _context.stop();
|
|
77
|
+
}
|
|
78
|
+
}, _callee, this);
|
|
79
|
+
}));
|
|
80
|
+
function init() {
|
|
81
|
+
return _init.apply(this, arguments);
|
|
82
|
+
}
|
|
83
|
+
return init;
|
|
84
|
+
}()
|
|
85
|
+
}, {
|
|
86
|
+
key: "initialize",
|
|
87
|
+
value: function () {
|
|
88
|
+
var _initialize = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
|
89
|
+
var adapter;
|
|
90
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
91
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
92
|
+
case 0:
|
|
93
|
+
if (navigator.gpu) {
|
|
94
|
+
_context2.next = 2;
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
throw new Error("WebGPU is not supported by this browser.");
|
|
98
|
+
case 2:
|
|
99
|
+
if (globalThis.GPUBufferUsage) {
|
|
100
|
+
_context2.next = 4;
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
throw new Error("GPUBufferUsage is not available in this WebGPU environment.");
|
|
104
|
+
case 4:
|
|
105
|
+
this.createCanvas();
|
|
106
|
+
_context2.next = 7;
|
|
107
|
+
return navigator.gpu.requestAdapter();
|
|
108
|
+
case 7:
|
|
109
|
+
adapter = _context2.sent;
|
|
110
|
+
if (adapter) {
|
|
111
|
+
_context2.next = 10;
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
throw new Error("Unable to obtain a WebGPU adapter.");
|
|
115
|
+
case 10:
|
|
116
|
+
_context2.next = 12;
|
|
117
|
+
return adapter.requestDevice();
|
|
118
|
+
case 12:
|
|
119
|
+
this.device = _context2.sent;
|
|
120
|
+
this.context = this.canvas.getContext("webgpu");
|
|
121
|
+
if (this.context) {
|
|
122
|
+
_context2.next = 16;
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
throw new Error("Unable to obtain a WebGPU canvas context.");
|
|
126
|
+
case 16:
|
|
127
|
+
this.format = navigator.gpu.getPreferredCanvasFormat();
|
|
128
|
+
this.resizeCanvas();
|
|
129
|
+
this.context.configure({
|
|
130
|
+
device: this.device,
|
|
131
|
+
format: this.format,
|
|
132
|
+
alphaMode: "premultiplied"
|
|
133
|
+
});
|
|
134
|
+
this.createUniformBuffer();
|
|
135
|
+
this.createPipeline();
|
|
136
|
+
case 21:
|
|
137
|
+
case "end":
|
|
138
|
+
return _context2.stop();
|
|
139
|
+
}
|
|
140
|
+
}, _callee2, this);
|
|
141
|
+
}));
|
|
142
|
+
function initialize() {
|
|
143
|
+
return _initialize.apply(this, arguments);
|
|
144
|
+
}
|
|
145
|
+
return initialize;
|
|
146
|
+
}()
|
|
147
|
+
}, {
|
|
148
|
+
key: "createCanvas",
|
|
149
|
+
value: function createCanvas() {
|
|
150
|
+
var _this$tmodel$$dom,
|
|
151
|
+
_this2 = this,
|
|
152
|
+
_this$tmodel$$dom2,
|
|
153
|
+
_this$tmodel$getParen;
|
|
154
|
+
if (this.canvas) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
var parent = (_this$tmodel$$dom = this.tmodel.$dom) === null || _this$tmodel$$dom === void 0 ? void 0 : _this$tmodel$$dom.element;
|
|
158
|
+
if (!parent) {
|
|
159
|
+
throw new Error("Unable to create a GPU child layer without a parent DOM element.");
|
|
160
|
+
}
|
|
161
|
+
var oldLayer = parent.querySelector('[data-targetjs-gpu-layer="true"]');
|
|
162
|
+
if (oldLayer) {
|
|
163
|
+
oldLayer.remove();
|
|
164
|
+
}
|
|
165
|
+
this.canvasLayer = document.createElement("div");
|
|
166
|
+
this.canvasLayer.setAttribute("data-targetjs-gpu-layer", "true");
|
|
167
|
+
Object.assign(this.canvasLayer.style, {
|
|
168
|
+
position: "absolute",
|
|
169
|
+
left: "0px",
|
|
170
|
+
top: "0px",
|
|
171
|
+
width: "100%",
|
|
172
|
+
height: "100%",
|
|
173
|
+
pointerEvents: "none",
|
|
174
|
+
zIndex: "0"
|
|
175
|
+
});
|
|
176
|
+
this.canvas = document.createElement("canvas");
|
|
177
|
+
this.canvas.setAttribute("data-targetjs-gpu-children", "true");
|
|
178
|
+
Object.assign(this.canvas.style, {
|
|
179
|
+
position: "absolute",
|
|
180
|
+
left: "0px",
|
|
181
|
+
top: "0px",
|
|
182
|
+
width: "1px",
|
|
183
|
+
height: "1px",
|
|
184
|
+
display: "block",
|
|
185
|
+
pointerEvents: "none"
|
|
186
|
+
});
|
|
187
|
+
this.canvasLayer.appendChild(this.canvas);
|
|
188
|
+
parent.prepend(this.canvasLayer);
|
|
189
|
+
this.onScroll = function () {
|
|
190
|
+
return _this2.tmodel.requestParticleRender();
|
|
191
|
+
};
|
|
192
|
+
this.scrollElements = _toConsumableArray(new Set([(_this$tmodel$$dom2 = this.tmodel.$dom) === null || _this$tmodel$$dom2 === void 0 ? void 0 : _this$tmodel$$dom2.element, (_this$tmodel$getParen = this.tmodel.getParent()) === null || _this$tmodel$getParen === void 0 || (_this$tmodel$getParen = _this$tmodel$getParen.$dom) === null || _this$tmodel$getParen === void 0 ? void 0 : _this$tmodel$getParen.element].filter(Boolean)));
|
|
193
|
+
var _iterator = _createForOfIteratorHelper(this.scrollElements),
|
|
194
|
+
_step;
|
|
195
|
+
try {
|
|
196
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
197
|
+
var element = _step.value;
|
|
198
|
+
element.addEventListener("scroll", this.onScroll, {
|
|
199
|
+
passive: true
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
} catch (err) {
|
|
203
|
+
_iterator.e(err);
|
|
204
|
+
} finally {
|
|
205
|
+
_iterator.f();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}, {
|
|
209
|
+
key: "createUniformBuffer",
|
|
210
|
+
value: function createUniformBuffer() {
|
|
211
|
+
this.uniformBuffer = this.device.createBuffer({
|
|
212
|
+
size: 32,
|
|
213
|
+
usage: globalThis.GPUBufferUsage.UNIFORM | globalThis.GPUBufferUsage.COPY_DST
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
}, {
|
|
217
|
+
key: "createPipeline",
|
|
218
|
+
value: function createPipeline() {
|
|
219
|
+
var shaderModule = this.device.createShaderModule({
|
|
220
|
+
code: "\n struct Uniforms {\n viewport: vec2f,\n contentOffset: vec2f,\n step: f32,\n transitionActive: f32,\n padding: vec2f,\n }\n\n @group(0) @binding(0)\n var<uniform> uniforms: Uniforms;\n\n struct VertexOutput {\n @builtin(position) position: vec4f,\n @location(0) localPosition: vec2f,\n @location(1) backgroundColor: vec4f,\n @location(2) size: vec2f,\n @location(3) borderRadius: f32,\n }\n\n fn getProgress(step: f32, steps: f32) -> f32 {\n if (uniforms.transitionActive < 0.5) {\n return 0.0;\n }\n\n if (steps < 0.0) {\n return 0.0;\n }\n\n if (steps == 0.0) {\n return 1.0;\n }\n\n return clamp(step / steps, 0.0, 1.0);\n }\n\n @vertex\n fn vertexMain(\n @location(0) childPosition: vec2f,\n @location(1) childSize: vec2f,\n @location(2) childBackgroundColor: vec4f,\n @location(3) childGeometry: vec2f,\n @location(4) targetPosition: vec2f,\n @location(5) targetSize: vec2f,\n @location(6) targetBackgroundColor: vec4f,\n @location(7) targetGeometry: vec2f,\n @location(8) targetPositionSteps: vec2f,\n @location(9) targetSizeSteps: vec2f,\n @location(10) targetBackgroundColorSteps: f32,\n @location(11) targetGeometrySteps: vec2f,\n @location(12) childScale: f32,\n @location(13) targetScale: f32,\n @location(14) targetScaleSteps: f32,\n @builtin(vertex_index) vertexIndex: u32\n ) -> VertexOutput {\n let corners = array<vec2f, 6>(\n vec2f(-1.0, -1.0),\n vec2f( 1.0, -1.0),\n vec2f(-1.0, 1.0),\n vec2f(-1.0, 1.0),\n vec2f( 1.0, -1.0),\n vec2f( 1.0, 1.0)\n );\n\n let xProgress = getProgress(uniforms.step, targetPositionSteps.x);\n let yProgress = getProgress(uniforms.step, targetPositionSteps.y);\n let widthProgress = getProgress(uniforms.step, targetSizeSteps.x);\n let heightProgress = getProgress(uniforms.step, targetSizeSteps.y);\n let backgroundColorProgress = getProgress(uniforms.step, targetBackgroundColorSteps);\n let borderRadiusProgress = getProgress(uniforms.step, targetGeometrySteps.x);\n let rotateProgress = getProgress(uniforms.step, targetGeometrySteps.y);\n let scaleProgress = getProgress(uniforms.step, targetScaleSteps);\n \n let position = vec2f(\n mix(childPosition.x, targetPosition.x, xProgress),\n mix(childPosition.y, targetPosition.y, yProgress)\n );\n\n let size = vec2f(\n mix(childSize.x, targetSize.x, widthProgress),\n mix(childSize.y, targetSize.y, heightProgress)\n );\n\n let backgroundColor = mix(childBackgroundColor, targetBackgroundColor, backgroundColorProgress);\n let borderRadius = mix(childGeometry.x, targetGeometry.x, borderRadiusProgress);\n let rotate = mix(childGeometry.y, targetGeometry.y, rotateProgress);\n let scale = mix(childScale, targetScale, scaleProgress);\n\n let localPosition = corners[vertexIndex];\n let localPixelPosition = localPosition * size * 0.5 * scale;\n\n let angle = rotate * 3.141592653589793 / 180.0;\n let cosine = cos(angle);\n let sine = sin(angle);\n\n let rotatedPosition = vec2f(\n localPixelPosition.x * cosine - localPixelPosition.y * sine,\n localPixelPosition.x * sine + localPixelPosition.y * cosine\n );\n\n let elementCenter = position - uniforms.contentOffset + size * 0.5;\n let pixelPosition = elementCenter + rotatedPosition;\n\n let clipPosition = vec2f(\n pixelPosition.x / uniforms.viewport.x * 2.0 - 1.0,\n 1.0 - pixelPosition.y / uniforms.viewport.y * 2.0\n );\n\n var output: VertexOutput;\n\n output.position = vec4f(clipPosition, 0.0, 1.0);\n output.localPosition = localPosition;\n output.backgroundColor = backgroundColor;\n output.size = size;\n output.borderRadius = borderRadius;\n\n return output;\n }\n @fragment\n fn fragmentMain(input: VertexOutput) -> @location(0) vec4f {\n let halfSize = max(input.size * 0.5, vec2f(0.0001));\n let radius = clamp(input.borderRadius, 0.0, min(halfSize.x, halfSize.y));\n let pixelPosition = input.localPosition * halfSize;\n let q = abs(pixelPosition) - (halfSize - vec2f(radius));\n let distance = length(max(q, vec2f(0.0))) + min(max(q.x, q.y), 0.0) - radius;\n let aa = max(fwidth(distance), 0.0001);\n\n if (radius <= 0.0) {\n return input.backgroundColor;\n }\n\n let coverage = 1.0 - smoothstep(-aa, aa, distance);\n\n if (coverage <= 0.0) {\n discard;\n }\n\n let alpha = input.backgroundColor.a * coverage;\n\n return vec4f(input.backgroundColor.rgb * alpha, alpha);\n }\n "
|
|
221
|
+
});
|
|
222
|
+
this.pipeline = this.device.createRenderPipeline({
|
|
223
|
+
layout: "auto",
|
|
224
|
+
vertex: {
|
|
225
|
+
module: shaderModule,
|
|
226
|
+
entryPoint: "vertexMain",
|
|
227
|
+
buffers: [{
|
|
228
|
+
arrayStride: 80,
|
|
229
|
+
stepMode: "instance",
|
|
230
|
+
attributes: [{
|
|
231
|
+
shaderLocation: 0,
|
|
232
|
+
offset: 0,
|
|
233
|
+
format: "float32x2"
|
|
234
|
+
}, {
|
|
235
|
+
shaderLocation: 1,
|
|
236
|
+
offset: 8,
|
|
237
|
+
format: "float32x2"
|
|
238
|
+
}, {
|
|
239
|
+
shaderLocation: 2,
|
|
240
|
+
offset: 16,
|
|
241
|
+
format: "float32x4"
|
|
242
|
+
}, {
|
|
243
|
+
shaderLocation: 3,
|
|
244
|
+
offset: 32,
|
|
245
|
+
format: "float32x2"
|
|
246
|
+
}, {
|
|
247
|
+
shaderLocation: 12,
|
|
248
|
+
offset: 68,
|
|
249
|
+
format: "float32"
|
|
250
|
+
}]
|
|
251
|
+
}, {
|
|
252
|
+
arrayStride: 80,
|
|
253
|
+
stepMode: "instance",
|
|
254
|
+
attributes: [{
|
|
255
|
+
shaderLocation: 4,
|
|
256
|
+
offset: 0,
|
|
257
|
+
format: "float32x2"
|
|
258
|
+
}, {
|
|
259
|
+
shaderLocation: 5,
|
|
260
|
+
offset: 8,
|
|
261
|
+
format: "float32x2"
|
|
262
|
+
}, {
|
|
263
|
+
shaderLocation: 6,
|
|
264
|
+
offset: 16,
|
|
265
|
+
format: "float32x4"
|
|
266
|
+
}, {
|
|
267
|
+
shaderLocation: 7,
|
|
268
|
+
offset: 32,
|
|
269
|
+
format: "float32x2"
|
|
270
|
+
}, {
|
|
271
|
+
shaderLocation: 8,
|
|
272
|
+
offset: 40,
|
|
273
|
+
format: "float32x2"
|
|
274
|
+
}, {
|
|
275
|
+
shaderLocation: 9,
|
|
276
|
+
offset: 48,
|
|
277
|
+
format: "float32x2"
|
|
278
|
+
}, {
|
|
279
|
+
shaderLocation: 10,
|
|
280
|
+
offset: 56,
|
|
281
|
+
format: "float32"
|
|
282
|
+
}, {
|
|
283
|
+
shaderLocation: 11,
|
|
284
|
+
offset: 60,
|
|
285
|
+
format: "float32x2"
|
|
286
|
+
}, {
|
|
287
|
+
shaderLocation: 13,
|
|
288
|
+
offset: 68,
|
|
289
|
+
format: "float32"
|
|
290
|
+
}, {
|
|
291
|
+
shaderLocation: 14,
|
|
292
|
+
offset: 72,
|
|
293
|
+
format: "float32"
|
|
294
|
+
}]
|
|
295
|
+
}]
|
|
296
|
+
},
|
|
297
|
+
fragment: {
|
|
298
|
+
module: shaderModule,
|
|
299
|
+
entryPoint: "fragmentMain",
|
|
300
|
+
targets: [{
|
|
301
|
+
format: this.format,
|
|
302
|
+
blend: {
|
|
303
|
+
color: {
|
|
304
|
+
srcFactor: "one",
|
|
305
|
+
dstFactor: "one-minus-src-alpha",
|
|
306
|
+
operation: "add"
|
|
307
|
+
},
|
|
308
|
+
alpha: {
|
|
309
|
+
srcFactor: "one",
|
|
310
|
+
dstFactor: "one-minus-src-alpha",
|
|
311
|
+
operation: "add"
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}]
|
|
315
|
+
},
|
|
316
|
+
primitive: {
|
|
317
|
+
topology: "triangle-list"
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
this.bindGroup = this.device.createBindGroup({
|
|
321
|
+
layout: this.pipeline.getBindGroupLayout(0),
|
|
322
|
+
entries: [{
|
|
323
|
+
binding: 0,
|
|
324
|
+
resource: {
|
|
325
|
+
buffer: this.uniformBuffer
|
|
326
|
+
}
|
|
327
|
+
}]
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
}, {
|
|
331
|
+
key: "toNumber",
|
|
332
|
+
value: function toNumber(value) {
|
|
333
|
+
var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
334
|
+
var number = typeof value === "string" ? parseFloat(value) : Number(value);
|
|
335
|
+
return Number.isFinite(number) ? number : defaultValue;
|
|
336
|
+
}
|
|
337
|
+
}, {
|
|
338
|
+
key: "getBackgroundColor",
|
|
339
|
+
value: function getBackgroundColor(value) {
|
|
340
|
+
if (!TUtil.isDefined(value)) {
|
|
341
|
+
return [0, 0, 0, 0];
|
|
342
|
+
}
|
|
343
|
+
if (Array.isArray(value)) {
|
|
344
|
+
return [this.toNumber(value[0], 0), this.toNumber(value[1], 0), this.toNumber(value[2], 0), this.toNumber(value[3], 0)];
|
|
345
|
+
}
|
|
346
|
+
var color = ColorUtil.color2Integers(value);
|
|
347
|
+
if (!color) {
|
|
348
|
+
return [0, 0, 0, 0];
|
|
349
|
+
}
|
|
350
|
+
var alpha = this.toNumber(color[3], 1);
|
|
351
|
+
if (alpha > 1) {
|
|
352
|
+
alpha /= 255;
|
|
353
|
+
}
|
|
354
|
+
return [this.toNumber(color[0], 0) / 255, this.toNumber(color[1], 0) / 255, this.toNumber(color[2], 0) / 255, alpha];
|
|
355
|
+
}
|
|
356
|
+
}, {
|
|
357
|
+
key: "getTargetStep",
|
|
358
|
+
value: function getTargetStep(steps, key) {
|
|
359
|
+
if (!steps || !Object.prototype.hasOwnProperty.call(steps, key)) {
|
|
360
|
+
return -1;
|
|
361
|
+
}
|
|
362
|
+
return Math.max(0, this.toNumber(steps[key], 0));
|
|
363
|
+
}
|
|
364
|
+
}, {
|
|
365
|
+
key: "writeParticleData",
|
|
366
|
+
value: function writeParticleData(particleData, offset, values, steps) {
|
|
367
|
+
var _values, _values2;
|
|
368
|
+
var x = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : (_values = values) === null || _values === void 0 ? void 0 : _values.x;
|
|
369
|
+
var y = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : (_values2 = values) === null || _values2 === void 0 ? void 0 : _values2.y;
|
|
370
|
+
values || (values = {});
|
|
371
|
+
var backgroundColor = this.getBackgroundColor(values.backgroundColor);
|
|
372
|
+
var opacity = Math.max(0, Math.min(1, this.toNumber(values.opacity, 1)));
|
|
373
|
+
particleData[offset] = this.toNumber(x, 0);
|
|
374
|
+
particleData[offset + 1] = this.toNumber(y, 0);
|
|
375
|
+
particleData[offset + 2] = this.toNumber(values.width, 0);
|
|
376
|
+
particleData[offset + 3] = this.toNumber(values.height, 0);
|
|
377
|
+
particleData[offset + 4] = backgroundColor[0];
|
|
378
|
+
particleData[offset + 5] = backgroundColor[1];
|
|
379
|
+
particleData[offset + 6] = backgroundColor[2];
|
|
380
|
+
particleData[offset + 7] = backgroundColor[3] * opacity;
|
|
381
|
+
particleData[offset + 8] = Math.max(0, this.toNumber(values.borderRadius, 0));
|
|
382
|
+
particleData[offset + 9] = this.toNumber(values.rotate, 0);
|
|
383
|
+
particleData[offset + 10] = this.getTargetStep(steps, "x");
|
|
384
|
+
particleData[offset + 11] = this.getTargetStep(steps, "y");
|
|
385
|
+
particleData[offset + 12] = this.getTargetStep(steps, "width");
|
|
386
|
+
particleData[offset + 13] = this.getTargetStep(steps, "height");
|
|
387
|
+
var backgroundColorSteps = this.getTargetStep(steps, "backgroundColor");
|
|
388
|
+
var opacitySteps = this.getTargetStep(steps, "opacity");
|
|
389
|
+
particleData[offset + 14] = opacitySteps >= 0 ? opacitySteps : backgroundColorSteps;
|
|
390
|
+
particleData[offset + 15] = this.getTargetStep(steps, "borderRadius");
|
|
391
|
+
particleData[offset + 16] = this.getTargetStep(steps, "rotate");
|
|
392
|
+
particleData[offset + 17] = this.toNumber(values.scale, 1);
|
|
393
|
+
particleData[offset + 18] = this.getTargetStep(steps, "scale");
|
|
394
|
+
}
|
|
395
|
+
}, {
|
|
396
|
+
key: "createChildData",
|
|
397
|
+
value: function createChildData(children) {
|
|
398
|
+
var particleData = new Float32Array(children.length * 20);
|
|
399
|
+
for (var i = 0; i < children.length; i++) {
|
|
400
|
+
var child = children[i];
|
|
401
|
+
var values = child.actualValues;
|
|
402
|
+
var _this$getParticlePosi = this.getParticlePosition(child, values),
|
|
403
|
+
x = _this$getParticlePosi.x,
|
|
404
|
+
y = _this$getParticlePosi.y;
|
|
405
|
+
this.writeParticleData(particleData, i * 20, values, undefined, x, y);
|
|
406
|
+
}
|
|
407
|
+
return particleData;
|
|
408
|
+
}
|
|
409
|
+
}, {
|
|
410
|
+
key: "createParticleData",
|
|
411
|
+
value: function createParticleData(values, targetSteps) {
|
|
412
|
+
var particleData = new Float32Array(values.length * 20);
|
|
413
|
+
for (var i = 0; i < values.length; i++) {
|
|
414
|
+
var child = this.tmodel.childHandles[i];
|
|
415
|
+
var _this$getParticlePosi2 = this.getParticlePosition(child, values[i]),
|
|
416
|
+
x = _this$getParticlePosi2.x,
|
|
417
|
+
y = _this$getParticlePosi2.y;
|
|
418
|
+
this.writeParticleData(particleData, i * 20, values[i], targetSteps === null || targetSteps === void 0 ? void 0 : targetSteps[i], x, y);
|
|
419
|
+
}
|
|
420
|
+
return particleData;
|
|
421
|
+
}
|
|
422
|
+
}, {
|
|
423
|
+
key: "uploadParticleBuffer",
|
|
424
|
+
value: function uploadParticleBuffer(buffer, particleData) {
|
|
425
|
+
if (!particleData.length) {
|
|
426
|
+
var _buffer;
|
|
427
|
+
(_buffer = buffer) === null || _buffer === void 0 || _buffer.destroy();
|
|
428
|
+
return undefined;
|
|
429
|
+
}
|
|
430
|
+
if (!buffer || buffer.size !== particleData.byteLength) {
|
|
431
|
+
var _buffer2;
|
|
432
|
+
(_buffer2 = buffer) === null || _buffer2 === void 0 || _buffer2.destroy();
|
|
433
|
+
buffer = this.device.createBuffer({
|
|
434
|
+
size: particleData.byteLength,
|
|
435
|
+
usage: globalThis.GPUBufferUsage.VERTEX | globalThis.GPUBufferUsage.COPY_DST
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
this.device.queue.writeBuffer(buffer, 0, particleData);
|
|
439
|
+
return buffer;
|
|
440
|
+
}
|
|
441
|
+
}, {
|
|
442
|
+
key: "setParticles",
|
|
443
|
+
value: function () {
|
|
444
|
+
var _setParticles = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(children) {
|
|
445
|
+
var initialized, particleData;
|
|
446
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
447
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
448
|
+
case 0:
|
|
449
|
+
children = Array.isArray(children) ? children : [children];
|
|
450
|
+
_context3.next = 3;
|
|
451
|
+
return this.init();
|
|
452
|
+
case 3:
|
|
453
|
+
initialized = _context3.sent;
|
|
454
|
+
if (initialized) {
|
|
455
|
+
_context3.next = 6;
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
return _context3.abrupt("return", false);
|
|
459
|
+
case 6:
|
|
460
|
+
particleData = this.createChildData(children);
|
|
461
|
+
this.particleCount = children.length;
|
|
462
|
+
this.particleBuffer = this.uploadParticleBuffer(this.particleBuffer, particleData);
|
|
463
|
+
this.targetParticleBuffer = this.uploadParticleBuffer(this.targetParticleBuffer, particleData);
|
|
464
|
+
this.transitionStep = 0;
|
|
465
|
+
this.transitionActive = false;
|
|
466
|
+
this.render();
|
|
467
|
+
return _context3.abrupt("return", true);
|
|
468
|
+
case 14:
|
|
469
|
+
case "end":
|
|
470
|
+
return _context3.stop();
|
|
471
|
+
}
|
|
472
|
+
}, _callee3, this);
|
|
473
|
+
}));
|
|
474
|
+
function setParticles(_x) {
|
|
475
|
+
return _setParticles.apply(this, arguments);
|
|
476
|
+
}
|
|
477
|
+
return setParticles;
|
|
478
|
+
}()
|
|
479
|
+
}, {
|
|
480
|
+
key: "updateParticles",
|
|
481
|
+
value: function () {
|
|
482
|
+
var _updateParticles = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(children) {
|
|
483
|
+
var initialized, particleData;
|
|
484
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
485
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
486
|
+
case 0:
|
|
487
|
+
children = Array.isArray(children) ? children : [children];
|
|
488
|
+
_context4.next = 3;
|
|
489
|
+
return this.init();
|
|
490
|
+
case 3:
|
|
491
|
+
initialized = _context4.sent;
|
|
492
|
+
if (initialized) {
|
|
493
|
+
_context4.next = 6;
|
|
494
|
+
break;
|
|
495
|
+
}
|
|
496
|
+
return _context4.abrupt("return", false);
|
|
497
|
+
case 6:
|
|
498
|
+
particleData = this.createChildData(children);
|
|
499
|
+
this.particleCount = children.length;
|
|
500
|
+
this.particleBuffer = this.uploadParticleBuffer(this.particleBuffer, particleData);
|
|
501
|
+
if (!children.length || !this.transitionActive) {
|
|
502
|
+
this.targetParticleBuffer = this.uploadParticleBuffer(this.targetParticleBuffer, particleData);
|
|
503
|
+
}
|
|
504
|
+
if (!children.length) {
|
|
505
|
+
this.transitionStep = 0;
|
|
506
|
+
this.transitionActive = false;
|
|
507
|
+
}
|
|
508
|
+
this.render();
|
|
509
|
+
return _context4.abrupt("return", true);
|
|
510
|
+
case 13:
|
|
511
|
+
case "end":
|
|
512
|
+
return _context4.stop();
|
|
513
|
+
}
|
|
514
|
+
}, _callee4, this);
|
|
515
|
+
}));
|
|
516
|
+
function updateParticles(_x2) {
|
|
517
|
+
return _updateParticles.apply(this, arguments);
|
|
518
|
+
}
|
|
519
|
+
return updateParticles;
|
|
520
|
+
}()
|
|
521
|
+
}, {
|
|
522
|
+
key: "setTargetParticles",
|
|
523
|
+
value: function () {
|
|
524
|
+
var _setTargetParticles = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(values, steps) {
|
|
525
|
+
var generation,
|
|
526
|
+
initialized,
|
|
527
|
+
currentChildren,
|
|
528
|
+
currentParticleData,
|
|
529
|
+
particleData,
|
|
530
|
+
_args5 = arguments;
|
|
531
|
+
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
532
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
533
|
+
case 0:
|
|
534
|
+
generation = _args5.length > 2 && _args5[2] !== undefined ? _args5[2] : this.tmodel.childGeneration;
|
|
535
|
+
values = Array.isArray(values) ? values : [values];
|
|
536
|
+
_context5.next = 4;
|
|
537
|
+
return this.init();
|
|
538
|
+
case 4:
|
|
539
|
+
initialized = _context5.sent;
|
|
540
|
+
if (!(!initialized || generation !== this.tmodel.childGeneration)) {
|
|
541
|
+
_context5.next = 7;
|
|
542
|
+
break;
|
|
543
|
+
}
|
|
544
|
+
return _context5.abrupt("return", false);
|
|
545
|
+
case 7:
|
|
546
|
+
currentChildren = this.tmodel.childHandles;
|
|
547
|
+
if (!(values.length !== currentChildren.length)) {
|
|
548
|
+
_context5.next = 10;
|
|
549
|
+
break;
|
|
550
|
+
}
|
|
551
|
+
return _context5.abrupt("return", false);
|
|
552
|
+
case 10:
|
|
553
|
+
if (this.particleCount !== currentChildren.length || !this.particleBuffer) {
|
|
554
|
+
currentParticleData = this.createChildData(currentChildren);
|
|
555
|
+
this.particleCount = currentChildren.length;
|
|
556
|
+
this.particleBuffer = this.uploadParticleBuffer(this.particleBuffer, currentParticleData);
|
|
557
|
+
}
|
|
558
|
+
particleData = this.createParticleData(values, steps);
|
|
559
|
+
this.targetParticleBuffer = this.uploadParticleBuffer(this.targetParticleBuffer, particleData);
|
|
560
|
+
this.transitionStep = 0;
|
|
561
|
+
this.transitionActive = true;
|
|
562
|
+
this.render();
|
|
563
|
+
return _context5.abrupt("return", true);
|
|
564
|
+
case 17:
|
|
565
|
+
case "end":
|
|
566
|
+
return _context5.stop();
|
|
567
|
+
}
|
|
568
|
+
}, _callee5, this);
|
|
569
|
+
}));
|
|
570
|
+
function setTargetParticles(_x3, _x4) {
|
|
571
|
+
return _setTargetParticles.apply(this, arguments);
|
|
572
|
+
}
|
|
573
|
+
return setTargetParticles;
|
|
574
|
+
}()
|
|
575
|
+
}, {
|
|
576
|
+
key: "setTransitionStep",
|
|
577
|
+
value: function setTransitionStep(step) {
|
|
578
|
+
step = Math.max(0, this.toNumber(step, 0));
|
|
579
|
+
if (step === this.transitionStep) {
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
this.transitionStep = step;
|
|
583
|
+
this.render();
|
|
584
|
+
}
|
|
585
|
+
}, {
|
|
586
|
+
key: "getRenderWidth",
|
|
587
|
+
value: function getRenderWidth() {
|
|
588
|
+
var modelWidth = Number(this.tmodel.getWidth()) || 1;
|
|
589
|
+
var parentWidth = Number(this.tmodel.getParent().getWidth()) || window.innerWidth || 1;
|
|
590
|
+
return Math.max(1, Math.min(modelWidth, parentWidth));
|
|
591
|
+
}
|
|
592
|
+
}, {
|
|
593
|
+
key: "getRenderHeight",
|
|
594
|
+
value: function getRenderHeight() {
|
|
595
|
+
var _this$tmodel$getParen2;
|
|
596
|
+
var modelHeight = Number(this.tmodel.getHeight()) || 1;
|
|
597
|
+
var parentHeight = Number((_this$tmodel$getParen2 = this.tmodel.getParent()) === null || _this$tmodel$getParen2 === void 0 ? void 0 : _this$tmodel$getParen2.getHeight()) || window.innerHeight || 1;
|
|
598
|
+
return Math.max(1, Math.min(modelHeight, parentHeight));
|
|
599
|
+
}
|
|
600
|
+
}, {
|
|
601
|
+
key: "updateCanvasLayerWidthHeight",
|
|
602
|
+
value: function updateCanvasLayerWidthHeight() {
|
|
603
|
+
if (!this.canvasLayer) {
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
var height = Math.max(this.getRenderHeight(), Number(this.tmodel.getContentHeight()) || 0);
|
|
607
|
+
var width = Math.max(this.getRenderWidth(), Number(this.tmodel.getContentWidth()) || 0);
|
|
608
|
+
var cssHeight = "".concat(height, "px");
|
|
609
|
+
var cssWidth = "".concat(width, "px");
|
|
610
|
+
if (this.canvasLayer.style.height !== cssHeight) {
|
|
611
|
+
this.canvasLayer.style.height = cssHeight;
|
|
612
|
+
}
|
|
613
|
+
if (this.canvasLayer.style.width !== cssWidth) {
|
|
614
|
+
this.canvasLayer.style.width = cssWidth;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
}, {
|
|
618
|
+
key: "resizeCanvas",
|
|
619
|
+
value: function resizeCanvas() {
|
|
620
|
+
var _this$device$limits;
|
|
621
|
+
if (!this.canvas || !this.device) {
|
|
622
|
+
return;
|
|
623
|
+
}
|
|
624
|
+
var width = this.getRenderWidth();
|
|
625
|
+
var height = this.getRenderHeight();
|
|
626
|
+
var requestedPixelRatio = window.devicePixelRatio || 1;
|
|
627
|
+
var maxTextureDimension = ((_this$device$limits = this.device.limits) === null || _this$device$limits === void 0 ? void 0 : _this$device$limits.maxTextureDimension2D) || 8192;
|
|
628
|
+
var pixelRatio = Math.min(requestedPixelRatio, maxTextureDimension / width, maxTextureDimension / height);
|
|
629
|
+
var pixelWidth = Math.max(1, Math.round(width * pixelRatio));
|
|
630
|
+
var pixelHeight = Math.max(1, Math.round(height * pixelRatio));
|
|
631
|
+
var cssWidth = "".concat(width, "px");
|
|
632
|
+
var cssHeight = "".concat(height, "px");
|
|
633
|
+
if (this.canvas.style.width !== cssWidth) {
|
|
634
|
+
this.canvas.style.width = cssWidth;
|
|
635
|
+
}
|
|
636
|
+
if (this.canvas.style.height !== cssHeight) {
|
|
637
|
+
this.canvas.style.height = cssHeight;
|
|
638
|
+
}
|
|
639
|
+
if (this.canvas.width !== pixelWidth) {
|
|
640
|
+
this.canvas.width = pixelWidth;
|
|
641
|
+
}
|
|
642
|
+
if (this.canvas.height !== pixelHeight) {
|
|
643
|
+
this.canvas.height = pixelHeight;
|
|
644
|
+
}
|
|
645
|
+
this.updateCanvasLayerWidthHeight();
|
|
646
|
+
}
|
|
647
|
+
}, {
|
|
648
|
+
key: "repositionCanvas",
|
|
649
|
+
value: function repositionCanvas() {
|
|
650
|
+
var _parent$$dom, _parent$$dom2;
|
|
651
|
+
if (!this.canvas || !this.canvasLayer || !this.tmodel.hasDom()) {
|
|
652
|
+
return false;
|
|
653
|
+
}
|
|
654
|
+
var tmodel = this.tmodel;
|
|
655
|
+
var parent = tmodel.getParent();
|
|
656
|
+
var domScrollLeft = (parent === null || parent === void 0 || (_parent$$dom = parent.$dom) === null || _parent$$dom === void 0 ? void 0 : _parent$$dom.getScrollLeft()) || tmodel.$dom.getScrollLeft() || 0;
|
|
657
|
+
var domScrollTop = (parent === null || parent === void 0 || (_parent$$dom2 = parent.$dom) === null || _parent$$dom2 === void 0 ? void 0 : _parent$$dom2.getScrollTop()) || tmodel.$dom.getScrollTop() || 0;
|
|
658
|
+
var scrollLeft = tmodel.getScrollLeft();
|
|
659
|
+
var scrollTop = tmodel.getScrollTop();
|
|
660
|
+
var x = domScrollLeft - tmodel.x;
|
|
661
|
+
var y = domScrollTop - tmodel.y;
|
|
662
|
+
this.canvasX = scrollLeft + x;
|
|
663
|
+
this.canvasY = scrollTop + y;
|
|
664
|
+
var transform = "translate3d(".concat(x, "px, ").concat(y, "px, 0px)");
|
|
665
|
+
if (this.canvas.style.transform !== transform) {
|
|
666
|
+
this.canvas.style.transform = transform;
|
|
667
|
+
return true;
|
|
668
|
+
}
|
|
669
|
+
return false;
|
|
670
|
+
}
|
|
671
|
+
}, {
|
|
672
|
+
key: "getParticlePosition",
|
|
673
|
+
value: function getParticlePosition(child) {
|
|
674
|
+
var values = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : child === null || child === void 0 ? void 0 : child.actualValues;
|
|
675
|
+
var layoutScrollLeft = Number(this.tmodel.getScrollLeft()) || 0;
|
|
676
|
+
var layoutScrollTop = Number(this.tmodel.getScrollTop()) || 0;
|
|
677
|
+
return {
|
|
678
|
+
x: child.allTargetMap.x ? values.x : child.x + layoutScrollLeft,
|
|
679
|
+
y: child.allTargetMap.y ? values.y : child.y + layoutScrollTop
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
}, {
|
|
683
|
+
key: "updateUniforms",
|
|
684
|
+
value: function updateUniforms() {
|
|
685
|
+
var width = this.getRenderWidth();
|
|
686
|
+
var height = this.getRenderHeight();
|
|
687
|
+
this.device.queue.writeBuffer(this.uniformBuffer, 0, new Float32Array([width, height, this.canvasX, this.canvasY, this.transitionStep, this.transitionActive ? 1 : 0, 0, 0]));
|
|
688
|
+
}
|
|
689
|
+
}, {
|
|
690
|
+
key: "waitForPresentedFrame",
|
|
691
|
+
value: function () {
|
|
692
|
+
var _waitForPresentedFrame = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
|
|
693
|
+
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
694
|
+
while (1) switch (_context6.prev = _context6.next) {
|
|
695
|
+
case 0:
|
|
696
|
+
if (!this.device) {
|
|
697
|
+
_context6.next = 3;
|
|
698
|
+
break;
|
|
699
|
+
}
|
|
700
|
+
_context6.next = 3;
|
|
701
|
+
return this.device.queue.onSubmittedWorkDone();
|
|
702
|
+
case 3:
|
|
703
|
+
_context6.next = 5;
|
|
704
|
+
return new Promise(function (resolve) {
|
|
705
|
+
requestAnimationFrame(function () {
|
|
706
|
+
requestAnimationFrame(resolve);
|
|
707
|
+
});
|
|
708
|
+
});
|
|
709
|
+
case 5:
|
|
710
|
+
case "end":
|
|
711
|
+
return _context6.stop();
|
|
712
|
+
}
|
|
713
|
+
}, _callee6, this);
|
|
714
|
+
}));
|
|
715
|
+
function waitForPresentedFrame() {
|
|
716
|
+
return _waitForPresentedFrame.apply(this, arguments);
|
|
717
|
+
}
|
|
718
|
+
return waitForPresentedFrame;
|
|
719
|
+
}()
|
|
720
|
+
}, {
|
|
721
|
+
key: "render",
|
|
722
|
+
value: function render() {
|
|
723
|
+
if (!this.device || !this.context) {
|
|
724
|
+
return;
|
|
725
|
+
}
|
|
726
|
+
this.resizeCanvas();
|
|
727
|
+
this.repositionCanvas();
|
|
728
|
+
var canDraw = this.pipeline && this.bindGroup && this.particleBuffer && this.targetParticleBuffer && this.particleCount > 0;
|
|
729
|
+
if (canDraw) {
|
|
730
|
+
this.updateUniforms();
|
|
731
|
+
}
|
|
732
|
+
var commandEncoder = this.device.createCommandEncoder();
|
|
733
|
+
var renderPass = commandEncoder.beginRenderPass({
|
|
734
|
+
colorAttachments: [{
|
|
735
|
+
view: this.context.getCurrentTexture().createView(),
|
|
736
|
+
clearValue: {
|
|
737
|
+
r: 0,
|
|
738
|
+
g: 0,
|
|
739
|
+
b: 0,
|
|
740
|
+
a: 0
|
|
741
|
+
},
|
|
742
|
+
loadOp: "clear",
|
|
743
|
+
storeOp: "store"
|
|
744
|
+
}]
|
|
745
|
+
});
|
|
746
|
+
if (canDraw) {
|
|
747
|
+
renderPass.setPipeline(this.pipeline);
|
|
748
|
+
renderPass.setBindGroup(0, this.bindGroup);
|
|
749
|
+
renderPass.setVertexBuffer(0, this.particleBuffer);
|
|
750
|
+
renderPass.setVertexBuffer(1, this.targetParticleBuffer);
|
|
751
|
+
renderPass.draw(6, this.particleCount);
|
|
752
|
+
}
|
|
753
|
+
renderPass.end();
|
|
754
|
+
this.device.queue.submit([commandEncoder.finish()]);
|
|
755
|
+
}
|
|
756
|
+
}, {
|
|
757
|
+
key: "completeTransition",
|
|
758
|
+
value: function completeTransition() {
|
|
759
|
+
if (!this.targetParticleBuffer || !this.transitionActive) {
|
|
760
|
+
return;
|
|
761
|
+
}
|
|
762
|
+
var previousBuffer = this.particleBuffer;
|
|
763
|
+
this.particleBuffer = this.targetParticleBuffer;
|
|
764
|
+
this.targetParticleBuffer = previousBuffer;
|
|
765
|
+
this.transitionStep = 0;
|
|
766
|
+
this.transitionActive = false;
|
|
767
|
+
this.render();
|
|
768
|
+
}
|
|
769
|
+
}, {
|
|
770
|
+
key: "captureBitmap",
|
|
771
|
+
value: function captureBitmap() {
|
|
772
|
+
if (!this.canvas || !this.device) {
|
|
773
|
+
return;
|
|
774
|
+
}
|
|
775
|
+
this.render();
|
|
776
|
+
return createImageBitmap(this.canvas);
|
|
777
|
+
}
|
|
778
|
+
}, {
|
|
779
|
+
key: "destroy",
|
|
780
|
+
value: function destroy() {
|
|
781
|
+
var _this$particleBuffer, _this$targetParticleB, _this$uniformBuffer, _this$context, _this$canvasLayer, _this$scrollElements;
|
|
782
|
+
var _iterator2 = _createForOfIteratorHelper((_this$scrollElements = this.scrollElements) !== null && _this$scrollElements !== void 0 ? _this$scrollElements : []),
|
|
783
|
+
_step2;
|
|
784
|
+
try {
|
|
785
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
786
|
+
var element = _step2.value;
|
|
787
|
+
element.removeEventListener("scroll", this.onScroll);
|
|
788
|
+
}
|
|
789
|
+
} catch (err) {
|
|
790
|
+
_iterator2.e(err);
|
|
791
|
+
} finally {
|
|
792
|
+
_iterator2.f();
|
|
793
|
+
}
|
|
794
|
+
this.scrollElements = undefined;
|
|
795
|
+
this.onScroll = undefined;
|
|
796
|
+
(_this$particleBuffer = this.particleBuffer) === null || _this$particleBuffer === void 0 || _this$particleBuffer.destroy();
|
|
797
|
+
(_this$targetParticleB = this.targetParticleBuffer) === null || _this$targetParticleB === void 0 || _this$targetParticleB.destroy();
|
|
798
|
+
(_this$uniformBuffer = this.uniformBuffer) === null || _this$uniformBuffer === void 0 || _this$uniformBuffer.destroy();
|
|
799
|
+
(_this$context = this.context) === null || _this$context === void 0 || _this$context.unconfigure();
|
|
800
|
+
(_this$canvasLayer = this.canvasLayer) === null || _this$canvasLayer === void 0 || _this$canvasLayer.remove();
|
|
801
|
+
this.particleBuffer = undefined;
|
|
802
|
+
this.targetParticleBuffer = undefined;
|
|
803
|
+
this.uniformBuffer = undefined;
|
|
804
|
+
this.bindGroup = undefined;
|
|
805
|
+
this.pipeline = undefined;
|
|
806
|
+
this.context = undefined;
|
|
807
|
+
this.device = undefined;
|
|
808
|
+
this.canvasLayer = undefined;
|
|
809
|
+
this.canvas = undefined;
|
|
810
|
+
this.format = undefined;
|
|
811
|
+
this.transitionStep = 0;
|
|
812
|
+
this.transitionActive = false;
|
|
813
|
+
this.particleCount = 0;
|
|
814
|
+
this.initializationPromise = undefined;
|
|
815
|
+
}
|
|
816
|
+
}]);
|
|
817
|
+
}();
|
|
818
|
+
export { ParticleRenderer };
|