UNPKG

@awayjs/graphics

Version:
102 lines (101 loc) 3.68 kB
export var WORKER_BODY = function () { var s = self; s.importScripts('__TESS__LIB__'); debug('Tess lib', Tess2); s.onmessage = function (_a) { var data = _a.data; var contours = data.contours, id = data.id, polySize = data.polySize, vertexSize = data.vertexSize, elementType = data.elementType, windingRule = data.windingRule, scaleRatio = data.scaleRatio; debug('Task qued:', id); if (!contours || !contours.length) { return sendError('Contours invald', id); } try { var start = performance.now(); var result = Tess2.tesselate({ windingRule: windingRule, elementType: elementType, polySize: polySize, vertexSize: vertexSize, contours: contours, }); if (!result) { return sendError('Result is empty!', id); } var buffer = buildBuffer(result, scaleRatio); var time = performance.now() - start; debug('Task ended:', id, time); return sendResult(buffer, time, id); } catch (e) { sendError(e.message, id); } }; function sendResult(buffer, time, id) { s.postMessage({ status: 'done', buffer: buffer, id: id, executionTime: time }, [buffer.buffer]); } function sendError(reason, id) { s.postMessage({ status: 'error', id: id, reason: reason }); } function isClockWiseXY(point1x, point1y, point2x, point2y, point3x, point3y) { return (point1x - point2x) * (point3y - point2y) - (point1y - point2y) * (point3x - point2x) >= 0; } function buildBuffer(res, scale) { var numElems = res.elements.length; var finalVerts = new Float32Array(res.elements.length * 2); scale = 1 / scale; var vindex = 0; var p1x = 0; var p1y = 0; var p2x = 0; var p2y = 0; var p3x = 0; var p3y = 0; for (var i = 0; i < numElems; i += 3) { p1x = scale * res.vertices[res.elements[i + 0] * 2 + 0]; p1y = scale * res.vertices[res.elements[i + 0] * 2 + 1]; p2x = scale * res.vertices[res.elements[i + 1] * 2 + 0]; p2y = scale * res.vertices[res.elements[i + 1] * 2 + 1]; p3x = scale * res.vertices[res.elements[i + 2] * 2 + 0]; p3y = scale * res.vertices[res.elements[i + 2] * 2 + 1]; if (isClockWiseXY(p1x, p1y, p2x, p2y, p3x, p3y)) { finalVerts[vindex++] = p3x; finalVerts[vindex++] = p3y; finalVerts[vindex++] = p2x; finalVerts[vindex++] = p2y; finalVerts[vindex++] = p1x; finalVerts[vindex++] = p1y; } else { finalVerts[vindex++] = p1x; finalVerts[vindex++] = p1y; finalVerts[vindex++] = p2x; finalVerts[vindex++] = p2y; finalVerts[vindex++] = p3x; finalVerts[vindex++] = p3y; } } return finalVerts; } function debug() { var data = []; for (var _i = 0; _i < arguments.length; _i++) { data[_i] = arguments[_i]; } // eslint-disable-next-line prefer-rest-params var args = Array.prototype.slice.call(arguments); args.unshift("[WORKER: ".concat(s.name, "]")); console.debug.apply(s, args); } s.postMessage({ status: 'ready' }); };