fabricJS - get object classes - fabricjs

I'm importing a simple SVG, including two paths. Both of them have CSS classes.
Once loaded in my canvas, I'd like to access these classes. Is it possible ?
I tried using myPath.class, returned undefined, and inspecting my object itself doesn't show anything that looks like my classes.

You need to add the class attribute to fabric.SHARED_ATTRIBUTES array.
Here is an example for rectangle.
fabric.Rect.ATTRIBUTE_NAMES.push('class');
canvas = new fabric.Canvas('editorCanvas', {
backgroundColor: 'white',
selectionLineWidth: 2,
width: $("#canvasContainer").width(),
height: $("#canvasContainer").height()
});
fabric.loadSVGFromString($('#textareaId').val(), function(objects, options) {
var obj = fabric.util.groupSVGElements(objects, options);
canvas.add(obj).renderAll();
alert(canvas.getObjects()[0].class);
});
#canvasContainer {
width: 100%;
height: 100vh;
background-color: gray;
}
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.3/fabric.js"></script>
<div id="canvasContainer">
<canvas id="editorCanvas"></canvas>
</div>
<textarea rows="4" cols="50" id="textareaId">
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 22.0.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<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"
width="100px" height="200px" viewBox="0 0 100 200" style="enable-background:new 0 0 100 200;" xml:space="preserve">
<style type="text/css">
.st0{fill:#8DC63F;}
</style>
<rect x="3.2" y="8.8" class="st0" width="92.6" height="72.4"/>
</svg>
</textarea>

Related

SVG <image> class does not work in Safari

I am using the <image> element within an <svg> in Safari.
Whilst the <image> displays in Chrome it does not display in Safari.
Here is my code
.swatch {
width: 10rem;
}
.swatch-base {
width: 100%;
height: 100%;
}
.filter-swatch {
width: 100%;
height: 100%;
mix-blend-mode: multiply;
}
<svg class="swatch" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 305 305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AOI2HlwLovYUUF8YxiG2BmqZD9ffvTYsK+IVUCFVRHyn1DvlrRRQRLMAIdhBKrnw6ZjvVVBUXKzAiLc+HLMiiigbEOy2gTILhhgPLr71JsVxh4rAicNUK5aExJoooK8KY8AfCtCdpIn/Wmx0XAf8QMMQMFVZOxOtFFBN2OCWsgWlAMhldrVUCl8UFFIwmhMtM6KKBXA/DNibIRs7FWc4u1rHw1wybZGzIC/wDVrRRQT/EA4WKcNDCqIAgUUUUH/9k=
" preserveAspectRatio="xMinYMin" onerror="" x="0" y="0"></image>
<rect class="filter-swatch" fill="#ecd5cf"></rect>
</svg>
Please note that within my code I have inlined the image so that no external resources are used and this answer will therefore still be useful when the image no longer lives on my server. In production though, I am referencing an image and not inlining it.
How can I display the image as is displayed in Chrome?
For Safari width and height on images must be specified as attributes and not via CSS.
Using CSS on images is new in SVG 2 and Safari hasn't implemented this part of SVG 2 yet.
.swatch {
width: 10rem;
}
.filter-swatch {
width: 100%;
height: 100%;
mix-blend-mode: multiply;
}
<svg class="swatch" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 305 305" preserveAspectRatio="xMidYMin slice">
<image class="swatch-base" xlink:href="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs+EJ+/9sAQwAIBgYHBgUIBwcHCQkICgwUDQwLCwwZEhMPFB0aHx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8nOT04MjwuMzQy/9sAQwEJCQkMCwwYDQ0YMiEcITIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy/8AAEQgAZABkAwEiAAIRAQMRAf/EABkAAAMBAQEAAAAAAAAAAAAAAAACAwEEB//EADoQAAIBAgMHAgQFAgQHAAAAAAECEQASAyExEyIyQVFhY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nuKoCjM8M7K02gjjPU+1BBeMAGwAMBPyjoe5rpww0IQArKN1iMkGeTd6TAh2FhLSCVvGvl/nXQApCCZnNUIyfuaDlwiBi7rbOAYLxkO/fmKzDVFYAFlERkRkOn1GnwgHxVCjaEkmGGTdz/oKUi1gZz1GWvVvcUGYhwrt44ij5VRogd+9FMWVWJXIMZuK5v3ooB3ucQ2001EXHp7DWtZlJYBzigxbP/EPT2FK+9iNcyvkoYocvpHencS+Je6MIW4qBn0Ve4oIIpLmMGQTERqctz2HWrYZllIS+7QT+Z/tUDhpIlW8rTquUBT1qmGgHEx5B9nz6W/vQMy7xDqHXJTl+ZHy/aptswhDYZa7rM4hHL7U7wjMWYTAm0DTt5UhZLHF5CgZ5aaQF70GqqEEiGk9TvnlHtTkrdiy4e/iCj80zG7/FYgnDxJIAEBui55Bf3pijK2LeFWBvWxujw70Bg2XBgGxCTJs+YjmOwqknZmQxBi8DXEPj2qeENmwLALGsRl0AHfnTtIRpIUJFzD5M9FoMw2BYA4ReNbdWPbsNKm2pkZ5XRlPSPbnToHw8ziHDjWzOMsgOvU1FtDLQF1PonSPfnQa6IXJKM5nNgcie1FSdipAOI2HlwLovYUUF8YxiG2BmqZD9ffvTYsK+IVUCFVRHyn1DvlrRRQRLMAIdhBKrnw6ZjvVVBUXKzAiLc+HLMiiigbEOy2gTILhhgPLr71JsVxh4rAicNUK5aExJoooK8KY8AfCtCdpIn/Wmx0XAf8QMMQMFVZOxOtFFBN2OCWsgWlAMhldrVUCl8UFFIwmhMtM6KKBXA/DNibIRs7FWc4u1rHw1wybZGzIC/wDVrRRQT/EA4WKcNDCqIAgUUUUH/9k=
" preserveAspectRatio="xMinYMin" onerror="" x="0" y="0" width="100%" height="100%"></image>
<rect class="filter-swatch" fill="#ecd5cf"></rect>
</svg>

Center SVG Object on responsive canvas

The SVG canvas is configured to adjust to the browser window and always fill 100% horizontally and vertically.
I want the blue cube to always be in the bottom center of the window and also stay the same size.
Is there any way to change the position of the cube or the viewBox, so that the cube always stays in the bottom center no matter what size the browser window has?
Thank you in advance for your help.
<!DOCTYPE html>
<html>
<head>
<style type="text/css" media="screen">
body { background:#eee; margin:0 }
svg {display:block; position:absolute; width:100%; height:100%; background:#fff;}
</style>
</head>
<body>
<svg xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%">
<rect x="0" y="0" width="100%" height="100%" fill="lightgreen"/> <!--Background-->
<rect x="0" y="0" width="100" height="100" fill="blue"/> <!--Cube-->
</svg>
</body>
</html>
I would use javascript for this. The clientWidth and clientHeight will give you the size of the SVG canvas.Alternatively you may use the getBoundingClientRect()
let x,y;
function init(){
x = svg.clientWidth/2 - 50;
y = svg.clientHeight - 100;
therect.setAttributeNS(null,"x",x)
therect.setAttributeNS(null,"y",y)
}
setTimeout(function() {
init();
addEventListener('resize', init, false);
}, 15);
body { background:#eee; margin:0 }
svg {display:block; position:absolute; width:100%; height:100%; background:#fff;}
<svg id="svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%">
<rect x="0" y="0" width="100%" height="100%" fill="lightgreen"/><!--Background-->
<rect id="therect" x="50%" y="100%" width="100" height="100" fill="blue"/><!--Cube-->
</svg>
You can move the viewbox origin to the center with viewbox="-50 -50 100 100",
and from there on draw all the elements taking into account the relative values set by viewbox:
<!DOCTYPE html>
<html>
<head>
<style type="text/css" media="screen">
body { background:#eee; margin:0 }
svg {display:block; position:absolute; width:100%; height:100%; background:#fff;}
</style>
</head>
<body>
<svg xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-50 -50 100 100" >
<rect x="-50vw" y="-50vw" width="100vw" height="100vw" fill="lightgreen"/> <!--Background-->
<rect x="-25" y="0" width="50" height="50" fill="blue"/> <!--Cube-->
</svg>
</body>
</html>
PS:I don't how well vw units are supported as svg attributes values so you might want to test that.

Adding ease-in and ease out in SVG hover stroke-width

I want to add ease-in-out over hover efect in svg map. I am creating the hover by stroke-width of the path. Is this possible at all?
Any other suggesting for creating such hovers in svg maps with easing are welcome. Thank you!
Link to Codepen
<!DOCTYPE html>
<html>
<head xmlns="http://www.w3.org/1999/xlink">
<meta charset="UTF-8">
<title>SVG Hover</title>
</head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" height="56mm" width="56mm" version="1.1" viewBox="0 0 200 200">
<g id="layer1" transform="translate(-277.14 -332.36)">
<rect id="rect3336" height="200" width="200" y="332.36" x="277.14" fill="#cecece"/>
<circle id="path4138" cx="380" cy="430" r="15"
onmouseover="this.style.stroke = '#3f27dd'; this.style['stroke-width'] = 50; this.style['stroke-opacity'] = 0.7;"
onmouseout="this.style.stroke = '#3f27dd'; this.style['stroke-width'] = 0;"/>
</g>
</body>
</html>
I don't know how to do it with JavaScript but I was able to do it with CSS. EDIT: I know but CSS looks cleaner.
JSfiddle
Here is my code:
<body>
<svg xmlns="http://www.w3.org/2000/svg" height="56mm" width="56mm" version="1.1" viewBox="0 0 200 200">
<g id="layer1" transform="translate(-277.14 -332.36)">
<rect id="rect3336" height="200" width="200" y="332.36" x="277.14" fill="#cecece"/>
<circle id="path4138" cx="380" cy="430" r="15"
/>
</g>
And CSS:
#path4138 {
transition: stroke-width 0.3s ease-in;
stroke: #3f27dd;
stroke-width: 0;
stroke-opacity: 0.7;
fill: pink;
pointer-events:all;
}
#path4138:hover {
stroke-width: 50;
fill: red;
transition: stroke-width 0.3s ease-out;
}

Cross browser compatibility with SVG background

I work on a app which supports on IE 10 and Chrome. For background images we go with svg sprite.Wherever we need to position icons we have defined a common class for the image and also defined class based on the icon.
Once we build the app, we found the positioning is fine on chrome browser and for IE we need to write a separate css fix based on the browser.
html
<ul>
<li><a class="pico menu href="javascript:void(0)">Home</a></li>
</ul>
My css looks like
.pico:after{
background:('sprite.svg') 0 0 no-repeat;
background-size: 280px 660px;
position:absolute;
content:""
}
.pico.menu:after{
background-position:-106px 6px;
}
.ie .pico.menu:after{
background-position:-110px 6px;
}
Is there anyway to have a generic styles for IE and Chrome ?. Or is the default behavior of the browsers.
<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"
width="300px" height="650px" viewBox="561 165 246 580" enable-background="new 561 165 246 580" xml:space="preserve">
<g>
<g>
<polygon fill="#F47621" points="580.3,230 585.5,235.2 590.8,230 589.8,229 585.5,233.1 581.3,229 "/>
</g>
<g>
<polygon fill="#F47621" points="580.3,225.5 585.5,230.7 590.8,225.5 589.8,224.6 585.5,228.8 581.3,224.6 "/>
</g>
</g>
</svg>
Here i cleaned up some of the code. Don't use the external url for your images of course.
Tested in chrome and IE 11 and IE 10. Both will display the image correctly.
.pico::after {
background: url(http://imgh.us/test_55.svg) 0 0 no-repeat;
background-size: 280px 660px;
position: absolute;
content: "";
display: inline-block;
width: 30px;
height: 20px;
}
.pico.menu::after {
background-position: -10px -65px;
}
svg {
border: 5px solid black;
}
<ul>
<li><a class="pico menu" href="javascript:void(0)">Home</a>
</li>
</ul>
<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" width="300px" height="650px" viewBox="561 165 246 580" enable-background="new 561 165 246 580" xml:space="preserve">
<polygon fill="#F47621" points="580.3,230 585.5,235.2 590.8,230 589.8,229 585.5,233.1 581.3,229 " />
<polygon fill="#F47621" points="580.3,225.5 585.5,230.7 590.8,225.5 589.8,224.6 585.5,228.8 581.3,224.6 " />
</svg>

How to make SVG break aspect ratio

I have and SVG and I need to fulfil the browser windows. It´s an with sag assigned to src attribute. No mather I do, always keep aspect ratio. I even force image width and height via CSS but I can´t make it work.
The url: https://dl.dropboxusercontent.com/u/814218/svg/svg_test.html
Any suggestion?
Finally, I find the solution by recommendation of #SVG IRC Mozilla Group. The related info is here: http://codepen.io/jonitrythall/blog/preserveaspectratio-in-svg
I need to use, viewBox and preserveAspectRatio attributes. That´s the solution.
Here the final sample SVG source code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<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 1282 802" preserveAspectRatio="defer none"
xml:space="preserve">
<polygon opacity="0.2" fill="#FF00FF" stroke="#93278F" stroke-miterlimit="10" points="-1.5,0 -1.5,800 638.5,400 "/>
<polygon opacity="0.2" fill="#009245" points="0,801 1280,801 640,401 "/>
<polygon opacity="0.2" fill="#FFFF00" stroke="#FFFF00" stroke-miterlimit="10" points="638.5,400 1278.5,800 1278.5,0 "/>
</svg>
Try using:
.autoHeight{
width: 100%;
height: 100%;
}
#bg img {
max-width: 100%;
width: 100%;
height: 100%;
}
And your html should looks like:
<body cz-shortcut-listen="true">
<div id="bg" class="autoHeight" style="width: 100%; height: 100%;">
<img class="autoHeight" src="triangles.svg" alt="" style="width: 100%; height: 100%;">
</div>
</body>
But, basically, you need use: for both, width and height: 100%
Do you mean you need something like this?
<html>
<head>
<style>
html, body, img#svg { width: 100%; height: 100%; border: 0; margin: 0; padding:0 }
</style>
</head>
<body>
<img id="svg" src="https://dl.dropboxusercontent.com/u/814218/svg/triangles.svg" />
</body>
</html>

Resources