UNPKG

14.6 kBJavaScriptView Raw
1module.exports =
2/******/ (function(modules) { // webpackBootstrap
3/******/ // The module cache
4/******/ var installedModules = {};
5/******/
6/******/ // The require function
7/******/ function __webpack_require__(moduleId) {
8/******/
9/******/ // Check if module is in cache
10/******/ if(installedModules[moduleId]) {
11/******/ return installedModules[moduleId].exports;
12/******/ }
13/******/ // Create a new module (and put it into the cache)
14/******/ var module = installedModules[moduleId] = {
15/******/ i: moduleId,
16/******/ l: false,
17/******/ exports: {}
18/******/ };
19/******/
20/******/ // Execute the module function
21/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
22/******/
23/******/ // Flag the module as loaded
24/******/ module.l = true;
25/******/
26/******/ // Return the exports of the module
27/******/ return module.exports;
28/******/ }
29/******/
30/******/
31/******/ // expose the modules object (__webpack_modules__)
32/******/ __webpack_require__.m = modules;
33/******/
34/******/ // expose the module cache
35/******/ __webpack_require__.c = installedModules;
36/******/
37/******/ // define getter function for harmony exports
38/******/ __webpack_require__.d = function(exports, name, getter) {
39/******/ if(!__webpack_require__.o(exports, name)) {
40/******/ Object.defineProperty(exports, name, {
41/******/ configurable: false,
42/******/ enumerable: true,
43/******/ get: getter
44/******/ });
45/******/ }
46/******/ };
47/******/
48/******/ // getDefaultExport function for compatibility with non-harmony modules
49/******/ __webpack_require__.n = function(module) {
50/******/ var getter = module && module.__esModule ?
51/******/ function getDefault() { return module['default']; } :
52/******/ function getModuleExports() { return module; };
53/******/ __webpack_require__.d(getter, 'a', getter);
54/******/ return getter;
55/******/ };
56/******/
57/******/ // Object.prototype.hasOwnProperty.call
58/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
59/******/
60/******/ // __webpack_public_path__
61/******/ __webpack_require__.p = "";
62/******/
63/******/ // Load entry module and return exports
64/******/ return __webpack_require__(__webpack_require__.s = 0);
65/******/ })
66/************************************************************************/
67/******/ ([
68/* 0 */
69/***/ (function(module, exports, __webpack_require__) {
70
71"use strict";
72
73
74Object.defineProperty(exports, "__esModule", {
75 value: true
76});
77exports.start = undefined;
78
79var _webassembly = __webpack_require__(1);
80
81var _webassembly2 = _interopRequireDefault(_webassembly);
82
83function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
84
85//
86// Canvas
87//
88var createCanvas = function createCanvas(x, y, width, height) {
89 // Create canvas
90 var canvas = document.createElement('canvas');
91 canvas.width = width;
92 canvas.height = height;
93
94 // Set css-based location
95 canvas.style.position = 'absolute';
96 canvas.style.left = String(x);
97 canvas.style.top = String(y);
98 canvas.style.zIndex = '-1';
99
100 // Append canvas to dom and return canvas
101 document.body.appendChild(canvas);
102 return canvas;
103};
104
105//
106// Drawing function
107// Generally (maybe always?) these will map 1:1 to the WASM-C imports
108//
109var clearCanvas = function clearCanvas(ctx) {
110 var x = 0;
111 var y = 0;
112 ctx.clearRect(x, y, ctx.canvas.width, ctx.canvas.height);
113};
114
115var drawVertex = function drawVertex(ctx, id, x, y, color) {
116 ctx.strokeStyle = 'rgba(' + color.r + ', ' + color.g + ', ' + color.b + ', ' + color.a + ')';
117 ctx.fillStyle = 'rgba(' + color.r + ', ' + color.g + ', ' + color.b + ', ' + color.a / 2 + ')';
118
119 var r = 12;
120 var startAngle = 0;
121 var endAngle = 2 * Math.PI;
122
123 ctx.beginPath();
124 ctx.arc(x, y, r, startAngle, endAngle, false);
125 ctx.stroke();
126 ctx.fill();
127
128 // ctx.font = '12px Arial black';
129 // ctx.fillStyle = 'black';
130 // ctx.textAlign = 'center';
131 // ctx.fillText(id, x, y);
132};
133
134var drawEdge = function drawEdge(ctx, x1, y1, x2, y2, color) {
135 ctx.strokeStyle = 'rgba(' + color.r + ', ' + color.g + ', ' + color.b + ', ' + color.a + ')';
136
137 ctx.beginPath();
138 ctx.moveTo(x1, y1);
139 ctx.lineTo(x2, y2);
140 ctx.stroke();
141};
142
143var drawTriangle = function drawTriangle(ctx, x1, y1, x2, y2, x3, y3, color) {
144 ctx.fillStyle = 'rgba(' + color.r + ', ' + color.g + ', ' + color.b + ', ' + color.a + ')';
145
146 ctx.beginPath();
147 ctx.moveTo(x1, y1);
148 ctx.lineTo(x2, y2);
149 ctx.lineTo(x3, y3);
150 // ctx.closePath();
151 ctx.fill();
152};
153
154//
155// Wasm
156//
157var loadWasm = async function loadWasm(wasmPath, ctx, vertexColor, edgeColor, triangleColor) {
158 var wasmModule = await _webassembly2.default.load(wasmPath, {
159 imports: {
160 jsSetInterval: function jsSetInterval(f, n) {
161 // setInterval is a JS function that calls the provided function every n milliseconds
162 setInterval(function () {
163 return wasmModule.exports.runCallback(f);
164 }, n);
165 },
166 jsClearCanvas: function jsClearCanvas() {
167 return clearCanvas(ctx);
168 },
169 jsDrawVertex: function jsDrawVertex(id, x, y) {
170 return drawVertex(ctx, id, x, y, vertexColor);
171 },
172 jsDrawEdge: function jsDrawEdge(x1, y1, x2, y2) {
173 return drawEdge(ctx, x1, y1, x2, y2, edgeColor);
174 },
175 jsDrawTriangle: function jsDrawTriangle(x1, y1, x2, y2, x3, y3) {
176 return drawTriangle(ctx, x1, y1, x2, y2, x3, y3, triangleColor);
177 }
178 }
179 });
180
181 return wasmModule;
182};
183
184//
185// Start
186//
187var start = exports.start = async function start(options, version) {
188 // Create the canvas
189 var canvas = createCanvas(options.x, options.y, options.width, options.height);
190 var ctx = canvas.getContext('2d');
191
192 // Start the wasm module
193 var wasmPath = 'https://unpkg.com/@benwiz/boba.wasm@' + version + '/dist/boba.wasm';
194 var wasmModule = await loadWasm(wasmPath, ctx, options.vertexColor, options.edgeColor, options.triangleColor);
195 wasmModule.exports.start(options.width, options.height, options.vertexMinRadius, options.vertexMaxRadius, options.vertexMinSpeed, options.vertexMaxSpeed, options.drawVertices, options.drawEdges, options.drawTriangles);
196};
197
198// const options = {
199// // Canvas
200// x: 0,
201// y: 0,
202// width: document.documentElement.scrollWidth,
203// height: document.documentElement.scrollHeight,
204// // Vertices
205// drawVertices: true,
206// vertexMinRadius: 8,
207// vertexMaxRadius: 16,
208// vertexMinSpeed: 0.5,
209// vertexMaxSpeed: 0.8,
210// vertexColor: {
211// r: 30,
212// g: 144,
213// b: 255,
214// a: 0.2,
215// },
216// // Edges
217// drawEdges: true,
218// edgeColor: {
219// r: 30,
220// g: 144,
221// b: 255,
222// a: 0.2,
223// },
224// // Triangles
225// drawTriangles: true,
226// triangleColor: {
227// r: 30,
228// g: 144,
229// b: 255,
230// a: 0.2,
231// },
232// };
233// start(options, version);
234
235/***/ }),
236/* 1 */
237/***/ (function(module, exports, __webpack_require__) {
238
239"use strict";
240
241
242module.exports = __webpack_require__(2);
243
244/***/ }),
245/* 2 */
246/***/ (function(module, exports, __webpack_require__) {
247
248"use strict";
249
250
251if (typeof window !== "undefined" && window) window.webassembly = exports;
252
253// Common aliases
254var getOwnPropertyNames = Object.getOwnPropertyNames;
255
256/**
257 * Describes a module instance as returned by {@link load}.
258 * @interface IModule
259 * @property {Object.<string,*>} exports Exports
260 * @property {Object.<string,*>} imports Imports
261 * @property {IMemory} memory Memory
262 * @property {Object.<string,*>} env Environment
263 */
264
265/**
266 * Describes a module memory instance.
267 * @interface IMemory
268 * @property {ArrayBuffer} buffer Underlying buffer
269 * @property {number} initial=1 Specified initial amount of memory in 64k pages
270 * @property {number} [maximum] If specified, maximum amount of memory in 64k pages
271 * @property {Uint8Array} U8 Byte-level view
272 * @property {Uint32Array} U32 Aligned unsigned 32-bit integer view
273 * @property {Int32Array} S32 Aligned signed 32-bit integer view
274 * @property {Float32Array} F32 Aligned 32-bit float view
275 * @property {Float64Array} F64 Aligned 64-bit double view
276 * @property {GetInt} getInt Reads a 32-bit signed integer starting at the specified memory offset (aligned to 4 bytes)
277 * @property {GetUint} getUint Reads a 32-bit unsigned integer starting at the specified memory offset (aligned to 4 bytes)
278 * @property {GetFloat} getFloat Reads a 32-bit float starting at the specified memory offset (aligned to 4 bytes)
279 * @property {GetDouble} getDouble Reads a 64-bit double starting at the specified memory offset (aligned to 8 bytes)
280 * @property {GetString} getString Reads the (zero-terminated, exclusive) string starting at the specified memory offset (aligned to 4 bytes)
281 */
282
283/**
284 * Loads a WebAssembly.
285 * @param {string} file File name
286 * @param {LoadOptions} [options] Options
287 * @returns {Promise.<IModule>} Promise resolving to the instantiated module
288 */
289function load(file, options) {
290
291 /**
292 * Options as used by {@link load}.
293 * @interface LoadOptions
294 * @property {number} [initialMemory=1] Initial memory in pages of 64k
295 * @property {number} [maximumMemory] Maximum memory in pages of 64k
296 * @property {Object.<string,*>} [imports] Imports
297 */
298
299 options || (options = {});
300
301 var imports = options.imports || {};
302
303 // Initialize memory
304
305 var memory = imports.memory;
306 if (!memory) {
307 var opts = { initial: options.initialMemory || 1 };
308 if (options.maximumMemory) opts.maximum = options.maximumMemory;
309 memory = new WebAssembly.Memory(opts);
310 memory.initial = options.initialMemory || 1;
311 memory.maximum = options.maximumMemory;
312 }
313
314 var table = imports.table;
315 if (!table) table = new WebAssembly.Table({ initial: 0, element: "anyfunc" });
316
317 function grow() {
318 var buf = memory.buffer;
319 memory.U8 = new Uint8Array(buf);
320 memory.S32 = new Int32Array(buf);
321 memory.U32 = new Uint32Array(buf);
322 memory.F32 = new Float32Array(buf);
323 memory.F64 = new Float64Array(buf);
324 }
325
326 grow();
327
328 // Add utilty to memory
329
330 /**
331 * Reads a 32-bit signed integer starting at the specified memory offset.
332 * @typedef GetInt
333 * @function
334 * @param {number} ptr Memory offset
335 * @returns {number} Signed 32-bit integer value
336 */
337 function getInt(ptr) {
338 return memory.S32[ptr >> 2];
339 }
340
341 memory.getInt = getInt;
342
343 /**
344 * Reads a 32-bit unsigned integer starting at the specified memory offset.
345 * @typedef GetUint
346 * @function
347 * @param {number} ptr Memory offset
348 * @returns {number} Unsigned 32-bit integer value
349 */
350 function getUint(ptr) {
351 return memory.U32[ptr >> 2];
352 }
353
354 memory.getUint = getUint;
355
356 /**
357 * Reads a 32-bit float starting at the specified memory offset.
358 * @typedef GetFloat
359 * @function
360 * @param {number} ptr Memory offset
361 * @returns {number} 32-bit float value
362 */
363 function getFloat(ptr) {
364 return memory.F32[ptr >> 2];
365 }
366
367 memory.getFloat = getFloat;
368
369 /**
370 * Reads a 64-bit double starting at the specified memory offset.
371 * @typedef GetDouble
372 * @function
373 * @param {number} ptr Memory offset
374 * @returns {number} 64-bit float value
375 */
376 function getDouble(ptr) {
377 return memory.F64[ptr >> 3];
378 }
379
380 memory.getDouble = getDouble;
381
382 /**
383 * Reads a (zero-terminated, exclusive) string starting at the specified memory offset.
384 * @typedef GetString
385 * @function
386 * @param {number} ptr Memory offset
387 * @returns {string} String value
388 */
389 function getString(ptr) {
390 var start = ptr >>>= 0;
391 while (memory.U8[ptr++]) {}
392 getString.bytes = ptr - start;
393 return String.fromCharCode.apply(null, memory.U8.subarray(start, ptr - 1));
394 }
395
396 memory.getString = getString;
397
398 // Initialize environment
399
400 var env = {};
401
402 env.memoryBase = imports.memoryBase || 0;
403 env.memory = memory;
404 env.tableBase = imports.tableBase || 0;
405 env.table = table;
406
407 // Add console to environment
408
409 function sprintf(ptr, base) {
410 var s = getString(ptr);
411 return base ? s.replace(/%([dfisu]|lf)/g, function ($0, $1) {
412 var val;
413 return base += $1 === "u" ? (val = getUint(base), 4) : $1 === "f" ? (val = getFloat(base), 4) : $1 === "s" ? (val = getString(getUint(base)), 4) : $1 === "lf" ? (val = getDouble(base), 8) : (val = getInt(base), 4), val;
414 }) : s;
415 }
416
417 getOwnPropertyNames(console).forEach(function (key) {
418 if (typeof console[key] === "function") // eslint-disable-line no-console
419 env["console_" + key] = function (ptr, base) {
420 console[key](sprintf(ptr, base)); // eslint-disable-line no-console
421 };
422 });
423
424 // Add Math to environment
425
426 getOwnPropertyNames(Math).forEach(function (key) {
427 if (typeof Math[key] === "function") env["Math_" + key] = Math[key];
428 });
429
430 // Add imports to environment
431
432 Object.keys(imports).forEach(function (key) {
433 return env[key] = imports[key];
434 });
435
436 // Add default exit listeners if not explicitly imported
437
438 if (!env._abort) env._abort = function (errno) {
439 throw Error("abnormal abort in " + file + ": " + errno);
440 };
441 if (!env._exit) env._exit = function (code) {
442 if (code) throw Error("abnormal exit in " + file + ": " + code);
443 };
444
445 // Finally, fetch the assembly and instantiate it
446
447 env._grow = grow;
448
449 return (typeof fetch === "function" && fetch || fetch_node)(file).then(function (result) {
450 return result.arrayBuffer();
451 }).then(function (buffer) {
452 return WebAssembly.instantiate(buffer, { env: env });
453 }).then(function (module) {
454 var instance = module.instance;
455 instance.imports = imports;
456 instance.memory = memory;
457 instance.env = env;
458 return instance;
459 });
460}
461
462exports.load = load;
463
464// Internal fetch API polyfill for node that doesn't trigger webpack
465var fs;
466function fetch_node(file) {
467 return new Promise(function (resolve, reject) {
468 return (fs || (fs = eval("equire".replace(/^/, "r"))("fs"))).readFile(file, function (err, data) {
469 return err ? reject(err) : resolve({ arrayBuffer: function arrayBuffer() {
470 return data;
471 } });
472 });
473 });
474}
475
476/***/ })
477/******/ ]);
478//# sourceMappingURL=bundle.js.map
\No newline at end of file