browser.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /* globals document, ImageData */
  2. const parseFont = require('./lib/parse-font')
  3. exports.parseFont = parseFont
  4. exports.createCanvas = function (width, height) {
  5. return Object.assign(document.createElement('canvas'), { width: width, height: height })
  6. }
  7. exports.createImageData = function (array, width, height) {
  8. // Browser implementation of ImageData looks at the number of arguments passed
  9. switch (arguments.length) {
  10. case 0: return new ImageData()
  11. case 1: return new ImageData(array)
  12. case 2: return new ImageData(array, width)
  13. default: return new ImageData(array, width, height)
  14. }
  15. }
  16. exports.loadImage = function (src, options) {
  17. return new Promise(function (resolve, reject) {
  18. const image = Object.assign(document.createElement('img'), options)
  19. function cleanup () {
  20. image.onload = null
  21. image.onerror = null
  22. }
  23. image.onload = function () { cleanup(); resolve(image) }
  24. image.onerror = function () { cleanup(); reject(new Error('Failed to load the image "' + src + '"')) }
  25. image.src = src
  26. })
  27. }