Create and Manipulate SVG in Vue project

后端 未结 2 857
予麋鹿
予麋鹿 2021-01-23 07:29

I\'m new to Vue.js and need to create a Vue component to create and manipulate SVG. From my understanding it\'s not best to use JQuery in a Vue component. However I would like

相关标签:
2条回答
  • 2021-01-23 07:57

    You can just directly bind the parts of the svg that you want to be dynamic. Here is a simple tutorial. You need to get the 'markup' of your svg out of javascript variables and into a template element.

    Here's a working codesandbox. Note that you can even animate your svg with CSS. Not bad, eh?

    Here's what one of your dynamic circle elements looks like...

    <circle cx="30" cy="30" r="15" fill="red" :opacity="redO" @mouseenter="redO = 1" @mouseleave="redO = .3"/>
    

    Your requirement to load the svg via webservice will complicate things. It's not the loading - the challenge is to inject the bindings into the source of the svg. You can do this with Dom methods and setAttribute(). setAttribute() will let you set whatever attributes you like, starting with : and @, so you're essentially transforming your svg into a vue template with javascript. This code will be brittle, and vulnerable to changes in the structure of the svg, but it will get you out of a hole. You need to do all the attribute injecting before you initialize Vue

    0 讨论(0)
  • 2021-01-23 08:08

    I just found this package, that will inline an svg for you, and give you access to some basic properties. It won't do everything you need, but it looks to be a very useful start.

    0 讨论(0)
提交回复
热议问题