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

useImage hook #253

Open
lavrton opened this issue Nov 21, 2024 · 7 comments
Open

useImage hook #253

lavrton opened this issue Nov 21, 2024 · 7 comments

Comments

@lavrton
Copy link
Member

lavrton commented Nov 21, 2024

I just released a new version with a little useImage hook. Usage:

<template>
  <v-stage :config="stageSize">
    <v-layer>
      <v-image :config="imageConfig" />
    </v-layer>
  </v-stage>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useImage } from 'vue-konva';

const stageSize = {
  width: window.innerWidth,
  height: window.innerHeight
};

const [image] = useImage('https://konvajs.org/assets/darth-vader.jpg');
const imageConfig = ref({
  x: 50,
  y: 50,
  image,
  width: 100,
  height: 100
});
</script>

If you read this, and interested in vue-konva. Try it. Let me know how it works for you.

@rsaleri
Copy link

rsaleri commented Nov 22, 2024

Tried on nuxt:
image

It doesn't like it...
image

@lavrton
Copy link
Member Author

lavrton commented Nov 22, 2024

Is it the last version of vue-konva?

@rsaleri
Copy link

rsaleri commented Nov 22, 2024

Yes, 3.2.0, I made sure to update before trying it.
It's strange, because Webstorm recognize it, but then nuxt die badly.

Oh, well, I finished working for today, I'll try again on monday.
Have a nice weekend ;)

@rsaleri
Copy link

rsaleri commented Nov 27, 2024

Apparently, after a couple of restart, the problem solved itself and now it's working.
Good job! (y)

@rsaleri
Copy link

rsaleri commented Dec 13, 2024

Hi, still me, sorry to bother, but is it possible to use it for dynamicly changing images?
I can't seem to wrap my head around it...

@lavrton
Copy link
Member Author

lavrton commented Dec 17, 2024

Like this:

<template>
  <div>
    <!-- Toggle Button -->
    <button @click="toggleImage">Toggle Image</button>

    <!-- Konva Stage -->
    <v-stage :config="stageSize">
      <v-layer>
        <!-- Display the Image -->
        <v-image
          v-if="currentImage"
          :config="{
            x: 100,
            y: 100,
            image: currentImage,
            width: 200,
            height: 200,
          }"
        />
      </v-layer>
    </v-stage>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useImage } from "vue-konva";

// Define stage size
const stageSize = {
  width: window.innerWidth,
  height: window.innerHeight,
};

// Image URLs
const imageUrls = [
  "https://konvajs.org/assets/yoda.jpg",
  "https://konvajs.org/assets/darth-vader.jpg",
];

// Reactive current URL
const currentUrl = ref(imageUrls[0]);

// useImage hook: Reactively use currentUrl
const [currentImage] = useImage(currentUrl);

// Toggle Image Function
const toggleImage = () => {
  currentUrl.value =
    currentUrl.value === imageUrls[0] ? imageUrls[1] : imageUrls[0];
};
</script>

You can also get url as property of a component.

https://codesandbox.io/p/sandbox/vue3-forked-zkfk4n?file=%2Fsrc%2FApp.vue%3A10%2C17

@rsaleri
Copy link

rsaleri commented Dec 19, 2024

Yep, my bad, I was passing ref.value instead of ref.

To avoid importing it everywhere in nuxt, I created composables/useImage.ts with this content

export { useImage } from 'vue-konva'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants