svgdotjs / svg.js

The lightweight library for manipulating and animating SVG
https://svgjs.dev
Other
11.01k stars 1.07k forks source link

Is there a Vue3 wrapper ? #1205

Closed Neon22 closed 11 months ago

Neon22 commented 3 years ago

Feature request

I love svgdotjs but would like to use it in Vue3 reactively for fairly complex weaving tool. I'm using Vite and Vue3 (Node.js). Does anyone know of a Vue3 wrapper ? Do I even need one ?

Svgdom doesn't appear to be fully formed but I think its regular svgdotjs I need instead.

There is non svgdotjs version here for Vue3 but it is very limited. I'd very much rather use svgdotjs.

Stackexchange suggests we don't need one (a wrapper) but I can't get it to work in Vite/Vue3:

To use version 3.x you would either just do it with esm imports:

import { SVG } from '@svgdotjs/svg.js' import '@svgdotjs/svg.draggable.js' // ... No need to use plugins for vue. Just use SVG() in your code when you need it. If you need other functionality like the "old" SVG.on() you need to import it, too: import { SVG, on } from '@svgdotjs/svg.js'

My Vite/Vue3 foo is weak. My environment is:

Neon22 commented 3 years ago

I found a closed issue that was very useful:

Fuzzyma commented 3 years ago

svg.js runs on the regurlar dom and cant work on a vdom that vue creates. If you want to use svg.js in vue you need to get a reference to a dom element and pass that to svg.js. However to be clear: You will not have the advantages of the vdom and reactivity with svg.js. Svg.js just builds a little bit of dom for you. You have to maintain that piece yourself.

Fuzzyma commented 11 months ago

For reference a quick example:

<template>
  <div ref="canvasRef" />
</template>

<script setup>
import { onMounted, onUpdated, ref } from 'vue'
import { SVG } from '@svgdotjs/svgjs'

const canvasRef = ref()

const drawCanvas = () => {
  const canvas = SVG().addTo(canvasRef.value)
  canvas.text('Hello World')
}

// have to redraw on every rerender
onMounted(drawCanvas)
onUpdated(drawCanvas)

</script>