Okay... I'm going nuts over here. I've started experimenting with SVG. Working with SVG and applying CSS classes to it works like a charm. I just cant figure out what i'm doing wrong, but i just cant get the class to work on a svg text element. I've stripped it all the way down and this is what i got:
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>Playground</title>
</head>
<body>
<style type="text/css">
.mainsvg {
height: 320px;
border: 1px solid red;
width: 400px;
}
.caption {
color: yellow;
}
</style>
<h2>SVG - Sandbox</h2>
<div>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" class="mainsvg">
<text x="65" y="40" class="caption">Fact</text>
</svg>
</div>
</body>
</html>
According to http://www.w3.org/TR/SVG/styling.html#ClassAttribute this should work...
Any hints/tips on what to change, or an alternative?
Setting the class is correct but the CSS color property has no effect on SVG. SVG uses fill and stroke properties. In your case you probably just need to change color to fill. This displays yellow text for me in Firefox.
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>Playground</title>
</head>
<body>
<style type="text/css">
.mainsvg {
height: 320px;
border: 1px solid red;
width: 400px;
}
.caption {
fill: yellow;
}
</style>
<h2>SVG - Sandbox</h2>
<div>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" class="mainsvg">
<text x="65" y="40" class="caption">Fact</text>
</svg>
</div>
</body>
</html>
This is the top Google result for coloring SVG text and to make it very clear for rookies like me, to color an SVG text element in 2022, use stroke, and fill.
fill="red"
stroke="#0000FF"
Just like you use stroke for the outline color and fill for the inside color of a shape, you can do the same thing for text in SVGs.
And the best news is, if you use fill="currentColor" instead of a hard coded color, you can set the SVG text with CSS.
svg {
color: red;
}
Click run code snippet to see an example right in this answer.
<svg height="202" width="202">
<circle cx="101" cy="101" r="100"
stroke="red"
stroke-width="1"
fill="none"
/>
<!-- fill="currentColor" if you want to use CSS to set the color of SVG text -->
<text x="100" y="100"
text-anchor="middle"
fill="red"
stroke="#0000FF"
stroke-width="1px"
alignment-baseline="middle"
font-variant="all-small-caps"
font-size="25"
font-weight="bold"
>
Font is Colored
</text>
The accepted answer doesn't work for me either, so I did more research, and found a solution.
If you wrap the SVG <text .../> element in an <g .../> (group) element it can work:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" class="mainsvg">
<g class="caption">
<text x="65" y="40" fill="currentcolor">Fact</text>
</g>
</svg>
This applies the CSS to the <g>, and then the <text> element inherits the currentcolor of the parent into its fill (or stroke).
I'm trying to animate along a path based on this post: move SVG group on path trail based on percentage of whole path trail
It works beautifully in all browsers but Internet Explorer. I've read lots of posts about the lack of support in IE but I still have enough users that use it that I need to consider it. Can I convert this to another method?
Here is my code (simplified icon here, more complex icon in CodePen):
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Jeep Test 5</title>
<style type="text/css">
#carRight{visibility: visible;}
#carLeft{visibility: hidden;}
#carRightIsoBack {visibility: hidden;}
#carRightIsoFront {visibility: hidden;}
#carLeftIsoBack {visibility: hidden;}
#carLeftIsoFront {visibility: hidden;}
</style>
<script src="../SiteAssets/js/jquery-1.8.1.min.js"></script>
</head>
<body>
<input type="range" id="theRange" value="0"/>
<div id="percentage"></div>
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 459 257" style="enable-background:new 0 0 459 257;" xml:space="preserve">
<style type="text/css">
#percentage{border:1px solid red; padding: 5px;}
svg{border:1px solid;overflow:visible; width:95vh; display:block; margin:1em auto;}
.st0{fill:none;stroke:#F1EA0D;stroke-width:5;stroke-miterlimit:10;}
.st1{fill:#1A1A1A;}
.st2{fill:#FF0000;}
</style>
<defs>
<g id="carRight" transform="translate(-90, -240)">
<circle class="st3" cx="134.5" cy="215.2" r="12"/>
</g>
</defs>
<path id="path" class="st0" d="M20.5,249.5c0,0,27,0,54,1s47.36-5,61-13c40.32-23.66,50.1-50.05,3-82c-35.12-23.83-65-19-97-44
c-22.4-17.5-21.92-28.85-1-41c31-18,101.15-43.54,158-27c55,16,48,30,45,61s-8,39-13,60s5.09,40.12,17,56c18,24,56.49,26.8,81,17
c10-4,33-17,29-48c-3.36-26.03-15-34-30-53s-4.97-25.67,7-27c18-2,32.57-1.81,59,7c24,8,54,9,65,4s1-22-9-32s-14-17-30-24
s-34-8-53-3s-28,11-44,14s-26-10-18-25s22-28,42-38c4-2,9-4,9-4"/>
<use id="theUse_car_right" transform="translate(0,20)" xlink:href="#carRight" />
</svg>
<script type="text/javascript">
let pathlength = path.getTotalLength();
let pos = path.getPointAtLength(0);
theUse_car_right.setAttributeNS(null,"x", pos.x);
theUse_car_right.setAttributeNS(null,"y", pos.y);
document.getElementById("percentage").textContent = "Completion=0%";
theRange.addEventListener("input", ()=>{
let perc = parseInt(theRange.value);
let leng = pathlength * perc/100;
pos = path.getPointAtLength(leng);
theUse_car_right.setAttributeNS(null,"x", pos.x);
theUse_car_right.setAttributeNS(null,"y", pos.y);
document.getElementById("percentage").textContent = "Completion=" + perc + "%";
})
</script>
</body>
</html>
Demo'd here: https://codepen.io/mrsgorgon/pen/ExNbEPN
The issue of your code is in the JavaScript.
Arrow function => is not supported in IE. You need to use the traditional function expression.
The input event on input range won't be triggerrd in IE. You need to use change event to monitor the change of the input range. But change event won't be triggered in other modern browsers, so we need to combine the two event handlers.
I define the function as moveit and combine the two event handlers like this:
<input type="range" id="theRange" value="0" oninput="moveit()" onchange="moveit()" />
The complete sample code is like this:
let pathlength = path.getTotalLength();
let pos = path.getPointAtLength(0);
theUse_car_right.setAttributeNS(null, "x", pos.x);
theUse_car_right.setAttributeNS(null, "y", pos.y);
document.getElementById("percentage").textContent = "Completion=0%";
function moveit() {
let perc = parseInt(theRange.value);
let leng = pathlength * perc / 100;
pos = path.getPointAtLength(leng);
theUse_car_right.setAttributeNS(null, "x", pos.x);
theUse_car_right.setAttributeNS(null, "y", pos.y);
document.getElementById("percentage").textContent = "Completion=" + perc + "%";
}
#carRight {
visibility: visible;
}
#carLeft {
visibility: hidden;
}
#carRightIsoBack {
visibility: hidden;
}
#carRightIsoFront {
visibility: hidden;
}
#carLeftIsoBack {
visibility: hidden;
}
#carLeftIsoFront {
visibility: hidden;
}
#percentage {
border: 1px solid red;
padding: 5px;
}
svg {
border: 1px solid;
overflow: visible;
width: 95vh;
display: block;
margin: 1em auto;
}
.st0 {
fill: none;
stroke: #F1EA0D;
stroke-width: 5;
stroke-miterlimit: 10;
}
.st1 {
fill: #1A1A1A;
}
.st2 {
fill: #FF0000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<input type="range" id="theRange" value="0" oninput="moveit()" onchange="moveit()" />
<div id="percentage"></div>
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 459 257" style="enable-background:new 0 0 459 257;" xml:space="preserve">
<defs>
<g id="carRight" transform="translate(-90, -240)">
<circle class="st3" cx="134.5" cy="215.2" r="12" />
</g>
</defs>
<path id="path" class="st0" d="M20.5,249.5c0,0,27,0,54,1s47.36-5,61-13c40.32-23.66,50.1-50.05,3-82c-35.12-23.83-65-19-97-44
c-22.4-17.5-21.92-28.85-1-41c31-18,101.15-43.54,158-27c55,16,48,30,45,61s-8,39-13,60s5.09,40.12,17,56c18,24,56.49,26.8,81,17
c10-4,33-17,29-48c-3.36-26.03-15-34-30-53s-4.97-25.67,7-27c18-2,32.57-1.81,59,7c24,8,54,9,65,4s1-22-9-32s-14-17-30-24
s-34-8-53-3s-28,11-44,14s-26-10-18-25s22-28,42-38c4-2,9-4,9-4" />
<use id="theUse_car_right" transform="translate(0,20)" xlink:href="#carRight" />
</svg>
The Goal
I'm trying to implement this…
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="https://d3plus.org/js/d3.js"></script>
<script src="https://d3plus.org/js/d3plus.js"></script>
<style>
html {
height: 100%;
}
body {
margin: 0;
height: 100%;
background: linear-gradient(to top, #ddfdff, #6DD5FA, #2980B9);
}
svg {
font-family: "Helvetica", "Arial", sans-serif;
height: 100%;
width: 100%;
background: transparent;
}
.type {
fill: #888;
text-anchor: middle;
}
.shape {
fill: red;
stroke: black;
}
.invis {
fill: transparent;
stroke: transparent;
}
.title {
fill: #ffebeb;
}
</style>
</head>
<body>
<svg>
<path id="Union" fill-rule="evenodd" clip-rule="evenodd" d="M584.924 352C566.379 596.253 442.233 717.371 312.762 721.625C314.849 722.912 315.945 724.404 315.439 725.873C314.85 727.582 313.363 728.819 311.349 729.634C313.835 731.14 315.947 732.306 317.185 732.541C324.33 733.901 329.242 736.83 328.72 740.738C326.639 756.297 285.436 758.859 279.246 744.483C277.43 740.263 282.545 736.546 290.185 734.22C291.876 733.706 291.976 731.183 291.936 728.476C288.699 727.054 286.268 725.043 285.663 722.579C285.504 721.933 285.607 721.353 285.928 720.839C157.97 709.163 31.5609 584.211 2.92421 352C-23.4665 138 133.209 0 293.924 0C454.639 0 600.034 153 584.924 352Z" fill="#C80E0E"/>
<!-- 'd3plus' uses this circle as a text mask. -->
<!-- Honestly, I don't fully understand how this works; -->
<!-- Changing the circle's radius yields strange effects. -->
<circle class="shape invis" r="15px" cx="0px" cy="0px"></circle>
<text id="circleResize" class="wrap title" x="0px" y="110px" font-size="2rem">
Why is it so difficult to center text in a shape like this while specifying curved outer bounds?
</text>
</svg>
<script>
// Wrap text in a circle, and size the text to fit.
d3plus.textwrap()
.container(d3.select("#circleResize"))
.width(620)
.height(1000)
.resize(true)
.draw();
</script>
</body>
</html>
…in Svelte.
The Problem
I can't seem to get d3plus to work with Svelte.
First I tried using <svelte:head> to house the <script src="https://d3plus.org… lines. I don't recall what happened when I tried that, but my hunch is that <svelte:head> only works with CSS, or other files whose content doesn't need to be parsed by Svelte. ¯_(ツ)_/¯?
At any rate, my next approach is represented (in part) below. I first saved d3.js and d3plus.js in src, then, as shown, I import the symbols directly.
In this second scenario, though, I keep getting this console error:
Uncaught TypeError: Cannot read property 'document' of undefined
at d3.js:8
at d3.js:9553
at createCommonjsModule (index.mjs:1328)
at index.mjs:1328
at main.js:8
Presumably what's happening here is that the d3 source is getting run before the app's had a chance to load, and so document remains undefined? Regardless, I'm stuck!
Here's the (broken) code, fwiw:
<script>
import d3 from './d3.js';
import d3plus from './d3plus.js'
d3plus.textwrap()
.container(d3.select("#circleResize"))
.resize(true)
.draw();
</script>
<style>
svg {
font-family: "Helvetica", "Arial", sans-serif;
height: 100%;
width: 100%;
background: transparent;
}
.type {
fill: #888;
text-anchor: middle;
}
.shape {
fill: red;
stroke: black;
}
.invis {
/* Fill will be transparent in final outlay */
fill: #10ef3394;
stroke: transparent;
stroke-width: .1rem;
}
.title {
fill: #ffebeb;
}
</style>
<svg>
<path id="Union" fill-rule="evenodd" clip-rule="evenodd" d="M584.924 352C566.379 596.253 442.233 717.371 312.762 721.625C314.849 722.912 315.945 724.404 315.439 725.873C314.85 727.582 313.363 728.819 311.349 729.634C313.835 731.14 315.947 732.306 317.185 732.541C324.33 733.901 329.242 736.83 328.72 740.738C326.639 756.297 285.436 758.859 279.246 744.483C277.43 740.263 282.545 736.546 290.185 734.22C291.876 733.706 291.976 731.183 291.936 728.476C288.699 727.054 286.268 725.043 285.663 722.579C285.504 721.933 285.607 721.353 285.928 720.839C157.97 709.163 31.5609 584.211 2.92421 352C-23.4665 138 133.209 0 293.924 0C454.639 0 600.034 153 584.924 352Z" fill="#C80E0E"/>
<circle class="shape invis" r="15px" cx="0px" cy="0px"></circle>
<text id="circleResize" class="wrap title" x="0px" y="110px" font-size="2rem">
Why is it so difficult to center text in a shape like this while specifying curved outer bounds?
</text>
</svg>
How can I get this working in Svelte??? 🥴
Working code
(With 👏 to #CD.. here on S.O., and #primos on Discord for their help!)
📝 The range slider and bound variables are just a bit of reactive fun to test moving the text and balloon together.
/public/index.html
<!doctype html>
<html>
<head>
<meta charset='utf8'>
<meta name='viewport' content='width=device-width'>
<title>Svelte app</title>
<link rel='icon' type='image/png' href='/favicon.png'>
<link rel='stylesheet' href='/global.css'>
<link rel='stylesheet' href='/bundle.css'>
<script src="https://d3plus.org/js/d3.js"></script> <!-- 👈 -->
<script src="https://d3plus.org/js/d3plus.js"></script> <!-- 👈 -->
<script defer src='/bundle.js'></script>
</head>
<body>
</body>
</html>
App.svelte
<script>
import { onMount } from 'svelte'; // 👈
onMount(() => { // 👈
d3plus.textwrap()
.container(d3.select("#circleResize"))
.resize(true)
.draw();
});
export let name;
let i = 0;
const minX = 293;
let windowWidth;
let skyHeight;
</script>
<svelte:window bind:innerWidth={windowWidth}/>
<style>
svg {
font-family: "Helvetica", "Arial", sans-serif;
height: 90%;
width: 100%;
background: transparent;
}
.type {
fill: #888;
text-anchor: middle;
}
.shape {
fill: red;
stroke: black;
}
.invis {
fill: transparent;
stroke: transparent;
stroke-width: .1rem;
}
.title {
fill: #ffebeb;
}
</style>
<label>
<input type="range" bind:value={i} max={windowWidth - (2 * minX)}>
</label>
<div>{i}</div>
<svg>
<path id="Union" fill-rule="evenodd" clip-rule="evenodd" transform="translate({i},0)" d="M584.924 352C566.379 596.253 442.233 717.371 312.762 721.625C314.849 722.912 315.945 724.404 315.439 725.873C314.85 727.582 313.363 728.819 311.349 729.634C313.835 731.14 315.947 732.306 317.185 732.541C324.33 733.901 329.242 736.83 328.72 740.738C326.639 756.297 285.436 758.859 279.246 744.483C277.43 740.263 282.545 736.546 290.185 734.22C291.876 733.706 291.976 731.183 291.936 728.476C288.699 727.054 286.268 725.043 285.663 722.579C285.504 721.933 285.607 721.353 285.928 720.839C157.97 709.163 31.5609 584.211 2.92421 352C-23.4665 138 133.209 0 293.924 0C454.639 0 600.034 153 584.924 352Z" fill="#C80E0E"/>
<circle class="shape invis" r="280px" cx="{minX}" cy="300px"></circle>
<text id="circleResize" class="wrap title" x="0px" y="110px" font-size="2rem" transform="translate({i},0)">
Why is it so difficult to center text in a shape like this while specifying curved outer bounds?
</text>
</svg>
I have an array of svg objects that I need to use. Each svg is inserted at two different sections of the HTML when the page is loaded and only one section is displayed at a given time. I use the display property and media queries to hide/unhide the complete section containing the SVGs and other tags.
Each SVG have at least two parts (A/B); part A is a single color but part B use a LinearGradient for Fill.
This method works fine in Chrome, and IE explorer but Firefox does not display part B of the SVG when the media query is called.
I tried changing the id of the linearGradient url to a class, removed the tags, use the and tags, changed the display property types, but nothing worked.
This is simplyfied version of the problem:
https://codepen.io/eddieWin/pen/JOoMLe
Thanks for any help you can give me.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<style type="text/css">
#one{
display: block;
}
#two{
display: none;
}
.svgIcon{
display: inline-block;
border: 2px solid red;
}
#container{
display: flex;
}
#media screen and (min-width: 568px) {
#one{
display: none;
}
#two{
display: block;
}
}
</style>
</head>
<body>
<section id="one">
<h1>One</h1>
<div class="svgContainer"></div>
</section>
<section id="two">
<div id="container">
<div>
<div class="svgContainer"></div>
</div>
<h1>Two</h1>
</div>
</section>
<script type="text/javascript">
var svgIcons = {
'svg1' : '\
<div class="svgIcon">\
<svg version="1.1" class="infoIcon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"\
width="72px" height="70px" viewBox="0 0 72 70">\
<defs>\
<linearGradient id="info_1_" gradientUnits="userSpaceOnUse" x1="32.895" y1="49.7476" x2="32.895" y2="28.2964">\
<stop offset="0" style="stop-color:#ff88ff"/>\
<stop offset="1" style="stop-color:#88dddd"/>\
</linearGradient>\
</defs>\
<circle cx="35" cy="35" r="30" fill="#555555"/>\
<circle cx="35" cy="35" r="20" fill="url(#info_1_)"/>\
</svg>\
</div>'
}
var svgContainers = document.getElementsByClassName('svgContainer');
for(var i = 0; i < svgContainers.length; i++){
svgContainers[i].innerHTML = svgIcons['svg1'];
}
</script>
</body>
</html>
I'm having trouble getting this svg to display correctly in IE10/IE11.
It works fine in Chrome and Firefox.
Here is a CodePen with my situation.
Any thoughts?
Start with a simplified test:
<!doctype html>
<head>
<style>
.rack-edit { transform: rotateY(140deg); }
.figure { display: block; width: 500px; height: 300px; }
</style>
</head>
<div class="rack-edit">
<figure class="back">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="444" height="294">
<rect stroke="black" stroke-width="10" width="10" height="10" />
</svg>
</figure>
</div>
Get that working the same in all three browsers, then go from there.