Giter Site home page Giter Site logo

Comments (7)

nshen avatar nshen commented on May 20, 2024 2

HI @andilem @silvia-odwyer I made a vite build example, please check.

Demo:
http://github.nshen.net/photon-vite/

Code:
https://github.com/nshen/photon-vite

I just forked photon and republished a npm package with wasm-pack build --target web

https://www.npmjs.com/package/photon-web

with vite-plugin-wasm-pack , it works.

from photon.

behunin avatar behunin commented on May 20, 2024 1

The problem comes from wasm-bindgen, the way it packages wasm causes all sorts of problems. Even when you do target web. I had to rework the whole package in order to get something working. Hopefully I will have some free time this winter to submit some PRs to wasm-bindgen.

from photon.

silvia-odwyer avatar silvia-odwyer commented on May 20, 2024 1

@nshen Thanks very much for this, it'll be incredibly useful for those who wish to use Photon with vite 😄 Your help is much appreciated, so thanks again! 🎉

from photon.

silvia-odwyer avatar silvia-odwyer commented on May 20, 2024

@behunin Hi Levi, could you show the code used and I'll take a look at it! Thanks 😄

from photon.

behunin avatar behunin commented on May 20, 2024
    function resize() {
      
      const ctx = small.value.getContext("2d");
      ctx.drawImage(orig.value, 0, 0);
      
      try {
        import("@silvia-odwyer/photon")
          .then((photon) => {
            let img = photon.open_image(small.value, ctx);

            photon.resize(img, 50, 50, 1);
            photon.putImageData(small.value, ctx, img);
          })
          .catch((e) => {
            console.log(e);
          });
      } catch (e) {
        console.log(e);
      }
    }

This is inside a Vue 3 setup function.
small is a ref to the <canvas>
orig is a ref to the <img />

I get the same error whether I use references or getElementById()

Thank you for your time! 😃

from photon.

andilem avatar andilem commented on May 20, 2024

I am getting the same (or a similar) error when using Vite (https://vitejs.dev) as build tool.

The problem seems to come from the fact that Vite uses the following WASM import mechanism: https://vitejs.dev/guide/features.html#webassembly. But in photon_rs_bg.js, the WASM import is defined as import * as wasm from './photon_rs_bg.wasm';, which doesn't work with Vite WASM import.

I found no easy solution, even copying the whole package in my repo and editing the import only led to a new error:

Uncaught (in promise) TypeError: WebAssembly.instantiate(): Import #0 module="./photon_rs_bg.js" error: module is not an object or function

Inspection of the .wasm showed that there are indeed imports from ./photon_rs_bg.js.

Being no JS/WASM expert, I have no idea how to solve this. Is it required to build the WASM with another type, like web (https://rustwasm.github.io/docs/wasm-bindgen/reference/deployment.html)?

@silvia-odwyer Could you tell us if you plan to support Vite or if you have any idea how to approach or work around this issue?

from photon.

silvia-odwyer avatar silvia-odwyer commented on May 20, 2024

@andilem Thanks for letting me know about this! I'd recommend opening an issue in the wasm-bindgen repository, as the WASM version of photon is built using it, so it's definitely worth letting them know also, and you'll be able to get it working with Vite in the future hopefully then 👍

from photon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.