How to use troika-worker-utils - 5 common examples

To help you get started, we’ve selected a few troika-worker-utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github protectwise / troika / packages / troika-3d-text / src / TextBuilder.js View on Github external
}))
  })
}

// Local assign impl so we don't have to import troika-core
function assign(toObj, fromObj) {
  for (let key in fromObj) {
    if (fromObj.hasOwnProperty(key)) {
      toObj[key] = fromObj[key]
    }
  }
  return toObj
}


export const fontProcessorWorkerModule = defineWorkerModule({
  dependencies: [
    CONFIG,
    SDF_DISTANCE_PERCENT,
    fontParser,
    createSDFGenerator,
    createFontProcessor
  ],
  init(config, sdfDistancePercent, fontParser, createSDFGenerator, createFontProcessor) {
    const sdfGenerator = createSDFGenerator({
      sdfTextureSize: config.sdfGlyphSize,
      sdfDistancePercent
    })
    return createFontProcessor(fontParser, sdfGenerator, {
      defaultFontUrl: config.defaultFontURL
    })
  }
github protectwise / troika / packages / troika-3d-text / src / TextBuilder.js View on Github external
fontParser,
    createSDFGenerator,
    createFontProcessor
  ],
  init(config, sdfDistancePercent, fontParser, createSDFGenerator, createFontProcessor) {
    const sdfGenerator = createSDFGenerator({
      sdfTextureSize: config.sdfGlyphSize,
      sdfDistancePercent
    })
    return createFontProcessor(fontParser, sdfGenerator, {
      defaultFontUrl: config.defaultFontURL
    })
  }
})

export const processInWorker = defineWorkerModule({
  dependencies: [fontProcessorWorkerModule, ThenableWorkerModule],
  init(fontProcessor, Thenable) {
    return function(args) {
      const thenable = new Thenable()
      fontProcessor.process(args, thenable.resolve)
      return thenable
    }
  },
  getTransferables(result) {
    // Mark array buffers as transferable to avoid cloning during postMessage
    const transferables = [
      result.glyphBounds.buffer,
      result.glyphAtlasIndices.buffer
    ]
    if (result.caretPositions) {
      transferables.push(result.caretPositions.buffer)
github protectwise / troika / packages / troika-3d-text / src / FontParser_Typr.js View on Github external
return function parse(buffer) {
    // Look to see if we have a WOFF file and convert it if so:
    const peek = new Uint8Array(buffer, 0, 4)
    const tag = Typr._bin.readASCII(peek, 0, 4)
    if (tag === 'wOFF') {
      buffer = woff2otf(buffer)
    } else if (tag === 'wOF2') {
      throw new Error('woff2 fonts not supported')
    }
    return wrapFontObj(Typr.parse(buffer))
  }
}


const workerModule = defineWorkerModule({
  dependencies: [typrFactory, woff2otfFactory, parserFactory],
  init(typrFactory, woff2otfFactory, parserFactory) {
    const Typr = typrFactory()
    const woff2otf = woff2otfFactory()
    return parserFactory(Typr, woff2otf)
  }
})


export default workerModule
github protectwise / troika / packages / troika-3d-ui / src / flex-layout / FlexLayoutProcessor.js View on Github external
top: yogaNode.getComputedTop(),
          width: yogaNode.getComputedWidth(),
          height: yogaNode.getComputedHeight()
        }
      })
      root.freeRecursive()

      callback(results)
    })
  }

  return process
}


const flexLayoutProcessorWorkerModule = defineWorkerModule({
  dependencies: [
    yogaFactory,
    fontProcessorWorkerModule,
    createFlexLayoutProcessor,
    ThenableWorkerModule
  ],
  init(yogaFactory, fontProcessor, create, Thenable) {
    const Yoga = yogaFactory()
    const process = create(Yoga, fontProcessor.loadFont, fontProcessor.measure)
    return function(styleTree) {
      const thenable = new Thenable()
      process(styleTree, thenable.resolve)
      return thenable
    }
  }
})
github protectwise / troika / packages / troika-3d-text / src / FontParser_OpenType.js View on Github external
}
          }
          callback(glyphObj, glyphX)
        })
      }
    }
  }

  return function parse(buffer) {
    const otFont = opentype.parse(buffer, {lowMemory: true})
    return wrapFontObj(otFont)
  }
}


const workerModule = defineWorkerModule({
  dependencies: [opentypeFactory, parserFactory],
  init(opentypeFactory, parserFactory) {
    const opentype = opentypeFactory()
    return parserFactory(opentype)
  }
})


export default workerModule

troika-worker-utils

Utilities for executing code in Web Workers

MIT
Latest version published 1 month ago

Package Health Score

83 / 100
Full package analysis

Popular troika-worker-utils functions

Similar packages