Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot make it work with Vuejs #128

Open
txstc55 opened this issue Jun 15, 2024 · 4 comments
Open

Cannot make it work with Vuejs #128

txstc55 opened this issue Jun 15, 2024 · 4 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@txstc55
Copy link

txstc55 commented Jun 15, 2024

Tried to implement a simple background removal in vuejs:

<template>
  <div class="w-full h-full">
    <canvas ref="canvas"></canvas>
    <button @click="uploadImage">Upload Image</button>
    <input
      type="file"
      ref="fileInput"
      @change="handleFileChange"
      style="display: none"
      accept="image/*"
    />
  </div>
</template>
<script>
import removeBackground from '@imgly/background-removal';


export default {
  name: "TextCanvas",
  methods: {
    uploadImage() {
      this.$refs.fileInput.click();
    },
    async handleFileChange(event) {
      const file = event.target.files[0];
      if (file) {
        const imageUrl = URL.createObjectURL(file);
        await this.processImage(imageUrl);
      }
    },
    async processImage(imageUrl) {
      const image = new Image();
      image.src = imageUrl;
      image.onload = async () => {
        const canvas = this.$refs.canvas;
        const ctx = canvas.getContext("2d");
        canvas.width = image.width;
        canvas.height = image.height;
        ctx.drawImage(image, 0, 0);

        await this.removeImageBackground();
      };
    },
    async removeImageBackground() {
      const canvas = this.$refs.canvas;
      const ctx = this.$refs.canvas.getContext('2d');
      const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
      removeBackground(imageData).then((blob) => {
        // The result is a blob encoded as PNG. It can be converted to an URL to be used as HTMLImage.src
        const url = URL.createObjectURL(blob);
        console.log(url);
      })
    },
  },
  mounted() {
  },
};
</script>

However I kept getting error:
(0 , imgly_background_removal__WEBPACK_IMPORTED_MODULE_0_.default) is not a function
TypeError: (0 , imgly_background_removal__WEBPACK_IMPORTED_MODULE_0_.default) is not a function
at Proxy.removeImageBackground (webpack-internal:///./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/components/TextCanvas.vue?vue&type=script&lang=js:38:76)
at image.onload (webpack-internal:///./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/components/TextCanvas.vue?vue&type=script&lang=js:30:20)

I also tried to replace the call to imglyRemoveBackground according to https://www.npmjs.com/package/@imgly/background-removal
I'm not sure why and I haven't seen any vuejs example with this package.

@DanielHauschildt
Copy link
Contributor

Do we have any vue experts here?

@DanielHauschildt DanielHauschildt added bug Something isn't working help wanted Extra attention is needed labels Aug 7, 2024
@pappikko
Copy link

maybe

❌ import removeBackground from '@imgly/background-removal';
⭕ import { removeBackground } from '@imgly/background-removal';

#125

@monfortm
Copy link

The brackets are needed for import yes

import { removeBackground } from '@imgly/background-removal';
// ...
const noBgBlob = await removeBackground(blob);

@arthaud-proust
Copy link

Confirming too that removeBackground should be imported with brackets, as hinted by types

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants