image-decode icon indicating copy to clipboard operation
image-decode copied to clipboard

Browser Support

Open z3dev opened this issue 4 years ago • 7 comments

I was able to use this library in the browser by adding a build script based on browserify.

  "scripts": {
    "build": "browserify index.js -o dist/image-decode.js --standalone decode",
    "test": "node test"
  }

The standalone library can then be used within the browsers.

Just wondering... are you planning on adding this?

z3dev avatar Oct 06 '20 02:10 z3dev

Good point. That's nice it worked. I think there might be a shortcut in browser, basically all formats are included in img element. I suppose byte data can be transfered to img element and then read as canvas. Or put directly to canvas. The impediment is async nature of decoding in browser... It would require major version change, because function will return a promise.

dy avatar Oct 06 '20 03:10 dy

Attaching the image to the DOM is a clever workaround for some use cases. However, I'm using images from drag-n-drop, and then converting those to 3D surfaces. This is part of the JSCAD project; www.jscad.xyz

4

Good point about the async nature of the decoding. I'm not sure that's 100% required.

Here's the code that produces the 3D surface.

const {primitives} = require('@jscad/modeling')

const fs = require('fs')

const { heightmap, extrudeSurface } = require('./jscad-surface')

// sets a global variable called 'decode'
let imageDecode = require('./image-decode')
// sets a local variable called 'decode'
// FIXME cannot use because of dependencies on NODE libraries, i.e. stream, etc.
//let decode = require('image-decode')

const main = () => {
  // read the image data from a file
  const imageData = decode(fs.readFileSync('project_image/MandM.jpg'))
  console.log('imageData:', imageData.width, imageData.height)

  // convert the image data into a heightmap
  const myheightmap = heightmap.fromImageData(imageData)

  // convert the heightmap into a 3D surface
  const mysurface = extrudeSurface({ scale: [1, 1, -20], smooth: 1, base: 25.0 }, myheightmap)
  console.log(mysurface.polygons.length)

  return mysurface
}

module.exports = {main}

P.S. Yeah. We have NODE.js package support inside the browser.

z3dev avatar Oct 06 '20 05:10 z3dev

@dy Here's another GREAT REASON to publish a web-ready package.

https://unpkg.com/

This is really cool.

z3dev avatar Oct 12 '20 02:10 z3dev

I’d be glad to add a PR for this. What do you think?

z3dev avatar Oct 29 '20 09:10 z3dev

Sure! I am sorry - not fully understood the issue - hope the change isn't going to be large. I still think that the right option would be moving package to async API, since it's well-supported nowadays, and using img workaround for the browser to minimize the size.

dy avatar Oct 29 '20 11:10 dy

See PR #7

z3dev avatar Nov 13 '20 05:11 z3dev

Note that similar modifications can be reproduce to https://github.com/dy/image-encode

With browserify index.js -o dist/image-encode.js -g uglifyify --standalone imageencode as "build"

ThibautSF avatar Mar 25 '21 13:03 ThibautSF