Skip to content

Instantly share code, notes, and snippets.

@tonmcg
Last active October 19, 2022 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonmcg/30c8d21b8b0b541ea438f23e6160e406 to your computer and use it in GitHub Desktop.
Save tonmcg/30c8d21b8b0b541ea438f23e6160e406 to your computer and use it in GitHub Desktop.
SVG Attributes + Vue

Using Vue.js to adjust properties on an SVG element.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>SVG Attributes</title>
<style type="text/css" media="screen">
body {
font-family: "Courier New", Georgia;
background: #f4f4f4;
}
h1 {
font-size: 3em;
padding: 0px;
font-weight: normal;
text-align: center;
text-transform: uppercase;
}
.lede {
font-style: italic;
font-weight: 400;
font-size: .9em;
font-size: 14px;
line-height: 1.4em;
text-align: center;
}
label {
display: inline-block;
margin-left: 10px;
width: 200px;
}
input {
margin-top: 5px;
position: absolute;
left: 200px;
width: 180px !important;
}
.control {
height: 30px;
}
</style>
</head>
<body>
<!-- circle template -->
<script type="text/x-template" id="svg-circle">
<circle
:r="properties.r"
:cx="properties.cx"
:cy="properties.cy"
:stroke="properties.stroke"
:fill="properties.fill"
:stroke-width="properties.strokeWidth"
:opacity="properties.opacity"
:stroke-opacity="properties.strokeOpacity"
:fill-opacity="properties.fillOpacity"
:stroke-dasharray="properties.strokeDasharray"
></circle>
</script>
<!-- Vue render -->
<div id="chart">
<h1 class="g-header centered">SVG Attributes</h1>
<p class="lede centered" style="margin-bottom:0rem">Adjusting SVG attributes using Vue.js</p>
<svg width="500" height="300">
<svg-circle :attributes="attributes"></svg-circle>
</svg>
<div v-for="attribute in attributes">
<div class="control" v-if="attribute.min !== undefined">
<label>{{ attribute.label }}</label>
<input type="range" v-model="attribute.value" :min="attribute.min" :max="attribute.max"
:step="attribute.step">
</div>
</div>
</div>
<script src="https://unpkg.com/vue"></script>
<script src="main.js"></script>
</body>
</html>
const attributes = [{
"label": "Radius",
"attr": "r",
"min": 0,
"max": 100,
"value": 75,
"step": 1
},
{
"label": "x-Axis Coordinate",
"attr": "cx",
"min": 100,
"max": 400,
"value": 250,
"step": 1
},
{
"label": "y-Axis Coordinate",
"attr": "cy",
"min": 100,
"max": 200,
"value": 150,
"step": 1
},
{
"label": "Stroke",
"attr": "stroke",
"value": "black"
},
{
"label": "Fill",
"attr": "fill",
"value": "steelblue"
},
{
"label": "Opacity",
"attr": "opacity",
"min": 0,
"max": 1,
"value": 0.7,
"step": 0.01
},
{
"label": "Fill Opacity",
"attr": "fillOpacity",
"min": 0,
"max": 1,
"value": 0.7,
"step": 0.01
},
{
"label": "Stroke Opacity",
"attr": "strokeOpacity",
"min": 0,
"max": 1,
"value": 0.7,
"step": 0.01
},
{
"label": "Stroke Width",
"attr": "strokeWidth",
"min": 0,
"max": 5,
"value": 3,
"step": 0.01
},
{
"label": "Stroke Dasharray",
"attr": "strokeDasharray",
"min": 0,
"max": 50,
"value": 0,
"step": 0.01
}
];
Vue.component('svg-circle', {
template: '#svg-circle',
props: ['attributes'],
computed: {
properties: function () {
var newObj = {};
attributes.forEach(function (d) {
newObj[d.attr] = d.value;
});
return newObj;
}
}
});
new Vue({
el: '#chart',
data: function () {
return {
attributes: attributes
};
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment